Search in sources :

Example 11 with Message

use of nl.nn.adapterframework.stream.Message in project iaf by ibissource.

the class PdfPipeTest method multiThreadedMailWithWordAttachment.

@Test
public void multiThreadedMailWithWordAttachment() throws Exception {
    pipe.setName("multiThreadedmailWithWordAttachment");
    pipe.setAction(DocumentAction.CONVERT);
    pipe.registerForward(new PipeForward("success", "dummy"));
    pipe.configure();
    pipe.start();
    PipeLineSession session = new PipeLineSession();
    List<URL> inputs = new ArrayList<URL>();
    for (int i = 0; i < 5; i++) {
        inputs.add(TestFileUtils.getTestFileURL("/PdfPipe/MailWithAttachments/mailWithWordAttachment.msg"));
    }
    String expected = TestFileUtils.getTestFileMessage("/PdfPipe/xml-results/mailWithWordAttachment.xml").asString();
    inputs.parallelStream().forEach(item -> {
        try {
            PipeRunResult prr = pipe.doPipe(Message.asMessage(new File(item.toURI())), session);
            Message result = prr.getResult();
            MatchUtils.assertXmlEquals("Conversion XML does not match", applyIgnores(result.asString()), applyIgnores(expected), true);
        } catch (Exception e) {
            fail("Failed to execute test " + e.getMessage());
        }
    });
}
Also used : PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) Message(nl.nn.adapterframework.stream.Message) ArrayList(java.util.ArrayList) PipeLineSession(nl.nn.adapterframework.core.PipeLineSession) PipeForward(nl.nn.adapterframework.core.PipeForward) File(java.io.File) URL(java.net.URL) IOException(java.io.IOException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) Test(org.junit.Test)

Example 12 with Message

use of nl.nn.adapterframework.stream.Message in project iaf by ibissource.

the class PdfPipeTest method attachFileToMainDoc.

@Test
public void attachFileToMainDoc() throws Exception {
    pipe.setAction(DocumentAction.COMBINE);
    pipe.setMainDocumentSessionKey("mainDoc");
    pipe.setFilenameToAttachSessionKey("attachedFilename");
    Message mainDoc = TestFileUtils.getNonRepeatableTestFileMessage("/PdfPipe/combine/maindoc.pdf");
    session.put(pipe.getMainDocumentSessionKey(), mainDoc);
    session.put(pipe.getFilenameToAttachSessionKey(), "attachedFile");
    Message fileToAttachMainDoc = TestFileUtils.getNonRepeatableTestFileMessage("/PdfPipe/combine/filetobeattached.pdf");
    PipeRunResult prr = doPipe(pipe, fileToAttachMainDoc, session);
    Message result = prr.getResult();
    Path resultingFile = Files.createTempFile("PdfPipeTest", ".pdf");
    Files.copy(result.asInputStream(), resultingFile, StandardCopyOption.REPLACE_EXISTING);
    String expectedFilePath = new File(TestFileUtils.getTestFileURL("/PdfPipe/combine/combined.pdf").toURI()).getCanonicalPath();
    // comparison
    PDFUtil pdfUtil = new PDFUtil();
    assertTrue(pdfUtil.compare(expectedFilePath, resultingFile.toFile().getCanonicalPath()));
}
Also used : Path(java.nio.file.Path) PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) Message(nl.nn.adapterframework.stream.Message) PDFUtil(com.testautomationguru.utility.PDFUtil) File(java.io.File) Test(org.junit.Test)

Example 13 with Message

use of nl.nn.adapterframework.stream.Message in project iaf by ibissource.

the class CmisSender method sendMessageForActionCreate.

