Search in sources :

Example 1 with MultipartEntityBuilder

use of nl.nn.adapterframework.http.mime.MultipartEntityBuilder in project iaf by ibissource.

the class HttpSender method createMultiPartEntity.

protected HttpEntity createMultiPartEntity(String message, ParameterValueList parameters, IPipeLineSession session) throws SenderException {
    MultipartEntityBuilder entity = MultipartEntityBuilder.create();
    entity.setCharset(Charset.forName(getCharSet()));
    if (isMtomEnabled())
        entity.setMtomMultipart();
    if (StringUtils.isNotEmpty(getInputMessageParam())) {
        entity.addPart(createMultipartBodypart(getInputMessageParam(), message));
        log.debug(getLogPrefix() + "appended stringpart [" + getInputMessageParam() + "] with value [" + message + "]");
    }
    if (parameters != null) {
        for (int i = 0; i < parameters.size(); i++) {
            ParameterValue pv = parameters.getParameterValue(i);
            String paramType = pv.getDefinition().getType();
            String name = pv.getDefinition().getName();
            if (Parameter.TYPE_INPUTSTREAM.equals(paramType)) {
                Object value = pv.getValue();
                if (value instanceof InputStream) {
                    InputStream fis = (InputStream) value;
                    String fileName = null;
                    String sessionKey = pv.getDefinition().getSessionKey();
                    if (sessionKey != null) {
                        fileName = (String) session.get(sessionKey + "Name");
                    }
                    entity.addPart(createMultipartBodypart(name, fis, fileName));
                    log.debug(getLogPrefix() + "appended filepart [" + name + "] with value [" + value + "] and name [" + fileName + "]");
                } else {
                    throw new SenderException(getLogPrefix() + "unknown inputstream [" + value.getClass() + "] for parameter [" + name + "]");
                }
            } else {
                String value = pv.asStringValue("");
                entity.addPart(createMultipartBodypart(name, value));
                log.debug(getLogPrefix() + "appended stringpart [" + name + "] with value [" + value + "]");
            }
        }
    }
    if (StringUtils.isNotEmpty(getMultipartXmlSessionKey())) {
        String multipartXml = (String) session.get(getMultipartXmlSessionKey());
        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();
                    // String partType = partElement.getAttribute("type");
                    String partName = partElement.getAttribute("name");
                    String partSessionKey = partElement.getAttribute("sessionKey");
                    String partMimeType = partElement.getAttribute("mimeType");
                    Object partObject = session.get(partSessionKey);
                    if (partObject instanceof InputStream) {
                        InputStream fis = (InputStream) partObject;
                        entity.addPart(createMultipartBodypart(partSessionKey, fis, partName, partMimeType));
                        log.debug(getLogPrefix() + "appended filepart [" + partSessionKey + "] with value [" + partObject + "] and name [" + partName + "]");
                    } else {
                        String partValue = (String) session.get(partSessionKey);
                        entity.addPart(createMultipartBodypart(partName, partValue, partMimeType));
                        log.debug(getLogPrefix() + "appended stringpart [" + partSessionKey + "]  with value [" + partValue + "]");
                    }
                }
            }
        }
    }
    // entity.seContentType(contentTypeMtom);
    return entity.build();
}
Also used : MultipartEntityBuilder(nl.nn.adapterframework.http.mime.MultipartEntityBuilder) ParameterValue(nl.nn.adapterframework.parameters.ParameterValue) 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)

Aggregations

InputStream (java.io.InputStream)1 SenderException (nl.nn.adapterframework.core.SenderException)1 MultipartEntityBuilder (nl.nn.adapterframework.http.mime.MultipartEntityBuilder)1 ParameterValue (nl.nn.adapterframework.parameters.ParameterValue)1 DomBuilderException (nl.nn.adapterframework.util.DomBuilderException)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1