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.HttpServletRequest;
024    import javax.servlet.http.HttpServletResponse;
025    
026    import org.apache.servicemix.jbi.container.JBIContainer;
027    import org.apache.servicemix.jbi.framework.ComponentMBeanImpl;
028    import org.springframework.context.ApplicationContext;
029    import org.springframework.web.context.support.WebApplicationContextUtils;
030    
031    /**
032     * This servlet is meant to be used when embedding ServiceMix in a web application so that so servicemix-http component
033     * will reuse the web container.
034     * 
035     * @author gnodet
036     */
037    public class HttpManagedServlet extends javax.servlet.http.HttpServlet {
038    
039        public static final String CONTAINER_PROPERTY = "container";
040        public static final String CONTAINER_DEFAULT = "jbi";
041    
042        public static final String COMPONENT_PROPERTY = "component";
043        public static final String COMPONENT_DEFAULT = "servicemix-http";
044    
045        public static final String MAPPING_PROPERTY = "mapping";
046    
047        private HttpProcessor processor;
048    
049        public void init(ServletConfig config) throws ServletException {
050            super.init(config);
051    
052            // Retrieve spring application context
053            ApplicationContext applicationContext = WebApplicationContextUtils
054                            .getRequiredWebApplicationContext(getServletContext());
055    
056            // Retrieve
057            String containerName = config.getInitParameter(CONTAINER_PROPERTY);
058            if (containerName == null) {
059                containerName = CONTAINER_DEFAULT;
060            }
061            JBIContainer container = (JBIContainer) applicationContext.getBean(containerName);
062            if (container == null) {
063                throw new IllegalStateException("Unable to find jbi container " + containerName);
064            }
065            String componentName = config.getInitParameter(COMPONENT_PROPERTY);
066            if (componentName == null) {
067                componentName = COMPONENT_DEFAULT;
068            }
069            ComponentMBeanImpl componentMBean = container.getComponent(componentName);
070            if (componentMBean == null) {
071                throw new IllegalStateException("Unable to find component " + componentName);
072            }
073            HttpComponent component = (HttpComponent) componentMBean.getComponent();
074            String mapping = config.getInitParameter(MAPPING_PROPERTY);
075            if (mapping != null) {
076                component.getConfiguration().setMapping(mapping);
077            }
078            processor = component.getMainProcessor();
079        }
080    
081        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException,
082                        IOException {
083            try {
084                processor.process(request, response);
085            } catch (IOException e) {
086                throw e;
087            } catch (ServletException e) {
088                throw e;
089            } catch (RuntimeException e) {
090                throw e;
091            } catch (Exception e) {
092                throw new ServletException("Failed to process request: " + e, e);
093            }
094        }
095    }