Tuesday, August 19, 2008

Intercepting HTTP Parameters on an Axis Endpoint in Mule

I've been trying to write an interceptor for Mule 1.4.3 container to intercept a message that contains a username and password for authentication. The interceptor is to act as a Permission Check interceptor described in the Mule docs.

I was able to come up with an approach that might fit my needs, although there could very well likely be a better approach.

My Mule 1.4.3 configuration is setup to take an HTTP request for an Axis endpoint. The HTTP request has a user and a password parameter. I have an interceptor, EchoInterceptor, that is going to look for the user and password parameters:
<mule-configuration id="Mule_Echo_Parameters_Axis" version="1.0">  
<transformer name="HttpRequestToSoapRequest"
<model name="echoAuth">
<mule-descriptor name="EchoAuth" implementation="EchoAuth">
<endpoint address="axis:http://localhost:65081/services"
transformers="HttpRequestToSoapRequest" />
<interceptor className="EchoInterceptor"/>

The EchoAuth class has an echo method whose invocation I want to intercept. The method simply echos the parameters its given. An HTTP request to the Axis endpoint looks like:

The interceptor class:
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mule.umo.Invocation;
import org.mule.umo.UMOException;
import org.mule.umo.UMOInterceptor;
import org.mule.umo.UMOMessage;
import org.mule.util.PropertiesUtils;

public class EchoInterceptor implements UMOInterceptor {

private final Log logger = LogFactory.getLog(this.getClass());

public UMOMessage intercept(Invocation invocation) throws UMOException {

logger.info("Intercepted method invocation.");

UMOMessage incomingMessage = invocation.getMessage();
logger.info("Incoming message: " + incomingMessage.toString());

String request = incomingMessage.getProperty("http.request").toString();
int i = request.indexOf('?');
String query = request.substring(i + 1);
logger.info("HTTP request query string: " + query);

// following example in Mule's HTTPRequestToSoapRequest class
Properties p = PropertiesUtils.getPropertiesFromQueryString(query);
String user = p.getProperty("user");
String password = p.getProperty("password");
logger.info("Query string parameter 'user': " + user);
logger.info("Query string parameter 'password': " + password);

UMOMessage resultingMessage = invocation.execute();
logger.info("Returning resulting message: " + resultingMessage.toString());

return resultingMessage;
The results in my mule.log, with a lot of the clutter omitted:
EchoInterceptor: Incoming message: org.mule.providers.soap.axis.AxisMessageAdapter
/org.mule.providers.soap.axis.AxisMessageAdapter... properties={
method=public abstract java.lang.String AuthService.echo(java.lang.String,java.lang.String)
EchoInterceptor: HTTP request query string: method=echo&user=Costanza&password=bosco
EchoInterceptor: Query string parameter 'user': Costanza
EchoInterceptor: Query string parameter 'password': bosco
EchoAuth: Received user: Costanza
EchoAuth: Received password: bosco
EchoAuth: Authentication: Costanza/bosco
EchoInterceptor: Returning resulting message:
In my real-world project, I need to send the user and password off to a
service for authentication, and halt processing. The interceptor doc lists a couple of methods for doing so I need to investigate further.

No comments:

Post a Comment