KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Iterator JavaDoc;
34
35 import javax.servlet.*;
36 import javax.servlet.http.*;
37
38 import com.sun.enterprise.webservice.monitoring.WebServiceEngineImpl;
39 import com.sun.enterprise.webservice.monitoring.HttpResponseInfoImpl;
40 import com.sun.enterprise.webservice.monitoring.ThreadLocalInfo;
41 import com.sun.enterprise.webservice.monitoring.JAXWSEndpointImpl;
42 import com.sun.enterprise.webservice.monitoring.WebServiceTesterServlet;
43 import com.sun.enterprise.webservice.monitoring.Endpoint;
44 import com.sun.enterprise.webservice.monitoring.EndpointType;
45 import com.sun.enterprise.webservice.monitoring.EndpointImpl;
46
47 import com.sun.enterprise.Switch;
48 import com.sun.enterprise.ComponentInvocation;
49 import com.sun.enterprise.InvocationManager;
50 import com.sun.enterprise.deployment.WebServicesDescriptor;
51 import com.sun.enterprise.deployment.WebService;
52 import com.sun.enterprise.deployment.WebServiceEndpoint;
53 import com.sun.enterprise.deployment.WebBundleDescriptor;
54 import com.sun.enterprise.deployment.WebComponentDescriptor;
55 import com.sun.enterprise.deployment.ResourceReferenceDescriptor;
56
57 import com.sun.enterprise.util.InjectionManagerImpl;
58
59 import com.sun.enterprise.security.jauth.ServerAuthConfig;
60
61 import com.sun.xml.ws.spi.runtime.WSRtObjectFactory;
62 import com.sun.xml.ws.spi.runtime.RuntimeEndpointInfo;
63 import com.sun.xml.ws.spi.runtime.SystemHandlerDelegate;
64 import com.sun.xml.ws.spi.runtime.WSConnection;
65 import com.sun.xml.ws.spi.runtime.WebServiceContext;
66 import javax.xml.ws.handler.MessageContext;
67 import javax.xml.ws.handler.MessageContext.Scope;
68 import javax.xml.ws.soap.SOAPBinding;
69 import javax.xml.ws.http.HTTPBinding;
70
71 // catalina
72
import org.apache.catalina.Loader;
73
74 import java.util.logging.Logger JavaDoc;
75 import java.util.logging.Level JavaDoc;
76 import com.sun.logging.LogDomains;
77
78 /**
79  * The JAX-WS dispatcher servlet.
80  *
81  */

