KickJava   Java API By Example, From Geeks To Geeks.

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


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

24
25 package com.sun.enterprise.webservice;
26
27 import java.util.Collection JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 import java.io.File JavaDoc;
32
33 import javax.servlet.*;
34 import javax.servlet.http.*;
35
36 import com.sun.enterprise.Switch;
37 import com.sun.enterprise.ComponentInvocation;
38 import com.sun.enterprise.InvocationManager;
39 import com.sun.enterprise.deployment.WebServicesDescriptor;
40 import com.sun.enterprise.deployment.WebService;
41 import com.sun.enterprise.deployment.WebServiceEndpoint;
42 import com.sun.enterprise.deployment.WebBundleDescriptor;
43 import com.sun.enterprise.deployment.WebComponentDescriptor;
44
45 import com.sun.enterprise.webservice.monitoring.WebServiceEngineImpl;
46 import com.sun.enterprise.webservice.monitoring.JAXRPCEndpointImpl;
47
48 import com.sun.enterprise.security.jauth.ServerAuthConfig;
49
50
51 // JAX-RPC SPI
52
import com.sun.xml.rpc.spi.JaxRpcObjectFactory;
53 import com.sun.xml.rpc.spi.runtime.Implementor;
54 import com.sun.xml.rpc.spi.runtime.ImplementorCache;
55 import com.sun.xml.rpc.spi.runtime.ImplementorCacheDelegate;
56 import com.sun.xml.rpc.spi.runtime.RuntimeEndpointInfo;
57 import com.sun.xml.rpc.spi.runtime.ServletDelegate;
58 import com.sun.xml.rpc.spi.runtime.ServletSecondDelegate;
59 import com.sun.xml.rpc.spi.runtime.SystemHandlerDelegate;
60
61
62 import java.util.logging.Logger JavaDoc;
63 import java.util.logging.Level JavaDoc;
64 import com.sun.logging.LogDomains;
65
66 /**
67  * This class is delegated to by the container-provided servlet-class
68  * that is written into the web.xml at deployment time. It overrides
69  * the JAXRPC servlet delegate to register endpoint information and
70  * intercept certain events.
71  */

