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