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     */
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: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
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        private ServletConfig config;
044    
045        public WebServiceContainerInvoker(Object pojo) {
046            this.pojo = pojo;
047        }
048    
049        public void init(ServletConfig config) throws ServletException {
050            this.config = config;
051            ServletContext context = config.getServletContext();
052            String webServiceContainerID = config.getInitParameter(WEBSERVICE_CONTAINER);
053            service = (WebServiceContainer) context.getAttribute(webServiceContainerID);
054        }
055    
056        public ServletConfig getServletConfig() {
057            return config;
058        }
059    
060        public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
061            res.setContentType("text/xml");
062            RequestAdapter request = new RequestAdapter((HttpServletRequest) req);
063            ResponseAdapter response = new ResponseAdapter((HttpServletResponse) res);
064    
065            // This is the guy the WebServiceContainer should invoke
066            req.setAttribute(WebServiceContainer.POJO_INSTANCE, pojo);
067    
068            req.setAttribute(WebServiceContainer.SERVLET_REQUEST, (HttpServletRequest) req);
069            req.setAttribute(WebServiceContainer.SERVLET_RESPONSE, (HttpServletResponse) res);
070            req.setAttribute(WebServiceContainer.SERVLET_CONTEXT, config.getServletContext());
071    
072            if (req.getParameter("wsdl") != null || req.getParameter("WSDL") != null) {
073                try {
074                    service.getWsdl(request, response);
075                } catch (IOException e) {
076                    throw e;
077                } catch (ServletException e) {
078                    throw e;
079                } catch (Exception e) {
080                    throw new ServletException("Could not fetch wsdl!", e);
081                }
082            } else {
083                try {
084                    service.invoke(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 process message!", e);
091                }
092            }
093        }
094    
095        public String getServletInfo() {
096            return null;
097        }
098    
099        public void destroy() {
100            service.destroy();
101        }
102    
103        private static class RequestAdapter implements WebServiceContainer.Request {
104            private final HttpServletRequest request;
105    
106            public RequestAdapter(HttpServletRequest request) {
107                this.request = request;
108            }
109    
110            public String getHeader(String name) {
111                return request.getHeader(name);
112            }
113    
114            public java.net.URI getURI() {
115                try {
116                    return new java.net.URI(request.getScheme(), null, request.getServerName(), request.getServerPort(), request.getRequestURI(), request.getQueryString(), null);
117                } catch (URISyntaxException e) {
118                    throw new IllegalStateException(e.getMessage(), e);
119                }
120            }
121    
122            public int getContentLength() {
123                return request.getContentLength();
124            }
125    
126            public String getContentType() {
127                return request.getContentType();
128            }
129    
130            public String getContextPath() {
131                return request.getContextPath();
132            }
133    
134            public InputStream getInputStream() throws IOException {
135                return request.getInputStream();
136            }
137    
138            public int getMethod() {
139                Integer method = (Integer) methods.get(request.getMethod());
140                return method == null ? UNSUPPORTED : method.intValue();
141            }
142    
143            public String getParameter(String name) {
144                return request.getParameter(name);
145            }
146    
147            public Map getParameters() {
148                return request.getParameterMap();
149            }
150    
151            private static final Map methods = new HashMap();
152    
153            static {
154                methods.put("OPTIONS", new Integer(OPTIONS));
155                methods.put("GET", new Integer(GET));
156                methods.put("HEAD", new Integer(HEAD));
157                methods.put("POST", new Integer(POST));
158                methods.put("PUT", new Integer(PUT));
159                methods.put("DELETE", new Integer(DELETE));
160                methods.put("TRACE", new Integer(TRACE));
161                methods.put("CONNECT", new Integer(CONNECT));
162            }
163    
164            public Object getAttribute(String s) {
165                return request.getAttribute(s);
166            }
167    
168            public void setAttribute(String s, Object o) {
169                request.setAttribute(s, o);
170            }
171    
172            public String getRemoteAddr() {
173                return request.getRemoteAddr();
174            }
175    
176        }
177    
178        private static class ResponseAdapter implements WebServiceContainer.Response {
179            private final HttpServletResponse response;
180    
181            public ResponseAdapter(HttpServletResponse response) {
182                this.response = response;
183            }
184            
185            public void setHeader(String name, String value) {
186                response.setHeader(name, value);
187            }
188    
189            public String getHeader(String name) {
190                throw new UnsupportedOperationException("Not possible to implement");
191            }
192    
193            public OutputStream getOutputStream() {
194                try {
195                    return response.getOutputStream();
196                } catch (IOException e) {
197                    throw (IllegalStateException) new IllegalStateException().initCause(e);
198                }
199            }
200    
201            public void setStatusCode(int code) {
202                response.setStatus(code);
203            }
204    
205            public int getStatusCode() {
206                throw new UnsupportedOperationException("Not possible to implement");
207            }
208    
209            public void setContentType(String type) {
210                response.setContentType(type);
211            }
212    
213            public String getContentType() {
214                return response.getContentType();
215            }
216    
217            public void setStatusMessage(String responseString) {
218                response.setStatus(getStatusCode(), responseString);
219            }
220            
221            public void flushBuffer() throws java.io.IOException{
222                response.flushBuffer();
223            }
224        }
225    }