KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > tomcat > TomcatEJBWebServiceContext


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.tomcat;
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.ServletException JavaDoc;
28
29 import org.apache.catalina.authenticator.BasicAuthenticator;
30 import org.apache.catalina.authenticator.DigestAuthenticator;
31 import org.apache.catalina.authenticator.SSLAuthenticator;
32 import org.apache.catalina.connector.Request;
33 import org.apache.catalina.connector.Response;
34 import org.apache.catalina.core.StandardContext;
35 import org.apache.catalina.deploy.LoginConfig;
36 import org.apache.catalina.deploy.SecurityCollection;
37 import org.apache.catalina.deploy.SecurityConstraint;
38 import org.apache.catalina.valves.ValveBase;
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41 import org.apache.geronimo.tomcat.realm.TomcatEJBWSGeronimoRealm;
42 import org.apache.geronimo.webservices.WebServiceContainer;
43
44 public class TomcatEJBWebServiceContext extends StandardContext{
45
46     private static final Log log = LogFactory.getLog(TomcatEJBWebServiceContext.class);
47
48     private final String JavaDoc contextPath;
49     private final WebServiceContainer webServiceContainer;
50     private final boolean isSecureTransportGuarantee;
51     private final ClassLoader JavaDoc classLoader;
52
53     public TomcatEJBWebServiceContext(String JavaDoc contextPath, WebServiceContainer webServiceContainer, String JavaDoc securityRealmName, String JavaDoc realmName, String JavaDoc transportGuarantee, String JavaDoc authMethod, ClassLoader JavaDoc classLoader) {
54
55         super();
56
57         this.contextPath = contextPath;
58         this.webServiceContainer = webServiceContainer;
59         this.setPath(contextPath);
60         this.setDocBase("");
61         this.setParentClassLoader(classLoader);
62         this.setDelegate(true);
63
64         log.debug("EJB Webservice Context = " + contextPath);
65         if (securityRealmName != null) {
66
67             TomcatEJBWSGeronimoRealm realm = new TomcatEJBWSGeronimoRealm();
68             realm.setAppName(securityRealmName);
69             realm.setUserClassNames("org.apache.geronimo.security.realm.providers.GeronimoUserPrincipal");
70             realm.setRoleClassNames("org.apache.geronimo.security.realm.providers.GeronimoGroupPrincipal");
71             setRealm(realm);
72             this.realm = realm;
73
74             if ("NONE".equals(transportGuarantee)) {
75                 isSecureTransportGuarantee = false;
76             } else if ("INTEGRAL".equals(transportGuarantee) ||
77                        "CONFIDENTIAL".equals(transportGuarantee)) {
78                 isSecureTransportGuarantee = true;
79             } else {
80                 throw new IllegalArgumentException JavaDoc("Invalid transport-guarantee: " + transportGuarantee);
81             }
82
83             if ("BASIC".equals(authMethod) ||
84                 "DIGEST".equals(authMethod) ||
85                 "CLIENT-CERT".equals(authMethod)) {
86
87                 //Setup a login configuration
88
LoginConfig loginConfig = new LoginConfig();
89                 loginConfig.setAuthMethod(authMethod);
90                 loginConfig.setRealmName(realmName);
91                 this.setLoginConfig(loginConfig);
92
93                 //Setup a default Security Constraint
94
SecurityCollection collection = new SecurityCollection();
95                 collection.addMethod("GET");
96                 collection.addMethod("POST");
97                 collection.addPattern("/*");
98                 collection.setName("default");
99                 SecurityConstraint sc = new SecurityConstraint();
100                 sc.addAuthRole("*");
101                 sc.addCollection(collection);
102                 sc.setAuthConstraint(true);
103                 sc.setUserConstraint(transportGuarantee);
104                 this.addConstraint(sc);
105                 this.addSecurityRole("default");
106
107                 //Set the proper authenticator
108
if ("BASIC".equals(authMethod) ){
109                     this.addValve(new BasicAuthenticator());
110                 } else if ("DIGEST".equals(authMethod) ){
111                     this.addValve(new DigestAuthenticator());
112                 } else if ("CLIENT-CERT".equals(authMethod) ){
113                     this.addValve(new SSLAuthenticator());
114                 }
115
116             } else {
117                 throw new IllegalArgumentException JavaDoc("Invalid authMethod: " + authMethod);
118             }
119         } else {
120             isSecureTransportGuarantee = false;
121         }
122         this.classLoader = classLoader;
123         this.addValve(new EJBWebServiceValve());
124
125     }
126
127     public class EJBWebServiceValve extends ValveBase{
128
129         public void invoke(Request req, Response res) throws IOException JavaDoc, ServletException JavaDoc {
130             req.setContentType("text/xml");
131             RequestAdapter request = new RequestAdapter(req);
132             ResponseAdapter response = new ResponseAdapter(res);
133             req.finishRequest();
134             if (req.getParameter("wsdl") != null) {
135                 try {
136                     webServiceContainer.getWsdl(request, response);
137                     //WHO IS RESPONSIBLE FOR CLOSING OUT?
138
} catch (IOException JavaDoc e) {
139                     throw e;
140                 } catch (Exception JavaDoc e) {
141                     log.error(e);
142                     res.sendError(500,"Could not fetch wsdl!");
143                     return;
144                 }
145             } else {
146                 if (isSecureTransportGuarantee) {
147                     if (!req.isSecure()) {
148                         res.sendError(403);
149                         return;
150                     }
151                 }
152                 Thread JavaDoc currentThread = Thread.currentThread();
153                 ClassLoader JavaDoc oldClassLoader = currentThread.getContextClassLoader();
154                 currentThread.setContextClassLoader(classLoader);
155                 try {
156                     try {
157                         webServiceContainer.invoke(request, response);
158                         req.finishRequest();
159                     } catch (IOException JavaDoc e) {
160                         throw e;
161                     } catch (Exception JavaDoc e) {
162                         res.sendError(500, "Could not process message!");
163                     }
164                 } finally {
165                     currentThread.setContextClassLoader(oldClassLoader);
166                 }
167             }
168         }
169
170     }
171
172     public static class RequestAdapter implements WebServiceContainer.Request {
173         private final Request request;
174         private URI JavaDoc uri;
175
176         public RequestAdapter(Request request) {
177             this.request = request;
178         }
179
180         public String JavaDoc getHeader(String JavaDoc name) {
181             return request.getHeader(name);
182         }
183
184         public java.net.URI JavaDoc getURI() {
185             if (uri == null) {
186                 try {
187                     String JavaDoc uriString = request.getScheme() + "://" + request.getServerName() + ":" + request.getLocalPort() + request.getRequestURI();
188                     //return new java.net.URI(uri.getScheme(),uri.getHost(),uri.getPath(),uri.);
189
uri = new java.net.URI JavaDoc(uriString);
190                 } catch (URISyntaxException JavaDoc e) {
191                     throw new IllegalStateException JavaDoc(e.getMessage());
192                 }
193             }
194             return uri;
195         }
196
197         public int getContentLength() {
198             return request.getContentLength();
199         }
200
201         public String JavaDoc getContentType() {
202             return request.getContentType();
203         }
204
205         public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
206             return request.getInputStream();
207         }
208
209         public int getMethod() {
210             Integer JavaDoc method = (Integer JavaDoc) methods.get(request.getMethod());
211             return method == null ? UNSUPPORTED : method.intValue();
212         }
213
214         public String JavaDoc getParameter(String JavaDoc name) {
215             return request.getParameter(name);
216         }
217
218         public Map JavaDoc getParameters() {
219             return request.getParameterMap();
220         }
221
222         public Object JavaDoc getAttribute(String JavaDoc name) {
223             return request.getAttribute(name);
224         }
225
226         public void setAttribute(String JavaDoc name, Object JavaDoc value) {
227             request.setAttribute(name, value);
228         }
229
230
231         private static final Map JavaDoc methods = new HashMap JavaDoc();
232
233         static {
234             methods.put("OPTIONS", new Integer JavaDoc(OPTIONS));
235             methods.put("GET", new Integer JavaDoc(GET));
236             methods.put("HEAD", new Integer JavaDoc(HEAD));
237             methods.put("POST", new Integer JavaDoc(POST));
238             methods.put("PUT", new Integer JavaDoc(PUT));
239             methods.put("DELETE", new Integer JavaDoc(DELETE));
240             methods.put("TRACE", new Integer JavaDoc(TRACE));
241             methods.put("CONNECT", new Integer JavaDoc(CONNECT));
242         }
243
244     }
245
246     public static class ResponseAdapter implements WebServiceContainer.Response {
247         private final Response response;
248
249         public ResponseAdapter(Response response) {
250             this.response = response;
251         }
252
253         public void setHeader(String JavaDoc name, String JavaDoc value) {
254             response.setHeader(name, value);
255         }
256
257         public String JavaDoc getHeader(String JavaDoc name) {
258             return response.getHeader(name);
259         }
260
261         public OutputStream JavaDoc getOutputStream() {
262             return response.getStream();
263         }
264
265         public void setStatusCode(int code) {
266             response.setStatus(code);
267         }
268
269         public int getStatusCode() {
270             return response.getStatus();
271         }
272
273         public void setContentType(String JavaDoc type) {
274             response.setContentType(type);
275         }
276
277         public String JavaDoc getContentType() {
278             return response.getContentType();
279         }
280
281         public void setStatusMessage(String JavaDoc responseString) {
282             response.setStatus(response.getStatus(), responseString);
283         }
284     }
285
286 }
287
Popular Tags