Search in sources :

Example 26 with PipeRunException

use of nl.nn.adapterframework.core.PipeRunException in project iaf by ibissource.

the class ChecksumPipe method doPipe.

public PipeRunResult doPipe(Object input, IPipeLineSession session) throws PipeRunException {
    String message = (String) input;
    String result;
    try {
        ChecksumGenerator cg = createChecksumGenerator();
        if (isInputIsFile()) {
            byte[] barr = new byte[1000];
            FileInputStream fis = new FileInputStream((String) input);
            int c;
            while ((c = fis.read(barr)) >= 0) {
                cg.update(barr, c);
            }
        } else {
            byte[] barr;
            if (StringUtils.isEmpty(getCharset())) {
                barr = message.getBytes();
            } else {
                barr = message.getBytes(getCharset());
            }
            cg.update(barr, barr.length);
        }
        result = cg.getResult();
        return new PipeRunResult(getForward(), result);
    } catch (Exception e) {
        throw new PipeRunException(this, "cannot calculate [" + getType() + "]" + (isInputIsFile() ? " on file [" + input + "]" : " using charset [" + getCharset() + "]"), e);
    }
}
Also used : PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) PipeRunException(nl.nn.adapterframework.core.PipeRunException) FileInputStream(java.io.FileInputStream) PipeRunException(nl.nn.adapterframework.core.PipeRunException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 27 with PipeRunException

use of nl.nn.adapterframework.core.PipeRunException in project iaf by ibissource.

the class CleanupOldFilesPipe method doPipe.

public PipeRunResult doPipe(Object input, IPipeLineSession session) throws PipeRunException {
    try {
        String filename;
        if (StringUtils.isNotEmpty(getFilePattern())) {
            filename = FileUtils.getFilename(getParameterList(), session, "", getFilePattern());
        } else {
            if (StringUtils.isNotEmpty(getFilePatternSessionKey())) {
                filename = FileUtils.getFilename(getParameterList(), session, "", (String) session.get(getFilePatternSessionKey()));
            } else {
                if (StringUtils.isEmpty((String) input)) {
                    throw new PipeRunException(this, "input empty, but should contain filename to delete");
                } else {
                    File in = new File(input.toString());
                    filename = in.getName();
                }
            }
        }
        List delFiles = getFilesForDeletion(filename);
        if (delFiles != null && delFiles.size() > 0) {
            for (Iterator fileIt = delFiles.iterator(); fileIt.hasNext(); ) {
                File file = (File) fileIt.next();
                String curfilename = file.getName();
                if (file.delete()) {
                    log.info(getLogPrefix(session) + "deleted file [" + file.getAbsolutePath() + "]");
                } else {
                    log.warn(getLogPrefix(session) + "could not delete file [" + file.getAbsolutePath() + "]");
                }
            }
        } else {
            log.info(getLogPrefix(session) + "no files match pattern [" + filename + "]");
        }
        if (isDeleteEmptySubdirectories()) {
            File file = new File(filename);
            if (file.exists()) {
                deleteEmptySubdirectories(getLogPrefix(session), file, 0);
            }
        }
        return new PipeRunResult(getForward(), input);
    } catch (Exception e) {
        throw new PipeRunException(this, "Error while deleting file(s)", e);
    }
}
Also used : PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) PipeRunException(nl.nn.adapterframework.core.PipeRunException) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) File(java.io.File) PipeRunException(nl.nn.adapterframework.core.PipeRunException)

Example 28 with PipeRunException

use of nl.nn.adapterframework.core.PipeRunException in project iaf by ibissource.

the class Json2XmlValidator method alignJson.

