Axis2 自定义 module 实现 handler 来log soap request and response Message

[b]自定义Module[/b]
[b]1.给module写一个单独的配置文件(module.xml),放在source 同目录下的META-INF 文件中[/b]



























[b]2.实现你的自己的Module extends from org.apache.axis2.modules.Module[/b]

import org.apache.axis2.AxisFault;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.description.AxisDescription;
import org.apache.axis2.description.AxisModule;
import org.apache.axis2.modules.Module;
import org.apache.neethi.Assertion;
import org.apache.neethi.Policy;

public class LoggingModule implements Module
{
// initialize the module
public void init(ConfigurationContext configContext, AxisModule module)
throws AxisFault
{
System.out.println("init");
}
public void engageNotify(AxisDescription axisDescription) throws AxisFault
{
}
// shutdown the module
public void shutdown(ConfigurationContext configurationContext)
throws AxisFault
{
System.out.println("shutdown");
}
public String[] getPolicyNamespaces()
{
return null;
}
public void applyPolicy(Policy policy, AxisDescription axisDescription)
throws AxisFault
{
}
public boolean canSupportAssertion(Assertion assertion)
{
return true;
}
}



[b]3.实现你的handler记录SOAP request and response Message.[/b]

import org.apache.axis2.AxisFault;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.engine.Handler;
import org.apache.axis2.handlers.AbstractHandler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class LogHandler extends AbstractHandler implements Handler
{
private static final Log log = LogFactory.getLog(LogHandler.class);
private String name;
public String getName()
{
return name;
}
public InvocationResponse invoke(MessageContext msgContext)
throws AxisFault
{
// 向Tomcat控制台输出请求和响应SOAP消息
System.out.println("Soap Request" + msgContext.getEnvelope().toString());
log.info(msgContext.getEnvelope().toString());
return InvocationResponse.CONTINUE;
}
public void revoke(MessageContext msgContext)
{
System.out.println("Soap Response" + msgContext.getEnvelope().toString());
log.info(msgContext.getEnvelope().toString());
}
public void setName(String name)
{
this.name = name;
}
}
[b]把以上module相关的东西用 "jar cvf logging.mar ." 打成一个mar的包。
注意一定要把module.xml放在跟source同目录的META-INF 文件中。然后把mar文件放在
tomcat下的webapp/axis2/WEB-INF/modules下。[/b]
[img]
d:/source.JPG
[/img]

[b]Service Class的实现类[/b]

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.xmlbeans.xml.stream.XMLStreamException;

public class SampleService {

public OMElement sayHello(OMElement element) {
element.build();
element.detach();

String rootName = element.getLocalName();
System.out.println("Reading " + rootName + " element");

OMElement childElement = element.getFirstElement();
String personToGreet = childElement.getText();

OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace(
"http://example1.org/example1", "example1");
OMElement method = fac.createOMElement("sayHelloResponse", omNs);
OMElement value = fac.createOMElement("greeting", omNs);
value.addChild(fac.createOMText(value, "Hello," + personToGreet));
method.addChild(value);

return method;
}

}


[b]部署此service需要的services.xml[/b]


This is a sample service created in the Axis2 User's Guide

[color=red][/color]
com.bank.webservice.SampleService


urn:sayHello



urn:ping



[b]注意配置来在此service中使用logging 这个module
用jar cvf 命令把此service打成一个aar包部署在tomcat 下的webapp/axis2/WEB-INF/services 文件下。

此外还要修改axis2.xml 加入我们的handler起作用的phase "loggingPhase"(红色配置).[/b]



class="org.apache.axis2.dispatchers.RequestURIBasedDispatcher">


class="org.apache.axis2.dispatchers.SOAPActionBasedDispatcher">




class="org.apache.axis2.dispatchers.AddressingBasedDispatcher">






class="org.apache.axis2.dispatchers.RequestURIBasedDispatcher"/>
class="org.apache.axis2.dispatchers.SOAPActionBasedDispatcher"/>
class="org.apache.axis2.dispatchers.RequestURIOperationDispatcher"/>
class="org.apache.axis2.dispatchers.SOAPMessageBodyBasedDispatcher"/>
class="org.apache.axis2.dispatchers.HTTPLocationBasedDispatcher"/>
class="org.apache.axis2.jaxws.dispatchers.GenericProviderDispatcher"/>
class="org.apache.axis2.jaxws.dispatchers.MustUnderstandValidationDispatcher"/>





[color=red][/color]

class="org.apache.axis2.jaxws.dispatchers.MustUnderstandChecker">








[color=darkred] [/color]











class="org.apache.axis2.dispatchers.AddressingBasedDispatcher">






class="org.apache.axis2.dispatchers.RequestURIBasedDispatcher"/>
class="org.apache.axis2.dispatchers.SOAPActionBasedDispatcher"/>
class="org.apache.axis2.dispatchers.RequestURIOperationDispatcher"/>
class="org.apache.axis2.dispatchers.SOAPMessageBodyBasedDispatcher"/>
class="org.apache.axis2.dispatchers.HTTPLocationBasedDispatcher"/>
class="org.apache.axis2.jaxws.dispatchers.GenericProviderDispatcher"/>
class="org.apache.axis2.jaxws.dispatchers.MustUnderstandValidationDispatcher"/>



[color=red][/color]





[color=red] [/color]








[b]好大功告成。写一个call service的client 类[/b]

import javax.xml.stream.XMLStreamException;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.Constants;
import org.apache.axis2.client.ServiceClient;

public class SampleClient {

private static EndpointReference targetEPR =
new EndpointReference(
"http://localhost:8080/axis2/services/SampleService");

public static OMElement greetUserPayload(String personToGreet) {
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace(
"http://example1.org/example1", "example1");
OMElement method = fac.createOMElement("sayHello", omNs);
OMElement value = fac.createOMElement("personToGreet",
omNs);
value.addChild(fac.createOMText(value, personToGreet));
method.addChild(value);
return method;
}

public static void main(String[] args) {
try {
OMElement payload =
SampleClient.greetUserPayload("John");
Options options = new Options();
options.setTo(targetEPR);

options.setTransportInProtocol(Constants.TRANSPORT_HTTP);

ServiceClient sender = new ServiceClient();
sender.setOptions(options);
OMElement result = sender.sendReceive(payload);

String response = result.getFirstElement().getText();
System.out.println(response);

} catch (Exception e) { //(XMLStreamException e) {
System.out.println(e.toString());
}
}

}



[b]如果call完后你的log文件中出现了一下log证明我们的handler已经起作用了,记录了soap response and request message。 如果你想在别的service中使用这个module中的handler 只要在那个services.xml中引用logging这个module就ok了。[/b]

Soap Request[color=darkred][/color]John
[INFO] John
Reading sayHello element
Soap ResponseHello,John
[INFO] Hello,John

经验总结,在许多情况下我们打出来的aar,跟mar 放在axis2的web-inf目录下,在axis在启动时会报aar中的services.xml 找不到,同样在mar中报module.xml找不到,在这种情况下,你直接把aar,或者mar解压开来放一个普通的文件夹中,直接把这个文件夹放到axis2的相应目录下,就ok了。

你可能感兴趣的:(Axis2)