KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > jetty6 > JettyEJBWebServiceContext


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.jetty6;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.net.URI JavaDoc;
23 import java.net.URISyntaxException JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29 import javax.servlet.ServletException JavaDoc;
30
31 import org.apache.geronimo.security.ContextManager;
32 import org.apache.geronimo.webservices.WebServiceContainer;
33 import org.mortbay.jetty.HttpException;
34 import org.mortbay.jetty.Request;
35 import org.mortbay.jetty.Response;
36 import org.mortbay.jetty.handler.ContextHandler;
37 import org.mortbay.jetty.security.Authenticator;
38 import org.mortbay.jetty.security.BasicAuthenticator;
39 import org.mortbay.jetty.security.ClientCertAuthenticator;
40 import org.mortbay.jetty.security.DigestAuthenticator;
41
42 /**
43  * Delegates requests to a WebServiceContainer which is presumably for an EJB WebService.
44  * <p/>
45  * WebServiceContainer delegates to an EJBContainer that will ultimately provide the JNDI,
46  * TX, and Security services for this web service.
47  * <p/>
48  * Nothing stopping us from using this for POJOs or other types of webservices if shared
49  * Context (JNDI, tx, security) wasn't required to be supplied by the web context.
50  * <p/>
51  * From a 10,000 foot view the Jetty architecture has:
52  * Container -> Context -> Holder -> Servlet
53  * <p/>
54  * A Container has multiple Contexts, typically webapps
55  * A Context provides the JNDI, TX, and Security for the webapp and has many Holders
56  * A Holder simply wraps each Servlet
57  * <p/>
58  * The POJO Web Service architecture on Jetty looks like this:
59  * Container -> WebApp Context -> JettyPOJOWebServiceHolder -> POJOWebServiceServlet
60  * <p/>
61  * The EJB Web Service architecure, on the other hand, creates one Context for each EJB:
62  * Container -> JettyEJBWebServiceContext
63  *
64  * @version $Rev: 482336 $ $Date: 2006-12-04 15:12:19 -0500 (Mon, 04 Dec 2006) $
65  */

