001 /** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one or more 004 * contributor license agreements. See the NOTICE file distributed with 005 * this work for additional information regarding copyright ownership. 006 * The ASF licenses this file to You under the Apache License, Version 2.0 007 * (the "License"); you may not use this file except in compliance with 008 * the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 package org.apache.camel.component.http; 019 020 import java.io.IOException; 021 import java.util.concurrent.ConcurrentHashMap; 022 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 /** 029 * @version $Revision: 549890 $ 030 */ 031 public class CamelServlet extends HttpServlet { 032 033 private ConcurrentHashMap<String, HttpConsumer> consumers=new ConcurrentHashMap<String, HttpConsumer>(); 034 035 public CamelServlet() { 036 } 037 038 @Override 039 protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 040 try { 041 042 // Is there a consumer registered for the request. 043 HttpConsumer consumer = resolve(request); 044 if( consumer == null ) { 045 response.sendError(HttpServletResponse.SC_NOT_FOUND); 046 return; 047 } 048 049 // Have the camel process the HTTP exchange. 050 HttpExchange exchange = new HttpExchange(consumer.getEndpoint(), request, response); 051 consumer.getProcessor().process(exchange); 052 053 // HC: The getBinding() is interesting because it illustrates the impedance miss-match between 054 // HTTP's stream oriented protocol, and Camels more message oriented protocol exchanges. 055 056 // now lets output to the response 057 consumer.getBinding().writeResponse(exchange); 058 059 } catch (Exception e) { 060 throw new ServletException(e); 061 } 062 } 063 064 protected HttpConsumer resolve(HttpServletRequest request) { 065 String path = request.getPathInfo(); 066 return consumers.get(path); 067 } 068 069 public void connect(HttpConsumer consumer) { 070 consumers.put(consumer.getPath(), consumer); 071 } 072 073 public void disconnect(HttpConsumer consumer) { 074 consumers.remove(consumer.getPath()); 075 } 076 077 }