Search in sources :

Example 31 with Message

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

the class HttpSender method handleMultipartResponse.

/**
 * return the first part as Message and put the other parts as InputStream in the PipeLineSession
 */
public static Message handleMultipartResponse(String mimeType, InputStream inputStream, PipeLineSession session) throws IOException {
    Message result = null;
    try {
        // the entire InputStream will be read here!
        InputStreamDataSource dataSource = new InputStreamDataSource(mimeType, inputStream);
        MimeMultipart mimeMultipart = new MimeMultipart(dataSource);
        for (int i = 0; i < mimeMultipart.getCount(); i++) {
            BodyPart bodyPart = mimeMultipart.getBodyPart(i);
            if (i == 0) {
                result = new PartMessage(bodyPart);
            } else {
                session.put("multipart" + i, new PartMessage(bodyPart));
            }
        }
    } catch (MessagingException e) {
        throw new IOException("Could not read mime multipart response", e);
    }
    return result;
}
Also used : FormBodyPart(org.apache.http.entity.mime.FormBodyPart) BodyPart(javax.mail.BodyPart) Message(nl.nn.adapterframework.stream.Message) MimeMultipart(javax.mail.internet.MimeMultipart) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException)

Example 32 with Message

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

the class HttpSender method elementToFormBodyPart.

protected FormBodyPart elementToFormBodyPart(Element element, PipeLineSession session) throws IOException {
    // Name of the part
    String partName = element.getAttribute("name");
    // SessionKey to retrieve data from
    String partSessionKey = element.getAttribute("sessionKey");
    // MimeType of the part
    String partMimeType = element.getAttribute("mimeType");
    Message partObject = session.getMessage(partSessionKey);
    if (partObject.isBinary()) {
        return createMultipartBodypart(partSessionKey, partObject.asInputStream(), partName, partMimeType);
    } else {
        return createMultipartBodypart(partName, partObject.asString(), partMimeType);
    }
}
Also used : Message(nl.nn.adapterframework.stream.Message)

Example 33 with Message

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

the class HttpSender method createMultiPartEntity.

protected HttpEntity createMultiPartEntity(String message, ParameterValueList parameters, PipeLineSession session) throws SenderException, IOException {
    MultipartEntityBuilder entity = MultipartEntityBuilder.create();
    entity.setCharset(Charset.forName(getCharSet()));
    if (postType.equals(PostType.MTOM))
        entity.setMtomMultipart();
    if (StringUtils.isNotEmpty(getFirstBodyPartName())) {
        entity.addPart(createMultipartBodypart(getFirstBodyPartName(), message));
        if (log.isDebugEnabled())
            log.debug(getLogPrefix() + "appended stringpart [" + getFirstBodyPartName() + "] with value [" + message + "]");
    }
    if (parameters != null) {
        for (ParameterValue pv : parameters) {
            String name = pv.getDefinition().getName();
            // Skip parameters that are configured as ignored
            if (skipParameter(name))
                continue;
            Message msg = pv.asMessage();
            if (msg.isBinary()) {
                InputStream fis = msg.asInputStream();
                String fileName = null;
                String sessionKey = pv.getDefinition().getSessionKey();
                if (sessionKey != null) {
                    fileName = session.getMessage(sessionKey + "Name").asString();
                }
                entity.addPart(createMultipartBodypart(name, fis, fileName));
                if (log.isDebugEnabled())
                    log.debug(getLogPrefix() + "appended filepart [" + name + "] with value [" + msg + "] and name [" + fileName + "]");
            } else {
                String value = msg.asString();
                entity.addPart(createMultipartBodypart(name, value));
                if (log.isDebugEnabled())
                    log.debug(getLogPrefix() + "appended stringpart [" + name + "] with value [" + value + "]");
            }
        }
    }
    if (StringUtils.isNotEmpty(getMultipartXmlSessionKey())) {
        String multipartXml = session.getMessage(getMultipartXmlSessionKey()).asString();
        log.debug(getLogPrefix() + "building multipart message with MultipartXmlSessionKey [" + multipartXml + "]");
        if (StringUtils.isEmpty(multipartXml)) {
            log.warn(getLogPrefix() + "sessionKey [" + getMultipartXmlSessionKey() + "] is empty");
        } else {
            Element partsElement;
            try {
                partsElement = XmlUtils.buildElement(multipartXml);
            } catch (DomBuilderException e) {
                throw new SenderException(getLogPrefix() + "error building multipart xml", e);
            }
            Collection<Node> parts = XmlUtils.getChildTags(partsElement, "part");
            if (parts == null || parts.size() == 0) {
                log.warn(getLogPrefix() + "no part(s) in multipart xml [" + multipartXml + "]");
            } else {
                Iterator<Node> iter = parts.iterator();
                while (iter.hasNext()) {
                    Element partElement = (Element) iter.next();
                    entity.addPart(elementToFormBodyPart(partElement, session));
                }
            }
        }
    }
    return entity.build();
}
Also used : MultipartEntityBuilder(nl.nn.adapterframework.http.mime.MultipartEntityBuilder) ParameterValue(nl.nn.adapterframework.parameters.ParameterValue) Message(nl.nn.adapterframework.stream.Message) Base64InputStream(org.apache.commons.codec.binary.Base64InputStream) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) DomBuilderException(nl.nn.adapterframework.util.DomBuilderException) SenderException(nl.nn.adapterframework.core.SenderException)

