1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.openehealth.ipf.commons.ihe.fhir;
18
19 import ca.uhn.fhir.context.FhirContext;
20 import ca.uhn.fhir.rest.api.MethodOutcome;
21 import ca.uhn.fhir.rest.api.server.IBundleProvider;
22 import org.hl7.fhir.instance.model.api.IBaseBundle;
23 import org.hl7.fhir.instance.model.api.IBaseResource;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import javax.security.cert.X509Certificate;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30 import java.io.Serializable;
31 import java.util.*;
32
33
34
35
36
37
38
39
40
41 public abstract class AbstractPlainProvider implements Serializable {
42
43 private static final Logger LOG = LoggerFactory.getLogger(AbstractPlainProvider.class);
44
45 private RequestConsumer consumer;
46
47 protected FhirContext getFhirContext() {
48 return consumer.getFhirContext();
49 }
50
51
52
53
54
55
56
57
58
59
60
61 protected final <R extends IBaseResource> R requestResource(
62 Object payload, Class<R> resultType,
63 HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
64 return requestResource(payload, null, resultType, httpServletRequest, httpServletResponse);
65 }
66
67
68
69
70
71
72
73
74
75
76
77
78 protected final <R extends IBaseResource> R requestResource(
79 Object payload, FhirSearchParameters parameters, Class<R> resultType,
80 HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
81 if (consumer == null) {
82 throw new IllegalStateException("Consumer is not initialized");
83 }
84 Map<String, Object> headers = enrichParameters(parameters, httpServletRequest);
85 return consumer.handleResourceRequest(payload, headers, resultType);
86 }
87
88
89
90
91
92
93
94
95
96
97 protected final <R extends IBaseResource> List<R> requestBundle(Object payload,
98 HttpServletRequest httpServletRequest,
99 HttpServletResponse httpServletResponse) {
100 return requestBundle(payload, null, httpServletRequest, httpServletResponse);
101 }
102
103
104
105
106
107
108
109
110
111
112
113 protected final <R extends IBaseResource> List<R> requestBundle(
114 Object payload, FhirSearchParameters parameters,
115 HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
116 return requestBundle(payload, parameters, null, httpServletRequest, httpServletResponse);
117 }
118
119
120
121
122
123
124
125
126
127
128
129
130 protected final <R extends IBaseResource> List<R> requestBundle(
131 Object payload, FhirSearchParameters parameters,
132 String resourceType,
133 HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
134 if (consumer == null) {
135 throw new IllegalStateException("Consumer is not initialized");
136 }
137 Map<String, Object> headers = enrichParameters(parameters, httpServletRequest);
138 if (resourceType != null) {
139 headers.put(Constants.FHIR_RESOURCE_TYPE_HEADER, resourceType);
140 }
141 return consumer.handleBundleRequest(payload, headers);
142 }
143
144
145
146
147
148
149
150
151
152
153
154
155 protected final IBundleProvider requestBundleProvider(
156 Object payload, FhirSearchParameters searchParameters,
157 HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
158 return requestBundleProvider(payload, searchParameters, null, httpServletRequest, httpServletResponse);
159 }
160
161
162
163
164
165
166
167
168
169
170
171
172
173 protected final IBundleProvider requestBundleProvider(
174 Object payload, FhirSearchParameters searchParameters,
175 String resourceType,
176 HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
177 if (consumer == null) {
178 throw new IllegalStateException("Consumer is not initialized");
179 }
180 Map<String, Object> headers = enrichParameters(searchParameters, httpServletRequest);
181 if (resourceType != null) {
182 headers.put(Constants.FHIR_RESOURCE_TYPE_HEADER, resourceType);
183 }
184 return consumer.handleBundleProviderRequest(payload, headers);
185 }
186
187
188
189
190
191
192
193
194
195 protected final MethodOutcome requestAction(
196 Object payload,
197 HttpServletRequest httpServletRequest,
198 HttpServletResponse httpServletResponse) {
199 return requestAction(payload, null, httpServletRequest, httpServletResponse);
200 }
201
202
203
204
205
206
207
208
209
210
211 protected final MethodOutcome requestAction(
212 Object payload,
213 FhirSearchParameters parameters,
214 HttpServletRequest httpServletRequest,
215 HttpServletResponse httpServletResponse) {
216 if (consumer == null) {
217 throw new IllegalStateException("Consumer is not initialized");
218 }
219 Map<String, Object> headers = enrichParameters(parameters, httpServletRequest);
220 return consumer.handleAction(payload, headers);
221 }
222
223
224
225
226
227
228
229
230
231 protected final <T extends IBaseBundle> T requestTransaction(
232 Object payload,
233 Class<T> bundleClass,
234 HttpServletRequest httpServletRequest,
235 HttpServletResponse httpServletResponse) {
236 if (consumer == null) {
237 throw new IllegalStateException("Consumer is not initialized");
238 }
239 Map<String, Object> headers = enrichParameters(null, httpServletRequest);
240 return consumer.handleTransactionRequest(payload, headers, bundleClass);
241 }
242
243 protected Map<String, Object> enrichParameters(FhirSearchParameters parameters, HttpServletRequest httpServletRequest) {
244
245 Map<String, Object> enriched = new HashMap<>();
246 enriched.put(Constants.HTTP_URI, httpServletRequest.getRequestURI());
247 enriched.put(Constants.HTTP_URL, httpServletRequest.getRequestURL().toString());
248 enriched.put(Constants.HTTP_METHOD, httpServletRequest.getMethod());
249 enriched.put(Constants.HTTP_QUERY, httpServletRequest.getQueryString());
250 enriched.put(Constants.HTTP_CHARACTER_ENCODING, httpServletRequest.getCharacterEncoding());
251 enriched.put(Constants.HTTP_CONTENT_TYPE, httpServletRequest.getContentType());
252 enriched.put(Constants.HTTP_PROTOCOL_VERSION, httpServletRequest.getProtocol());
253 enriched.put(Constants.HTTP_SCHEME, httpServletRequest.getScheme());
254 enriched.put(Constants.HTTP_CLIENT_IP_ADDRESS, httpServletRequest.getRemoteAddr());
255
256 Map<String, List<String>> headers = extractHttpHeaders(httpServletRequest);
257 enriched.put(Constants.HTTP_HEADERS, headers);
258
259 String cipherSuite = (String) httpServletRequest.getAttribute("javax.servlet.request.cipher_suite");
260 if (cipherSuite != null) {
261 enriched.put(Constants.HTTP_X509_CERTIFICATES, httpServletRequest.getAttribute(X509Certificate.class.getName()));
262 }
263
264 if (parameters != null) {
265 enriched.put(Constants.FHIR_REQUEST_PARAMETERS, parameters);
266 }
267 return enriched;
268 }
269
270
271
272
273
274 private static Map<String, List<String>> extractHttpHeaders(HttpServletRequest httpServletRequest) {
275 Map<String, List<String>> result = new HashMap<>();
276 Enumeration<String> headerNames = httpServletRequest.getHeaderNames();
277 if (headerNames != null) {
278 while (headerNames.hasMoreElements()) {
279 String name = headerNames.nextElement();
280 Enumeration<String> headers = httpServletRequest.getHeaders(name);
281 if (headers != null) {
282 List<String> list = new ArrayList<>();
283 while (headers.hasMoreElements()) {
284 list.add(headers.nextElement());
285 }
286 if (!list.isEmpty()) {
287 result.put(name, list);
288 }
289 }
290 }
291 }
292 return result;
293 }
294
295
296
297
298 public RequestConsumer getConsumer() {
299 return consumer;
300 }
301
302
303 public void setConsumer(RequestConsumer consumer) {
304 if (this.consumer != null) {
305 throw new IllegalStateException("This provider is already used by a different consumer: " + consumer);
306 }
307 this.consumer = consumer;
308 LOG.info("Connected consumer {} to provider {}", consumer, this);
309 }
310
311 public void unsetConsumer(RequestConsumer consumer) {
312 if (this.consumer == consumer) {
313 this.consumer = null;
314 LOG.info("Disconnected consumer {} from provider {}", consumer, this);
315 }
316 }
317
318 }