protected PipeRunResult alignJson(String messageToValidate, IPipeLineSession session, boolean responseMode) throws PipeRunException, XmlValidatorException {
    ValidationContext context;
    ValidatorHandler validatorHandler;
    try {
        context = validator.createValidationContext(session, getRootValidations(responseMode), getInvalidRootNamespaces());
        validatorHandler = validator.getValidatorHandler(session, context);
    } catch (ConfigurationException e) {
        throw new PipeRunException(this, "Cannot create ValidationContext", e);
    }
    String resultEvent;
    String out = null;
    try {
        Json2Xml aligner = new Json2Xml(validatorHandler, context.getXsModels(), isCompactJsonArrays(), getMessageRoot(responseMode), isStrictJsonArraySyntax());
        if (StringUtils.isNotEmpty(getTargetNamespace())) {
            aligner.setTargetNamespace(getTargetNamespace());
        }
        aligner.setDeepSearch(isDeepSearch());
        aligner.setErrorHandler(context.getErrorHandler());
        aligner.setFailOnWildcards(isFailOnWildcards());
        ParameterList parameterList = getParameterList();
        if (parameterList != null) {
            ParameterResolutionContext prc = new ParameterResolutionContext(messageToValidate, session, isNamespaceAware(), false);
            Map<String, Object> parametervalues = null;
            parametervalues = prc.getValueMap(parameterList);
            aligner.setOverrideValues(parametervalues);
        }
        JsonStructure jsonStructure = Json.createReader(new StringReader(messageToValidate)).read();
        if (getOutputFormat(session, responseMode).equalsIgnoreCase(FORMAT_JSON)) {
            Xml2Json xml2json = new Xml2Json(aligner, isCompactJsonArrays(), !isJsonWithRootElements());
            aligner.setContentHandler(xml2json);
            aligner.startParse(jsonStructure);
            out = xml2json.toString();
        } else {
            Source source = aligner.asSource(jsonStructure);
            out = XmlUtils.source2String(source, isProduceNamespaceLessXml());
        }
    } catch (Exception e) {
        resultEvent = validator.finalizeValidation(context, session, e);
    }
    resultEvent = validator.finalizeValidation(context, session, null);
    PipeForward forward = determineForward(resultEvent, session, responseMode);
    PipeRunResult result = new PipeRunResult(forward, out);
    return result;
}
Also used : ValidatorHandler(javax.xml.validation.ValidatorHandler) PipeForward(nl.nn.adapterframework.core.PipeForward) Source(javax.xml.transform.Source) PipeRunException(nl.nn.adapterframework.core.PipeRunException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) XmlValidatorException(nl.nn.adapterframework.validation.XmlValidatorException) ValidationContext(nl.nn.adapterframework.validation.ValidationContext) PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) PipeRunException(nl.nn.adapterframework.core.PipeRunException) StringReader(java.io.StringReader) ParameterList(nl.nn.adapterframework.parameters.ParameterList) ParameterResolutionContext(nl.nn.adapterframework.parameters.ParameterResolutionContext) Xml2Json(nl.nn.adapterframework.align.Xml2Json) Json2Xml(nl.nn.adapterframework.align.Json2Xml) JsonStructure(javax.json.JsonStructure)

Example 29 with PipeRunException

use of nl.nn.adapterframework.core.PipeRunException in project iaf by ibissource.

the class Json2XmlValidator method doPipe.

/**
 * Validate the XML or JSON string. The format is automatically detected.
 * @param input a String
 * @param session a {@link nl.nn.adapterframework.core.IPipeLineSession Pipelinesession}
 *
 * @throws PipeRunException when <code>isThrowException</code> is true and a validationerror occurred.
 */
@Override
public PipeRunResult doPipe(Object input, IPipeLineSession session, boolean responseMode) throws PipeRunException {
    String messageToValidate = input == null ? "{}" : input.toString();
    int i = 0;
    while (i < messageToValidate.length() && Character.isWhitespace(messageToValidate.charAt(i))) i++;
    if (i >= messageToValidate.length()) {
        messageToValidate = "{}";
        storeInputFormat(FORMAT_JSON, session, responseMode);
    } else {
        char firstChar = messageToValidate.charAt(i);
        if (firstChar == '<') {
            // message is XML
            if (isAcceptNamespaceLessXml()) {
                messageToValidate = addNamespace(messageToValidate);
            // if (log.isDebugEnabled()) log.debug("added namespace to message ["+messageToValidate+"]");
            }
            storeInputFormat(FORMAT_XML, session, responseMode);
            if (!getOutputFormat(session, responseMode).equalsIgnoreCase(FORMAT_JSON)) {
                PipeRunResult result = super.doPipe(messageToValidate, session, responseMode);
                if (isProduceNamespaceLessXml()) {
                    String msg = (String) result.getResult();
                    msg = XmlUtils.removeNamespaces(msg);
                    result.setResult(msg);
                }
                return result;
            }
            try {
                return alignXml2Json(messageToValidate, session, responseMode);
            } catch (Exception e) {
                throw new PipeRunException(this, "Alignment of XML to JSON failed", e);
            }
        }
        if (firstChar != '{' && firstChar != '[') {
            throw new PipeRunException(this, "message is not XML or JSON, because it starts with [" + firstChar + "] and not with '<', '{' or '['");
        }
        storeInputFormat(FORMAT_JSON, session, responseMode);
    }
    try {
        return alignJson(messageToValidate, session, responseMode);
    } catch (XmlValidatorException e) {
        throw new PipeRunException(this, "Cannot align JSON", e);
    }
}
Also used : PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) PipeRunException(nl.nn.adapterframework.core.PipeRunException) XmlValidatorException(nl.nn.adapterframework.validation.XmlValidatorException) PipeRunException(nl.nn.adapterframework.core.PipeRunException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) XmlValidatorException(nl.nn.adapterframework.validation.XmlValidatorException)

