2016-02-08 35 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); 
      } 
     } 
    }