private Message sendMessageForActionCreate(Session cmisSession, Message message, PipeLineSession session, ParameterValueList pvl) throws SenderException {
    String fileName = null;
    try {
        fileName = session.getMessage(getParameterOverriddenAttributeValue(pvl, "filenameSessionKey", getFilenameSessionKey())).asString();
    } catch (IOException e) {
        throw new SenderException("Unable to get filename from session key [" + getFilenameSessionKey() + "]", e);
    }
    String mediaType;
    Map<String, Object> props = new HashMap<String, Object>();
    Element cmisElement;
    try {
        if (XmlUtils.isWellFormed(message, "cmis")) {
            cmisElement = XmlUtils.buildElement(message);
        } else {
            cmisElement = XmlUtils.buildElement("<cmis/>");
        }
        String objectTypeId = XmlUtils.getChildTagAsString(cmisElement, "objectTypeId");
        if (StringUtils.isNotEmpty(objectTypeId)) {
            props.put(PropertyIds.OBJECT_TYPE_ID, objectTypeId);
        } else {
            props.put(PropertyIds.OBJECT_TYPE_ID, "cmis:document");
        }
        String name = XmlUtils.getChildTagAsString(cmisElement, "name");
        if (StringUtils.isEmpty(fileName)) {
            fileName = XmlUtils.getChildTagAsString(cmisElement, "fileName");
        }
        mediaType = XmlUtils.getChildTagAsString(cmisElement, "mediaType");
        if (StringUtils.isNotEmpty(name)) {
            props.put(PropertyIds.NAME, name);
        } else if (StringUtils.isNotEmpty(fileName)) {
            props.put(PropertyIds.NAME, fileName);
        } else {
            props.put(PropertyIds.NAME, "[unknown]");
        }
        Element propertiesElement = XmlUtils.getFirstChildTag(cmisElement, "properties");
        if (propertiesElement != null) {
            processProperties(propertiesElement, props);
        }
    } catch (DomBuilderException e) {
        throw new SenderException(getLogPrefix() + "exception parsing [" + message + "]", e);
    }
    if (StringUtils.isEmpty(mediaType)) {
        mediaType = getDefaultMediaType();
    }
    ContentStream contentStream;
    try {
        Message inputFromSessionKey = session.getMessage(getParameterOverriddenAttributeValue(pvl, "fileSessionKey", getFileSessionKey()));
        if (convert2Base64 && inputFromSessionKey.asObject() instanceof String) {
            inputFromSessionKey = new Message(Base64.decodeBase64(inputFromSessionKey.asByteArray()));
        }
        long fileLength = inputFromSessionKey.size();
        contentStream = cmisSession.getObjectFactory().createContentStream(fileName, fileLength, mediaType, inputFromSessionKey.asInputStream());
    } catch (IOException e) {
        throw new SenderException(e);
    }
    if (isUseRootFolder()) {
        Folder folder = cmisSession.getRootFolder();
        Document document = folder.createDocument(props, contentStream, VersioningState.NONE);
        log.debug(getLogPrefix() + "created new document [ " + document.getId() + "]");
        return new Message(document.getId());
    }
    ObjectId objectId = cmisSession.createDocument(props, null, contentStream, VersioningState.NONE);
    log.debug(getLogPrefix() + "created new document [ " + objectId.getId() + "]");
    return new Message(objectId.getId());
}
Also used : Message(nl.nn.adapterframework.stream.Message) HashMap(java.util.HashMap) ObjectId(org.apache.chemistry.opencmis.client.api.ObjectId) Element(org.w3c.dom.Element) IOException(java.io.IOException) Folder(org.apache.chemistry.opencmis.client.api.Folder) Document(org.apache.chemistry.opencmis.client.api.Document) ContentStream(org.apache.chemistry.opencmis.commons.data.ContentStream) CmisObject(org.apache.chemistry.opencmis.client.api.CmisObject) DomBuilderException(nl.nn.adapterframework.util.DomBuilderException) SenderException(nl.nn.adapterframework.core.SenderException)

Example 14 with Message

use of nl.nn.adapterframework.stream.Message in project iaf by ibissource.

the class CmisSender method sendMessageForActionGet.

