Search in sources :

Example 41 with PipeForward

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

the class StreamPipe method doPipe.

@Override
public PipeRunResult doPipe(Object input, IPipeLineSession session) throws PipeRunException {
    Object result = input;
    String inputString;
    if (input instanceof String) {
        inputString = (String) input;
    } else {
        inputString = "";
    }
    ParameterResolutionContext prc = new ParameterResolutionContext(inputString, session, isNamespaceAware());
    Map parameters = null;
    ParameterList parameterList = getParameterList();
    if (parameterList != null) {
        try {
            parameters = prc.getValueMap(parameterList);
        } catch (ParameterException e) {
            throw new PipeRunException(this, "Could not resolve parameters", e);
        }
    }
    InputStream inputStream = null;
    OutputStream outputStream = null;
    HttpServletRequest httpRequest = null;
    HttpServletResponse httpResponse = null;
    String contentType = null;
    String contentDisposition = null;
    if (parameters != null) {
        if (parameters.get("inputStream") != null) {
            inputStream = (InputStream) parameters.get("inputStream");
        }
        if (parameters.get("outputStream") != null) {
            outputStream = (OutputStream) parameters.get("outputStream");
        }
        if (parameters.get("httpRequest") != null) {
            httpRequest = (HttpServletRequest) parameters.get("httpRequest");
        }
        if (parameters.get("httpResponse") != null) {
            httpResponse = (HttpServletResponse) parameters.get("httpResponse");
        }
        if (parameters.get("contentType") != null) {
            contentType = (String) parameters.get("contentType");
        }
        if (parameters.get("contentDisposition") != null) {
            contentDisposition = (String) parameters.get("contentDisposition");
        }
    }
    if (inputStream == null && input instanceof InputStream) {
        inputStream = (InputStream) input;
    }
    try {
        if (httpResponse != null) {
            HttpSender.streamResponseBody(inputStream, contentType, contentDisposition, httpResponse, log, getLogPrefix(session));
        } else if (httpRequest != null) {
            StringBuilder partsString = new StringBuilder("<parts>");
            String firstStringPart = null;
            List<AntiVirusObject> antiVirusObjects = new ArrayList<AntiVirusObject>();
            if (ServletFileUpload.isMultipartContent(httpRequest)) {
                log.debug(getLogPrefix(session) + "request with content type [" + httpRequest.getContentType() + "] and length [" + httpRequest.getContentLength() + "] contains multipart content");
                DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
                ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);
                List<FileItem> items = servletFileUpload.parseRequest(httpRequest);
                int fileCounter = 0;
                int stringCounter = 0;
                log.debug(getLogPrefix(session) + "multipart request items size [" + items.size() + "]");
                String lastFoundFileName = null;
                String lastFoundAVStatus = null;
                String lastFoundAVMessage = null;
                for (FileItem item : items) {
                    if (item.isFormField()) {
                        // Process regular form field (input
                        // type="text|radio|checkbox|etc", select, etc).
                        String fieldValue = item.getString();
                        String fieldName = item.getFieldName();
                        if (isCheckAntiVirus() && fieldName.equalsIgnoreCase(getAntiVirusPartName())) {
                            log.debug(getLogPrefix(session) + "found antivirus status part [" + fieldName + "] with value [" + fieldValue + "]");
                            lastFoundAVStatus = fieldValue;
                        } else if (isCheckAntiVirus() && fieldName.equalsIgnoreCase(getAntiVirusMessagePartName())) {
                            log.debug(getLogPrefix(session) + "found antivirus message part [" + fieldName + "] with value [" + fieldValue + "]");
                            lastFoundAVMessage = fieldValue;
                        } else {
                            log.debug(getLogPrefix(session) + "found string part [" + fieldName + "] with value [" + fieldValue + "]");
                            if (isExtractFirstStringPart() && firstStringPart == null) {
                                firstStringPart = fieldValue;
                            } else {
                                String sessionKeyName = "part_string" + (++stringCounter > 1 ? stringCounter : "");
                                addSessionKey(session, sessionKeyName, fieldValue);
                                partsString.append("<part type=\"string\" name=\"" + fieldName + "\" sessionKey=\"" + sessionKeyName + "\" size=\"" + fieldValue.length() + "\"/>");
                            }
                        }
                    } else {
                        // Process form file field (input type="file").
                        if (lastFoundFileName != null && lastFoundAVStatus != null) {
                            antiVirusObjects.add(new AntiVirusObject(lastFoundFileName, lastFoundAVStatus, lastFoundAVMessage));
                            lastFoundFileName = null;
                            lastFoundAVStatus = null;
                            lastFoundAVMessage = null;
                        }
                        log.debug(getLogPrefix(session) + "found file part [" + item.getName() + "]");
                        String sessionKeyName = "part_file" + (++fileCounter > 1 ? fileCounter : "");
                        String fileName = FilenameUtils.getName(item.getName());
                        InputStream is = item.getInputStream();
                        int size = is.available();
                        String mimeType = item.getContentType();
                        if (size > 0) {
                            addSessionKey(session, sessionKeyName, is, fileName);
                        } else {
                            addSessionKey(session, sessionKeyName, null);
                        }
                        partsString.append("<part type=\"file\" name=\"" + fileName + "\" sessionKey=\"" + sessionKeyName + "\" size=\"" + size + "\" mimeType=\"" + mimeType + "\"/>");
                        lastFoundFileName = fileName;
                    }
                }
                if (lastFoundFileName != null && lastFoundAVStatus != null) {
                    antiVirusObjects.add(new AntiVirusObject(lastFoundFileName, lastFoundAVStatus, lastFoundAVMessage));
                }
            } else {
                log.debug(getLogPrefix(session) + "request with content type [" + httpRequest.getContentType() + "] and length [" + httpRequest.getContentLength() + "] does NOT contain multipart content");
            }
            partsString.append("</parts>");
            if (isExtractFirstStringPart()) {
                result = adjustFirstStringPart(firstStringPart, session);
                session.put(getMultipartXmlSessionKey(), partsString.toString());
            } else {
                result = partsString.toString();
            }
            if (!antiVirusObjects.isEmpty()) {
                for (AntiVirusObject antiVirusObject : antiVirusObjects) {
                    if (!antiVirusObject.getStatus().equalsIgnoreCase(getAntiVirusPassedMessage())) {
                        String errorMessage = "multipart contains file [" + antiVirusObject.getFileName() + "] with antivirus status [" + antiVirusObject.getStatus() + "] and message [" + StringUtils.defaultString(antiVirusObject.getMessage()) + "]";
                        PipeForward antiVirusFailedForward = findForward(ANTIVIRUS_FAILED_FORWARD);
                        if (antiVirusFailedForward == null) {
                            throw new PipeRunException(this, errorMessage);
                        } else {
                            if (antiVirusFailureAsSoapFault) {
                                errorMessage = createSoapFaultMessage(errorMessage);
                            }
                            if (StringUtils.isEmpty(getAntiVirusFailureReasonSessionKey())) {
                                return new PipeRunResult(antiVirusFailedForward, errorMessage);
                            } else {
                                session.put(getAntiVirusFailureReasonSessionKey(), errorMessage);
                                return new PipeRunResult(antiVirusFailedForward, result);
                            }
                        }
                    }
                }
            }
        } else {
            Misc.streamToStream(inputStream, outputStream);
        }
    } catch (IOException e) {
        throw new PipeRunException(this, "IOException streaming input to output", e);
    } catch (FileUploadException e) {
        throw new PipeRunException(this, "FileUploadException getting multiparts from httpServletRequest", e);
    }
    return new PipeRunResult(getForward(), result);
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) PipeForward(nl.nn.adapterframework.core.PipeForward) HttpServletRequest(javax.servlet.http.HttpServletRequest) FileItem(org.apache.commons.fileupload.FileItem) PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) PipeRunException(nl.nn.adapterframework.core.PipeRunException) ParameterList(nl.nn.adapterframework.parameters.ParameterList) ParameterException(nl.nn.adapterframework.core.ParameterException) ArrayList(java.util.ArrayList) ParameterList(nl.nn.adapterframework.parameters.ParameterList) List(java.util.List) ParameterResolutionContext(nl.nn.adapterframework.parameters.ParameterResolutionContext) Map(java.util.Map) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 42 with PipeForward

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

