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.camel.component.servlet;
018    
019    import javax.servlet.ServletConfig;
020    import javax.servlet.ServletException;
021    
022    import org.apache.camel.component.http.CamelServlet;
023    import org.apache.camel.component.http.HttpConsumer;
024    import org.slf4j.Logger;
025    import org.slf4j.LoggerFactory;
026    
027    /**
028     * Camel HTTP servlet which can be used in Camel routes to route servlet invocations in routes.
029     */
030    public class CamelHttpTransportServlet extends CamelServlet {
031        private static final long serialVersionUID = -1797014782158930490L;
032        private static final transient Logger LOG = LoggerFactory.getLogger(CamelHttpTransportServlet.class);
033    
034        private HttpRegistry httpRegistry;
035    
036        @Override
037        public void init(ServletConfig config) throws ServletException {
038            super.init(config);
039            if (httpRegistry == null) {
040                httpRegistry = DefaultHttpRegistry.getSingletonHttpRegistry();
041            }
042            httpRegistry.register(this);
043            LOG.info("Initialized CamelHttpTransportServlet[{}]", getServletName());
044        }
045        
046        @Override
047        public void destroy() {
048            httpRegistry.unregister(this);
049            LOG.info("Destroyed CamelHttpTransportServlet[{}]", getServletName());
050        }
051        
052        private ServletEndpoint getServletEndpoint(HttpConsumer consumer) {
053            if (!(consumer.getEndpoint() instanceof ServletEndpoint)) {
054                throw new RuntimeException("Invalid consumer type. Must be ServletEndpoint but is " 
055                        + consumer.getClass().getName());
056            }
057            return (ServletEndpoint)consumer.getEndpoint();
058        }
059    
060        @Override
061        public void connect(HttpConsumer consumer) {
062            ServletEndpoint endpoint = getServletEndpoint(consumer);
063            if (endpoint.getServletName() != null && endpoint.getServletName().equals(getServletName())) {
064                super.connect(consumer);
065            }
066        }
067    
068    }
069