通过jaxws修改soap message.
项目需要,要对java的soap messge进行改动。
主要是改动header和body部分。
代码如下。
code 1 [java 取出body,加一个header]
-----------------------------我是分割线 code 1-------------------------------
SOAPMessage message = messageContext.getMessage();
//把soap的body提出来。
//Creates a new DOM org.w3c.dom.Document and sets the first child of this SOAPBody as
//it's document element. The child SOAPElement is removed as part of the process.
Document doc = message.getSOAPBody().extractContentAsDocument();
StringWriter output = new StringWriter();
TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc), new StreamResult(output));
//这个就是soapbody的字符串表示了
String bodyString = output.toString();
String encryptBodyString = encryptString(bodyString);
SOAPElement soapElement = message.getSOAPHeader().addChildElement(
HeaderConstant.DataQName);
soapElement.addTextNode(encryptBodyString);
-----------------------------我是分割线 code 1-------------------------------
code 2 [java 设置body,删除一个header]
-----------------------------我是分割线 code 2-------------------------------
SOAPMessage message = messageContext.getMessage();
SOAPHeader header = message.getSOAPHeader();
Iterator dataIterator = header.getChildElements(HeaderConstant.DataQName);
SOAPElement dataElements = (SOAPElement) dataIterator.next();
bodayString = dataElements.getValue();
String decryptString = decryptString(bodayString);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new ByteArrayInputStream(decryptString.getBytes()));
message.getSOAPBody().addDocument(doc);
-----------------------------我是分割线 code 2-------------------------------