Java – jax-ws always sends MTOM attachments inline
Basically, I want to create a web service client to send an MTOM SOAP message through a proxy method I have created my service artifact from the web service WSDL The message was created correctly, but when I enabled MTOM and added an attachment, the attachment was always sent inline, not a separate mime part It is enabled like MTOM, but for some reason it decides not to optimize the message, so send it inline Running the same code through soap UI gives the correct result, so I know that the service itself will accept it
This is the basic code for me to create a soap request and send it I enable MTOM feature, but also try soapbinding setMTOMEnabled(true); For both methods, I have used ((soapbinding) binding) Ismtomenabled() to check if it is set to enabled
// initiate services.... // create service and enable mtom WebServiceBlah service = new WebServiceBlah(new URL(wsdlURL),SERVICE_NAME); WebServiceBlahPort port = service.getWebServiceBlahPort(new MTOMFeature(true,3072)); // load file File file = new File("/home/mypdf.pdf"); FileInputStream fileinputstream = new FileInputStream(file); int numberBytes = fileinputstream.available(); byte bytearray[] = new byte[numberBytes]; fileinputstream.read(bytearray); fileinputstream.close(); // create uploadResult UploadResult request = new UploadResult(); // create attachment AttachmentType attachment = new AttachmentType(); attachment.setContentType("application/doc"); attachment.setValue(bytearray); // create result and add attachment to it RenderedResult result = new RenderedResult(); result.setResult(attachment); result.setResultContentType("pdf"); result.setResultName("a pdf file"); // add result to request request.getResult().add(result); // send request port.UploadResults(request);
What I get is that my attachment is sent inline, as shown below (captured with Wireshark)
POST /blah/ws/ HTTP/1.1 Content-type: multipart/related;start="<rootpart*15c3ee3b-60c7-4726-a52c-8080965e4536@example.jaxws.sun.com>";type="application/xop+xml";boundary="uuid:15c3ee3b-60c7-4726-a52c-8080965e4536";start-info="text/xml" Soapaction: "" Accept: text/xml,multipart/related,text/html,image/gif,image/jpeg,*; q=.2,*/*; q=.2 User-Agent: JAX-WS RI 2.1.6 in JDK 6 Host: 123.123.123.123 Connection: keep-alive Content-Length: 12372 --uuid:15c3ee3b-60c7-4726-a52c-8080965e4536 Content-Id: <rootpart*15c3ee3b-60c7-4726-a52c-8080965e4536@example.jaxws.sun.com> Content-Type: application/xop+xml;charset=utf-8;type="text/xml" Content-@R_228_301@: binary <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> <S:Header></S:Header> <S:Body> <ns2:uploadResult xmlns:xmime="http://www.w3.org/2005/05/xmlmime"> <renderedResult> <result xmime:contentType="application/doc">JVBERi0xLjQKJaqrrK0KNCAwIG9iago8</result> <resultContentType>pdf</resultContentType> <resultName>a pdf file</resultName> </renderedResult> </ns2:uploadResult> </S:Body> </S:Envelope> --uuid:15c3ee3b-60c7-4726-a52c-8080965e4536
What I want is to replace the attachment in the result tag with the inline tag and attachment added to the SOAP message in different mime parts for example
<result xmime:contentType='application/doc'> <inc:Include href="cid:myid3" xmlns:inc='http://www.w3.org/2004/08/xop/include'/> </result>
Then add the following to the SOAP message
------=_Part_10_28027205.1314348995670 Content-Type: application/pdf Content-@R_228_301@: binary Content-ID: cid:myid3 Content-Disposition: attachment; name="mypdf.pdf" JVBERi0xLjQKJaqrrK0KNCAwIG9iago8
Solution
Many things can affect whether MTOM attachments are actually used
On the server, the first thing is obvious: check that your service implementation has @ MTOM annotations You can also use the threshold () attribute to adjust the threshold from this comment (as Steve J has mentioned)
Sometimes a handler on the server may interfere with the use of MTOM attachments Any handler that serializes a SOAP message into a string or byte array (usually a debug style handler used to write the message content to the log) will prevent the use of MTOM attachments If possible, try disabling your handler chain and see if MTOM attachments pass after that