Example 34 with Message

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

the class MultipartHttpSender method elementToFormBodyPart.

@Override
protected FormBodyPart elementToFormBodyPart(Element element, PipeLineSession session) throws IOException {
    // File or otherwise
    String partType = element.getAttribute("type");
    // Name of the part
    String partName = element.getAttribute("name");
    // Name of the file
    String fileName = (StringUtils.isNotEmpty(element.getAttribute("filename"))) ? element.getAttribute("filename") : element.getAttribute("fileName");
    // SessionKey to retrieve data from
    String sessionKey = element.getAttribute("sessionKey");
    // MimeType of the part
    String mimeType = element.getAttribute("mimeType");
    // Value when SessionKey is empty or not set
    String partValue = element.getAttribute("value");
    Message partObject = session.getMessage(sessionKey);
    if (partObject != null && partObject.isBinary()) {
        InputStream fis = partObject.asInputStream();
        if (StringUtils.isNotEmpty(fileName)) {
            return createMultipartBodypart(partName, fis, fileName, mimeType);
        } else if ("file".equalsIgnoreCase(partType)) {
            return createMultipartBodypart(sessionKey, fis, partName, mimeType);
        } else {
            return createMultipartBodypart(partName, fis, null, mimeType);
        }
    } else {
        String value = partObject.asString();
        if (StringUtils.isEmpty(value))
            value = partValue;
        return createMultipartBodypart(partName, value, mimeType);
    }
}
Also used : Message(nl.nn.adapterframework.stream.Message) InputStream(java.io.InputStream)

Example 35 with Message

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

the class ForEachAttachmentPipe method itemToMessage.

@Override
protected Message itemToMessage(A item) throws SenderException {
    FS ifs = getFileSystem();
    XmlBuilder result = new XmlBuilder("attachment");
    try {
        result.addAttribute("name", ifs.getAttachmentName(item));
        result.addAttribute("filename", ifs.getAttachmentFileName(item));
        result.addAttribute("contentType", ifs.getAttachmentContentType(item));
        result.addAttribute("size", ifs.getAttachmentSize(item));
        Map<String, Object> attachmentProperties = ifs.getAdditionalAttachmentProperties(item);
        if (attachmentProperties != null) {
            XmlBuilder properties = new XmlBuilder("properties");
            Set<String> excludes = getExcludePropertiesSet();
            Set<String> includes = getOnlyPropertiesSet();
            if (excludes != null || includes == null) {
                for (Entry<String, Object> entry : attachmentProperties.entrySet()) {
                    if (excludes == null || !excludes.contains(entry.getKey())) {
                        XmlBuilder property = new XmlBuilder("property");
                        property.addAttribute("name", entry.getKey());
                        if (entry.getValue() != null) {
                            property.setValue(entry.getValue().toString());
                        }
                        properties.addSubElement(property);
                    }
                }
            } else {
                for (String key : includes) {
                    XmlBuilder property = new XmlBuilder("property");
                    property.addAttribute("name", key);
                    Object value = attachmentProperties.get(key);
                    if (value != null) {
                        property.setValue(value.toString());
                    }
                    properties.addSubElement(property);
                }
            }
            result.addSubElement(properties);
        }
    } catch (Exception e) {
        throw new SenderException("unable to read attachment attributes", e);
    }
    return new Message(result.toXML());
}
Also used : Message(nl.nn.adapterframework.stream.Message) XmlBuilder(nl.nn.adapterframework.util.XmlBuilder) SenderException(nl.nn.adapterframework.core.SenderException) PipeStartException(nl.nn.adapterframework.core.PipeStartException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) 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