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.servicemix.http;
018    
019    import java.io.IOException;
020    
021    import javax.servlet.ServletConfig;
022    import javax.servlet.ServletException;
023    import javax.servlet.http.HttpServlet;
024    import javax.servlet.http.HttpServletRequest;
025    import javax.servlet.http.HttpServletResponse;
026    
027    public class HttpBridgeServlet extends HttpServlet {
028    
029        /**
030         * Generated serial version UID
031         */
032        private static final long serialVersionUID = -7995806514300732777L;
033    
034        private HttpProcessor processor;
035    
036        public HttpProcessor getProcessor() {
037            return processor;
038        }
039    
040        public void setProcessor(HttpProcessor processor) {
041            this.processor = processor;
042        }
043    
044        public void init(ServletConfig config) throws ServletException {
045            super.init(config);
046            if (processor == null) {
047                processor = (HttpProcessor) getServletContext().getAttribute("processor");
048                if (processor == null) {
049                    throw new ServletException("No binding property available on the servlet context");
050                }
051            }
052        }
053    
054        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
055            try {
056                getProcessor().process(request, response);
057            } catch (IOException e) {
058                throw e;
059            } catch (ServletException e) {
060                throw e;
061            } catch (RuntimeException e) {
062                throw e;
063            } catch (Exception e) {
064                throw new ServletException("Failed to process request: " + e, e);
065            }
066        }
067    
068        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
069                        IOException {
070            try {
071                getProcessor().process(request, response);
072            } catch (IOException e) {
073                throw e;
074            } catch (ServletException e) {
075                throw e;
076            } catch (RuntimeException e) {
077                throw e;
078            } catch (Exception e) {
079                throw new ServletException("Failed to process request: " + e, e);
080            }
081        }
082    
083    }