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