KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > webservices > WebServiceContainerInvoker


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.webservices;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.net.URISyntaxException JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25 import javax.servlet.Servlet JavaDoc;
26 import javax.servlet.ServletConfig JavaDoc;
27 import javax.servlet.ServletContext JavaDoc;
28 import javax.servlet.ServletException JavaDoc;
29 import javax.servlet.ServletRequest JavaDoc;
30 import javax.servlet.ServletResponse JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33
34 /**
35  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
36  */

37 public class WebServiceContainerInvoker implements Servlet JavaDoc {
38
39     public static final String JavaDoc WEBSERVICE_CONTAINER = WebServiceContainerInvoker.class.getName()+"@WebServiceContainer";
40
41     private final Object JavaDoc pojo;
42     private WebServiceContainer service;
43
44     public WebServiceContainerInvoker(Object JavaDoc pojo) {
45         this.pojo = pojo;
46     }
47
48     public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc {
49         ServletContext JavaDoc context = config.getServletContext();
50         String JavaDoc webServiceContainerID = config.getInitParameter(WEBSERVICE_CONTAINER);
51         service = (WebServiceContainer) context.getAttribute(webServiceContainerID);
52     }
53
54     public ServletConfig JavaDoc getServletConfig() {
55         return null;
56     }
57
58     public void service(ServletRequest JavaDoc req, ServletResponse JavaDoc res) throws ServletException JavaDoc, IOException JavaDoc {
59         res.setContentType("text/xml");
60         RequestAdapter request = new RequestAdapter((HttpServletRequest JavaDoc) req);
61         ResponseAdapter response = new ResponseAdapter((HttpServletResponse JavaDoc) res);
62
63         // This is the guy the WebServiceContainer should invoke
64
req.setAttribute(WebServiceContainer.POJO_INSTANCE, pojo);
65
66         if (req.getParameter("wsdl") != null || req.getParameter("WSDL") != null) {
67             try {
68                 service.getWsdl(request, response);
69             } catch (IOException JavaDoc e) {
70                 throw e;
71             } catch (ServletException JavaDoc e) {
72                 throw e;
73             } catch (Exception JavaDoc e) {
74                 throw new ServletException JavaDoc("Could not fetch wsdl!", e);
75             }
76         } else {
77             try {
78                 service.invoke(request, response);
79             } catch (IOException JavaDoc e) {
80                 throw e;
81             } catch (ServletException JavaDoc e) {
82                 throw e;
83             } catch (Exception JavaDoc e) {
84                 throw new ServletException JavaDoc("Could not process message!", e);
85             }
86         }
87     }
88
89     public String JavaDoc getServletInfo() {
90         return null;
91     }
92
93     public void destroy() {
94     }
95
96     private static class RequestAdapter implements WebServiceContainer.Request {
97         private final HttpServletRequest JavaDoc request;
98
99         public RequestAdapter(HttpServletRequest JavaDoc request) {
100             this.request = request;
101         }
102
103         public String JavaDoc getHeader(String JavaDoc name) {
104             return request.getHeader(name);
105         }
106
107         public java.net.URI JavaDoc getURI() {
108             try {
109                 return new java.net.URI JavaDoc(request.getScheme(), null, request.getServerName(), request.getServerPort(), request.getRequestURI(), request.getQueryString(), null);
110             } catch (URISyntaxException JavaDoc e) {
111                 throw new IllegalStateException JavaDoc(e.getMessage());
112             }
113         }
114
115         public int getContentLength() {
116             return request.getContentLength();
117         }
118
119         public String JavaDoc getContentType() {
120             return request.getContentType();
121         }
122
123         public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
124             return request.getInputStream();
125         }
126
127         public int getMethod() {
128             Integer JavaDoc method = (Integer JavaDoc) methods.get(request.getMethod());
129             return method == null ? UNSUPPORTED : method.intValue();
130         }
131
132         public String JavaDoc getParameter(String JavaDoc name) {
133             return request.getParameter(name);
134         }
135
136         public Map JavaDoc getParameters() {
137             return request.getParameterMap();
138         }
139
140
141         private static final Map JavaDoc methods = new HashMap JavaDoc();
142
143         static {
144             methods.put("OPTIONS", new Integer JavaDoc(OPTIONS));
145             methods.put("GET", new Integer JavaDoc(GET));
146             methods.put("HEAD", new Integer JavaDoc(HEAD));
147             methods.put("POST", new Integer JavaDoc(POST));
148             methods.put("PUT", new Integer JavaDoc(PUT));
149             methods.put("DELETE", new Integer JavaDoc(DELETE));
150             methods.put("TRACE", new Integer JavaDoc(TRACE));
151             methods.put("CONNECT", new Integer JavaDoc(CONNECT));
152         }
153
154         public Object JavaDoc getAttribute(String JavaDoc s) {
155             return request.getAttribute(s);
156         }
157
158         public void setAttribute(String JavaDoc s, Object JavaDoc o) {
159             request.setAttribute(s, o);
160         }
161
162     }
163
164     private static class ResponseAdapter implements WebServiceContainer.Response {
165         private final HttpServletResponse JavaDoc response;
166
167         public ResponseAdapter(HttpServletResponse JavaDoc response) {
168             this.response = response;
169         }
170
171         public void setHeader(String JavaDoc name, String JavaDoc value) {
172             response.setHeader(name, value);
173         }
174
175         public String JavaDoc getHeader(String JavaDoc name) {
176             throw new UnsupportedOperationException JavaDoc("Not possible to implement");
177         }
178
179         public OutputStream JavaDoc getOutputStream() {
180             try {
181                 return response.getOutputStream();
182             } catch (IOException JavaDoc e) {
183                 throw (IllegalStateException JavaDoc) new IllegalStateException JavaDoc().initCause(e);
184             }
185         }
186
187         public void setStatusCode(int code) {
188             response.setStatus(code);
189         }
190
191         public int getStatusCode() {
192             throw new UnsupportedOperationException JavaDoc("Not possible to implement");
193         }
194
195         public void setContentType(String JavaDoc type) {
196             response.setContentType(type);
197         }
198
199         public String JavaDoc getContentType() {
200             return response.getContentType();
201         }
202
203         public void setStatusMessage(String JavaDoc responseString) {
204             response.setStatus(getStatusCode(), responseString);
205         }
206     }
207 }
208
Popular Tags