Java SOAP Message addAttachment(SOAPMessage soapMessage, String payloadId, String contentType, byte[] content)

Here you can find the source of addAttachment(SOAPMessage soapMessage, String payloadId, String contentType, byte[] content)

Description

add Attachment

License

Apache License

Declaration

public static void addAttachment(SOAPMessage soapMessage,
            String payloadId, String contentType, byte[] content)
            throws Exception 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import javax.xml.soap.AttachmentPart;

import javax.xml.soap.SOAPMessage;

import java.io.ByteArrayInputStream;

import java.io.InputStream;

import java.util.HashMap;

import java.util.Map;

public class Main {
    public static void addAttachment(SOAPMessage soapMessage,
            String contentId, String contentType, InputStream content)
            throws Exception {
        addAttachment(soapMessage, contentId, contentType, content,
                new HashMap<String, String>());
    }// w w  w.  j  a v  a  2s .c  o m

    public static void addAttachment(SOAPMessage soapMessage,
            String payloadId, String contentType, byte[] content)
            throws Exception {
        addAttachment(soapMessage, payloadId, contentType,
                new ByteArrayInputStream(content));
    }

    public static void addAttachment(SOAPMessage soapMessage,
            String payloadId, String contentType, byte[] content,
            Map<String, String> mimeHeaders) throws Exception {
        addAttachment(soapMessage, payloadId, contentType,
                new ByteArrayInputStream(content), mimeHeaders);
    }

    public static void addAttachment(SOAPMessage soapMessage,
            String contentId, String contentType, InputStream content,
            Map<String, String> headers) throws Exception {
        AttachmentPart attachmentPart = soapMessage.createAttachmentPart();
        //attachmentPart.setContentId(encodeContentId(contentId));
        attachmentPart.setMimeHeader("Content-ID",
                encodeContentId(contentId));
        //        attachmentPart.setMimeHeader("Content-ID",contentId);
        attachmentPart.setContentType(contentType);
        attachmentPart.setRawContent(content, contentType);
        for (Map.Entry<String, String> header : headers.entrySet()) {
            attachmentPart
                    .addMimeHeader(header.getKey(), header.getValue());
        }
        soapMessage.addAttachmentPart(attachmentPart);
        soapMessage.saveChanges();
        soapMessage.getMimeHeaders().setHeader(
                "Content-Type",
                soapMessage.getMimeHeaders().getHeader("Content-Type")[0]
                        + "; start=\"<soapPart@jentrata.org>\"");

    }

    public static String encodeContentId(String contentId) {
        if (contentId != null && !contentId.startsWith("<")
                && !contentId.endsWith(">")) {
            return "<" + contentId + ">";
        } else {
            return contentId;
        }
    }
}

Related

  1. addMessageHandler(Object binding, SOAPHandler handler)
  2. addMimeHeader(SOAPMessage message, String name, String value)
  3. addNamespace(SOAPMessage message, String prefix, String uri)
  4. constructMessage(String mimeHdrsFile, String msgFile)