Search in sources :

Example 26 with MotechException

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;
}
Also used : MotechException(org.motechproject.commons.api.MotechException) TaskHandlerException(org.motechproject.tasks.exception.TaskHandlerException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) ActionParameter(org.motechproject.tasks.domain.mds.channel.ActionParameter)

Example 27 with MotechException

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();
    }
}
Also used : MotechException(org.motechproject.commons.api.MotechException) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) Properties(java.util.Properties)

Example 28 with MotechException

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());
}
Also used : MotechException(org.motechproject.commons.api.MotechException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 29 with MotechException

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);
        }
    }
}
Also used : MotechException(org.motechproject.commons.api.MotechException) InputStream(java.io.InputStream) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) Properties(java.util.Properties)

Example 30 with MotechException

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);
}
Also used : MotechException(org.motechproject.commons.api.MotechException) IOException(java.io.IOException) Properties(java.util.Properties)

Aggregations

MotechException (org.motechproject.commons.api.MotechException)33 IOException (java.io.IOException)25 InputStream (java.io.InputStream)9 Properties (java.util.Properties)8 ArrayList (java.util.ArrayList)6 MessageDigest (java.security.MessageDigest)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 HashMap (java.util.HashMap)4 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 Map (java.util.Map)3 ObjectName (javax.management.ObjectName)3 SettingsRecord (org.motechproject.config.domain.SettingsRecord)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 OutputStream (java.io.OutputStream)2 DigestInputStream (java.security.DigestInputStream)2 QueueViewMBean (org.apache.activemq.broker.jmx.QueueViewMBean)2 TaskHandlerException (org.motechproject.tasks.exception.TaskHandlerException)2 Bundle (org.osgi.framework.Bundle)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 Resource (org.springframework.core.io.Resource)2