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.http;
018    
019    import java.io.IOException;
020    import java.util.concurrent.ConcurrentHashMap;
021    
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    /**
028     * @version $Revision: 640731 $
029     */
030    public class CamelServlet extends HttpServlet {
031    
032        private ConcurrentHashMap<String, HttpConsumer> consumers = new ConcurrentHashMap<String, HttpConsumer>();
033    
034        public CamelServlet() {
035        }
036    
037        @Override
038        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
039            try {
040    
041                // Is there a consumer registered for the request.
042                HttpConsumer consumer = resolve(request);
043                if (consumer == null) {
044                    response.sendError(HttpServletResponse.SC_NOT_FOUND);
045                    return;
046                }
047    
048                // Have the camel process the HTTP exchange.
049                HttpExchange exchange = new HttpExchange(consumer.getEndpoint(), request, response);
050                consumer.getProcessor().process(exchange);
051    
052                // HC: The getBinding() is interesting because it illustrates the
053                // impedance miss-match between
054                // HTTP's stream oriented protocol, and Camels more message oriented
055                // protocol exchanges.
056    
057                // now lets output to the response
058                consumer.getBinding().writeResponse(exchange, response);
059    
060            } catch (Exception e) {
061                throw new ServletException(e);
062            }
063        }
064    
065        protected HttpConsumer resolve(HttpServletRequest request) {
066            String path = request.getPathInfo();
067            return consumers.get(path);
068        }
069    
070        public void connect(HttpConsumer consumer) {
071            consumers.put(consumer.getPath(), consumer);
072        }
073    
074        public void disconnect(HttpConsumer consumer) {
075            consumers.remove(consumer.getPath());
076        }
077    
078    }