package org.jboss.webservice.client;
import org.jboss.axis.AxisFault;
import org.jboss.axis.ConfigurationException;
import org.jboss.axis.EngineConfiguration;
import org.jboss.axis.Handler;
import org.jboss.axis.MessageContext;
import org.jboss.axis.client.AxisClient;
import org.jboss.axis.client.Call;
import org.jboss.axis.utils.Messages;
import org.jboss.logging.Logger;
import org.jboss.webservice.handler.ClientHandlerChain;
import javax.xml.rpc.JAXRPCException;
import javax.xml.rpc.handler.HandlerChain;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class ClientEngine extends AxisClient
{
static final long serialVersionUID = 53844570311989118L;
protected static Logger log = Logger.getLogger(ClientEngine.class);
private Map handlerChainMap = new HashMap();
public ClientEngine(EngineConfiguration config)
{
super(config);
}
public void registerHandlerChain(String portName, List infos, Set roles)
{
ClientHandlerChain chain = new ClientHandlerChain(infos, roles);
handlerChainMap.put(portName, chain);
}
public void invoke(MessageContext msgContext) throws AxisFault
{
log.debug("invoke: " + msgContext);
String hName = null;
Handler handler = null;
MessageContext previousContext = getCurrentMessageContext();
try
{
setCurrentMessageContext(msgContext);
msgContext.setPastPivot(false);
ClientHandlerChain handlerChain = (ClientHandlerChain)getHandlerChain(msgContext);
if (handlerChain.getState() == ClientHandlerChain.STATE_CREATED)
{
handlerChain.init(null);
}
if ((handler = getGlobalRequest()) != null)
handler.invoke(msgContext);
if (handlerChain.handleRequest(msgContext) == false)
{
log.warn("FIXME: handlerChain.handleRequest() returned false");
return;
}
hName = msgContext.getTransportName();
if (hName != null && (handler = getTransport(hName)) != null)
{
handler.invoke(msgContext);
}
else
{
throw new AxisFault(Messages.getMessage("noTransport00", hName));
}
boolean isFaultMessage = false;
if (isFaultMessage && handlerChain.handleFault(msgContext) == false)
{
log.warn("FIXME: handlerChain.handleFault() returned false");
return;
}
else if (handlerChain.handleResponse(msgContext) == false)
{
log.warn("FIXME: handlerChain.handleResponse() returned false");
return;
}
if ((handler = getGlobalResponse()) != null)
{
handler.invoke(msgContext);
}
}
catch (ConfigurationException e)
{
throw new IllegalStateException(e.toString());
}
finally
{
setCurrentMessageContext(previousContext);
}
}
private HandlerChain getHandlerChain(MessageContext msgContext)
{
HandlerChain handlerChain = null;
if (handlerChainMap.size() == 0)
handlerChain = new ClientHandlerChain(null, null);
if (handlerChainMap.size() == 1)
{
String portName = (String)handlerChainMap.keySet().iterator().next();
handlerChain = (HandlerChain)handlerChainMap.get(portName);
log.debug("Using handler chain for port: " + portName);
}
Call call = (Call)msgContext.getProperty(MessageContext.CALL);
if (call == null)
throw new JAXRPCException("Cannot obtain current call");
if (call.getPortName() != null)
{
String portName = call.getPortName().getLocalPart();
handlerChain = (HandlerChain)handlerChainMap.get(portName);
log.debug("Using handler chain for port: " + portName);
}
if (handlerChain == null)
{
handlerChain = new ClientHandlerChain(null, null);
log.debug("Using empty handler chain");
}
return handlerChain;
}
}