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.ServletConfig;
023    import javax.servlet.ServletException;
024    import javax.servlet.http.HttpServlet;
025    import javax.servlet.http.HttpServletRequest;
026    import javax.servlet.http.HttpServletResponse;
027    
028    import org.apache.camel.Exchange;
029    import org.apache.camel.ExchangePattern;
030    import org.apache.camel.component.http.helper.HttpHelper;
031    import org.apache.camel.impl.DefaultExchange;
032    import org.slf4j.Logger;
033    import org.slf4j.LoggerFactory;
034    
035    /**
036     * @version 
037     */
038    public class CamelServlet extends HttpServlet {
039        private static final long serialVersionUID = -7061982839117697829L;
040        protected final transient Logger log = LoggerFactory.getLogger(getClass());
041        
042        /**
043         *  We have to define this explicitly so the name can be set as we can not always be
044         *  sure that it is already set via the init method
045         */
046        private String servletName;
047    
048        private ConcurrentHashMap<String, HttpConsumer> consumers = new ConcurrentHashMap<String, HttpConsumer>();
049       
050        @Override
051        public void init(ServletConfig config) throws ServletException {
052            super.init(config);
053            this.servletName = config.getServletName();
054        }
055    
056        @Override
057        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
058            if (log.isTraceEnabled()) {
059                log.trace("Service: " + request);
060            }
061    
062            // Is there a consumer registered for the request.
063            HttpConsumer consumer = resolve(request);
064            if (consumer == null) {
065                log.debug("No consumer to service request {}", request);
066                response.sendError(HttpServletResponse.SC_NOT_FOUND);
067                return;
068            }
069    
070            // are we suspended?
071            if (consumer.isSuspended()) {
072                log.debug("Consumer suspended, cannot service request {}", request);
073                response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
074                return;
075            }
076    
077            // create exchange and set data on it
078            Exchange exchange = new DefaultExchange(consumer.getEndpoint(), ExchangePattern.InOut);
079            if (consumer.getEndpoint().isBridgeEndpoint()) {
080                exchange.setProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.TRUE);
081            }
082            if (consumer.getEndpoint().isDisableStreamCache()) {
083                exchange.setProperty(Exchange.DISABLE_HTTP_STREAM_CACHE, Boolean.TRUE);
084            }
085    
086            HttpHelper.setCharsetFromContentType(request.getContentType(), exchange);
087            exchange.setIn(new HttpMessage(exchange, request, response));
088    
089            try {
090                if (log.isTraceEnabled()) {
091                    log.trace("Processing request for exchangeId: " + exchange.getExchangeId());
092                }
093                // process the exchange
094                consumer.getProcessor().process(exchange);
095            } catch (Exception e) {
096                exchange.setException(e);
097            }
098    
099            try {
100                if (log.isTraceEnabled()) {
101                    log.trace("Writing response for exchangeId: " + exchange.getExchangeId());
102                }
103    
104                // now lets output to the response
105                consumer.getBinding().writeResponse(exchange, response);
106            } catch (IOException e) {
107                log.error("Error processing request", e);
108                throw e;
109            } catch (Exception e) {
110                log.error("Error processing request", e);
111                throw new ServletException(e);
112            }
113        }
114    
115        protected HttpConsumer resolve(HttpServletRequest request) {
116            String path = request.getPathInfo();
117            if (path == null) {
118                return null;
119            }
120            HttpConsumer answer = consumers.get(path);
121                   
122            if (answer == null) {
123                for (String key : consumers.keySet()) {
124                    if (consumers.get(key).getEndpoint().isMatchOnUriPrefix() && path.startsWith(key)) {
125                        answer = consumers.get(key);
126                        break;
127                    }
128                }
129            }
130            return answer;
131        }
132    
133        public void connect(HttpConsumer consumer) {
134            log.debug("Connecting consumer: {}", consumer);
135            consumers.put(consumer.getPath(), consumer);
136        }
137    
138        public void disconnect(HttpConsumer consumer) {
139            log.debug("Disconnecting consumer: {}", consumer);
140            consumers.remove(consumer.getPath());
141        }
142    
143        public String getServletName() {
144            return servletName;
145        }
146    
147        public void setServletName(String servletName) {
148            this.servletName = servletName;
149        }
150        
151    }