82 public class JAXWSServlet extends HttpServlet {
83
84     private static Logger JavaDoc logger = LogDomains.getLogger(LogDomains.WEB_LOGGER);
85     private ServletContext servletContext;
86     
87     private WebServiceEndpoint endpoint_;
88     
89     private WSRtObjectFactory rpcFactory_;
90
91     private String JavaDoc urlPattern;
92     
93     private String JavaDoc contextRoot;
94
95     private boolean jaxwsInitDone = false;
96     
97     private WebServiceEngineImpl wsEngine_;
98     private JAXWSEndpointImpl endpointImpl_;
99
100     ClassLoader JavaDoc classLoader;
101     
102     private com.sun.xml.ws.server.Tie tie = new com.sun.xml.ws.server.Tie();
103
104     public void init(ServletConfig servletConfig) throws ServletException {
105         try {
106             super.init(servletConfig);
107             this.servletContext = servletConfig.getServletContext();
108             wsEngine_ = WebServiceEngineImpl.getInstance();
109             // Register endpoints here
110
doInit(servletConfig);
111         } catch (Throwable JavaDoc e) {
112             throw new ServletException(e);
113         }
114     }
115
116     public void destroy() {
117         JAXWSRuntimeEpiRegistry.getInstance().removeRuntimeInfo(contextRoot);
118         try {
119             (new WsUtil()).doPreDestroy(endpoint_, classLoader);
120         } catch (Throwable JavaDoc t) {
121             logger.log(Level.WARNING, "@PreDestroy lifecycle call failed for service"
122                     + endpoint_.getName(), t);
123         }
124         wsEngine_.removeHandler(endpoint_);
125     }
126
127     protected void doPost(HttpServletRequest request,
128                           HttpServletResponse response)
129         throws ServletException {
130
131         if (("Tester".equalsIgnoreCase(request.getQueryString())) &&
132              (!(HTTPBinding.HTTP_BINDING.equals(endpoint_.getProtocolBinding())))) {
133             Endpoint endpt = wsEngine_.getEndpoint(request.getRequestURL().toString());
134             if (endpt!=null && Boolean.parseBoolean(endpt.getDescriptor().getDebugging())) {
135                 WebServiceTesterServlet.invoke(request, response,
136                         endpt.getDescriptor());
137                 return;
138             }
139         }
140                 
141         // check if we need to trace this...
142
String JavaDoc messageId=null;
143         if((wsEngine_.getGlobalMessageListener()!=null) &&
144              (!(HTTPBinding.HTTP_BINDING.equals(endpoint_.getProtocolBinding())))) {
145             Endpoint endpt = wsEngine_.getEndpoint(request.getRequestURL().toString());
146             messageId = wsEngine_.preProcessRequest(endpt);
147             if (messageId!=null) {
148                 ThreadLocalInfo config = new ThreadLocalInfo(messageId, request);
149                 wsEngine_.getThreadLocal().set(config);
150             }
151         }
152
153         // We need to call runtimeEndpointInfo.init at the very first doPost call for this service
154
if(!jaxwsInitDone) {
155             // come to this synchronized only if the flag is not set
156
synchronized(this) {
157                 // multiple clients could have requested the service at once and
158
// all of them could be waiting at this syncronized block;
159
// so do one more check to ensure that jaxws.init is done only once
160
if(!jaxwsInitDone) {
161                     String JavaDoc path = request.getRequestURI().substring(request.getContextPath().length());
162                     JAXWSRuntimeEpiRegistry.getInstance().getRuntimeEndpointInfo(contextRoot, urlPattern, path).init();
163                     jaxwsInitDone = true;
164                 }
165             }
166         }
167         
168         // lookup registered URLs, create JAXRPC connection object and forward the request
169
try {
170             RuntimeEndpointInfo targetEndpoint = getEndpointFor(request);
171             if (targetEndpoint != null) {
172                 WSConnection connection = rpcFactory_.createWSConnection(request, response);
173                 WebServiceContext wsCtxt = targetEndpoint.getWebServiceContext();
174                 MessageContext msgCtxt = rpcFactory_.createMessageContext();
175                 msgCtxt.put(MessageContext.SERVLET_CONTEXT, servletContext);
176                 msgCtxt.setScope(MessageContext.SERVLET_CONTEXT, Scope.APPLICATION);
177                 msgCtxt.put(MessageContext.SERVLET_REQUEST, request);
178                 msgCtxt.setScope(MessageContext.SERVLET_REQUEST, Scope.APPLICATION);
179                 msgCtxt.put(MessageContext.SERVLET_RESPONSE, response);
180                 msgCtxt.setScope(MessageContext.SERVLET_RESPONSE, Scope.APPLICATION);
181                 msgCtxt.put(EndpointImpl.MESSAGE_ID, messageId);
182                 msgCtxt.put(MessageContext.HTTP_REQUEST_METHOD, request.getMethod());
183                 msgCtxt.setScope(MessageContext.HTTP_REQUEST_METHOD, Scope.APPLICATION);
184                 if (request.getQueryString() != null) {
185                     msgCtxt.put(MessageContext.QUERY_STRING, request.getQueryString());
186                     msgCtxt.setScope(MessageContext.QUERY_STRING, Scope.APPLICATION);
187                 }
188                 if (request.getPathInfo() != null) {
189                     msgCtxt.put(MessageContext.PATH_INFO, request.getPathInfo());
190                     msgCtxt.setScope(MessageContext.PATH_INFO, Scope.APPLICATION);
191                 }
192                 msgCtxt.put(MessageContext.HTTP_REQUEST_HEADERS, connection.getHeaders());
193                 msgCtxt.setScope(MessageContext.HTTP_REQUEST_HEADERS, Scope.APPLICATION);
194                 wsCtxt.setMessageContext(msgCtxt);
195                 tie.handle(connection, targetEndpoint);
196             } else {
197                 throw new ServletException("Service not found");
198             }
199         } catch(Throwable JavaDoc t) {
200             ServletException se = new ServletException();
201             se.initCause(t);
202             throw se;
203         }
204
205         if (messageId!=null) {
206             HttpResponseInfoImpl info = new HttpResponseInfoImpl(response);
207             wsEngine_.postProcessResponse(messageId, info);
208         }
209     }
210
211     protected void doGet(HttpServletRequest request,
212                          HttpServletResponse response)
213         throws ServletException, IOException JavaDoc {
214         
215         if (("Tester".equalsIgnoreCase(request.getQueryString())) &&
216              (!(HTTPBinding.HTTP_BINDING.equals(endpoint_.getProtocolBinding())))) {
217             
218             Endpoint endpt = wsEngine_.getEndpoint(request.getRequestURL().toString());
219             if( (endpt.getDescriptor().isSecure()) ||
220                 (endpt.getDescriptor().getMessageSecurityBinding() != null) ) {
221                 // disable tester for secured services
222
String JavaDoc message = endpt.getDescriptor().getWebService().getName() +
223                         " is a secured webservice; Tester feature is not supported for secured services";
224                 (new WsUtil()).writeInvalidMethodType(response, message);
225                 return;
226             }
227             if (endpt!=null && Boolean.parseBoolean(endpt.getDescriptor().getDebugging())) {
228                 Loader loader = (Loader) endpt.getDescriptor().getBundleDescriptor().getExtraAttribute("WEBLOADER");
229                 if (loader != null) {
230                     endpt.getDescriptor().getBundleDescriptor().setClassLoader(loader.getClassLoader());
231                     endpt.getDescriptor().getBundleDescriptor().removeExtraAttribute("WEBLOADER");
232                 }
233                 WebServiceTesterServlet.invoke(request, response,
234                         endpt.getDescriptor());
235                 return;
236             }
237         }
238         
239         // If it is not a "Tester request" and it is not a WSDL request,
240
// this might be a restful service
241
if (!("WSDL".equalsIgnoreCase(request.getQueryString())) &&
242               (HTTPBinding.HTTP_BINDING.equals(endpoint_.getProtocolBinding()))) {
243             doPost(request, response);
244             return;
245         }
246         
247         // normal WSDL retrieval invocation
248
WsUtil wsUtil = new WsUtil();
249         try {
250             wsUtil.handleGet(request, response, endpoint_);
251         } catch(Exception JavaDoc e) {
252             logger.log(Level.WARNING, "Servlet web service endpoint '" +
253                endpoint_.getEndpointName() + "' HTTP GET error", e);
254         }
255     }
256     
257     private void doInit(ServletConfig servletConfig) throws ServletException {
258         String JavaDoc servletName = "unknown";
259
260         try {
261             InvocationManager invManager =
262                 Switch.getSwitch().getInvocationManager();
263             ComponentInvocation inv = invManager.getCurrentInvocation();
264             Object JavaDoc containerContext = inv.getContainerContext();
265
266             WebBundleDescriptor webBundle = (WebBundleDescriptor)
267                 Switch.getSwitch().getDescriptorFor(containerContext);
268             classLoader = Thread.currentThread().getContextClassLoader();
269             servletName = servletConfig.getServletName();
270             contextRoot = webBundle.getContextRoot();
271             WebComponentDescriptor webComponent =
272                 webBundle.getWebComponentByCanonicalName(servletName);
273
274             if( webComponent != null ) {
275                 WebServicesDescriptor webServices = webBundle.getWebServices();
276                 Collection JavaDoc endpoints =
277                     webServices.getEndpointsImplementedBy(webComponent);
278                 // Only 1 endpoint per servlet is supported, even though
279
// data structure implies otherwise.
280
endpoint_ = (WebServiceEndpoint) endpoints.iterator().next();
281
282                 // need to invoke the endpoint lifecylcle
283
if(!(HTTPBinding.HTTP_BINDING.equals(endpoint_.getProtocolBinding()))) {
284                     // Doing this so that restful service are not monitored
285
endpointImpl_ = (JAXWSEndpointImpl)
286                         wsEngine_.createHandler((SystemHandlerDelegate)null, endpoint_);
287                 }
288
289                 registerEndpoint();
290
291             } else {
292                 throw new ServletException(servletName + " not found");
293             }
294         } catch(Throwable JavaDoc t) {
295             logger.log(Level.WARNING, "Servlet web service endpoint '" +
296                        servletName + "' failure", t);
297             ServletException se = new ServletException();
298             se.initCause(t);
299             throw se;
300         }
301     }
302
303     private void registerEndpoint()
304         throws Exception JavaDoc {
305
306         //
307
// Convert J2EE deployment descriptor information into
308
// JAXRPC endpoint data structure
309
//
310

311         rpcFactory_ = WSRtObjectFactory.newInstance();
312         RuntimeEndpointInfo endpointInfo =
313         rpcFactory_.createRuntimeEndpointInfo();
314         
315         Object JavaDoc serviceEndpoint =
316                 Class.forName(endpoint_.getServletImplClass(), true, classLoader).newInstance();
317
318         // Inject @Resource for the endpoint
319
new InjectionManagerImpl().injectInstance(serviceEndpoint);
320         
321         // For JAXWS Servlet endpoint both implementor class and the implementor are the same
322
endpointInfo.setImplementorClass(Class.forName(endpoint_.getServletImplClass(), true, classLoader));
323         endpointInfo.setImplementor(serviceEndpoint);
324
325         // Set webservice context here
326
// If the endpoint has a WebServiceContext with @Resource then
327
// that has to be used
328
WebServiceContext wsc = null;
329         WebBundleDescriptor bundle = (WebBundleDescriptor)endpoint_.getBundleDescriptor();
330         Iterator JavaDoc<ResourceReferenceDescriptor> it = bundle.getResourceReferenceDescriptors().iterator();
331         while(it.hasNext()) {
332             ResourceReferenceDescriptor r = it.next();
333             if(r.isWebServiceContext()) {
334                 try {
335                     javax.naming.InitialContext JavaDoc ic = new javax.naming.InitialContext JavaDoc();
336                     wsc = (WebServiceContext) ic.lookup("java:comp/env/" + r.getName());
337                 } catch (Throwable JavaDoc t) {} // Swallowed intentionally
338
}
339         }
340         if(wsc == null) {
341             wsc = new WebServiceContextImpl();
342         }
343         endpointInfo.setWebServiceContext(wsc);
344
345         // For web components, this will be relative to the web app
346
// context root. Make sure there is a leading slash.
347
String JavaDoc uri = endpoint_.getEndpointAddressUri();
348         urlPattern = uri.startsWith("/") ? uri : "/" + uri;
349
350         // Set wsdl and catalog info
351
java.net.URL JavaDoc catalogURL = null;
352         File JavaDoc catalogFile = new File JavaDoc(endpoint_.getBundleDescriptor().getDeploymentDescriptorDir() +
353                 File.separator + "jax-ws-catalog.xml");
354         if(catalogFile.exists()) {
355             catalogURL = catalogFile.toURL();
356         }
357         endpointInfo.setWsdlInfo(endpoint_.getWebService().getWsdlFileUrl(),
358                 rpcFactory_.createEntityResolver(catalogURL));
359         
360         // Set Service name and port name
361
endpointInfo.setServiceName(endpoint_.getServiceName());
362         endpointInfo.setPortName(endpoint_.getWsdlPort());
363         
364         // Create Binding and Set the System handler delegate here
365
endpointInfo.setBinding(rpcFactory_.createBinding(endpoint_.getProtocolBinding()));
366         
367         // If BindingId is SOAPXXHTTP_MTOM_BINDING, enable MTOM
368
WsUtil wsu = new WsUtil();
369         wsu.setMtom(endpointInfo.getBinding(), endpoint_);
370         
371         // Process handlers
372
wsu.configureJAXWSServiceHandlers(endpoint_, endpointInfo);
373
374     SystemHandlerDelegate delegate =
375         JAXWSSystemHandlerDelegateFactory.getServletDelegate(endpoint_);
376
377     // set the appropriate delegate in the SOAPMessageDispatcher
378
if (endpointImpl_ != null) {
379         endpointImpl_.setParent(delegate);
380         endpointInfo.getBinding().setSystemHandlerDelegate(endpointImpl_);
381     } else {
382         endpointInfo.getBinding().setSystemHandlerDelegate(delegate);
383     }
384
385         registerEndpointUrlPattern(endpointInfo);
386         wsu.doPostConstruct(endpointInfo.getImplementorClass(),
387                 serviceEndpoint);
388     }
389     
390     private void registerEndpointUrlPattern(RuntimeEndpointInfo info) {
391         JAXWSRuntimeEpiRegistry.getInstance().
392                 addRuntimeEndpointInfo(contextRoot, urlPattern,info);
393     }
394     
395     private RuntimeEndpointInfo getEndpointFor(HttpServletRequest request) {
396         String JavaDoc path = request.getRequestURI().substring(request.getContextPath().length());
397         return JAXWSRuntimeEpiRegistry.getInstance().
398                 getRuntimeEndpointInfo(contextRoot, urlPattern, path);
399     }
400     
401 }
402
Popular Tags