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