package org.jboss.webservice.server;
import org.jboss.axis.providers.java.RPCInvocation;
import org.jboss.axis.providers.java.RPCProvider;
import org.jboss.ejb.plugins.AbstractInterceptor;
import org.jboss.invocation.Invocation;
import org.jboss.invocation.InvocationKey;
import org.jboss.webservice.Constants;
import javax.xml.rpc.handler.HandlerChain;
import javax.xml.rpc.handler.soap.SOAPMessageContext;
public class ServiceEndpointInterceptor
extends AbstractInterceptor
{
public Object invoke(final Invocation mi) throws Exception
{
SOAPMessageContext msgContext = (SOAPMessageContext)mi.getPayloadValue(InvocationKey.SOAP_MESSAGE_CONTEXT);
if (msgContext == null)
{
return getNext().invoke(mi);
}
HandlerChain handlerChain = (HandlerChain)msgContext.getProperty(Constants.HANDLER_CHAIN);
if (handlerChain == null)
throw new IllegalStateException("Cannot obtain handler chain from msg context");
RPCInvocation invocation = (RPCInvocation)msgContext.getProperty(RPCProvider.RPC_INVOCATION);
if (invocation == null)
throw new IllegalStateException("Cannot obtain RPCInvocation from message context");
try
{
if (handlerChain.handleRequest(msgContext) == false)
{
log.warn("FIXME: handlerChain.handleRequest() returned false");
return null;
}
RPCInvocation invAfterRequestHandler = (RPCInvocation)msgContext.getProperty(RPCProvider.RPC_INVOCATION);
if (invocation.equals(invAfterRequestHandler) == false)
{
Object[] args = invAfterRequestHandler.getArgValues();
if (args.length != mi.getArguments().length)
throw new IllegalArgumentException("Invalid argument list in RPCInvocation");
for (int i = 0; i < args.length; i++)
{
Class miClass = mi.getArguments()[i].getClass();
Class rpcClass = args[i].getClass();
if (miClass.isAssignableFrom(rpcClass) == false)
throw new IllegalArgumentException("RPCInvocation argument cannot be assigned: " + miClass.getName() + " != " + rpcClass.getName());
mi.getArguments()[i] = args[i];
}
}
}
catch (Exception e)
{
log.error("Error processing request handler chain", e);
throw e;
}
Object resObj = null;
try
{
resObj = getNext().invoke(mi);
}
catch (Exception e)
{
msgContext.setProperty(Constants.LAST_FAULT, e);
log.error("Error from service endpoint, processing fault handler chain", e);
if (handlerChain.handleFault(msgContext) == false)
{
log.warn("FIXME: handlerChain.handleFault() returned false");
return null;
}
throw e;
}
try
{
invocation.prepareResponseEnvelope(resObj);
if (handlerChain.handleResponse(msgContext) == false)
{
log.warn("FIXME: handlerChain.handleResponse() returned false");
return null;
}
}
catch (Exception e)
{
log.error("Error processing response handler chain", e);
throw e;
}
return resObj;
}
}