使用SOAPHandler
1.SOAPLoggingHandler implements SOAPHandlerimport 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 {
// 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 list = new ArrayList();
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 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的实现类
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee">
authHandler
com.zhao.common.SOAPLoggingHandler
//这个文件的名字可以随意起 ,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);
}