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.components.http;
018    
019    import javax.jbi.JBIException;
020    import javax.servlet.ServletConfig;
021    import javax.servlet.ServletException;
022    import javax.servlet.http.HttpServlet;
023    import javax.servlet.http.HttpServletRequest;
024    import javax.servlet.http.HttpServletResponse;
025    
026    import java.io.IOException;
027    
028    /**
029     * A Servlet which dispatches requests into the JBI container and returns the result.
030     *
031     * @version $Revision: 2153 $
032     */
033    public class BindingServlet extends HttpServlet {
034    
035        private HttpBinding binding;
036    
037        /**
038         * get da binding
039         *
040         * @return the binding
041         */
042        public HttpBinding getBinding() {
043            return binding;
044        }
045    
046        public void setBinding(HttpBinding binding) {
047            this.binding = binding;
048        }
049    
050    
051        public void init(ServletConfig config) throws ServletException {
052            super.init(config);
053            if (binding == null) {
054                binding = (HttpBinding) getServletContext().getAttribute("binding");
055                if (binding == null) {
056                    binding = createHttpBinding(config);
057                }
058                    if (binding == null) {
059                    throw new ServletException("No binding property available on the servlet context");
060                }
061            }
062        }
063    
064        
065        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
066            try {
067                getBinding().process(request, response);
068            }
069            catch (JBIException e) {
070                throw new ServletException("Failed to process JBI request: " + e, e);
071            }
072        }
073    
074        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
075            try {
076                getBinding().process(request, response);
077            }
078            catch (JBIException e) {
079                throw new ServletException("Failed to process JBI request: " + e, e);
080            }
081        }
082    
083        protected HttpBinding createHttpBinding(ServletConfig config) throws ServletException {
084            // lets default to in/out
085            return new HttpInOutBinding();
086        }
087    
088    }