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.web.http;
018    
019    import java.io.IOException;
020    import java.lang.reflect.InvocationTargetException;
021    import java.lang.reflect.Method;
022    
023    import javax.jbi.component.Component;
024    import javax.servlet.ServletConfig;
025    import javax.servlet.ServletException;
026    import javax.servlet.http.HttpServlet;
027    import javax.servlet.http.HttpServletRequest;
028    import javax.servlet.http.HttpServletResponse;
029    
030    import org.apache.servicemix.jbi.container.JBIContainer;
031    import org.apache.servicemix.jbi.framework.ComponentMBeanImpl;
032    import org.springframework.context.ApplicationContext;
033    import org.springframework.web.context.support.WebApplicationContextUtils;
034    
035    /**
036     * This servlet is meant to be used when with the servicemix-http component.
037     * It is based on org.apache.servicemix.http.HttpManagedServlet, but
038     * uses introspection to be able to use it with an installed component,
039     * rather than an embedded component.
040     *  
041     * @author gnodet
042     */
043    public class HttpManagedServlet extends HttpServlet {
044    
045        public static final String CONTAINER_PROPERTY = "container";
046        public static final String CONTAINER_DEFAULT = "jbi";
047        
048        public static final String COMPONENT_PROPERTY = "component";
049        public static final String COMPONENT_DEFAULT = "servicemix-http";
050        
051        public static final String MAPPING_PROPERTY = "mapping";
052        
053        private JBIContainer container;
054        private Object processor;
055        private Method processorMethod;
056        
057        public void init(ServletConfig config) throws ServletException {
058            super.init(config);
059            
060            // Retrieve spring application context
061            ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
062            
063            // Retrieve 
064            String containerName = config.getInitParameter(CONTAINER_PROPERTY);
065            if (containerName == null) {
066                containerName = CONTAINER_DEFAULT;
067            }
068            container = (JBIContainer) applicationContext.getBean(containerName);
069            if (container == null) {
070                throw new IllegalStateException("Unable to find jbi container " + containerName);
071            }
072        }
073        
074        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
075            try {
076                if (processor == null) {
077                    String componentName = getServletConfig().getInitParameter(COMPONENT_PROPERTY);
078                    if (componentName == null) {
079                        componentName = COMPONENT_DEFAULT;
080                    }
081                    ComponentMBeanImpl mbean = container.getComponent(componentName);
082                    if (mbean == null) {
083                        throw new ServletException("Component " + componentName + " not installed");
084                    }
085                    Component component = mbean.getComponent();
086                    Method mth = component.getClass().getMethod("getMainProcessor", (Class[]) null);
087                    processor = mth.invoke(component, (Object[]) null);
088                    processorMethod = processor.getClass().getMethod("process", new Class[] { HttpServletRequest.class, HttpServletResponse.class });
089                }
090                processorMethod.invoke(processor, new Object[] { request, response });
091            } catch (ServletException e) {
092                throw e;
093            } catch (RuntimeException e) {
094                throw e;
095            } catch (InvocationTargetException e) {
096                throw new ServletException("Failed to process request: " + e.getTargetException(), e.getTargetException());
097            } catch (Exception e) {
098                throw new ServletException("Failed to process request: " + e, e);
099            }
100        }
101    }