the class XmlWellFormedChecker method doPipe.

public PipeRunResult doPipe(Object input, IPipeLineSession session) {
    if (XmlUtils.isWellFormed(input.toString(), getRoot())) {
        throwEvent(AbstractXmlValidator.XML_VALIDATOR_VALID_MONITOR_EVENT);
        return new PipeRunResult(getForward(), input);
    }
    throwEvent(AbstractXmlValidator.XML_VALIDATOR_PARSER_ERROR_MONITOR_EVENT);
    PipeForward forward = findForward("parserError");
    if (forward == null) {
        forward = findForward("failure");
    }
    return new PipeRunResult(forward, input);
}
Also used : PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) PipeForward(nl.nn.adapterframework.core.PipeForward)

Example 43 with PipeForward

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

the class SoapValidatorTest method getSuccess.

private PipeForward getSuccess() {
    PipeForward forward = new PipeForward();
    forward.setName("success");
    return forward;
}
Also used : PipeForward(nl.nn.adapterframework.core.PipeForward)

Example 44 with PipeForward

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

the class Json2XmlValidatorSmileyTest method xmlToJsonViaPipe.

public String xmlToJsonViaPipe(String xml) throws ConfigurationException, PipeStartException, PipeRunException {
    Json2XmlValidator json2xml = new Json2XmlValidator();
    json2xml.setWarn(false);
    json2xml.setSchema(xsd);
    json2xml.setRoot("x");
    json2xml.setOutputFormat(json2xml.FORMAT_JSON);
    json2xml.setThrowException(true);
    json2xml.registerForward(new PipeForward("success", null));
    json2xml.configure();
    json2xml.start();
    IPipeLineSession pipeLineSession = new PipeLineSessionBase();
    PipeRunResult prr = json2xml.doPipe(xml, pipeLineSession);
    return (String) prr.getResult();
}
Also used : PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) Json2XmlValidator(nl.nn.adapterframework.pipes.Json2XmlValidator) PipeForward(nl.nn.adapterframework.core.PipeForward) IPipeLineSession(nl.nn.adapterframework.core.IPipeLineSession) PipeLineSessionBase(nl.nn.adapterframework.core.PipeLineSessionBase)