72 public class ServletWebServiceDelegate extends ServletSecondDelegate {
73
74     private static Logger JavaDoc logger = LogDomains.getLogger(LogDomains.WEB_LOGGER);
75
76     private WebServiceEndpoint endpoint_;
77
78     private ServletConfig servletConfig_;
79
80     private ServletDelegate rpcDelegate_;
81
82     private JaxRpcObjectFactory rpcFactory_;
83     
84     private WebServiceEngineImpl wsEngine_;
85     private JAXRPCEndpointImpl endpointImpl_;
86
87     public ServletWebServiceDelegate (ServletDelegate firstDelegate) {
88         rpcDelegate_ = firstDelegate;
89         rpcFactory_ = JaxRpcObjectFactory.newInstance();
90         wsEngine_ = WebServiceEngineImpl.getInstance();
91     }
92
93
94     public void postInit(ServletConfig servletConfig) throws ServletException {
95
96         servletConfig_ = servletConfig;
97         String JavaDoc servletName = "unknown";
98
99         try {
100             InvocationManager invManager =
101                 Switch.getSwitch().getInvocationManager();
102             ComponentInvocation inv = invManager.getCurrentInvocation();
103             Object JavaDoc containerContext = inv.getContainerContext();
104
105             WebBundleDescriptor webBundle = (WebBundleDescriptor)
106                 Switch.getSwitch().getDescriptorFor(containerContext);
107             ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
108             servletName = servletConfig.getServletName();
109             WebComponentDescriptor webComponent =
110                 webBundle.getWebComponentByCanonicalName(servletName);
111
112             if( webComponent != null ) {
113                 WebServicesDescriptor webServices = webBundle.getWebServices();
114                 Collection JavaDoc endpoints =
115                     webServices.getEndpointsImplementedBy(webComponent);
116                 // Only 1 endpoint per servlet is supported, even though
117
// data structure implies otherwise.
118
endpoint_ = (WebServiceEndpoint) endpoints.iterator().next();
119                 registerEndpoint(classLoader);
120
121         // if a conventional authentication mechanism has NOT been configured
122
// for the endpoint create and install system handler for web services
123
// security
124
SystemHandlerDelegate securityHandlerDelegate = null;
125         if (!endpoint_.hasAuthMethod()) {
126             try {
127             ServerAuthConfig config = ServerAuthConfig.getConfig
128                 (com.sun.enterprise.security.jauth.AuthConfig.SOAP,
129                  endpoint_.getMessageSecurityBinding(),
130                  WSSCallbackHandler.getInstance());
131             if (config != null) {
132                 securityHandlerDelegate =
133                 new ServletSystemHandlerDelegate(config);
134                 rpcDelegate_.setSystemHandlerDelegate(securityHandlerDelegate);
135             }
136             } catch (Exception JavaDoc e) {
137             logger.log(Level.SEVERE,
138                    "Servlet Webservice security configuration Failure", e);
139             }
140         }
141                 // need to invoke the endpoint lifecylcle
142
endpointImpl_ = (JAXRPCEndpointImpl)wsEngine_.createHandler(securityHandlerDelegate, endpoint_);
143                 rpcDelegate_.setSystemHandlerDelegate(endpointImpl_);
144
145             } else {
146                 throw new ServletException(servletName + " not found");
147             }
148         } catch(Throwable JavaDoc t) {
149             logger.log(Level.WARNING, "Servlet web service endpoint '" +
150                        servletName + "' failure", t);
151             ServletException se = new ServletException();
152             se.initCause(t);
153             throw se;
154         }
155     }
156     
157     public void destroy() {
158         wsEngine_.removeHandler(endpoint_);
159     }
160
161     public void doGet(HttpServletRequest request,
162                       HttpServletResponse response) throws ServletException {
163         
164         // normal WSDL retrieval invocation
165
WsUtil wsUtil = new WsUtil();
166         try {
167             wsUtil.handleGet(request, response, endpoint_);
168         } catch(Exception JavaDoc e) {
169             logger.log(Level.WARNING, "Servlet web service endpoint '" +
170                endpoint_.getEndpointName() + "' HTTP GET error", e);
171         }
172     }
173     
174     public void doPost(HttpServletRequest request,
175                         HttpServletResponse response) throws ServletException {
176        
177         rpcDelegate_.doPost(request, response);
178         
179     }
180
181     public void warnMissingContextInformation() {
182         // context info not used within j2ee integration, so override
183
// this method to prevent warning message
184
}
185
186     public ImplementorCache createImplementorCache(ServletConfig sc) {
187         ImplementorCache ic = rpcFactory_.createImplementorCache(sc);
188         ImplementorCacheDelegate delegate =
189                             new ImplementorCacheDelegateImpl(sc);
190         ic.setDelegate(delegate);
191         return ic;
192     }
193
194     private void registerEndpoint(ClassLoader JavaDoc loader)
195         throws Exception JavaDoc {
196
197         //
198
// Convert J2EE deployment descriptor information into
199
// JAXRPC endpoint data structure
200
//
201

202         RuntimeEndpointInfo endpointInfo =
203         rpcFactory_.createRuntimeEndpointInfo();
204         
205         Class JavaDoc serviceEndpointInterfaceClass =
206             loader.loadClass(endpoint_.getServiceEndpointInterface());
207         Class JavaDoc implementationClass =
208             loader.loadClass(endpoint_.getServletImplClass());
209         String JavaDoc tieClassName = endpoint_.getTieClassName();
210         if(tieClassName != null) {
211             Class JavaDoc tieClass = loader.loadClass(tieClassName);
212             endpointInfo.setTieClass(tieClass);
213         }
214
215         endpointInfo.setRemoteInterface(serviceEndpointInterfaceClass);
216         endpointInfo.setImplementationClass(implementationClass);
217
218         endpointInfo.setName(endpoint_.getEndpointName());
219
220         WebService webService = endpoint_.getWebService();
221
222
223         // No need to set model file name or wsdl file, since we override
224
// the code that serves up the final WSDL.
225
//endpointInfo.setModelFileName()
226
//endpointInfo.setWSDLFileName()
227

228         endpointInfo.setDeployed(true);
229         endpointInfo.setPortName(endpoint_.getWsdlPort());
230
231         endpointInfo.setServiceName(endpoint_.getServiceName());
232
233         // For web components, this will be relative to the web app
234
// context root. Make sure there is a leading slash.
235
String JavaDoc uri = endpoint_.getEndpointAddressUri();
236         uri = uri.startsWith("/") ? uri : "/" + uri;
237         endpointInfo.setUrlPattern(uri);
238
239         rpcDelegate_.registerEndpointUrlPattern(endpointInfo);
240     }
241
242 }
243
Popular Tags