1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package net.sourceforge.hiveremoting.caucho; |
16 | |
17 | import java.io.InputStream; |
18 | import java.io.OutputStream; |
19 | import java.util.Enumeration; |
20 | import java.util.HashMap; |
21 | import java.util.Map; |
22 | |
23 | import javax.servlet.ServletException; |
24 | import javax.servlet.http.HttpServletRequest; |
25 | import javax.servlet.http.HttpServletResponse; |
26 | |
27 | import org.apache.commons.logging.Log; |
28 | import org.apache.commons.logging.LogFactory; |
29 | import org.apache.hivemind.internal.ServicePoint; |
30 | |
31 | import com.caucho.hessian.io.SerializerFactory; |
32 | |
33 | /** |
34 | * @author Jean-Francois Poilpret |
35 | */ |
36 | public abstract class AbstractRemoteServiceInvoker implements RemoteServiceInvoker |
37 | { |
38 | static private final Log _logger = LogFactory.getLog(AbstractRemoteServiceInvoker.class); |
39 | |
40 | protected AbstractRemoteServiceInvoker( PublishServiceContribution contrib, |
41 | SerializerFactory factory) |
42 | { |
43 | ServicePoint service = contrib.getServiceId(); |
44 | _logger.debug("<init> '" + service.getExtensionPointId() + "'"); |
45 | _factory = factory; |
46 | _serviceId = contrib.getUrlPath(); |
47 | _secure = contrib.isSecure(); |
48 | _handler = contrib.getContextHandler(); |
49 | _logger.debug("<init> '" + service.getExtensionPointId() + "' ended"); |
50 | } |
51 | |
52 | //CSOFF: IllegalCatchCheck |
53 | public void invoke(HttpServletRequest request, HttpServletResponse response) |
54 | throws ServletException |
55 | { |
56 | if (_secure && !request.isSecure()) |
57 | { |
58 | _logger.error("invoke(" + _serviceId + ") request is not secure"); |
59 | throw new ServletException("Request must be secure but is not!"); |
60 | } |
61 | if (_handler != null) |
62 | { |
63 | Map<String, String> context = new HashMap<String, String>(); |
64 | //#### Note that the following call may return null with some |
65 | // Servlet Containers (which ones? I dunno) |
66 | Enumeration headers = request.getHeaderNames(); |
67 | while (headers.hasMoreElements()) |
68 | { |
69 | String key = (String) headers.nextElement(); |
70 | String value = request.getHeader(key); |
71 | context.put(key, value); |
72 | } |
73 | _handler.extractContext(context); |
74 | } |
75 | try |
76 | { |
77 | invoke(request.getInputStream(), response.getOutputStream()); |
78 | } |
79 | catch (RuntimeException e) |
80 | { |
81 | _logger.error("invoke(" + _serviceId + ")", e); |
82 | throw e; |
83 | } |
84 | catch (ServletException e) |
85 | { |
86 | _logger.error("invoke(" + _serviceId + ")", e); |
87 | throw e; |
88 | } |
89 | catch (Throwable e) |
90 | { |
91 | _logger.error("invoke(" + _serviceId + ")", e); |
92 | throw new ServletException(e); |
93 | } |
94 | finally |
95 | { |
96 | if (_handler != null) |
97 | { |
98 | Map<String, String> context = new HashMap<String, String>(); |
99 | _handler.initContext(context); |
100 | for (Map.Entry<String, String> entry: context.entrySet()) |
101 | { |
102 | response.setHeader(entry.getKey(), entry.getValue()); |
103 | } |
104 | } |
105 | } |
106 | } |
107 | //CSON: IllegalCatchCheck |
108 | |
109 | //CSOFF: IllegalThrowsCheck |
110 | abstract protected void invoke(InputStream is, OutputStream os) |
111 | throws Throwable; |
112 | //CSON: IllegalThrowsCheck |
113 | |
114 | protected final SerializerFactory _factory; |
115 | protected final String _serviceId; |
116 | protected final boolean _secure; |
117 | protected final RemoteContextHandler _handler; |
118 | } |