Wednesday, December 3, 2008

Adding Axis2 handlers programmatically

Following the thread here, thought to have this blog post on the $subject.

This explains how you could add handlers in Axis2 programmatically.

package org.apache.ws.axis2;

import java.util.ArrayList;
import java.util.Iterator;

import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.engine.AxisConfiguration;
import org.apache.axis2.engine.Phase;
import org.apache.axis2.engine.ServiceLifeCycle;
import org.apache.axis2.phaseresolver.PhaseException;

/**
* The service implementation class
*/
public class SimpleService implements ServiceLifeCycle {

/**
* This will be called during the deployment time of the service.
* Irrespective of the service scope this method will be called
*/
public void startUp(ConfigurationContext configctx, AxisService service) {
AxisConfiguration config = null;
ArrayList phases = null;

config = service.getAxisConfiguration();
phases = config.getInFlowPhases();

for (Iterator iterator = phases.iterator(); iterator.hasNext();) {
Phase phase = (Phase) iterator.next();
if ("Security".equals(phase.getPhaseName())) {
SimpleHandler handler = new SimpleHandler();
try {
// This will be the last handler under Security phase
phase.setPhaseLast(handler);
} catch (PhaseException e) {
e.printStackTrace();
}
System.out.println("Added SimpleHandler under Sceurity phase...");
return;
}
}
}

/**
* This will be called during the system shut down time. Irrespective of the
* service scope this method will be called
*/
public void shutDown(ConfigurationContext configctx, AxisService service) {
System.out.println("ServiceLifeCycle:::shutDown()");

}

/**
* The echo method which will be exposed as the echo operation of the web
* service
*/
public String echo() {
return "Hi";
}

}

<service name="SimpleService" class="org.apache.ws.axis2.SimpleService" scope="application">
<operation name="echo">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</operation>
<parameter name="ServiceClass" locked="false">org.apache.ws.axis2.SimpleService</parameter>
</service>

package org.apache.ws.axis2;

import org.apache.axis2.AxisFault;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.description.HandlerDescription;
import org.apache.axis2.description.Parameter;
import org.apache.axis2.description.PhaseRule;
import org.apache.axis2.engine.Handler;

public class SimpleHandler implements Handler {

/**
* {@inheritDoc}
*/
public InvocationResponse invoke(MessageContext msgContext) throws AxisFault {
System.out.println("Service Invoked : " + msgContext.getAxisService().getName());
System.out.println("Operation Invoked : "
+ msgContext.getAxisOperation().getName().getLocalPart());
return InvocationResponse.CONTINUE;
}

/**
* {@inheritDoc}
*/
public HandlerDescription getHandlerDesc() {
HandlerDescription handlerDesc = null;
PhaseRule rule = null;

rule = new PhaseRule("Security");
rule.setPhaseLast(true);

handlerDesc = new HandlerDescription();
handlerDesc.setHandler(this);
handlerDesc.setName(this.getName());
handlerDesc.setRules(rule);

return handlerDesc;
}

/**
* {@inheritDoc}
*/
public String getName() {
return "SimpleHandler";
}

/**
* {@inheritDoc}
*/
public void cleanup() {
}

/**
* {@inheritDoc}
*/
public void flowComplete(MessageContext msgContext) {
}

/**
* {@inheritDoc}
*/
public Parameter getParameter(String name) {
return null;
}

/**
* {@inheritDoc}
*/
public void init(HandlerDescription handlerDesc) {
}

}

6 comments:

crystal said...

Thank you so much!!polo shirt men'ssweate,Burberry Polo Shirts lacoste sweater, ralph lauren Columbia Jackets,ski clothing. Free Shipping, PayPal Payment. Enjoy your shopping experience on mensclothingus.com.You can find the father who desire fashionable, intellectual mens clothing simultaneously.

crystal said...

Awesome!!!Best wishes for you !!cheap polo shirts is the father of the summer should be prepared to most commonly used item, it has both style and shape of Ralph Lauren Polo, and vest with a random function polo ralph lauren, so that in the short-sleeved apply to both on many occasions, the pink and black color men's polo shirts brought into effect, lightweight cotton, linen texture to demonstrate masculine temperament and sense of fashion exhaustively.

crystal said...

God bless you!I really agree with your opinions.Also,there are some new fashion things here,gillette razor blades.gillette mach3 razor bladesfor men.As for ladies,gillette venus razor blades must the best gift for you in summer,gillette fusion blades are all the best choice for you.

crystal said...

Perfect!!You are a outstanding person!Have you ever wore chaussures puma,Here are the most popular puma CAT,Puma shoes store gives some preview of puma speed cat,and casual but no sweat puma basket.

venus said...

Do not mean bad.Thank you so much!I just want to show some fashion things to all of you.I like puma speed, puma femmes and other puma shoes. These puma sport items are at store recently and available for anyone.

venus said...

Fantastic!God bless you!Meanwhile,you can visit my China Wholesale,we have the highest quality but the lowest price fashion products wholesale from China.Here are the most popular China Wholesale productsfor all of you.You can visit http://chinaclothes.net.Also the polo clothing is a great choice for you.