001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.webservices;
017:
018: import java.io.IOException;
019: import java.io.InputStream;
020: import java.io.OutputStream;
021: import java.net.URISyntaxException;
022: import java.util.HashMap;
023: import java.util.Map;
024: import javax.servlet.Servlet;
025: import javax.servlet.ServletConfig;
026: import javax.servlet.ServletContext;
027: import javax.servlet.ServletException;
028: import javax.servlet.ServletRequest;
029: import javax.servlet.ServletResponse;
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpServletResponse;
032:
033: /**
034: * @version $Rev: 550523 $ $Date: 2007-06-25 08:02:09 -0700 (Mon, 25 Jun 2007) $
035: */
036: public class WebServiceContainerInvoker implements Servlet {
037:
038: public static final String WEBSERVICE_CONTAINER = WebServiceContainerInvoker.class
039: .getName()
040: + "@WebServiceContainer";
041:
042: private final Object pojo;
043: private WebServiceContainer service;
044: private ServletConfig config;
045:
046: public WebServiceContainerInvoker(Object pojo) {
047: this .pojo = pojo;
048: }
049:
050: public void init(ServletConfig config) throws ServletException {
051: this .config = config;
052: ServletContext context = config.getServletContext();
053: String webServiceContainerID = config
054: .getInitParameter(WEBSERVICE_CONTAINER);
055: service = (WebServiceContainer) context
056: .getAttribute(webServiceContainerID);
057: }
058:
059: public ServletConfig getServletConfig() {
060: return config;
061: }
062:
063: public void service(ServletRequest req, ServletResponse res)
064: throws ServletException, IOException {
065: res.setContentType("text/xml");
066: RequestAdapter request = new RequestAdapter(
067: (HttpServletRequest) req);
068: ResponseAdapter response = new ResponseAdapter(
069: (HttpServletResponse) res);
070:
071: // This is the guy the WebServiceContainer should invoke
072: req.setAttribute(WebServiceContainer.POJO_INSTANCE, pojo);
073:
074: req.setAttribute(WebServiceContainer.SERVLET_REQUEST,
075: (HttpServletRequest) req);
076: req.setAttribute(WebServiceContainer.SERVLET_RESPONSE,
077: (HttpServletResponse) res);
078: req.setAttribute(WebServiceContainer.SERVLET_CONTEXT, config
079: .getServletContext());
080:
081: if (req.getParameter("wsdl") != null
082: || req.getParameter("WSDL") != null) {
083: try {
084: service.getWsdl(request, response);
085: } catch (IOException e) {
086: throw e;
087: } catch (ServletException e) {
088: throw e;
089: } catch (Exception e) {
090: throw new ServletException("Could not fetch wsdl!", e);
091: }
092: } else {
093: try {
094: service.invoke(request, response);
095: } catch (IOException e) {
096: throw e;
097: } catch (ServletException e) {
098: throw e;
099: } catch (Exception e) {
100: throw new ServletException(
101: "Could not process message!", e);
102: }
103: }
104: }
105:
106: public String getServletInfo() {
107: return null;
108: }
109:
110: public void destroy() {
111: service.destroy();
112: }
113:
114: private static class RequestAdapter implements
115: WebServiceContainer.Request {
116: private final HttpServletRequest request;
117:
118: public RequestAdapter(HttpServletRequest request) {
119: this .request = request;
120: }
121:
122: public String getHeader(String name) {
123: return request.getHeader(name);
124: }
125:
126: public java.net.URI getURI() {
127: try {
128: return new java.net.URI(request.getScheme(), null,
129: request.getServerName(), request
130: .getServerPort(), request
131: .getRequestURI(), request
132: .getQueryString(), null);
133: } catch (URISyntaxException e) {
134: throw new IllegalStateException(e.getMessage(), e);
135: }
136: }
137:
138: public int getContentLength() {
139: return request.getContentLength();
140: }
141:
142: public String getContentType() {
143: return request.getContentType();
144: }
145:
146: public String getContextPath() {
147: return request.getContextPath();
148: }
149:
150: public InputStream getInputStream() throws IOException {
151: return request.getInputStream();
152: }
153:
154: public int getMethod() {
155: Integer method = (Integer) methods.get(request.getMethod());
156: return method == null ? UNSUPPORTED : method.intValue();
157: }
158:
159: public String getParameter(String name) {
160: return request.getParameter(name);
161: }
162:
163: public Map getParameters() {
164: return request.getParameterMap();
165: }
166:
167: private static final Map methods = new HashMap();
168:
169: static {
170: methods.put("OPTIONS", new Integer(OPTIONS));
171: methods.put("GET", new Integer(GET));
172: methods.put("HEAD", new Integer(HEAD));
173: methods.put("POST", new Integer(POST));
174: methods.put("PUT", new Integer(PUT));
175: methods.put("DELETE", new Integer(DELETE));
176: methods.put("TRACE", new Integer(TRACE));
177: methods.put("CONNECT", new Integer(CONNECT));
178: }
179:
180: public Object getAttribute(String s) {
181: return request.getAttribute(s);
182: }
183:
184: public void setAttribute(String s, Object o) {
185: request.setAttribute(s, o);
186: }
187:
188: public String getRemoteAddr() {
189: return request.getRemoteAddr();
190: }
191:
192: }
193:
194: private static class ResponseAdapter implements
195: WebServiceContainer.Response {
196: private final HttpServletResponse response;
197:
198: public ResponseAdapter(HttpServletResponse response) {
199: this .response = response;
200: }
201:
202: public void setHeader(String name, String value) {
203: response.setHeader(name, value);
204: }
205:
206: public String getHeader(String name) {
207: throw new UnsupportedOperationException(
208: "Not possible to implement");
209: }
210:
211: public OutputStream getOutputStream() {
212: try {
213: return response.getOutputStream();
214: } catch (IOException e) {
215: throw (IllegalStateException) new IllegalStateException()
216: .initCause(e);
217: }
218: }
219:
220: public void setStatusCode(int code) {
221: response.setStatus(code);
222: }
223:
224: public int getStatusCode() {
225: throw new UnsupportedOperationException(
226: "Not possible to implement");
227: }
228:
229: public void setContentType(String type) {
230: response.setContentType(type);
231: }
232:
233: public String getContentType() {
234: return response.getContentType();
235: }
236:
237: public void setStatusMessage(String responseString) {
238: response.setStatus(getStatusCode(), responseString);
239: }
240:
241: public void flushBuffer() throws java.io.IOException {
242: response.flushBuffer();
243: }
244: }
245: }
|