Search in sources :

Example 21 with MotechException

use of org.motechproject.commons.api.MotechException in project motech by motech.

the class ConfigurationServiceImpl method loadSettingsFromStream.

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()));
            // startup loaded
            return settingsRecord;
        } catch (IOException e) {
            throw new MotechException("Error loading configuration", e);
        }
    } catch (NoSuchAlgorithmException e) {
        throw new MotechException("MD5 algorithm not available", e);
    }
}
Also used : MotechException(org.motechproject.commons.api.MotechException) DigestInputStream(java.security.DigestInputStream) SettingsRecord(org.motechproject.config.domain.SettingsRecord) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 22 with MotechException

use of org.motechproject.commons.api.MotechException in project motech by motech.

the class KeyEvaluator method manipulateValue.

private String manipulateValue(List<String> manipulations, String value) throws TaskHandlerException {
    String manipulateValue = value;
    for (String manipulation : manipulations) {
        if (manipulation.contains("format")) {
            String formatElements = manipulation.substring(FORMAT_PATTERN_BEGIN_INDEX, manipulation.length() - 1);
            if (isNotBlank(formatElements)) {
                String[] items = formatElements.split(",");
                for (int i = 0; i < items.length; ++i) {
                    String item = items[i];
                    if (item.startsWith("{{") && item.endsWith("}}")) {
                        item = item.substring(2, item.length() - 2);
                        KeyInformation subKey = KeyInformation.parse(item);
                        Object subValue = getValue(subKey);
                        items[i] = subValue != null ? subValue.toString() : "";
                    }
                }
                manipulateValue = String.format(manipulateValue, items);
            }
        } else {
            try {
                manipulateValue = manipulate(manipulation, manipulateValue);
            } catch (MotechException e) {
                String msg = e.getMessage();
                if ("task.warning.manipulation".equalsIgnoreCase(msg)) {
                    taskContext.publishWarningActivity(msg, manipulation);
                } else {
                    throw new TaskHandlerException(TRIGGER, msg, e, manipulation);
                }
            }
        }
    }
    return manipulateValue;
}
Also used : MotechException(org.motechproject.commons.api.MotechException) TaskHandlerException(org.motechproject.tasks.exception.TaskHandlerException) KeyInformation(org.motechproject.tasks.domain.KeyInformation)

Example 23 with MotechException

use of org.motechproject.commons.api.MotechException in project motech by motech.

the class TasksPostExecutionHandler method getRetriesFromSettings.

private int getRetriesFromSettings(Task task, Map<String, Object> params) {
    List<Integer> retryInterval = new ArrayList<>();
    int numberOfRetries;
    try (InputStream retries = settings.getRawConfig(TASK_PROPERTIES_FILE_NAME)) {
        if (retries != null) {
            Properties props = new Properties();
            props.load(retries);
            for (Map.Entry<Object, Object> entry : props.entrySet()) {
                retryInterval.add(0, Integer.valueOf(entry.getValue().toString()));
            }
            numberOfRetries = retryInterval.size();
        } else {
            numberOfRetries = 0;
        }
    } catch (IOException e) {
        throw new MotechException("Error loading raw file config to properties", e);
    }
    setRetryParameters(task, params, retryInterval, numberOfRetries);
    return numberOfRetries;
}
Also used : MotechException(org.motechproject.commons.api.MotechException) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Properties(java.util.Properties) HashMap(java.util.HashMap) Map(java.util.Map)

Example 24 with MotechException

use of org.motechproject.commons.api.MotechException in project motech by motech.

the class PomReader method getPomDoc.

private Document getPomDoc() {
    if (pomDoc == null) {
        try {
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            documentBuilderFactory.setIgnoringComments(true);
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            pomDoc = documentBuilder.parse(pomFilePath);
        } catch (SAXException | IOException | ParserConfigurationException e) {
            throw new MotechException("Unable to read the pom file", e);
        }
    }
    return pomDoc;
}
Also used : MotechException(org.motechproject.commons.api.MotechException) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 25 with MotechException

use of org.motechproject.commons.api.MotechException in project motech by motech.

the class SettingsController method saveSettings.

/**
 * Saves the given settings.
 *
 * @param settings  the settings to be saved, not null
 */
@ResponseStatus(HttpStatus.OK)
@RequestMapping(value = "/settings", method = RequestMethod.POST)
public void saveSettings(@RequestBody SettingsDto settings) {
    String taskPossibleErrors = settings.getTaskPossibleErrors();
    try (OutputStream os = new ByteArrayOutputStream()) {
        settings.getTaskRetriesProps().store(os, "TaskRetries");
        if (settings.isValid()) {
            settingsFacade.setProperty(TASK_POSSIBLE_ERRORS, taskPossibleErrors);
            settingsFacade.saveRawConfig(TASK_PROPERTIES_FILE_NAME, new String(os.toString()));
        } else {
            throw new IllegalArgumentException("Settings are not valid");
        }
    } catch (IOException e) {
        throw new MotechException("Error parsing task retries", e);
    }
}
Also used : MotechException(org.motechproject.commons.api.MotechException) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

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