private Message sendMessageForActionGet(Session cmisSession, Message message, PipeLineSession session, ParameterValueList pvl) throws SenderException, TimeoutException {
    if (Message.isEmpty(message)) {
        throw new SenderException(getLogPrefix() + "input string cannot be empty but must contain a documentId");
    }
    CmisObject object = null;
    try {
        object = getCmisObject(cmisSession, message);
    } catch (CmisObjectNotFoundException e) {
        if (StringUtils.isNotEmpty(getResultOnNotFound())) {
            log.info(getLogPrefix() + "document with id [" + message + "] not found", e);
            return new Message(getResultOnNotFound());
        }
        throw new SenderException(e);
    }
    Document document = (Document) object;
    try {
        boolean getProperties = isGetProperties();
        boolean getDocumentContent = isGetDocumentContent();
        if (pvl != null) {
            if (pvl.contains("getProperties"))
                getProperties = pvl.get("getProperties").asBooleanValue(isGetProperties());
            if (pvl.contains("getDocumentContent"))
                getDocumentContent = pvl.get("getDocumentContent").asBooleanValue(isGetDocumentContent());
        }
        if (isStreamResultToServlet()) {
            HttpServletResponse response = (HttpServletResponse) session.get(PipeLineSession.HTTP_RESPONSE_KEY);
            ContentStream contentStream = document.getContentStream();
            InputStream inputStream = contentStream.getStream();
            String contentType = contentStream.getMimeType();
            if (StringUtils.isNotEmpty(contentType)) {
                log.debug(getLogPrefix() + "setting response Content-Type header [" + contentType + "]");
                response.setHeader("Content-Type", contentType);
            }
            String contentDisposition = "attachment; filename=\"" + contentStream.getFileName() + "\"";
            log.debug(getLogPrefix() + "setting response Content-Disposition header [" + contentDisposition + "]");
            response.setHeader("Content-Disposition", contentDisposition);
            OutputStream outputStream;
            outputStream = response.getOutputStream();
            Misc.streamToStream(inputStream, outputStream);
            log.debug(getLogPrefix() + "copied document content input stream [" + inputStream + "] to output stream [" + outputStream + "]");
            return Message.nullMessage();
        } else if (getProperties) {
            if (getDocumentContent) {
                ContentStream contentStream = document.getContentStream();
                InputStream inputStream = contentStream.getStream();
                if (convert2Base64) {
                    byte[] bytes = Misc.streamToBytes(inputStream);
                    session.put(getFileSessionKey(), Base64.encodeBase64String(bytes));
                } else {
                    session.put(getFileSessionKey(), inputStream);
                }
            }
            XmlBuilder cmisXml = new XmlBuilder("cmis");
            XmlBuilder propertiesXml = new XmlBuilder("properties");
            for (Iterator<Property<?>> it = document.getProperties().iterator(); it.hasNext(); ) {
                Property<?> property = it.next();
                propertiesXml.addSubElement(CmisUtils.getPropertyXml(property));
            }
            cmisXml.addSubElement(propertiesXml);
            return new Message(cmisXml.toXML());
        } else {
            ContentStream contentStream = document.getContentStream();
            InputStream inputStream = contentStream.getStream();
            if (StringUtils.isNotEmpty(getFileSessionKey())) {
                if (convert2Base64) {
                    byte[] bytes = Misc.streamToBytes(inputStream);
                    session.put(getFileSessionKey(), Base64.encodeBase64String(bytes));
                } else {
                    session.put(getFileSessionKey(), inputStream);
                }
                return Message.nullMessage();
            }
            session.put("contentStream:MimeType", contentStream.getMimeType());
            session.put("contentStream:Filename", contentStream.getFileName());
            MessageContext context = new MessageContext();
            context.withName(contentStream.getFileName()).withMimeType(contentStream.getMimeType());
            return new Message(inputStream, context);
        }
    } catch (IOException e) {
        throw new SenderException(e);
    }
}
Also used : Message(nl.nn.adapterframework.stream.Message) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Document(org.apache.chemistry.opencmis.client.api.Document) ContentStream(org.apache.chemistry.opencmis.commons.data.ContentStream) CmisObjectNotFoundException(org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException) Iterator(java.util.Iterator) XmlBuilder(nl.nn.adapterframework.util.XmlBuilder) CmisObject(org.apache.chemistry.opencmis.client.api.CmisObject) MessageContext(nl.nn.adapterframework.stream.MessageContext) SenderException(nl.nn.adapterframework.core.SenderException) Property(org.apache.chemistry.opencmis.client.api.Property)

Example 15 with Message

use of nl.nn.adapterframework.stream.Message in project iaf by ibissource.

the class CmisSender method sendMessageForActionFind.