66 public class JettyEJBWebServiceContext extends ContextHandler {
67
68     private final String JavaDoc contextPath;
69     private final WebServiceContainer webServiceContainer;
70     private final Authenticator authenticator;
71     private final JAASJettyRealm realm;
72     private final boolean isConfidentialTransportGuarantee;
73     private final boolean isIntegralTransportGuarantee;
74     private final ClassLoader JavaDoc classLoader;
75
76
77     public JettyEJBWebServiceContext(String JavaDoc contextPath, WebServiceContainer webServiceContainer, InternalJAASJettyRealm internalJAASJettyRealm, String JavaDoc realmName, String JavaDoc transportGuarantee, String JavaDoc authMethod, ClassLoader JavaDoc classLoader) {
78         this.contextPath = contextPath;
79         this.webServiceContainer = webServiceContainer;
80         if (internalJAASJettyRealm != null) {
81             realm = new JAASJettyRealm(realmName, internalJAASJettyRealm);
82             //TODO
83
//not used???
84
//setUserRealm(realm);
85
// this.realm = realm;
86
if ("NONE".equals(transportGuarantee)) {
87                 isConfidentialTransportGuarantee = false;
88                 isIntegralTransportGuarantee = false;
89             } else if ("INTEGRAL".equals(transportGuarantee)) {
90                 isConfidentialTransportGuarantee = false;
91                 isIntegralTransportGuarantee = true;
92             } else if ("CONFIDENTIAL".equals(transportGuarantee)) {
93                 isConfidentialTransportGuarantee = true;
94                 isIntegralTransportGuarantee = false;
95             } else {
96                 throw new IllegalArgumentException JavaDoc("Invalid transport-guarantee: " + transportGuarantee);
97             }
98             if ("BASIC".equals(authMethod)) {
99                 authenticator = new BasicAuthenticator();
100             } else if ("DIGEST".equals(authMethod)) {
101                 authenticator = new DigestAuthenticator();
102             } else if ("CLIENT-CERT".equals(authMethod)) {
103                 authenticator = new ClientCertAuthenticator();
104             } else if ("NONE".equals(authMethod)) {
105                 authenticator = null;
106             } else {
107                 throw new IllegalArgumentException JavaDoc("Invalid authMethod: " + authMethod);
108             }
109         } else {
110             realm = null;
111             authenticator = null;
112             isConfidentialTransportGuarantee = false;
113             isIntegralTransportGuarantee = false;
114         }
115         this.classLoader = classLoader;
116     }
117
118     public String JavaDoc getName() {
119         //need a better name
120
return contextPath;
121     }
122
123     public void handle(String JavaDoc target, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res, int dispatch)
124             throws IOException JavaDoc, ServletException JavaDoc
125     {
126         //TODO
127
//shouldn't set this on the request !
128
//req.setContentType("text/xml");
129

130         //TODO
131
//do we need to check that this request should be handled by this handler?
132
if (! target.startsWith(contextPath)) {
133             return;
134         }
135         Request JavaDoc jettyRequest = (Request JavaDoc) req;
136         Response JavaDoc jettyResponse = (Response JavaDoc) res;
137         RequestAdapter request = new RequestAdapter(jettyRequest);
138         ResponseAdapter response = new ResponseAdapter(jettyResponse);
139
140         if (req.getParameter("wsdl") != null) {
141             try {
142                 webServiceContainer.getWsdl(request, response);
143                 //WHO IS RESPONSIBLE FOR CLOSING OUT?
144
} catch (IOException JavaDoc e) {
145                 throw e;
146             } catch (Exception JavaDoc e) {
147                 throw (HttpException) new HttpException(500, "Could not fetch wsdl!").initCause(e);
148             }
149         } else {
150             if (isConfidentialTransportGuarantee) {
151                 if (!req.isSecure()) {
152                     throw new HttpException(403, null);
153                 }
154             } else if (isIntegralTransportGuarantee) {
155                 if (!jettyRequest.getConnection().isIntegral(jettyRequest)) {
156                     throw new HttpException(403, null);
157                 }
158             }
159             Thread JavaDoc currentThread = Thread.currentThread();
160             ClassLoader JavaDoc oldClassLoader = currentThread.getContextClassLoader();
161             currentThread.setContextClassLoader(classLoader);
162             //hard to imagine this could be anything but null, but....
163
// Subject oldSubject = ContextManager.getCurrentCaller();
164
try {
165                 if (authenticator != null) {
166                     String JavaDoc pathInContext = org.mortbay.util.URIUtil.canonicalPath(req.getContextPath());
167                     if (authenticator.authenticate(realm, pathInContext, jettyRequest, jettyResponse) == null) {
168                         throw new HttpException(403, null);
169                     }
170                 } else {
171                     //EJB will figure out correct defaultSubject shortly
172
//TODO consider replacing the GenericEJBContainer.DefaultSubjectInterceptor with this line
173
//setting the defaultSubject.
174
ContextManager.popCallers(null);
175                 }
176                 try {
177                     webServiceContainer.invoke(request, response);
178                     jettyRequest.setHandled(true);
179                 } catch (IOException JavaDoc e) {
180                     throw e;
181                 } catch (Exception JavaDoc e) {
182                     throw (HttpException) new HttpException(500, "Could not process message!").initCause(e);
183                 }
184             } finally {
185 // ContextManager.setCurrentCaller(oldSubject);
186
currentThread.setContextClassLoader(oldClassLoader);
187             }
188         }
189
190     }
191
192     public String JavaDoc getContextPath() {
193         return contextPath;
194     }
195
196     public String JavaDoc getSecurityRealmName() {
197         if (realm == null) {
198             return null;
199         } else {
200             return realm.getSecurityRealmName();
201         }
202     }
203
204     public static class RequestAdapter implements WebServiceContainer.Request {
205         private final Request JavaDoc request;
206         private URI JavaDoc uri;
207
208         public RequestAdapter(Request JavaDoc request) {
209             this.request = request;
210         }
211
212         public String JavaDoc getHeader(String JavaDoc name) {
213             return request.getHeader(name);
214         }
215
216         public java.net.URI JavaDoc getURI() {
217             if (uri == null) {
218                 try {
219                     String JavaDoc uriString = request.getScheme() + "://" + request.getRemoteHost() + ":" + request.getRemotePort() + request.getRequestURI();
220                     //return new java.net.URI(uri.getScheme(),uri.getHost(),uri.getPath(),uri.);
221
uri = new java.net.URI JavaDoc(uriString);
222                 } catch (URISyntaxException JavaDoc e) {
223                     throw new IllegalStateException JavaDoc(e.getMessage());
224                 }
225             }
226             return uri;
227         }
228
229         public int getContentLength() {
230             return request.getContentLength();
231         }
232
233         public String JavaDoc getContentType() {
234             return request.getContentType();
235         }
236
237         public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
238             return request.getInputStream();
239         }
240
241         public int getMethod() {
242             Integer JavaDoc method = (Integer JavaDoc) methods.get(request.getMethod());
243             return method == null ? UNSUPPORTED : method.intValue();
244         }
245
246         public String JavaDoc getParameter(String JavaDoc name) {
247             return request.getParameter(name);
248         }
249
250         public Map JavaDoc getParameters() {
251             return request.getParameterMap();
252         }
253
254         public Object JavaDoc getAttribute(String JavaDoc name) {
255             return request.getAttribute(name);
256         }
257
258         public void setAttribute(String JavaDoc name, Object JavaDoc value) {
259             request.setAttribute(name, value);
260         }
261
262
263         private static final Map JavaDoc methods = new HashMap JavaDoc();
264
265         static {
266             methods.put("OPTIONS", new Integer JavaDoc(OPTIONS));
267             methods.put("GET", new Integer JavaDoc(GET));
268             methods.put("HEAD", new Integer JavaDoc(HEAD));
269             methods.put("POST", new Integer JavaDoc(POST));
270             methods.put("PUT", new Integer JavaDoc(PUT));
271             methods.put("DELETE", new Integer JavaDoc(DELETE));
272             methods.put("TRACE", new Integer JavaDoc(TRACE));
273             methods.put("CONNECT", new Integer JavaDoc(CONNECT));
274         }
275
276     }
277
278     public static class ResponseAdapter implements WebServiceContainer.Response {
279         private final Response JavaDoc response;
280
281         public ResponseAdapter(Response JavaDoc response) {
282             this.response = response;
283         }
284
285         public void setHeader(String JavaDoc name, String JavaDoc value) {
286             response.setHeader(name, value);
287         }
288
289         public String JavaDoc getHeader(String JavaDoc name) {
290             return response.getHeader(name);
291         }
292
293         public OutputStream JavaDoc getOutputStream() {
294             try {
295                 return response.getOutputStream();
296             } catch (IOException JavaDoc e) {
297                 throw new IllegalStateException JavaDoc(e.getMessage());
298             }
299         }
300
301         public void setStatusCode(int code) {
302             response.setStatus(code);
303         }
304
305         public int getStatusCode() {
306             return response.getStatus();
307          }
308
309         public void setContentType(String JavaDoc type) {
310             response.setContentType(type);
311         }
312
313         public String JavaDoc getContentType() {
314             return response.getContentType();
315         }
316
317         public void setStatusMessage(String JavaDoc responseString) {
318             response.setStatus(response.getStatus(), responseString);
319         }
320     }
321
322 }
323
Popular Tags