1 /**
2 *
3 * Copyright 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.webservices;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.OutputStream;
22 import java.net.URISyntaxException;
23 import java.util.HashMap;
24 import java.util.Map;
25 import javax.servlet.Servlet;
26 import javax.servlet.ServletConfig;
27 import javax.servlet.ServletContext;
28 import javax.servlet.ServletException;
29 import javax.servlet.ServletRequest;
30 import javax.servlet.ServletResponse;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33
34 /**
35 * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
36 */
37 public class WebServiceContainerInvoker implements Servlet {
38
39 public static final String WEBSERVICE_CONTAINER = WebServiceContainerInvoker.class.getName()+"@WebServiceContainer";
40
41 private final Object pojo;
42 private WebServiceContainer service;
43
44 public WebServiceContainerInvoker(Object pojo) {
45 this.pojo = pojo;
46 }
47
48 public void init(ServletConfig config) throws ServletException {
49 ServletContext context = config.getServletContext();
50 String webServiceContainerID = config.getInitParameter(WEBSERVICE_CONTAINER);
51 service = (WebServiceContainer) context.getAttribute(webServiceContainerID);
52 }
53
54 public ServletConfig getServletConfig() {
55 return null;
56 }
57
58 public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
59 res.setContentType("text/xml");
60 RequestAdapter request = new RequestAdapter((HttpServletRequest) req);
61 ResponseAdapter response = new ResponseAdapter((HttpServletResponse) res);
62
63
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 e) {
70 throw e;
71 } catch (ServletException e) {
72 throw e;
73 } catch (Exception e) {
74 throw new ServletException("Could not fetch wsdl!", e);
75 }
76 } else {
77 try {
78 service.invoke(request, response);
79 } catch (IOException e) {
80 throw e;
81 } catch (ServletException e) {
82 throw e;
83 } catch (Exception e) {
84 throw new ServletException("Could not process message!", e);
85 }
86 }
87 }
88
89 public String getServletInfo() {
90 return null;
91 }
92
93 public void destroy() {
94 }
95
96 private static class RequestAdapter implements WebServiceContainer.Request {
97 private final HttpServletRequest request;
98
99 public RequestAdapter(HttpServletRequest request) {
100 this.request = request;
101 }
102
103 public String getHeader(String name) {
104 return request.getHeader(name);
105 }
106
107 public java.net.URI getURI() {
108 try {
109 return new java.net.URI(request.getScheme(), null, request.getServerName(), request.getServerPort(), request.getRequestURI(), request.getQueryString(), null);
110 } catch (URISyntaxException e) {
111 throw new IllegalStateException(e.getMessage());
112 }
113 }
114
115 public int getContentLength() {
116 return request.getContentLength();
117 }
118
119 public String getContentType() {
120 return request.getContentType();
121 }
122
123 public InputStream getInputStream() throws IOException {
124 return request.getInputStream();
125 }
126
127 public int getMethod() {
128 Integer method = (Integer) methods.get(request.getMethod());
129 return method == null ? UNSUPPORTED : method.intValue();
130 }
131
132 public String getParameter(String name) {
133 return request.getParameter(name);
134 }
135
136 public Map getParameters() {
137 return request.getParameterMap();
138 }
139
140
141 private static final Map methods = new HashMap();
142
143 static {
144 methods.put("OPTIONS", new Integer(OPTIONS));
145 methods.put("GET", new Integer(GET));
146 methods.put("HEAD", new Integer(HEAD));
147 methods.put("POST", new Integer(POST));
148 methods.put("PUT", new Integer(PUT));
149 methods.put("DELETE", new Integer(DELETE));
150 methods.put("TRACE", new Integer(TRACE));
151 methods.put("CONNECT", new Integer(CONNECT));
152 }
153
154 public Object getAttribute(String s) {
155 return request.getAttribute(s);
156 }
157
158 public void setAttribute(String s, Object o) {
159 request.setAttribute(s, o);
160 }
161
162 }
163
164 private static class ResponseAdapter implements WebServiceContainer.Response {
165 private final HttpServletResponse response;
166
167 public ResponseAdapter(HttpServletResponse response) {
168 this.response = response;
169 }
170
171 public void setHeader(String name, String value) {
172 response.setHeader(name, value);
173 }
174
175 public String getHeader(String name) {
176 throw new UnsupportedOperationException("Not possible to implement");
177 }
178
179 public OutputStream getOutputStream() {
180 try {
181 return response.getOutputStream();
182 } catch (IOException e) {
183 throw (IllegalStateException) new IllegalStateException().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("Not possible to implement");
193 }
194
195 public void setContentType(String type) {
196 response.setContentType(type);
197 }
198
199 public String getContentType() {
200 return response.getContentType();
201 }
202
203 public void setStatusMessage(String responseString) {
204 response.setStatus(getStatusCode(), responseString);
205 }
206 }
207 }