Example 30 with PipeRunException

use of nl.nn.adapterframework.core.PipeRunException in project iaf by ibissource.

the class JsonPipe method doPipe.

@Override
public PipeRunResult doPipe(Object input, IPipeLineSession session) throws PipeRunException {
    if (input == null) {
        throw new PipeRunException(this, getLogPrefix(session) + "got null input");
    }
    if (!(input instanceof String)) {
        throw new PipeRunException(this, getLogPrefix(session) + "got an invalid type as input, expected String, got " + input.getClass().getName());
    }
    try {
        String stringResult = (String) input;
        String actualDirection = getDirection();
        String actualVersion = getVersion();
        if ("json2xml".equalsIgnoreCase(actualDirection)) {
            JSONTokener jsonTokener = new JSONTokener(stringResult);
            if (stringResult.startsWith("{")) {
                JSONObject jsonObject = new JSONObject(jsonTokener);
                stringResult = XML.toString(jsonObject);
            }
            if (stringResult.startsWith("[")) {
                JSONArray jsonArray = new JSONArray(jsonTokener);
                stringResult = XML.toString(jsonArray);
            }
            if (addXmlRootElement()) {
                boolean isWellFormed = XmlUtils.isWellFormed(stringResult);
                if (!isWellFormed) {
                    stringResult = "<root>" + stringResult + "</root>";
                }
            }
        }
        if ("xml2json".equalsIgnoreCase(actualDirection)) {
            if ("2".equals(actualVersion)) {
                stringResult = (String) input;
                ParameterResolutionContext prc = new ParameterResolutionContext(stringResult, session, true, true);
                TransformerPool transformerPool = TransformerPool.configureTransformer0(getLogPrefix(null), classLoader, null, null, "/xml/xsl/xml2json.xsl", null, false, null, true);
                stringResult = transformerPool.transform(prc.getInputSource(), null);
            } else {
                JSONObject jsonObject = XML.toJSONObject(stringResult);
                stringResult = jsonObject.toString();
            }
        }
        return new PipeRunResult(getForward(), stringResult);
    } catch (Exception e) {
        throw new PipeRunException(this, getLogPrefix(session) + " Exception on transforming input", e);
    }
}
Also used : JSONTokener(org.json.JSONTokener) PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) JSONObject(org.json.JSONObject) PipeRunException(nl.nn.adapterframework.core.PipeRunException) JSONArray(org.json.JSONArray) ParameterResolutionContext(nl.nn.adapterframework.parameters.ParameterResolutionContext) TransformerPool(nl.nn.adapterframework.util.TransformerPool) PipeRunException(nl.nn.adapterframework.core.PipeRunException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException)

Aggregations

PipeRunException (nl.nn.adapterframework.core.PipeRunException)101 PipeRunResult (nl.nn.adapterframework.core.PipeRunResult)65 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)40 IOException (java.io.IOException)34 ParameterResolutionContext (nl.nn.adapterframework.parameters.ParameterResolutionContext)32 PipeForward (nl.nn.adapterframework.core.PipeForward)20 ParameterException (nl.nn.adapterframework.core.ParameterException)16 ParameterValueList (nl.nn.adapterframework.parameters.ParameterValueList)13 Parameter (nl.nn.adapterframework.parameters.Parameter)12 InputStream (java.io.InputStream)10 File (java.io.File)9 Map (java.util.Map)9 ParameterList (nl.nn.adapterframework.parameters.ParameterList)8 FixedQuerySender (nl.nn.adapterframework.jdbc.FixedQuerySender)7 DomBuilderException (nl.nn.adapterframework.util.DomBuilderException)7 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)6 PipeStartException (nl.nn.adapterframework.core.PipeStartException)6 ArrayList (java.util.ArrayList)5 ZipInputStream (java.util.zip.ZipInputStream)5 IAdapter (nl.nn.adapterframework.core.IAdapter)5