StreamPipe extends FixedForwardPipe { @Override public PipeRunResult doPipe(Message message, IPipeLineSession session) throws PipeRunException { Object result = message; Map<String,Object> parameters = null; ParameterList parameterList = getParameterList(); if (parameterList != null) { try { parameters = parameterList.getValues(message, session).getValueMap(); } 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; String redirectLocation = 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 (parameters.get("redirectLocation") != null) { redirectLocation = (String) parameters.get("redirectLocation"); } } try { if (inputStream == null) { inputStream = message.asInputStream(); } if (httpResponse != null) { HttpSender.streamResponseBody(inputStream, contentType, contentDisposition, httpResponse, log, getLogPrefix(session), redirectLocation); } 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()) { 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 { 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); }  @Override PipeRunResult doPipe(Message message, IPipeLineSession session); @IbisDoc({"(only used for parameter <code>httprequest</code>) when true the first part is not put in a session key but returned to the pipeline (as the result of this pipe)", "false"}) void setExtractFirstStringPart(boolean b); boolean isExtractFirstStringPart(); String getMultipartXmlSessionKey(); @IbisDoc({"(only used when <code>extractfirststringpart=true</code>) the session key to put the xml in with info about the stored parts", "<code>multipartxml</code>"}) void setMultipartXmlSessionKey(String multipartXmlSessionKey); @IbisDoc({"(only used for parameter <code>httprequest</code>) when true parts are checked for antivirus scan returncode. these antivirus scan parts have been added by another application (so the antivirus scan is not performed in this pipe). for each file part an antivirus scan part have been added by this other application (directly after this file part)", "false"}) void setCheckAntiVirus(boolean b); boolean isCheckAntiVirus(); String getAntiVirusPartName(); @IbisDoc({"(only used for parameter <code>httprequest</code> and when <code>checkantivirus=true</code>) name of antivirus scan status parts", "<code>antivirus_rc</code>"}) void setAntiVirusPartName(String antiVirusPartName); String getAntiVirusMessagePartName(); @IbisDoc({"(only used for parameter <code>httprequest</code> and when <code>checkantivirus=true</code>) name of antivirus scan message parts", "<code>antivirus_msg</code>"}) void setAntiVirusMessagePartName(String antiVirusMessagePartName); String getAntiVirusPassedMessage(); @IbisDoc({"(only used for parameter <code>httprequest</code> and when <code>checkantivirus=true</code>) message of antivirus scan parts which indicates the antivirus scan passed", "<code>pass</code>"}) void setAntiVirusPassedMessage(String antiVirusPassedMessage); @IbisDoc({"(only used for parameter <code>httprequest</code> and when <code>checkantivirus=true</code>) when true and the antivirusfailed forward is specified and the antivirus scan did not pass, a soap fault is returned instead of only a plain error message", "false"}) void setAntiVirusFailureAsSoapFault(boolean b); boolean getAntiVirusFailureAsSoapFault(); @IbisDoc({"(only used for parameter <code>httprequest</code> and when <code>checkantivirus=true</code>) if not empty and the antivirusfailed forward is specified and the antivirus scan did not pass, the error message (or soap fault) is stored in this session key and the first string part is returned to the pipeline", ""}) void setAntiVirusFailureReasonSessionKey(String antiVirusFailureReasonSessionKey); String getAntiVirusFailureReasonSessionKey(); static final String ANTIVIRUS_FAILED_FORWARD; }
@Test public void doPipeHttpRequestTest() throws Exception { MockMultipartHttpServletRequest request = createMultipartHttpRequest(); pipe.addParameter(createHttpRequestParameter(request, session)); pipe.configure(); pipe.start(); PipeRunResult pipeRunResult = doPipe(pipe, "", session); assertEquals("success", pipeRunResult.getPipeForward().getName()); String expectedResult = "<parts>" + "<part type=\"string\" name=\"string1\" sessionKey=\"part_string\" size=\"19\"/>" + "<part type=\"file\" name=\"doc001.pdf\" sessionKey=\"part_file\" size=\"26358\" mimeType=\"application/octet-stream; charset=ISO-8859-1\"/>" + "<part type=\"file\" name=\"doc002.pdf\" sessionKey=\"part_file2\" size=\"25879\" mimeType=\"application/octet-stream; charset=ISO-8859-1\"/>" + "</parts>"; assertEquals(expectedResult, pipeRunResult.getResult().asString()); }
@Test public void doPipeHttpRequestAntiVirusTest() throws Exception { MockMultipartHttpServletRequest request = createMultipartHttpRequest(pipe, true); pipe.addParameter(createHttpRequestParameter(request, session)); pipe.configure(); pipe.start(); PipeRunResult pipeRunResult = doPipe(pipe, "", session); assertEquals("success", pipeRunResult.getPipeForward().getName()); String expectedResult = "<parts>" + "<part type=\"string\" name=\"string1\" sessionKey=\"part_string\" size=\"19\"/>" + "<part type=\"file\" name=\"doc001.pdf\" sessionKey=\"part_file\" size=\"26358\" mimeType=\"application/octet-stream; charset=ISO-8859-1\"/>" + "<part type=\"string\" name=\"antivirus_rc\" sessionKey=\"part_string2\" size=\"4\"/>" + "<part type=\"file\" name=\"doc002.pdf\" sessionKey=\"part_file2\" size=\"25879\" mimeType=\"application/octet-stream; charset=ISO-8859-1\"/>" + "<part type=\"string\" name=\"antivirus_rc\" sessionKey=\"part_string3\" size=\"4\"/>" + "</parts>"; assertEquals(expectedResult, pipeRunResult.getResult().asString()); }