private void parseRequiredConfigString(String time) { String[] hourMinutes = time.split(":"); if (hourMinutes.length != 2) { throw new MotechException( "The configuration for daily time is not configured correctly, it should be formatted as ##:## (e.g. 10:00)"); } try { hour = Integer.parseInt(hourMinutes[0]); } catch (NumberFormatException e) { throw new MotechException("The hour value for daily polling configuration must be a number", e); } if (hour < 0 || hour > LAST_HOUR_IN_DAY) { throw new MotechException("The hour value for the daily polling configuration must be between 0 and 23"); } try { minute = Integer.parseInt(hourMinutes[1]); } catch (NumberFormatException e) { throw new MotechException("The minute value for daily polling configuration must be a number", e); } if (minute < 0 || minute > LAST_MINUTE_IN_HOUR) { throw new MotechException("The minute value for the daily polling configuration must be between 0 and 59"); } }
@Override public Object convert(Class type, Object value) { try { return new DateTime(new SimpleDateFormat(DATE_PATTERN + " " + TIME_PATTERN_12_HOURS).parse((String) value)); } catch (ParseException e) { throw new MotechException("Encountered exception while parsing mobile form", e); } } }
private FieldValidator getValidationHandler(Annotation annotation) { Annotation validationMarker = annotation.annotationType().getAnnotation(ValidationMarker.class); if (validationMarker == null) { throw new MotechException("Field validator has not been annotated with ValidationMarker, " + annotation.annotationType().getName()); } try { Class<FieldValidator> fieldValidatorClass = (Class<FieldValidator>) validationMarker.annotationType().getMethod("handler").invoke(validationMarker); return fieldValidatorClass.newInstance(); } catch (Exception e) { throw new MotechException("Exception while instantiating validation handler, this should never happen", e); } }
@Override public void handle(MotechEvent event) { try { callHandler(event); } catch (Exception e) { LoggerFactory.getLogger(bean.getClass()).error("Failed to handle event", e); throw new MotechException("Failed to handle event", e); } }
private MotechSecurityConfiguration loadDefaultSecurityConfiguration() { try (InputStream in = this.getClass().getClassLoader().getResourceAsStream(DEFAULT_SECURITY_CONFIG_FILE)) { LOGGER.debug("Load default security rules from: {}", DEFAULT_SECURITY_CONFIG_FILE); MotechSecurityConfiguration config = (MotechSecurityConfiguration) motechJsonReader.readFromStream(in, MotechSecurityConfiguration.class); loadedDefaultSecurityConfiguration = true; return config; } catch (IOException e) { throw new MotechException("Error while loading json file", e); } }
public String getFileContent(String fileName, String formGroupName) { String xformFilePath = join(asList(XFORMS_FOLDER, formGroupName, fileName), File.separator); try { return org.apache.commons.io.IOUtils.toString(getClass().getClassLoader().getResourceAsStream(xformFilePath)); } catch (Exception e) { throw new MotechException("Encountered error while loading openxdata forms", e); } } }
public StubServer start() { try { server.start(); } catch (Exception e) { throw new MotechException("Stub Sever Could not be started", e); } return this; }
public void stop() { try { server.stop(); } catch (Exception e) { throw new MotechException("Stub Server could not be stopped", e); } }
@Override public Object convert(Class type, Object value) { try { Date parsedDate = new SimpleDateFormat(TIME_PATTERN_12_HOURS).parse((String) value); return Time.parseTime(new SimpleDateFormat(TIME_PATTERN_24_HOURS).format(parsedDate), ":"); } catch (ParseException e) { throw new MotechException("Encountered exception while parsing mobile form", e); } } }
private void parseIntervalConfigString(String pollingInterval) { if (pollingInterval.endsWith("m")) { // minutes intervalUnit = 1; try { interval = Integer.parseInt(pollingInterval.substring(0, pollingInterval.length() - 1)); } catch (NumberFormatException e) { throw new MotechException( "The interval value for the interval polling configuration must be a number, e.g. 1m", e); } } else if (pollingInterval.endsWith("h")) { // hours intervalUnit = 60; try { interval = Integer.parseInt(pollingInterval.substring(0, pollingInterval.length() - 1)); } catch (NumberFormatException e) { throw new MotechException( "The interval value for the interval polling configuration must be a number, e.g 1h", e); } } else { throw new MotechException( "The interval value time unit for the interval configuration must be either: m or h"); } } }
private void startAdmin() { Bundle adminBundle = getAdminBundle(); if (adminBundle == null) { LOGGER.warn("Admin bundle not installed"); } else if (adminBundle.getState() == Bundle.ACTIVE) { LOGGER.warn("Admin bundle already active"); } else { try { adminBundle.start(); } catch (BundleException e) { throw new MotechException("Cannot start admin bundle", e); } } }
@Override public void afterPropertiesSet() { if (getKey() == null || getKey().trim().isEmpty()) { throw new MotechException("Transition key is required"); } if (getNode() == null) { throw new MotechException("Transition node id is required"); } }
public void saveRawConfig(String filename, Resource resource) { rawConfig.put(filename, resource); String version = (bundle != null && bundle.getVersion() != null) ? bundle.getVersion().toString() : ""; String bundleSymbolicName = bundle != null ? bundle.getSymbolicName() : ""; try { InputStream is = resource.getInputStream(); if (configurationService != null) { configurationService.saveRawConfig(getSymbolicName(), version, bundleSymbolicName, filename, is); } } catch (IOException e) { throw new MotechException("Error saving file " + filename, e); } }
public void saveConfigProperties(String filename, Properties properties) { config.put(filename, properties); String version = (bundle != null && bundle.getVersion() != null) ? bundle.getVersion().toString() : ""; String bundleSymbolicName = bundle != null ? bundle.getSymbolicName() : ""; if (propsRegistered) { try { if (configurationService != null) { configurationService.addOrUpdateProperties(getSymbolicName(), version, bundleSymbolicName, filename, properties, defaultConfig.get(filename)); } } catch (IOException e) { throw new MotechException("Can't save settings " + filename, e); } } }
@Override public void afterPropertiesSet() { if (getMessage() == null || getMessage().trim().isEmpty()) { throw new MotechException("Message content is required"); } }
@Before public void setUp() { // read passed properties buildDir = System.getProperty("buildDir"); if (StringUtils.isBlank(buildDir)) { throw new MotechException("Build directory not defined"); } chrootDir = System.getProperty(getChrootDirProp()); if (StringUtils.isBlank(chrootDir)) { throw new MotechException("Chroot dir needs to be scpecified. Run with -DchrootDir="); } String tmpDir = System.getProperty("tmpDir"); if (StringUtils.isBlank(tmpDir)) { tmpDir = "/tmp"; } script = tmpDir + File.separatorChar + "motech-osi-it.sh"; errorFile = buildDir + File.separatorChar + ERROR_FILENAME; }
public InputStream getRawConfig(String filename) { InputStream is = null; if (rawConfigRegistered) { // read from platform try { is = configurationService.getRawConfig(getSymbolicName(), filename, rawConfig.get(filename)); } catch (IOException e) { throw new MotechException("Error loading file " + filename, e); } } else { // read resource Resource resource = rawConfig.get(filename); if (resource != null) { try { is = resource.getInputStream(); } catch (IOException e) { throw new MotechException("Error reading raw config", e); } } } return is; }
private SettingsRecord loadSettingsFromStream(org.springframework.core.io.Resource motechSettings) { try { MessageDigest digest = MessageDigest.getInstance("MD5"); try (DigestInputStream dis = new DigestInputStream(motechSettings.getInputStream(), digest)) { //load configFileSettings and calculate MD5 hash SettingsRecord settingsRecord = new SettingsRecord(); settingsRecord.load(dis); settingsRecord.setConfigFileChecksum(new String(digest.digest())); return settingsRecord; // startup loaded } catch (IOException e) { throw new MotechException("Error loading configuration", e); } } catch (NoSuchAlgorithmException e) { throw new MotechException("MD5 algorithm not available", e); } }
private SettingsRecord loadSettingsFromStream(Resource motechSettings) { try { MessageDigest digest = MessageDigest.getInstance("MD5"); try (DigestInputStream dis = new DigestInputStream(motechSettings.getInputStream(), digest)) { //load configFileSettings and calculate MD5 hash SettingsRecord settingsRecord = new SettingsRecord(); settingsRecord.load(dis); settingsRecord.setConfigFileChecksum(new String(digest.digest())); return settingsRecord; // startup loaded } catch (IOException e) { throw new MotechException("Error loading configuration", e); } } catch (NoSuchAlgorithmException e) { throw new MotechException("MD5 algorithm not available", e); } }
@Override public void afterPropertiesSet() { if (getName() == null || getName().trim().isEmpty()) { throw new MotechException("Tree name is required"); } if (getNodes() == null || getNodes().size() == 0) { throw new MotechException("Tree nodes is required"); } }