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!
1 Answers
Answers 1
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);
0 comments:
Post a Comment