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    import org.apache.camel.ExchangePattern;
028    import org.apache.camel.impl.DefaultExchange;
029    
030    /**
031     * @version $Revision: 801145 $
032     */
033    public class CamelServlet extends HttpServlet {
034    
035        private static final long serialVersionUID = -7061982839117697829L;
036    
037        private ConcurrentHashMap<String, HttpConsumer> consumers = new ConcurrentHashMap<String, HttpConsumer>();
038        private boolean matchOnUriPrefix;
039    
040        public CamelServlet(boolean matchOnUriPrefix) {
041            this.matchOnUriPrefix = matchOnUriPrefix;
042        }
043        
044        @Override
045        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
046            try {
047    
048                // Is there a consumer registered for the request.
049                HttpConsumer consumer = resolve(request);
050                if (consumer == null) {
051                    response.sendError(HttpServletResponse.SC_NOT_FOUND);
052                    return;
053                }
054    
055                // Have the camel process the HTTP exchange.
056                DefaultExchange exchange = new DefaultExchange(consumer.getEndpoint(), ExchangePattern.InOut);
057                exchange.setIn(new HttpMessage(exchange, request, response));
058                consumer.getProcessor().process(exchange);
059    
060                // HC: The getBinding() is interesting because it illustrates the
061                // impedance miss-match between
062                // HTTP's stream oriented protocol, and Camels more message oriented
063                // protocol exchanges.
064    
065                // now lets output to the response
066                consumer.getBinding().writeResponse(exchange, response);
067    
068            } catch (Exception e) {
069                e.printStackTrace();
070                throw new ServletException(e);
071            }
072        }
073    
074        protected HttpConsumer resolve(HttpServletRequest request) {
075            String path = request.getPathInfo();
076            if (path == null) {
077                return null;
078            }
079            HttpConsumer answer = consumers.get(path);
080                   
081            if (answer == null && matchOnUriPrefix) {
082                for (String key : consumers.keySet()) {
083                    if (path.startsWith(key)) {
084                        answer = consumers.get(key);
085                        break;
086                    }
087                }
088            }
089            return answer;
090        }
091    
092        public void connect(HttpConsumer consumer) {
093            consumers.put(consumer.getPath(), consumer);
094        }
095    
096        public void disconnect(HttpConsumer consumer) {
097            consumers.remove(consumer.getPath());
098        }
099    
100        public boolean isMatchOnUriPrefix() {
101            return matchOnUriPrefix;
102        }
103        
104        public void setMatchOnUriPrefix(boolean matchOnUriPrefix) {
105            this.matchOnUriPrefix = matchOnUriPrefix;
106        }
107    }