Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
CamelServlet |
|
| 0.0;0 |
1 | /** |
|
2 | * |
|
3 | * Licensed to the Apache Software Foundation (ASF) under one or more |
|
4 | * contributor license agreements. See the NOTICE file distributed with |
|
5 | * this work for additional information regarding copyright ownership. |
|
6 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
|
7 | * (the "License"); you may not use this file except in compliance with |
|
8 | * the License. You may obtain a copy of the License at |
|
9 | * |
|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 | * |
|
12 | * Unless required by applicable law or agreed to in writing, software |
|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 | * See the License for the specific language governing permissions and |
|
16 | * limitations under the License. |
|
17 | */ |
|
18 | package org.apache.camel.component.http; |
|
19 | ||
20 | import java.io.IOException; |
|
21 | import java.util.concurrent.ConcurrentHashMap; |
|
22 | ||
23 | import javax.servlet.ServletException; |
|
24 | import javax.servlet.http.HttpServlet; |
|
25 | import javax.servlet.http.HttpServletRequest; |
|
26 | import javax.servlet.http.HttpServletResponse; |
|
27 | ||
28 | /** |
|
29 | * @version $Revision: 549890 $ |
|
30 | */ |
|
31 | public class CamelServlet extends HttpServlet { |
|
32 | ||
33 | 2 | private ConcurrentHashMap<String, HttpConsumer> consumers=new ConcurrentHashMap<String, HttpConsumer>(); |
34 | ||
35 | 2 | public CamelServlet() { |
36 | 2 | } |
37 | ||
38 | @Override |
|
39 | protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
|
40 | try { |
|
41 | ||
42 | // Is there a consumer registered for the request. |
|
43 | 2 | HttpConsumer consumer = resolve(request); |
44 | 2 | if( consumer == null ) { |
45 | 0 | response.sendError(HttpServletResponse.SC_NOT_FOUND); |
46 | 0 | return; |
47 | } |
|
48 | ||
49 | // Have the camel process the HTTP exchange. |
|
50 | 2 | HttpExchange exchange = new HttpExchange(consumer.getEndpoint(), request, response); |
51 | 2 | consumer.getProcessor().process(exchange); |
52 | ||
53 | // HC: The getBinding() is interesting because it illustrates the impedance miss-match between |
|
54 | // HTTP's stream oriented protocol, and Camels more message oriented protocol exchanges. |
|
55 | ||
56 | // now lets output to the response |
|
57 | 2 | consumer.getBinding().writeResponse(exchange); |
58 | ||
59 | 0 | } catch (Exception e) { |
60 | 0 | throw new ServletException(e); |
61 | 2 | } |
62 | 2 | } |
63 | ||
64 | protected HttpConsumer resolve(HttpServletRequest request) { |
|
65 | 2 | String path = request.getPathInfo(); |
66 | 2 | return consumers.get(path); |
67 | } |
|
68 | ||
69 | public void connect(HttpConsumer consumer) { |
|
70 | 2 | consumers.put(consumer.getPath(), consumer); |
71 | 2 | } |
72 | ||
73 | public void disconnect(HttpConsumer consumer) { |
|
74 | 2 | consumers.remove(consumer.getPath()); |
75 | 2 | } |
76 | ||
77 | } |