private Message sendMessageForActionFind(Session cmisSession, Message message) throws SenderException, TimeoutException {
    Element queryElement = null;
    try {
        if (XmlUtils.isWellFormed(message, "query")) {
            queryElement = XmlUtils.buildElement(message);
        } else {
            queryElement = XmlUtils.buildElement("<query/>");
        }
    } catch (DomBuilderException e) {
        throw new SenderException(e);
    }
    String statement = XmlUtils.getChildTagAsString(queryElement, "statement");
    String maxItems = XmlUtils.getChildTagAsString(queryElement, "maxItems");
    String skipCount = XmlUtils.getChildTagAsString(queryElement, "skipCount");
    String searchAllVersions = XmlUtils.getChildTagAsString(queryElement, "searchAllVersions");
    String includeAllowableActions = XmlUtils.getChildTagAsString(queryElement, "includeAllowableActions");
    OperationContext operationContext = cmisSession.createOperationContext();
    if (StringUtils.isNotEmpty(maxItems)) {
        operationContext.setMaxItemsPerPage(Integer.parseInt(maxItems));
    }
    boolean sav = false;
    if (StringUtils.isNotEmpty(searchAllVersions)) {
        sav = Boolean.parseBoolean(searchAllVersions);
    }
    if (StringUtils.isNotEmpty(includeAllowableActions)) {
        operationContext.setIncludeAllowableActions(Boolean.parseBoolean(searchAllVersions));
    }
    XmlBuilder cmisXml = new XmlBuilder("cmis");
    ItemIterable<QueryResult> q = cmisSession.query(statement, sav, operationContext);
    if (q == null) {
        cmisXml.addAttribute("totalNumItems", 0);
    } else {
        if (StringUtils.isNotEmpty(skipCount)) {
            long sc = Long.parseLong(skipCount);
            q = q.skipTo(sc);
        }
        if (StringUtils.isNotEmpty(maxItems)) {
            q = q.getPage();
        }
        XmlBuilder rowsetXml = new XmlBuilder("rowset");
        for (QueryResult qResult : q) {
            XmlBuilder rowXml = new XmlBuilder("row");
            for (PropertyData<?> property : qResult.getProperties()) {
                rowXml.addSubElement(CmisUtils.getPropertyXml(property));
            }
            rowsetXml.addSubElement(rowXml);
        }
        cmisXml.addAttribute("totalNumItems", q.getTotalNumItems());
        cmisXml.addSubElement(rowsetXml);
    }
    return new Message(cmisXml.toXML());
}
Also used : OperationContext(org.apache.chemistry.opencmis.client.api.OperationContext) QueryResult(org.apache.chemistry.opencmis.client.api.QueryResult) Message(nl.nn.adapterframework.stream.Message) Element(org.w3c.dom.Element) XmlBuilder(nl.nn.adapterframework.util.XmlBuilder) DomBuilderException(nl.nn.adapterframework.util.DomBuilderException) SenderException(nl.nn.adapterframework.core.SenderException)

Aggregations

Message (nl.nn.adapterframework.stream.Message)598 Test (org.junit.Test)385 PipeLineSession (nl.nn.adapterframework.core.PipeLineSession)220 PipeRunResult (nl.nn.adapterframework.core.PipeRunResult)114 IOException (java.io.IOException)112 SenderException (nl.nn.adapterframework.core.SenderException)97 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)54 ParameterValueList (nl.nn.adapterframework.parameters.ParameterValueList)54 Parameter (nl.nn.adapterframework.parameters.Parameter)52 PipeForward (nl.nn.adapterframework.core.PipeForward)41 Date (java.util.Date)37 TimeoutException (nl.nn.adapterframework.core.TimeoutException)31 UrlMessage (nl.nn.adapterframework.stream.UrlMessage)31 PipeRunException (nl.nn.adapterframework.core.PipeRunException)30 ByteArrayInputStream (java.io.ByteArrayInputStream)29 InputStream (java.io.InputStream)29 ParameterList (nl.nn.adapterframework.parameters.ParameterList)28 ListenerException (nl.nn.adapterframework.core.ListenerException)27 ParameterException (nl.nn.adapterframework.core.ParameterException)25 SAXException (org.xml.sax.SAXException)19