3

Can someone please tell me how can I set the content type for a soap service in java?I want to set the content type as "multipart/related". I searched trough a lot of question but I cannot figure it out how can I do it.

I have something like this:

The proxy class:

@WebService(name = "DocumentManagementForUnderwritingService",targetNamespace = "myNameSpace")
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
@XmlSeeAlso({
   //some other classes
})
public interface DocumentManagementForUnderwritingService {

  @WebMethod
    @WebResult(name = "uploadDocumentResponse", targetNamespace = "myNameSpace", partName = "Body")
    public UploadDocumentResponse uploadDocument(@WebParam(name = "uploadDocumentRequest", targetNamespace = ""myNameSpace", partName = "Body")
    UploadDocumentRequest body) throws ServiceException, SystemException ;

}

Request Class

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "uploadDocumentRequest", propOrder = {
    "positionId",
    "username",
    "documentItemId",
    "documentCod"
    })
    public class UploadDocumentRequest {

    @XmlElement(required = true)
    protected String positionId;
    @XmlElement(required = true)
    protected String username;
    protected String documentItemId;
    protected String documentCod;

    //setters & getters 
}

In the class who calls the service (and I think here I have to set somehow the content type)

 BindingProvider bp = (BindingProvider) proxy;
  UploadDocumentRequest request = new UploadDocumentRequest();
  request.setDocumentItemId(input.getDocumentItemId());
  request.setPositionId(input.getPositionId());

UploadDocumentResponse response = proxy.uploadDocument(request);

I also chained a handler where I'm trying to set the Mime_type and adding an attachment:

private Static final String MULTIPART_MYME_TYPE="multipart/related";

 @Override
    public boolean handleMessage(SOAPMessageContext context) {
        Boolean isRequest = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

        try {

            if (isRequest) {

                if (context.containsKey("pdf")) {
                    byte[] arr = (byte[]) context.get("pdf");

                    SOAPMessage soapMsg = context.getMessage();
                    soapMsg.getMimeHeaders().addHeader("Content-type", MULTIPART_MIME_TYPE);
                    AttachmentPart attachment = createAttachment(soapMsg, arr, "test.pdf", context);
                    soapMsg.addAttachmentPart(attachment);
                    Iterator<AttachmentPart> it = context.getMessage().getAttachments();
                    while (it.hasNext()) {
                        AttachmentPart att = it.next();
                        System.out.println(att.getContent());
                    }
                    System.out.println("ok");
                }
            }
        } catch (Exception e) {
            return false;
        }

        return true;

    }


 private AttachmentPart createAttachment(SOAPMessage msg, byte[] payload, String fileId, SOAPMessageContext context) {

        @SuppressWarnings("unchecked")
        Map<String, DataHandler> attachmentsMap = (Map<String, DataHandler>) context.get(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS);


        ByteArrayDataSource ds = new ByteArrayDataSource(payload, MULTIPART_MIME_TYPE);
        DataHandler dh = new DataHandler(ds);

        AttachmentPart attachmentPart = msg.createAttachmentPart();

        attachmentPart.setContent(new ByteArrayInputStream(payload), MULTIPART_MIME_TYPE);
        attachmentPart.setContentId(fileId);

        String contentDisposition = "Content-Disposition: attachment; name=\"" + fileId + "\"";
        attachmentPart.addMimeHeader("Content-Disposition", contentDisposition);

        msg.addAttachmentPart(attachmentPart);

        attachmentsMap.put(fileId, dh);

        context.put(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS, attachmentsMap);

        context.getMessage().getAttachments();

        return attachmentPart;
    }

Thank you in advance!

Aditzu
  • 696
  • 1
  • 14
  • 25
  • And which WS implementation you are using? I dont think its accessible via JEE api anyway - look here http://stackoverflow.com/questions/2372336/jax-ws-change-content-type-to-content-type-because-server-is-hyper-sensitive – JIV Sep 13 '16 at 10:21
  • Unfortunately I already saw that question. I tried with "Content-Type" and also with "Content-type" and i still got the same problem – Aditzu Sep 13 '16 at 14:28
  • Well, then maybe you can try attach HandlerChain to WS but I am not sure if it works in client mode as well (maybe should?) or probably easier way would be use some proxy server to rewrite this header. – JIV Sep 13 '16 at 14:36
  • I also did that. I edited my question. Please find it above. – Aditzu Sep 13 '16 at 14:44
  • What about https://docs.oracle.com/javase/7/docs/api/javax/xml/soap/SOAPMessage.html#saveChanges%28%29 – JIV Sep 14 '16 at 07:19
  • I tried also to call saveChanges after I added the attachment to the message but unfortunately I have the same result. No attachment arrived on server side – Aditzu Sep 14 '16 at 07:40

1 Answers1

0

Cast the binding to a SOAPBinding and there is a flag to enable MTOM.

import javax.xml.ws.soap.SOAPBinding;

BindingProvider bp = (BindingProvider) proxy;

// Set binding and MTOM
SOAPBinding binding = (SOAPBinding) bp.getBinding();
binding.setMTOMEnabled(true);
Brian from state farm
  • 2,825
  • 12
  • 17