Example 45 with PipeForward

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

the class Json2XmlValidatorSmileyTest method jsonToXmlViaPipe.

public String jsonToXmlViaPipe(String json) throws ConfigurationException, PipeStartException, PipeRunException {
    Json2XmlValidator json2xml = new Json2XmlValidator();
    json2xml.setWarn(false);
    json2xml.setSchema(xsd);
    json2xml.setRoot("x");
    json2xml.setThrowException(true);
    json2xml.registerForward(new PipeForward("success", null));
    json2xml.configure();
    json2xml.start();
    IPipeLineSession pipeLineSession = new PipeLineSessionBase();
    PipeRunResult prr = json2xml.doPipe(json, pipeLineSession);
    return (String) prr.getResult();
}
Also used : PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) Json2XmlValidator(nl.nn.adapterframework.pipes.Json2XmlValidator) PipeForward(nl.nn.adapterframework.core.PipeForward) IPipeLineSession(nl.nn.adapterframework.core.IPipeLineSession) PipeLineSessionBase(nl.nn.adapterframework.core.PipeLineSessionBase)

Aggregations

PipeForward (nl.nn.adapterframework.core.PipeForward)49 PipeRunResult (nl.nn.adapterframework.core.PipeRunResult)23 PipeRunException (nl.nn.adapterframework.core.PipeRunException)21 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)10 Test (org.junit.Test)8 IOException (java.io.IOException)7 ParameterResolutionContext (nl.nn.adapterframework.parameters.ParameterResolutionContext)7 ParameterException (nl.nn.adapterframework.core.ParameterException)6 ParameterList (nl.nn.adapterframework.parameters.ParameterList)6 IPipe (nl.nn.adapterframework.core.IPipe)4 Map (java.util.Map)3 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)3 ConfigurationWarnings (nl.nn.adapterframework.configuration.ConfigurationWarnings)3 IPipeLineSession (nl.nn.adapterframework.core.IPipeLineSession)3 ITransactionalStorage (nl.nn.adapterframework.core.ITransactionalStorage)3 PipeLineSessionBase (nl.nn.adapterframework.core.PipeLineSessionBase)3 ApiWsdlXmlValidator (nl.nn.adapterframework.extensions.api.ApiWsdlXmlValidator)3 ParameterValueList (nl.nn.adapterframework.parameters.ParameterValueList)3 StatisticsKeeper (nl.nn.adapterframework.statistics.StatisticsKeeper)3 InputStream (java.io.InputStream)2