KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  *
3  * Copyright 2003-2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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.jetty;
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 org.apache.geronimo.webservices.WebServiceContainer;
28 import org.mortbay.http.HttpContext;
29 import org.mortbay.http.HttpException;
30 import org.mortbay.http.HttpHandler;
31 import org.mortbay.http.HttpRequest;
32 import org.mortbay.http.HttpResponse;
33
34 /**
35  * Delegates requests to a WebServiceContainer which is presumably for an EJB WebService.
36  *
37  * WebServiceContainer delegates to an EJBContainer that will ultimately provide the JNDI,
38  * TX, and Security services for this web service.
39  *
40  * Nothing stopping us from using this for POJOs or other types of webservices if shared
41  * Context (JNDI, tx, security) wasn't required to be supplied by the web context.
42  *
43  * From a 10,000 foot view the Jetty architecture has:
44  * Container -> Context -> Holder -> Servlet
45  *
46  * A Container has multiple Contexts, typically webapps
47  * A Context provides the JNDI, TX, and Security for the webapp and has many Holders
48  * A Holder simply wraps each Servlet
49  *
50  * The POJO Web Service architecture on Jetty looks like this:
51  * Container -> WebApp Context -> JettyPOJOWebServiceHolder -> POJOWebServiceServlet
52  *
53  * The EJB Web Service architecure, on the other hand, creates one Context for each EJB:
54  * Container -> JettyEJBWebServiceContext
55  *
56  * @version $Rev: $ $Date: $
57  */

58 public class JettyEJBWebServiceContext extends HttpContext implements HttpHandler {
59
60     private final String JavaDoc contextPath;
61     private final WebServiceContainer webServiceContainer;
62
63     private HttpContext httpContext;
64
65     public JettyEJBWebServiceContext(String JavaDoc contextPath, WebServiceContainer webServiceContainer) {
66         this.contextPath = contextPath;
67         this.webServiceContainer = webServiceContainer;
68     }
69
70     public String JavaDoc getName() {
71         //need a better name
72
return contextPath;
73     }
74
75     public HttpContext getHttpContext() {
76         return httpContext;
77     }
78
79     public void initialize(HttpContext httpContext) {
80         this.httpContext = httpContext;
81     }
82
83     public void handle(HttpRequest req, HttpResponse res) throws HttpException, IOException JavaDoc {
84         req.setContentType("text/xml");
85         RequestAdapter request = new RequestAdapter(req);
86         ResponseAdapter response = new ResponseAdapter(res);
87             
88         if (req.getParameter("wsdl") != null) {
89             try {
90                 webServiceContainer.getWsdl(request,response);
91             } catch (IOException JavaDoc e) {
92                 throw e;
93             } catch (Exception JavaDoc e) {
94                 throw (HttpException) new HttpException(500, "Could not fetch wsdl!").initCause(e);
95             }
96         } else {
97             try {
98                 webServiceContainer.invoke(request,response);
99                 req.setHandled(true);
100             } catch (IOException JavaDoc e) {
101                 throw e;
102             } catch (Exception JavaDoc e) {
103                 throw (HttpException) new HttpException(500, "Could not process message!").initCause(e);
104             }
105         }
106             
107     }
108
109     public String JavaDoc getContextPath() {
110         return contextPath;
111     }
112
113     public static class RequestAdapter implements WebServiceContainer.Request {
114         private final HttpRequest request;
115         private URI JavaDoc uri;
116
117         public RequestAdapter(HttpRequest request) {
118             this.request = request;
119         }
120
121         public String JavaDoc getHeader(String JavaDoc name) {
122             return request.getField(name);
123         }
124
125         public java.net.URI JavaDoc getURI() {
126             if( uri==null ) {
127                 try {
128                     String JavaDoc uriString = request.getScheme()+"://"+request.getHost()+":"+request.getPort()+request.getURI();
129                     //return new java.net.URI(uri.getScheme(),uri.getHost(),uri.getPath(),uri.);
130
uri = new java.net.URI JavaDoc(uriString);
131                 } catch (URISyntaxException JavaDoc e) {
132                     throw new IllegalStateException JavaDoc(e.getMessage());
133                 }
134             }
135             return uri;
136         }
137
138         public int getContentLength() {
139             return request.getContentLength();
140         }
141
142         public String JavaDoc getContentType() {
143             return request.getContentType();
144         }
145
146         public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
147             return request.getInputStream();
148         }
149
150         public int getMethod() {
151             Integer JavaDoc method = (Integer JavaDoc) methods.get(request.getMethod());
152             return method == null ? UNSUPPORTED: method.intValue();
153         }
154
155         public String JavaDoc getParameter(String JavaDoc name) {
156             return request.getParameter(name);
157         }
158
159         public Map JavaDoc getParameters() {
160             return request.getParameters();
161         }
162
163         public Object JavaDoc getAttribute(String JavaDoc name) {
164             return request.getAttribute(name);
165         }
166
167         public void setAttribute(String JavaDoc name, Object JavaDoc value){
168             request.setAttribute(name, value);
169         }
170
171
172         private static final Map JavaDoc methods = new HashMap JavaDoc();
173
174         static {
175             methods.put("OPTIONS", new Integer JavaDoc(OPTIONS));
176             methods.put("GET", new Integer JavaDoc(GET));
177             methods.put("HEAD", new Integer JavaDoc(HEAD));
178             methods.put("POST", new Integer JavaDoc(POST));
179             methods.put("PUT", new Integer JavaDoc(PUT));
180             methods.put("DELETE", new Integer JavaDoc(DELETE));
181             methods.put("TRACE", new Integer JavaDoc(TRACE));
182             methods.put("CONNECT", new Integer JavaDoc(CONNECT));
183         }
184
185     }
186
187     public static class ResponseAdapter implements WebServiceContainer.Response {
188         private final HttpResponse response;
189
190         public ResponseAdapter(HttpResponse response) {
191             this.response = response;
192         }
193
194         public void setHeader(String JavaDoc name, String JavaDoc value) {
195             response.setField(name, value);
196         }
197
198         public String JavaDoc getHeader(String JavaDoc name) {
199             return response.getField(name);
200         }
201
202         public OutputStream JavaDoc getOutputStream() {
203             return response.getOutputStream();
204         }
205
206         public void setStatusCode(int code) {
207             response.setStatus(code);
208         }
209
210         public int getStatusCode() {
211             return response.getStatus();
212         }
213
214         public void setContentType(String JavaDoc type) {
215             response.setContentType(type);
216         }
217
218         public String JavaDoc getContentType() {
219             return response.getContentType();
220         }
221
222         public void setStatusMessage(String JavaDoc responseString) {
223             response.setStatus(response.getStatus(), responseString);
224         }
225     }
226
227 }
228
Popular Tags