JAX-WS 始终内联发送 MTOM 附件
基本上我想创建一个 Web 服务客户端来通过代理方法发送 mtom Soap 消息。我已经从 Web 服务 wsdl 创建了我的服务工件。消息已正确创建,但是当我启用 mtom 并添加附件时,附件始终以内联方式发送,而不是在单独的 mime 部分中发送。它就像启用了 mtom,但由于某种原因它决定不优化消息,因此内联发送它。通过soapui运行相同的代码给出了正确的结果,所以我知道服务本身会接受它。
这是我创建肥皂请求并发送它的基本代码。 我启用了 mtomfeature,但也尝试使用soapBinding.setMTOMEnabled(true); 来实现它 对于这两种方法,我都使用 ((SOAPBinding) binding).isMTOMEnabled()
对其进行了调试,以检查它是否已设置为启用。
// 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);
我得到的是我的附件是内联发送的,如下所示。 (用wireshark捕获)
POST /blah/ws/ HTTP/1.1
Content-type: multipart/related;start="<rootpart*[email protected]>";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*[email protected]>
Content-Type: application/xop+xml;charset=utf-8;type="text/xml"
Content-Transfer-Encoding: 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
我想要的是将结果标签中的附件替换为内联标签,并将附件添加到不同mime部分中的soap消息中。例如
<result xmime:contentType='application/doc'>
<inc:Include href="cid:myid3" xmlns:inc='http://www.w3.org/2004/08/xop/include'/>
</result>
然后将以下内容添加到肥皂消息中
------=_Part_10_28027205.1314348995670
Content-Type: application/pdf
Content-Transfer-Encoding: binary
Content-ID: cid:myid3
Content-Disposition: attachment; name="mypdf.pdf"
JVBERi0xLjQKJaqrrK0KNCAwIG9iago8
Basically I want to create a web services client to send a mtom soap message via the proxy method. I have created my service artifacts fine from the web service wsdl. The message is created correctly, however when I enable mtom and add an attachment, the attachment is always sent inline and not in a separate mime part. Its like mtom is enabled but for some reason it decides not to optimize the message and so sends it inline. Running the same code through soapui gives the correct result so I know the service itself will accept it.
Here is my basic code for creating a soap request and sending it.
I enable mtomfeature but have also tried doing it with soapBinding.setMTOMEnabled(true);
For both methods I have debugged it with ((SOAPBinding) binding).isMTOMEnabled()
to check that it is set to being 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 my attachment is sent inline as seen below. (captured with wireshark)
POST /blah/ws/ HTTP/1.1
Content-type: multipart/related;start="<rootpart*[email protected]>";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*[email protected]>
Content-Type: application/xop+xml;charset=utf-8;type="text/xml"
Content-Transfer-Encoding: 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 for the attachment in the result tag to be replaced with the inline tag and the attachment added to the soap message in a different mime part. eg
<result xmime:contentType='application/doc'>
<inc:Include href="cid:myid3" xmlns:inc='http://www.w3.org/2004/08/xop/include'/>
</result>
And then the following added to the soap message
------=_Part_10_28027205.1314348995670
Content-Type: application/pdf
Content-Transfer-Encoding: binary
Content-ID: cid:myid3
Content-Disposition: attachment; name="mypdf.pdf"
JVBERi0xLjQKJaqrrK0KNCAwIG9iago8
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
许多因素都会影响 MTOM 附件是否实际被使用。
在服务器上,首先是显而易见的事情:检查您的服务实现是否具有
@MTOM
注释。您还可以使用threshold()
属性从此注释调整阈值(正如 SteveJ 已经提到的)。有时,服务器上的处理程序可能会干扰是否使用 MTOM 附件。任何将 SOAP 消息序列化为字符串或字节数组的处理程序(常见于将消息内容写入日志的调试样式处理程序)将阻止使用 MTOM 附件。如果可能,请尝试禁用处理程序链并查看 MTOM 附件之后是否可以通过。
A number of things can affect whether MTOM attachments are actually used.
On the server, firstly the obvious: check that your service implementation has the
@MTOM
annotation. You can also adjust the threshold value (as SteveJ has already mentioned) from this annotation using thethreshold()
property.Sometimes handlers on the server can interfere with whether MTOM attachments are used. Any handler serializes a SOAP message to string or byte array (common for debugging style handlers that write message content to logs) will prevent MTOM attachments from being used. If possible, try disabling your handler chain and seeing if MTOM attachments come through after that.
我遇到了同样的麻烦(内联附件),但似乎只是错误地监视 SOAP 消息(通过 MessageHandler):它在
MtomCodec.encode(Packet packet,输出流输出)
。真实消息可以在OutputStream out
的 java 代码中看到:com\sun\xml\ws\jaxws-rt\2.2.10\jaxws-rt-2.2.10-sources.jar! \com\sun\xml\ws\encoding\MtomCodec.java
仅适用于客户端的
new MTOMFeature(true, 3072)
边。I've bump with the same trouble (inline attachments), but it seems just wrong monitoring SOAP message (by MessageHandler): it plays before (in client) and after (at server) of
MtomCodec.encode(Packet packet, OutputStream out)
. Real message could be seen in java code atOutputStream out
:com\sun\xml\ws\jaxws-rt\2.2.10\jaxws-rt-2.2.10-sources.jar!\com\sun\xml\ws\encoding\MtomCodec.java
Works just with
new MTOMFeature(true, 3072)
at client side.