Search in sources :

Example 1 with MessageContext

use of nl.nn.adapterframework.stream.MessageContext 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 2 with MessageContext

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

the class JMSFacade method getContext.

public MessageContext getContext(javax.jms.Message message) throws JMSException {
    MessageContext result = new MessageContext();
    result.withName(message.getJMSMessageID());
    result.withModificationTime(message.getJMSTimestamp());
    Enumeration<String> names = message.getPropertyNames();
    while (names.hasMoreElements()) {
        String name = names.nextElement();
        result.put(name, message.getObjectProperty(name));
    }
    return result;
}
Also used : MessageContext(nl.nn.adapterframework.stream.MessageContext)

Example 3 with MessageContext

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

the class MessageUtils method getContext.

/**
 * Fetch metadata from the {@link HttpServletRequest} such as Content-Length, Content-Type (mimetype + charset)
 */
public static MessageContext getContext(HttpServletRequest request) {
    MessageContext result = new MessageContext();
    result.withCharset(request.getCharacterEncoding());
    int contentLength = request.getContentLength();
    if (contentLength >= 0) {
        result.withSize(contentLength);
    }
    String contentType = request.getContentType();
    if (StringUtils.isNotEmpty(contentType)) {
        try {
            ContentType parsedContentType = ContentType.parse(contentType);
            if (parsedContentType.getMimeType() != null) {
                result.withMimeType(parsedContentType.getMimeType());
            }
            Charset parsedCharset = parsedContentType.getCharset();
            if (parsedCharset != null) {
                result.withCharset(parsedCharset.displayName());
            }
        } catch (Exception e) {
            // For now just log when we cannot parse, perhaps we should abort the request?
            LOG.warn("unable to parse charset from contentType [{}]", contentType, e);
        }
    }
    Enumeration<String> names = request.getHeaderNames();
    while (names.hasMoreElements()) {
        String name = names.nextElement();
        result.put(name, request.getHeader(name));
    }
    return result;
}
Also used : ContentType(org.apache.http.entity.ContentType) Charset(java.nio.charset.Charset) MessageContext(nl.nn.adapterframework.stream.MessageContext) IOException(java.io.IOException)

Example 4 with MessageContext

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

the class ParameterTest method testContextKeyWithSessionKeyAndXPath.

@Test
public void testContextKeyWithSessionKeyAndXPath() throws ConfigurationException, ParameterException {
    Parameter p = new Parameter();
    p.setName("dummy");
    p.setSessionKey("fakeSessionKey");
    p.setContextKey("fakeContextKey");
    p.setXpathExpression("count(root/a)");
    p.configure();
    Message input = new Message("fakeMessage1", new MessageContext().with("fakeContextKey", "<root><a/><a/></root>"));
    Message sessionValue = new Message("fakeMessage2", new MessageContext().with("fakeContextKey", "<root><a/><a/><a/></root>"));
    PipeLineSession session = new PipeLineSession();
    session.put("fakeSessionKey", sessionValue);
    ParameterValueList alreadyResolvedParameters = new ParameterValueList();
    assertEquals("3", p.getValue(alreadyResolvedParameters, input, session, false));
}
Also used : Message(nl.nn.adapterframework.stream.Message) PipeLineSession(nl.nn.adapterframework.core.PipeLineSession) MessageContext(nl.nn.adapterframework.stream.MessageContext) Test(org.junit.Test)

Example 5 with MessageContext

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

the class ParameterTest method testContextKeyWithSessionKey.

@Test
public void testContextKeyWithSessionKey() throws ConfigurationException, ParameterException {
    Parameter p = new Parameter();
    p.setName("dummy");
    p.setSessionKey("fakeSessionKey");
    p.setContextKey("fakeContextKey");
    p.configure();
    Message input = new Message("fakeMessage1", new MessageContext().with("fakeContextKey", "fakeContextValue1"));
    Message sessionValue = new Message("fakeMessage2", new MessageContext().with("fakeContextKey", "fakeContextValue2"));
    PipeLineSession session = new PipeLineSession();
    session.put("fakeSessionKey", sessionValue);
    ParameterValueList alreadyResolvedParameters = new ParameterValueList();
    assertEquals("fakeContextValue2", p.getValue(alreadyResolvedParameters, input, session, false));
}
Also used : Message(nl.nn.adapterframework.stream.Message) PipeLineSession(nl.nn.adapterframework.core.PipeLineSession) MessageContext(nl.nn.adapterframework.stream.MessageContext) Test(org.junit.Test)

Aggregations

MessageContext (nl.nn.adapterframework.stream.MessageContext)7 Message (nl.nn.adapterframework.stream.Message)5 PipeLineSession (nl.nn.adapterframework.core.PipeLineSession)4 Test (org.junit.Test)4 IOException (java.io.IOException)2 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Charset (java.nio.charset.Charset)1 Iterator (java.util.Iterator)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 SenderException (nl.nn.adapterframework.core.SenderException)1 XmlBuilder (nl.nn.adapterframework.util.XmlBuilder)1 CmisObject (org.apache.chemistry.opencmis.client.api.CmisObject)1 Document (org.apache.chemistry.opencmis.client.api.Document)1 Property (org.apache.chemistry.opencmis.client.api.Property)1 ContentStream (org.apache.chemistry.opencmis.commons.data.ContentStream)1 CmisObjectNotFoundException (org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException)1 ContentType (org.apache.http.entity.ContentType)1