XFire截取SOAP報(bào)文,并輸出到控制臺的方法:
方法一(spring沒有集成XFire):
1. 在XFire service.xml中配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>AddTwoInteger</name>
<serviceClass>cal.IAddTwoInteger</serviceClass>
<implementationClass>cal.AddTwoIntegerImpl</implementationClass>
<style>wrapped</style>
<use>literal</use>
<scope>application</scope>
<outHandlers>
<handler handlerClass="org.codehaus.xfire.util.dom.DOMInHandler"/>
<handler handlerClass ="cal.AuthenticationHandler" ></handler >
</outHandlers>
<inHandlers>
<handler handlerClass="org.codehaus.xfire.util.dom.DOMInHandler"/>
<handler handlerClass ="cal.AuthenticationHandler" ></handler >
</inHandlers>
</service>
</beans>
2. 添加InHandler文件
package cal;
import org.codehaus.xfire.MessageContext;
import org.codehaus.xfire.handler.AbstractHandler;
import org.jdom.Element;
import org.codehaus.xfire.util.dom.DOMInHandler;
import org.dom4j.io.DOMReader;
import org.w3c.dom.Document;
public class AuthenticationHandler extends AbstractHandler {
public void invoke(MessageContext cfx) throws Exception {
Document inputDoc = (Document) cfx.getInMessage().getProperty(DOMInHandler.DOM_MESSAGE);
if(inputDoc!=null){
System.out.println("------------input Soap xml-------------");
System.out.println(this.buildDocment(inputDoc).asXML());
}
}
/** converts a W3C DOM document into a dom4j document */
public org.dom4j.Document buildDocment(org.w3c.dom.Document domDocument) {
DOMReader xmlReader = new DOMReader();
return xmlReader.read(domDocument);
}
}
方法二(spring集成XFire):
1. xfire.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 引入XFire預(yù)配置信息 -->
<import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
<!-- 定義訪問的url -->
<bean
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/UserService.ws">
<ref bean="UserService" />
</entry>
</map>
</property>
</bean>
<!-- 使用XFire導(dǎo)出器 -->
<bean id="baseWebService"
class="org.codehaus.xfire.spring.remoting.XFireExporter"
lazy-init="false" abstract="true">
<!-- 引用xfire.xml中定義的工廠 -->
<property name="serviceFactory" ref="xfire.serviceFactory" />
<!-- 引用xfire.xml中的xfire實(shí)例 -->
<property name="xfire" ref="xfire" />
</bean>
<bean id="UserService" parent="baseWebService">
<!-- 業(yè)務(wù)服務(wù)bean -->
<property name="serviceBean" ref="userService" />
<!-- 業(yè)務(wù)服務(wù)bean的窄接口類 -->
<property name="serviceClass" value="com.jy.service.UserService" />
<property name="inHandlers">
<list>
<ref bean="domInHandler"/>
<ref bean="addressingHandler"/>
</list>
</property>
</bean>
<bean id="domInHandler" class="org.codehaus.xfire.util.dom.DOMInHandler"/>
<bean id="addressingHandler" class="com.jy.interceptor.SoapInHandler"/>
</beans>
2. 添加InHandler.java
package com.jy.interceptor;
import org.codehaus.xfire.MessageContext;
import org.codehaus.xfire.handler.AbstractHandler;
import org.codehaus.xfire.util.dom.DOMInHandler;
import org.dom4j.io.DOMReader;
import org.w3c.dom.Document;
public class SoapInHandler extends AbstractHandler {
public void invoke(MessageContext arg0) throws Exception {
Document inputDoc = (Document) arg0.getInMessage().getProperty(DOMInHandler.DOM_MESSAGE);
if(inputDoc!=null){
System.out.println("------------input Soap xml-------------");
System.out.println(this.buildDocment(inputDoc).asXML());
}
}
/** converts a W3C DOM document into a dom4j document */
public org.dom4j.Document buildDocment(org.w3c.dom.Document domDocument) {
DOMReader xmlReader = new DOMReader();
return xmlReader.read(domDocument);
}
}