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.Exchange;
028    import org.apache.camel.ExchangePattern;
029    import org.apache.camel.impl.DefaultExchange;
030    import org.apache.commons.logging.Log;
031    import org.apache.commons.logging.LogFactory;
032    
033    /**
034     * @version $Revision: 957588 $
035     */
036    public class CamelServlet extends HttpServlet {
037    
038        private static final long serialVersionUID = -7061982839117697829L;
039        protected final transient Log log = LogFactory.getLog(getClass());
040    
041        private ConcurrentHashMap<String, HttpConsumer> consumers = new ConcurrentHashMap<String, HttpConsumer>();
042       
043        @Override
044        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
045            try {
046    
047                // Is there a consumer registered for the request.
048                HttpConsumer consumer = resolve(request);
049                if (consumer == null) {
050                    response.sendError(HttpServletResponse.SC_NOT_FOUND);
051                    return;
052                }
053    
054                // are we suspended?
055                if (consumer.isSuspended()) {
056                    response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
057                    return;
058                }
059    
060                // create exchange and set data on it
061                Exchange exchange = new DefaultExchange(consumer.getEndpoint(), ExchangePattern.InOut);
062                if (consumer.getEndpoint().isBridgeEndpoint()) {
063                    exchange.setProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.TRUE);
064                }
065                if (consumer.getEndpoint().isDisableStreamCache()) {
066                    exchange.setProperty(Exchange.DISABLE_HTTP_STREAM_CACHE, Boolean.TRUE);
067                }
068                exchange.setIn(new HttpMessage(exchange, request, response));
069    
070                // Have the camel process the HTTP exchange.
071                consumer.getProcessor().process(exchange);
072    
073                // now lets output to the response
074                consumer.getBinding().writeResponse(exchange, response);
075    
076            } catch (Exception e) {
077                log.error("Error processing request", e);
078                throw new ServletException(e);
079            }
080        }
081    
082        protected HttpConsumer resolve(HttpServletRequest request) {
083            String path = request.getPathInfo();
084            if (path == null) {
085                return null;
086            }
087            HttpConsumer answer = consumers.get(path);
088                   
089            if (answer == null) {
090                for (String key : consumers.keySet()) {
091                    if (consumers.get(key).getEndpoint().isMatchOnUriPrefix() && path.startsWith(key)) {
092                        answer = consumers.get(key);
093                        break;
094                    }
095                }
096            }
097            return answer;
098        }
099    
100        public void connect(HttpConsumer consumer) {
101            consumers.put(consumer.getPath(), consumer);
102        }
103    
104        public void disconnect(HttpConsumer consumer) {
105            consumers.remove(consumer.getPath());
106        }
107        
108    }