2016-02-08 34 views
0

需要在Web服務中設置什麼,以便以param屬性格式發送和接收soap消息?例如,我需要與參數發送SOAP請求在該屬性名稱/值格式:如何以屬性名稱/值格式發送和接收soap消息?

<param name="controller_name">CPA Central</param> 

,並以類似的屬性名稱/值格式接受他們:

<attribute name="channel_number">1</attribute> 

我從字面上一派14個小時,無法找到如何做到這一點!如果有人能指出我的方向,我會非常感激。

回答

0

這就是我最終做到的。看起來很荒謬,你必須攔截即將離任的soap消息並重新格式化它,而不是以正確的格式創建它。但是如果你像我一樣被迫使用像JAX-WS這樣的魔術黑盒子,你會堅持使用他們提供的自動格式。

public class InneoquestLogicalHandler implements LogicalHandler<LogicalMessageContext> { 
    private static Logger logger = Logger.getLogger(InneoquestSoapHandler.class); 

    @Override 
    public boolean handleMessage(LogicalMessageContext context) { 
     boolean isResponse = (Boolean)context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); 
     if(!isResponse){ 
      logger.debug("InneoquestLogicalHandler.handleMessage(): this is a soap request"); 
     } 
     else { 
      logger.debug("InneoquestLogicalHandler.handleMessage(): this is a soap response"); 
      try { 
       try { 
        transform(context); 
       } catch (TransformerConfigurationException ex) { 
        java.util.logging.Logger.getLogger(InneoquestLogicalHandler.class.getName()).log(Level.SEVERE, null, ex); 
       } catch (SOAPException ex) { 
        java.util.logging.Logger.getLogger(InneoquestLogicalHandler.class.getName()).log(Level.SEVERE, null, ex); 
       } 
      } catch (TransformerException ex) { 
       java.util.logging.Logger.getLogger(InneoquestLogicalHandler.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } 
    return true; 
    } 

    private void transform(LogicalMessageContext context) throws TransformerConfigurationException, TransformerException, SOAPException { 
     LogicalMessage msg = context.getMessage(); 
     Source source = msg.getPayload(); 
     Transformer xFormer = TransformerFactory.newInstance().newTransformer(); 
     xFormer.setOutputProperty("omit-xml-declaration", "yes"); 
     DOMResult result = new DOMResult(); 
     xFormer.transform(source,result); 
     Document doc = (Document) result.getNode(); 
     transformNodeList(doc,doc.getChildNodes()); 
     source = new DOMSource(doc); 
     msg.setPayload(source); 
    } 

    private void transformNodeList(Document doc, NodeList nodeList) { 
     for (int i=0; i< nodeList.getLength(); i++) { 
      Node childNode = nodeList.item(i); 
      if (childNode.getNodeName().equals("channel_number")) { 
       Element elem = (Element)childNode; 
       doc.renameNode(elem, elem.getNamespaceURI(), "attribute"); 
       elem.setAttribute("name", "channel_number"); 
      } 
      else if (childNode.getNodeName().equals("count")) { 
       Element elem = (Element)childNode; 
       doc.renameNode(elem, elem.getNamespaceURI(), "response"); 
       elem.setAttribute("rows", elem.getTextContent()); 
       elem.setTextContent(""); 
       elem.setAttribute("type", "success"); 
      } 

      NodeList children = childNode.getChildNodes(); 
      if (children != null) { 
       transformNodeList(doc,children); 
      } 
     } 
    }