001    /** 
002     * 
003     * Copyright 2004 Protique Ltd
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     **/
018    package org.codehaus.activesoap.transport.http;
019    
020    import org.codehaus.activesoap.RestService;
021    
022    import javax.servlet.ServletException;
023    import javax.servlet.http.HttpServlet;
024    import javax.servlet.http.HttpServletRequest;
025    import javax.servlet.http.HttpServletResponse;
026    import java.io.IOException;
027    
028    /**
029     * A Servlet for a REST or SOAP endpoint where some XML is HTTP POSTed and the response is returned.
030     *
031     * @version $Revision: 1.1 $
032     */
033    public class EndpointServlet extends HttpServlet {
034    
035        public static final String SERVICE_ATTRIBUTE = "org.codehaus.activesoap.Service";
036    
037        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
038            response.setContentType("application/soap+xml");
039            try {
040                getRestService().invoke(request.getReader(), response.getWriter());
041            }
042            catch (ServletException e) {
043                throw e;
044            }
045            catch (IOException e) {
046                throw e;
047            }
048            catch (Exception e) {
049                throw new ServletException("Failed to process SOAP request: " + e, e);
050            }
051        }
052    
053        public RestService getRestService() throws ServletException {
054            RestService service = (RestService) getServletContext().getAttribute(SERVICE_ATTRIBUTE);
055            if (service == null) {
056                throw new ServletException("No attribute available for '" + SERVICE_ATTRIBUTE + "'");
057            }
058            return service;
059        }
060    }