use of org.motechproject.commons.api.MotechException in project motech by motech.
the class TaskActionExecutor method createParameters.
private Map<String, Object> createParameters(TaskActionInformation info, ActionEvent action, KeyEvaluator keyEvaluator) throws TaskHandlerException {
SortedSet<ActionParameter> actionParameters = action.getActionParameters();
Map<String, Object> parameters = new HashMap<>(actionParameters.size());
for (ActionParameter actionParameter : actionParameters) {
String key = actionParameter.getKey();
if (info.getValues().containsKey(key)) {
String template = info.getValues().get(key);
if (template == null) {
throw new TaskHandlerException(TRIGGER, "task.error.templateNull", key, action.getDisplayName());
}
switch(actionParameter.getType()) {
case LIST:
parameters.put(key, convertToList((List<String>) LIST.parse(template), keyEvaluator));
break;
case MAP:
parameters.put(key, convertToMap(template, keyEvaluator));
break;
default:
try {
String userInput = keyEvaluator.evaluateTemplateString(template);
Object obj = actionParameter.getType().parse(userInput);
parameters.put(key, obj);
} catch (MotechException ex) {
throw new TaskHandlerException(TRIGGER, ex.getMessage(), ex, key);
}
}
} else {
if (actionParameter.isRequired()) {
throw new TaskHandlerException(TRIGGER, "task.error.taskActionNotContainsField", action.getDisplayName(), key);
} else if (actionParameter.getType() == MAP) {
parameters.put(key, new HashMap<>());
} else if (actionParameter.getType() == LIST) {
parameters.put(key, new ArrayList<>());
} else {
parameters.put(key, null);
}
}
}
return parameters;
}
use of org.motechproject.commons.api.MotechException in project motech by motech.
the class EmailChannelBundleIT method deliver.
@Override
public void deliver(String from, String recipient, InputStream data) throws IOException {
messageReceived = true;
try {
MimeMessage mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties()), data);
receivedMessageText = (String) mimeMessage.getContent();
} catch (MessagingException e) {
throw new MotechException("Unable to parse message", e);
}
synchronized (lock) {
lock.notify();
}
}
use of org.motechproject.commons.api.MotechException in project motech by motech.
the class SettingsController method setSettings.
@RequestMapping(value = "/settings", method = RequestMethod.POST)
@PreAuthorize(EmailRolesConstants.HAS_ANY_EMAIL_ROLE)
@ResponseStatus(HttpStatus.OK)
public void setSettings(@RequestBody SettingsDto settings) {
String host = settings.getHost();
String port = settings.getPort();
String username = settings.getUsername();
String password = settings.getPassword();
String days = settings.getLogPurgeTime();
String purgeEnabled = settings.getLogPurgeEnable();
StringBuilder exceptionMessage = new StringBuilder();
if (isBlank(host)) {
exceptionMessage.append(String.format(REQUIRED_FORMAT, MAIL_HOST_PROPERTY)).append(NEW_LINE);
}
if (isBlank(port)) {
exceptionMessage.append(String.format(REQUIRED_FORMAT, MAIL_PORT_PROPERTY)).append(NEW_LINE);
} else if (!isNumeric(port)) {
exceptionMessage.append(String.format(NUMERIC_FORMAT, MAIL_PORT_PROPERTY)).append(NEW_LINE);
}
if (TRUE.equals(purgeEnabled) && (!isNumeric(days))) {
exceptionMessage.append(String.format(NUMERIC_FORMAT, MAIL_LOG_PURGE_TIME_PROPERY)).append(NEW_LINE);
}
if (exceptionMessage.length() > 0) {
throw new IllegalStateException(exceptionMessage.toString());
}
OutputStream os = new ByteArrayOutputStream();
try {
settings.getAdditionalProps().store(os, "AdditionalEmailProperties");
} catch (IOException e) {
throw new MotechException("Error parsing additional email properties", e);
}
settingsFacade.saveConfigProperties(EMAIL_PROPERTIES_FILE_NAME, settings.toProperties());
settingsFacade.saveRawConfig(EMAIL_ADDITIONAL_PROPERTIES_FILE_NAME, new String(os.toString()));
if (emailPurger != null) {
emailPurger.handleSettingsChange();
}
mailSender.setHost(host);
mailSender.setPort(Integer.valueOf(port));
mailSender.setUsername(username);
mailSender.setPassword(password);
mailSender.setJavaMailProperties(settings.getAdditionalProps());
}
use of org.motechproject.commons.api.MotechException in project motech by motech.
the class MdsConfig method setConfig.
public void setConfig(List<Resource> resources) {
for (Resource configFile : resources) {
try (InputStream is = configFile.getInputStream()) {
Properties props = new Properties();
props.load(is);
config.put(getResourceFileName(configFile), props);
} catch (IOException e) {
throw new MotechException("Cant load config file " + configFile.getFilename(), e);
}
}
}
use of org.motechproject.commons.api.MotechException in project motech by motech.
the class SettingsFacade method getProperties.
/**
* Returns properties from a resource with given filename.
*
* @param filename the resource filename
* @return properties stored in the file
*/
public Properties getProperties(String filename) {
if (propsRegistered) {
try {
Properties p = configurationService.getBundleProperties(getBundleSymbolicName(), filename, defaultConfig.get(filename));
config.put(filename, p);
} catch (IOException e) {
throw new MotechException("Can't read settings", e);
}
}
Properties result = config.get(filename);
if (result == null) {
result = defaultConfig.get(filename);
}
return (result == null ? new Properties() : result);
}
Aggregations