截获 抓取webservice的请求报文内容

使用SOAPHandler

1.soAPLoggingHandler implements SOAPHandler<SOAPMessageContext>

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import javax.xml.soap.soAPBody;
import javax.xml.soap.soAPElement;
import javax.xml.soap.soAPException;
import javax.xml.soap.soAPFactory;
import javax.xml.soap.soAPHeader;
import javax.xml.soap.soAPMessage;
import javax.xml.soap.Text;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.soAPHandler;
import javax.xml.ws.handler.soap.soAPMessageContext;
import org.apache.log4j.Logger;
import com.pansontech.ws.Service;
/*
* This simple SOAPHandler will output the contents of incoming
* and outgoing messages.
*/
public class SOAPLoggingHandler implements SOAPHandler<SOAPMessageContext> {
// change this to redirect output if desired
private static PrintStream out = System.out;
static Logger logger = Logger.getLogger(Service.class);
public Set getHeaders() {
return null;
}
@Override
public boolean handleMessage(SOAPMessageContext smc) {
System.out.println("Server : handleMessage()......");
logToSystemOut(smc);
return true;
}
@Override
public boolean handleFault(SOAPMessageContext smc) {
System.out.println("Server : handleFault()......");
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);
SOAPMessage message = smc.getMessage();
if (outboundProperty.booleanValue()) {
out.println("\nOutbound message:");
logger.error("\nOutbound message:");
} else {
out.println("\nInbound message:");
logger.error("\nInbound message:");
try {
SOAPBody body = message.getSOAPBody();
//              SOAPHeader header = message.getSOAPHeader();
//              Iterator it = body.getNamespacePrefixes();
//              while(it.hasNext()){
//                  Object obj = it.next();
//                  System.out.println("obj="+obj);
//              }
//              System.out.println("it="+it);
//
//              System.out.println("Prefix="+body.getPrefix());
Iterator it2 = body.getChildElements();
while(it2.hasNext()){
Object tmp = it2.next();
if (tmp instanceof Text ){
Text txt = (Text)tmp;
System.err.println(txt.getWholeText());
}
else {
SOAPElement sOAPElement = (SOAPElement) tmp;
Iterator it3 = sOAPElement.getChildElements();
ArrayList<SOAPElement> list = new ArrayList<SOAPElement>();
while (it3.hasNext()){
Object tmp1 = it3.next();
if (tmp1 instanceof SOAPElement){
SOAPElement element = (SOAPElement)tmp1;
//element.removeNamespaceDeclaration(element.getPrefix());
//System.err.println(element.getLocalName()+","+element.lookupPrefix(element.getNamespaceURI())+","+element.getNamespaceURI());
element.detachNode();   //1 方法 deleteNode 删除调用它的 XML 元素(节点):先删除,后添加
list.add(element);
}
}
Iterator<SOAPElement> it4 = list.iterator();
while (it4.hasNext()){
SOAPElement oldElement = it4.next();
SOAPElement newElement = SOAPFactory.newInstance().createElement(oldElement.getLocalName());
newElement.setValue(oldElement.getValue());
sOAPElement.addChildElement(newElement);    //2 方法 deleteNode 删除调用它的 XML 元素(节点):先删除,后添加
}
}
}
//保存修改
message.saveChanges();
} catch (SOAPException e1) {
// Todo Auto-generated catch block
e1.printstacktrace();
}
}
try {
message.writeto(out);
out.println("");   // just to add a newline
logger.error("message="+message);
} catch (Exception e) {
out.println("Exception in handler: " + e);
}
}
}


2. Service增加标注@HandlerChain(file = "handlers.xml")

/** Web service 接口类
*
*/
@WebService
@HandlerChain(file = "handlers.xml")
@SOAPBinding(style=SOAPBinding.Style.RPC)
public class Service {
@Resource
WebServiceContext wsContext;
//do something
}

3.在src下,放handlers.xml

//内容是配置处理handler的实现类
<?xml version="1.0" encoding="UTF-8"?>
<handler-chains xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee">
<handler-chain>
<handler>
<handler-name>authHandler</handler-name>
<handler-class>com.zhao.common.soAPLoggingHandler</handler-class>
</handler>
</handler-chain>
</handler-chains>
//这个文件的名字可以随意起 ,Service增加标注@HandlerChain(file = "handlers.xml"),这里保持一致就可以

 

 

在webservice被调用方法内部获取报文内容

MessageContext mc = wsContext.getMessageContext();
try {
Field field = mc.getClass().getDeclaredField("packet");
field.setAccessible(true);
Packet packet = (Packet) field.get(mc);
String s_packet = packet.toString();
int index = s_packet.indexOf("<?xml");
String xml = "";
if(index >= 0){
xml = s_packet.substring(index,s_packet.length());
}
logger.error("packet="+packet);
logger.error("xml="+xml);
System.out.println("xml="+xml);
} catch (Exception e1) {
e1.printstacktrace();
logger.error("错误",e1);
}

 

 

参考:http://doc.java.sun.com/DocWeb/api/javax.xml.soap.SOAPEnvelope

相关文章

1.使用ajax调用varxhr;functioninvoke(){if(window.ActiveXO...
               好不容易把WebService服务器...
1新建一个工程项目用来做服务端增加一个MyService1类文件pac...
packagecom.transsion.util;importjava.io.BufferedReader;i...
再生产wsdl文件时重写描述文件1usingSystem;2usingSystem.Co...
一般情况下,使用eclipse自带的jax-ws生成webservice会自动生...