KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > webservice > Ejb3SystemHandlerDelegate


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.webservice;
25
26 import java.util.logging.Level JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import java.rmi.UnmarshalException JavaDoc;
29
30 import com.sun.enterprise.webservice.monitoring.EndpointImpl;
31
32 import com.sun.xml.ws.spi.runtime.SystemHandlerDelegate;
33 import com.sun.xml.ws.spi.runtime.MessageContext;
34 import com.sun.enterprise.webservice.monitoring.JAXWSEndpointImpl;
35
36 import com.sun.enterprise.Switch;
37 import com.sun.ejb.Container;
38 import com.sun.ejb.Invocation;
39 import com.sun.enterprise.InvocationManager;
40
41 /**
42  *
43  * @author dochez
44  */

45 public class Ejb3SystemHandlerDelegate implements SystemHandlerDelegate {
46     
47     private JAXWSEndpointImpl endpoint;
48     private WsUtil wsUtil = new WsUtil();
49     
50     /** Creates a new instance of Ejb3SystemHandlerDelegate */
51     public Ejb3SystemHandlerDelegate(JAXWSEndpointImpl endpoint) {
52         this.endpoint = endpoint;
53     }
54     
55
56    /**
57     * The processRequest method is invoked with an object that
58     * implements com.sun.xml.rpc.spi.runtime.SOAPMessageContext.
59     * <p>
60     * When this method is called by the WSServletDelegate
61     * (on the server side of jaxws servlet container invocation processing)
62     * it must be called just before the call to implementor.getTie().handle(),
63     * and at the time of the request message and the following properties
64     * must have been set on the SOAPMessageContext.
65     * <p>
66     * com.sun.xml.rpc.server.http.MessageContextProperties.IMPLEMENTOR
67     * <br>
68     * This property must be set to the com.sun.xml.rpc.spi.runtime.Implementor
69     * object corresponding to the target endpoint.
70     *
71     * NOTE: I'd like us to be able to hang the ServletAuthContext off the Implementor.
72     *
73     * <p>
74     * com.sun.xml.rpc.server.http.MessageContextProperties.HTTP_SERVLET_REQUEST
75     * <br>
76     * This property must be
77     * set to the javax.servlet.http.HttpServletRequest object containing the
78     * JAXWS invocation.
79     * <p>
80     * com.sun.xml.rpc.server.http.MessageContextProperties.HTTP_SERVLET_RESPONSE
81     * <br>
82     * This property must be
83     * set to the javax.servlet.http.HttpServletResponse object corresponding to
84     * the JAXWS invocation.
85     * <p>
86     * com.sun.xml.rpc.server.MessageContextProperties.HTTP_SERVLET_CONTEXT
87     * <br>
88     * This property must be
89     * set to the javax.servlet.ServletContext object corresponding to web application
90     * in which the JAXWS servlet is running.
91     * @param messageContext the SOAPMessageContext object containing the request
92     * message and the properties described above.
93     * @return true if processing by the delegate was such that the caller
94     * should continue with its normal message processing. Returns false if the
95     * processing by the delegate resulted in the messageContext containing a response
96     * message that should be returned without the caller proceding to its normal
97     * message processing.
98     * @throws java.lang.RuntimeException when the processing by the delegate failed,
99     * without yielding a response message. In this case, the expectation is that
100     * the caller will return a HTTP layer response code reporting that an internal
101     * error occured.
102     */

103     public boolean processRequest(MessageContext messageContext)throws Exception JavaDoc {
104         String JavaDoc messageID = (String JavaDoc) messageContext.get(EndpointImpl.MESSAGE_ID);
105         System.out.println("process request : Message ID " + messageID);
106         
107         // Trace if necessary
108
if (messageID!=null || (endpoint!=null && endpoint.hasListeners())) {
109             endpoint.processRequest(messageContext);
110         }
111     
112         if (endpoint.getDescriptor().hasHandlerChain()) {
113             
114             // some handlers are defined.
115
Switch theSwitch = Switch.getSwitch();
116             InvocationManager invManager = theSwitch.getInvocationManager();
117             Invocation inv = (Invocation) invManager.getCurrentInvocation();
118             Container container = (Container) inv.container;
119             
120             Method JavaDoc m = messageContext.getMethod();
121             if (m!=null) {
122                 try {
123                     inv.method = m;
124
125                     inv.setWebServiceMethod(inv.method);
126                
127                     if ( !container.authorize(inv) ) {
128                         inv.exception = new Exception JavaDoc
129                             ("Client not authorized for invocation of "
130                              + inv.method);
131                     }
132                 } catch(Exception JavaDoc e) {
133                     String JavaDoc errorMsg = "Error unmarshalling method for ejb "
134                             + endpoint.getDescriptor().getEjbComponentImpl().getName();
135                     inv.exception = new UnmarshalException JavaDoc(errorMsg);
136                     inv.exception.initCause(e);
137                 }
138             } else {
139                 inv.setWebServiceMethod(null);
140             }
141         
142             if( inv.exception != null ) {
143                 WsUtil.getDefaultLogger().log(Level.WARNING, "preRequestHandlerHook", inv.exception);
144                 // TODO : I need access to the SAAJ 1.3 to write the soap fault,
145
// commenting for now
146
wsUtil.throwSOAPFaultException(inv.exception.getMessage(),
147                         messageContext);
148             }
149         }
150         return true;
151     }
152
153              
154     /**
155      * Called after request handlers are invoked
156      */

157     public void preInvokeEndpointHook(MessageContext messageContext) {
158        
159         
160         if (endpoint.getDescriptor().hasHandlerChain()) {
161             // some handlers are defined.
162
Switch theSwitch = Switch.getSwitch();
163             InvocationManager invManager = theSwitch.getInvocationManager();
164             Invocation inv = (Invocation) invManager.getCurrentInvocation();
165             Container container = (Container) inv.container;
166             
167             Method JavaDoc m = messageContext.getMethod();
168             if (m!=null) {
169                 try {
170                     Method JavaDoc webServiceMethodInPreHandler = inv.getWebServiceMethod();
171                     
172                     if( webServiceMethodInPreHandler != null ) {
173                         // Now that application handlers have run, do another method
174
// lookup and compare the results with the original one. This
175
// ensures that the application handlers have not changed
176
// the message context in any way that would impact which
177
// method is invoked.
178
if( !webServiceMethodInPreHandler.equals(m) ) {
179                             inv.exception = new UnmarshalException JavaDoc
180                                     ("Original method " + webServiceMethodInPreHandler +
181                                     " does not match post-handler method " + m);
182                         }
183                     }
184                 } catch(Exception JavaDoc e) {
185                     String JavaDoc errorMsg = "Exception while getting method for " +
186                             ((inv != null ) ?
187                                 ((Container) inv.container).getEjbDescriptor().getName() : "");
188                     inv.exception = new UnmarshalException JavaDoc(errorMsg);
189                     inv.exception.initCause(e);
190                 }
191                 
192                 if( inv.exception != null ) {
193                     WsUtil.getDefaultLogger().log(Level.WARNING, "postEjbHandlerError", inv.exception);
194
195                     wsUtil.throwSOAPFaultException(inv.exception.getMessage(),
196                           messageContext);
197                 }
198             }
199         }
200     }
201     
202    /**
203     * The processResponse method is invoked with an object that
204     * implements com.sun.xml.rpc.spi2.runtime.SOAPMessageContext.
205     * <p>
206     * When this method is called by the WSServletDelegate
207     * (on the server side of jaxws servlet container invocation processing)
208     * it must be called just just after the call to implementor.getTie().handle().
209     * In the special case where the handle method throws an exception, the
210     * processResponse message must not be called.
211     * <p>
212     * The SOAPMessageContext passed to the processRequest and handle messages is
213     * passed to the processResponse method.
214     * @throws java.lang.RuntimeException when the processing by the delegate failed,
215     * in which case the caller is expected to return an HTTP layer
216     * response code reporting that an internal error occured.
217     */

218     public void processResponse(MessageContext messageContext) throws Exception JavaDoc {
219         String JavaDoc messageID = (String JavaDoc) messageContext.get(EndpointImpl.MESSAGE_ID);
220         System.out.println("process response : Message ID " + messageID);
221         
222         // Trace if necessary
223
if (messageID!=null || (endpoint!=null && endpoint.hasListeners())) {
224             endpoint.processResponse(messageContext);
225         }
226     }
227 }
228
Popular Tags