KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > webservice > server > ServiceEndpointInterceptor


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.webservice.server;
8
9 // $Id: ServiceEndpointInterceptor.java,v 1.4.4.2 2005/03/02 14:32:32 tdiesler Exp $
10

11 import org.jboss.axis.providers.java.RPCInvocation;
12 import org.jboss.axis.providers.java.RPCProvider;
13 import org.jboss.ejb.plugins.AbstractInterceptor;
14 import org.jboss.invocation.Invocation;
15 import org.jboss.invocation.InvocationKey;
16 import org.jboss.webservice.Constants;
17
18 import javax.xml.rpc.handler.HandlerChain JavaDoc;
19 import javax.xml.rpc.handler.soap.SOAPMessageContext JavaDoc;
20
21 /**
22  * This Interceptor does the ws4ee handler processing.
23  * <p/>
24  * According to the ws4ee spec the handler logic must be invoked after the container
25  * applied method level security to the invocation. I don't think we can use Axis handlers
26  * for ws4ee handler processing.
27  *
28  * @author Thomas.Diesler@jboss.org
29  * @version $Revision: 1.4.4.2 $
30  */

31 public class ServiceEndpointInterceptor
32         extends AbstractInterceptor
33 {
34    // Interceptor implementation --------------------------------------
35

36    /**
37     * Before and after we call the service endpoint bean, we process the handler chains.
38     * <p/>
39     * The handler chain implemantation may replace the RPCInvocation object in the message context
40     * if it finds the the handlers have modyfied the SOAPEnvelope.
41     * <p/>
42     * When you change the implementation here, make sure you do the same in the InvokerProviderJSE
43     */

44    public Object JavaDoc invoke(final Invocation mi) throws Exception JavaDoc
45    {
46
47       // It's not for us
48
SOAPMessageContext JavaDoc msgContext = (SOAPMessageContext JavaDoc)mi.getPayloadValue(InvocationKey.SOAP_MESSAGE_CONTEXT);
49       if (msgContext == null)
50       {
51          return getNext().invoke(mi);
52       }
53
54       // Handlers need to be Tx. Therefore we must invoke the handler chain after the TransactionInterceptor.
55
// Invoking the ws4ee handlers from an axis handler would be far too early in the invocation path.
56

57       HandlerChain JavaDoc handlerChain = (HandlerChain JavaDoc)msgContext.getProperty(Constants.HANDLER_CHAIN);
58       if (handlerChain == null)
59          throw new IllegalStateException JavaDoc("Cannot obtain handler chain from msg context");
60
61       // Get the invocation object from the message context
62
RPCInvocation invocation = (RPCInvocation)msgContext.getProperty(RPCProvider.RPC_INVOCATION);
63       if (invocation == null)
64          throw new IllegalStateException JavaDoc("Cannot obtain RPCInvocation from message context");
65
66       try
67       {
68          // Call the request handler chain
69
if (handlerChain.handleRequest(msgContext) == false)
70          {
71             log.warn("FIXME: handlerChain.handleRequest() returned false");
72             return null;
73          }
74
75          // The handler chain might have replaced the invocation object
76
RPCInvocation invAfterRequestHandler = (RPCInvocation)msgContext.getProperty(RPCProvider.RPC_INVOCATION);
77
78          // Replace the arguments in the method invocation
79
if (invocation.equals(invAfterRequestHandler) == false)
80          {
81             Object JavaDoc[] args = invAfterRequestHandler.getArgValues();
82             if (args.length != mi.getArguments().length)
83                throw new IllegalArgumentException JavaDoc("Invalid argument list in RPCInvocation");
84
85             for (int i = 0; i < args.length; i++)
86             {
87                Class JavaDoc miClass = mi.getArguments()[i].getClass();
88                Class JavaDoc rpcClass = args[i].getClass();
89                if (miClass.isAssignableFrom(rpcClass) == false)
90                   throw new IllegalArgumentException JavaDoc("RPCInvocation argument cannot be assigned: " + miClass.getName() + " != " + rpcClass.getName());
91
92                mi.getArguments()[i] = args[i];
93             }
94          }
95       }
96       catch (Exception JavaDoc e)
97       {
98          log.error("Error processing request handler chain", e);
99          throw e;
100       }
101
102       Object JavaDoc resObj = null;
103       try
104       {
105          // Call the next interceptor in the chain
106
resObj = getNext().invoke(mi);
107       }
108       catch (Exception JavaDoc e)
109       {
110          msgContext.setProperty(Constants.LAST_FAULT, e);
111          log.error("Error from service endpoint, processing fault handler chain", e);
112
113          // Call the response handler chain
114
if (handlerChain.handleFault(msgContext) == false)
115          {
116             log.warn("FIXME: handlerChain.handleFault() returned false");
117             return null;
118          }
119
120          // Throw the exception from the service endpoint to axis
121
throw e;
122       }
123
124       try
125       {
126          // Prepare the response envelope for the response handlers
127
invocation.prepareResponseEnvelope(resObj);
128
129          // Call the response handler chain
130
if (handlerChain.handleResponse(msgContext) == false)
131          {
132             log.warn("FIXME: handlerChain.handleResponse() returned false");
133             return null;
134          }
135
136          // There is no way we can get a new resObj from the response SOAPEnvelope.
137
// Sucker is the response handler that modifies the response value.
138
// The RPCInvocation will ignore another call to prepareResponseEnvelope
139
// because this could potentially overwrite what the handlers did.
140
}
141       catch (Exception JavaDoc e)
142       {
143          log.error("Error processing response handler chain", e);
144          throw e;
145       }
146
147       return resObj;
148    }
149 }
150
Popular Tags