2011-08-26 60 views
10

基本上我想創建一個Web服務客戶端通過代理方法來發送一個MTOM SOAP消息。我已經從Web服務wsdl中創建了我的服務工件。消息已正確創建,但是,當啓用mtom並添加附件時,附件始終以內聯方式發送,而不是單獨的MIME部分。它像mtom一樣被啓用,但由於某種原因,它決定不優化消息,因此將它內聯發送。通過soapui運行相同的代碼給出正確的結果,所以我知道服務本身會接受它。JAX-WS總是發送MTOM附件內聯

這裏是我創建SOAP請求和發送它的基本代碼。 啓用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 

我想是在結果標籤附連到與所述內嵌代碼被替換和安裝添加到SOAP消息在不同的MIME部分。例如

<result xmime:contentType='application/doc'> 
    <inc:Include href="cid:myid3" xmlns:inc='http://www.w3.org/2004/08/xop/include'/> 
</result> 

然後將以下加入到SOAP消息

------=_Part_10_28027205.1314348995670 
Content-Type: application/pdf 
Content-Transfer-Encoding: binary 
Content-ID: cid:myid3 
Content-Disposition: attachment; name="mypdf.pdf" 
JVBERi0xLjQKJaqrrK0KNCAwIG9iago8 
+3

你解決了嗎?我有完全一樣的問題。 JAX-WS文檔還說:「由JAXB 2.0規範xs:base64Binary和xs:hexBinary映射到java定義的是byte []。 JAX-WS實現已經設置了1KB byte []大小的閾值。這個閾值可以在客戶端的RequestContext並在MessageContext的在服務器端使用執行特定的屬性com.sun.xml.ws.developer.JAXWSProperties.MTOM_THRESHOLD_VALUE進行修改。您在上面的代碼中將閾值設置爲3072。設置爲0,1,...似乎沒有區別。 – SteveJ

回答

1

許多因素會影響MTOM附件是否是用於實際上

在服務器上,首先顯而易見的:檢查您的服務實現有@MTOM註解。您還可以使用threshold()屬性從此註釋中調整閾值(如SteveJ已經提到的)。

有時在服務器上的處理程序可以與是否使用MTOM附件干擾。任何處理程序將一個SOAP消息序列化爲字符串或字節數組(通常用於將消息內容寫入日誌的調試樣式處理程序)將阻止使用MTOM附件。如果可能,請嘗試禁用您的處理程序鏈,然後查看是否在此之後通過MTOM附件。

0

我碰到了同樣的麻煩(內聯附件),但它似乎只是錯誤監控SOAP消息(通過MessageHandler):它在MtomCodec.encode(Packet packet, OutputStream out)之前(在客戶端)和之後(在服務器)播放。真正的信息可以在Java代碼中可以看出,在OutputStream out:在客戶端只是new MTOMFeature(true, 3072)

com\sun\xml\ws\jaxws-rt\2.2.10\jaxws-rt-2.2.10-sources.jar!\com\sun\xml\ws\encoding\MtomCodec.java

  for(ByteArrayBuffer bos : mtomAttachments){ 
      bos.write(out); 
     } 

作品。