001 /** 002 * 003 * Copyright 2004 The Apache Software Foundation 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * 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 */ 017 package org.apache.geronimo.webservices; 018 019 import java.io.IOException; 020 import java.io.InputStream; 021 import java.io.OutputStream; 022 import java.net.URISyntaxException; 023 import java.util.HashMap; 024 import java.util.Map; 025 import javax.servlet.Servlet; 026 import javax.servlet.ServletConfig; 027 import javax.servlet.ServletContext; 028 import javax.servlet.ServletException; 029 import javax.servlet.ServletRequest; 030 import javax.servlet.ServletResponse; 031 import javax.servlet.http.HttpServletRequest; 032 import javax.servlet.http.HttpServletResponse; 033 034 /** 035 * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $ 036 */ 037 public class WebServiceContainerInvoker implements Servlet { 038 039 public static final String WEBSERVICE_CONTAINER = WebServiceContainerInvoker.class.getName()+"@WebServiceContainer"; 040 041 private final Object pojo; 042 private WebServiceContainer service; 043 044 public WebServiceContainerInvoker(Object pojo) { 045 this.pojo = pojo; 046 } 047 048 public void init(ServletConfig config) throws ServletException { 049 ServletContext context = config.getServletContext(); 050 String webServiceContainerID = config.getInitParameter(WEBSERVICE_CONTAINER); 051 service = (WebServiceContainer) context.getAttribute(webServiceContainerID); 052 } 053 054 public ServletConfig getServletConfig() { 055 return null; 056 } 057 058 public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { 059 res.setContentType("text/xml"); 060 RequestAdapter request = new RequestAdapter((HttpServletRequest) req); 061 ResponseAdapter response = new ResponseAdapter((HttpServletResponse) res); 062 063 // This is the guy the WebServiceContainer should invoke 064 req.setAttribute(WebServiceContainer.POJO_INSTANCE, pojo); 065 066 if (req.getParameter("wsdl") != null || req.getParameter("WSDL") != null) { 067 try { 068 service.getWsdl(request, response); 069 } catch (IOException e) { 070 throw e; 071 } catch (ServletException e) { 072 throw e; 073 } catch (Exception e) { 074 throw new ServletException("Could not fetch wsdl!", e); 075 } 076 } else { 077 try { 078 service.invoke(request, response); 079 } catch (IOException e) { 080 throw e; 081 } catch (ServletException e) { 082 throw e; 083 } catch (Exception e) { 084 throw new ServletException("Could not process message!", e); 085 } 086 } 087 } 088 089 public String getServletInfo() { 090 return null; 091 } 092 093 public void destroy() { 094 } 095 096 private static class RequestAdapter implements WebServiceContainer.Request { 097 private final HttpServletRequest request; 098 099 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 }