2017-05-25 104 views
0

我正在使用WebServices和Apache Camel並使用dataFormat作爲POJO來請求該webservice。我成功地能夠調用服務,但我想記錄請求和響應SOAP消息,但我無法訪問,因爲SOAP消息是由CXF從POJO類創建的。dataFormat爲POJO時獲取請求和響應SOAP消息

當dataFormat是POJO時,有什麼方法可以記錄請求和響應SOAP消息嗎?

+0

看CXF攔截日誌消息。 – Namphibian

回答

0

我這是怎麼記錄的SOAP請求,並在我的項目響應的例子,希望這有助於

BindingProvider bindingProvider = ((BindingProvider) PortType); 
List<Handler> handlerChain = bindingProvider.getBinding().getHandlerChain(); 
handlerChain.add(new SOAPLoggingHandler());  
bindingProvider.getBinding().setHandlerChain(handlerChain); 

和類SOAPLoggingHandler

public class SOAPLoggingHandler implements SOAPHandler<SOAPMessageContext> { 

    // change this to redirect output if desired 
    public static Logger logger = Logger.getLogger("GetCustomerDataLand"); 

    public Set<QName> getHeaders() { 
     return null; 
    } 

    public boolean handleMessage(SOAPMessageContext smc) { 
     logToSystemOut(smc); 
     return true; 
    } 

    public boolean handleFault(SOAPMessageContext smc) { 
     logToSystemOut(smc); 
     return true; 
    } 

    // nothing to clean up 
    public void close(MessageContext messageContext) { 
    } 

    /* 
    * Check the MESSAGE_OUTBOUND_PROPERTY in the context to see if this is an 
    * outgoing or incoming message. Write a brief message to the print stream 
    * and output the message. The writeTo() method can throw SOAPException or 
    * IOException 
    */ 
    private void logToSystemOut(SOAPMessageContext smc) { 
     Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); 

     if (outboundProperty.booleanValue()) { 
      logger.info(new SimpleDateFormat("yyyy-MM-dd HH:mm:sss").format(new Date()) + "\nOutbound message:"); 
     } else { 
      logger.info(new SimpleDateFormat("yyyy-MM-dd HH:mm:sss").format(new Date()) + "\nInbound message:"); 
     } 

     SOAPMessage message = smc.getMessage(); 
     try { 

      ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
      message.writeTo(stream); 
      String msg = new String(stream.toByteArray(), "utf-8"); 

      logger.info(toPrettyString(msg)); 
      // message.writeTo(out); 
      logger.info(""); // just to add a newline 
     } catch (Exception e) { 
      logger.info(new SimpleDateFormat("yyyy-MM-dd HH:mm:sss").format(new Date()) + "Exception in handler: " 
        + org.apache.commons.lang.exception.ExceptionUtils.getStackTrace(e)); 
     } 
    } 

    public String toPrettyString(String xml) { 

     try { 
      final InputSource src = new InputSource(new StringReader(xml)); 
      final Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src) 
        .getDocumentElement(); 
      final Boolean keepDeclaration = Boolean.valueOf(xml.startsWith("<?xml")); 
      final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); 
      final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); 
      final LSSerializer writer = impl.createLSSerializer(); 

      writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); 
      writer.getDomConfig().setParameter("xml-declaration", keepDeclaration); 
      return writer.writeToString(document); 
     } catch (Exception e) { 
      throw new RuntimeException(e); 
     } 
    } 
}