2016-03-04 27 views
0

我試圖獲得從Web服務調用返回的PDF。使用SoapUI和其他日誌記錄功能,我可以看到PDF已附在回覆中。 但我試圖抓住PDF失敗。 PDF的DataHandler不包含任何數據。使用jax-ws無法從SOAP使用MTOM獲取PDF

// Call prepared and executed. result is returned object filled from sub-system. 
System.out.println("Some value = " + result.getSomeValue()); 

DataHandler dh = result.getAttachedPDF(); 
OutputStream fos=new FileOutputStream("/tmp/out.pdf"); 
dh.writeTo(fos); 
fos.close(); 

所以文件/tmp/out.pdf是空的。

InputStream inputstream = dh.getInputStream(); 
byte[] buffer = new byte[1024]; 
int bytesRead; 
while((bytesRead = inputstream.read(buffer)) != -1) { 
    System.out.println("Read " + bytesRead); 
    fos.write(buffer); 
} 
fos.close(); 

我試圖明確在我的客戶端啓用MTOM三種不同的方法:使用緩衝器收益率相同的結果編寫PDF

// First enabling 
port = service.getPort(new MTOMFeature(true)); 

// Second enabling 
Map<String, Object> ctxt = bp.getRequestContext(); 
ctxt.put("com.sun.xml.internal.ws.transport.http.client.streaming.chunk.size", 8192); 

// Third enabling. 
SOAPBinding binding = (SOAPBinding) bp.getBinding(); 
if(binding.isMTOMEnabled()) { 
    System.out.println("MTOM enabled"); 
} else { 
    binding.setMTOMEnabled(true);   
    System.out.println("MTOM NOT enabled"); 
} 

我得到「MTOM啓用」我是否使用方法1或2或不。

我在調試器(netbeans)中運行調用並檢查'result'對象。 DataHandler的所有字段爲空或值爲0.

我試圖將wsdl文件導入到Eclipse中的項目中。當Eclipse在生成的源代碼中報告多個錯誤時,我放棄了這條道路。相反,我讓SoapUI生成類(wsimport和wsdl2java),並將其與我的代碼一起使用。我取得了同樣的結果,空的PDF。

使用一種SOAPHandler攔截郵件作爲這裏建議: http://java.globinch.com/enterprise-java/web-services/jax-ws/logging-tracing-web-service-xml-request-response-jax-ws/

截獲的信息我已經保存並存儲到文件,並能節省每一部分。

不知道該從哪裏出發。我確實覺得有一些我已經錯過了。公共靜態void main(String [] args)拋出Exception { SVVCommonHeaderType header = new SVVCommonHeaderType();這是實際代碼。 header.setSystemId(SYSTEM_ID); header.setBrukerId(「用戶名」); (UUID.randomUUID()。toString());

GetKontrollseddel gs = new GetKontrollseddel(); 
    gs.setKontrollseddelNr(new BigInteger("3000036367")); 

    Kontrollseddel2 service = new Kontrollseddel2(); 
    Kontrollseddel2PortType port = service.getKontrollseddel2Port(new MTOMFeature(true)); 
    BindingProvider bp = (BindingProvider) port; 

    Map<String, Object> ctxt = bp.getRequestContext(); 
    ctxt.put("com.sun.xml.internal.ws.transport.http.client.streaming.chunk.size", 8192); 
    ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, ENDPOINT_ADDRESS); 
    ctxt.put(BindingProvider.USERNAME_PROPERTY, username); 
    ctxt.put(BindingProvider.PASSWORD_PROPERTY, password); 

    SOAPBinding binding = (SOAPBinding) bp.getBinding(); 
    if(binding.isMTOMEnabled()) { 
     System.out.println("MTOM enabled"); 
    } else { 
     binding.setMTOMEnabled(true);   
     System.out.println("MTOM NOT enabled"); 
    } 

    GetKontrollseddelResponse resp = port.getKontrollseddel(gs, header); 
    DataHandler dh = resp.getKontrollseddel2().getKontrollseddelPDF(); 
    OutputStream fos=new FileOutputStream("/tmp/out.pdf"); 
    dh.writeTo(fos); 

    // Alternative way to write, same result 
    /* 
    InputStream inputstream = dh.getInputStream(); 

    byte[] buffer = new byte[1024]; 
    int bytesRead; 
    while((bytesRead = inputstream.read(buffer)) != -1) { 
     System.out.println("Read " + bytesRead); 
     fos.write(buffer); 
    } 
    */ 

    fos.close(); 
} 

編輯: 正如我在提取PDF中的日誌類添加代碼的解決方法。 handleMessage()看起來像

@Override 
public boolean handleMessage(SOAPMessageContext arg0) { 
    SOAPMessage message= arg0.getMessage(); 
    boolean isOutboundMessage= (Boolean)arg0.get (MessageContext.MESSAGE_OUTBOUND_PROPERTY); 

    if(!isOutboundMessage){ 
     Iterator iter = message.getAttachments(); 
     while(iter.hasNext()) { 
      AttachmentPart part = (AttachmentPart) iter.next(); 
      try { 
       attachmentInputstream = (InputStream) part.getContent(); 
      } catch(Exception ex) { 
       System.out.println("Exception in handling attachment"); 
       ex.printStackTrace(System.out); 
       attachmentInputstream = null; 
      } 
     } 
    } 

    return true; 
} 

如果要使用此解決方法,我必須添加一些額外的處理代碼。我稍後再決定。

回答

0

事實證明,問題的原因是在發件人端有一些防火牆對SOAP附件進行了一些重寫。無論如何,這個令人困惑的jax-ws在SoapUI處理消息的時候。

我從消息生產者那裏得到的答案是,他們有一個代理將頭從XOP轉換爲文本,而MTOM需要XOP。

有了這個,我的代碼完美無瑕。