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