1 /*
2 * Copyright 2016 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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
25 import java.util.List;
26 import java.util.Map;
27
28 /**
29 * Consumer interface of FHIR requests. Plain providers or resource providers forward the
30 * request data received to an instance of this interface to be processed. This decouples
31 * request reception from request handling, so you can e.g. use the IHE resource providers
32 * outside of Apache Camel.
33 * <p>
34 * The request parameters may contain the following parameters entries indicating a "lazy-loading" mode
35 * on searches.
36 * <ul>
37 * <li>{@link Constants#FHIR_REQUEST_SIZE_ONLY}: if this entry is present, the requester expects only
38 * the result size to be returned in an parameter entry with the same name and calls {@link #handleSizeRequest(Object, Map)}
39 * to do so. If possible, implementations should only request the result size from the backend rather than
40 * a complete result set.</li>
41 * <li>{@link Constants#FHIR_FROM_INDEX} and {@link Constants#FHIR_TO_INDEX}: if these entries are present,
42 * the requester expects only the specified subset of the result. If possible, implementations
43 * should only request this result set from the backend rather than a complete result set.</li>
44 * </ul>
45 * </p>
46 *
47 * @author Christian Ohr
48 * @since 3.1
49 */
50
51 public interface RequestConsumer {
52
53 FhirContext getFhirContext();
54
55 /**
56 * Handles a Create / Update / Validate / Delete action request.
57 *
58 * @param payload request payload
59 * @param headers request parameters, e.g. search parameters
60 * @return result of the action execution
61 */
62 MethodOutcome handleAction(Object payload, Map<String, Object> headers);
63
64 /**
65 * Handles the request for a resource
66 *
67 * @param payload request payload
68 * @param headers request parameters, e.g. search parameters
69 * @param resultType type of the returned resource
70 * @param <R> type of the returned resource
71 * @return resource to be returned
72 */
73 <R extends IBaseResource> R handleResourceRequest(Object payload, Map<String, Object> headers, Class<R> resultType);
74
75 /**
76 * Handles the (search) request for a bundle, effectively being a list of resources.
77 * <p>
78 * If {@link #supportsLazyLoading()}
79 * returns true, the headers may contain {@link Constants#FHIR_FROM_INDEX} and {@link Constants#FHIR_TO_INDEX} entries,
80 * indicating that only a part of the result is requested. Implementations must return only the requested entries.
81 * </p>
82 *
83 * @param payload request payload
84 * @param headers request parameters, e.g. search parameters or
85 * @param <R> type of the returned resources contained in the bundle
86 * @return list of resources to be returned
87 */
88 <R extends IBaseResource> List<R> handleBundleRequest(Object payload, Map<String, Object> headers);
89
90 /**
91 * Handles the request for a bundle provider, effectively constructing a list of resources. The returned
92 * IBundleProvider takes over the responsibility to fetch the required subset of the result, usually
93 * by indirectly calling {@link #handleBundleRequest(Object, Map)} as required.
94 *
95 * @param payload request payload
96 * @param headers request parameters, e.g. search parameters
97 * @return a bundle provider
98 */
99 IBundleProvider handleBundleProviderRequest(Object payload, Map<String, Object> headers);
100
101 /**
102 * Handles transaction requests
103 *
104 * @param payload request payload
105 * @param headers request parameters
106 * @return transaction response bundle
107 */
108 <T extends IBaseBundle> T handleTransactionRequest(Object payload, Map<String, Object> headers, Class<T> bundleClass);
109
110 /**
111 * Optional method that request the result size of a bundle request. Only used for lazy
112 * bundle providers. The headers contain a {@link Constants#FHIR_REQUEST_SIZE_ONLY} entry flag.
113 * This method only needs to be implemented is {@link #supportsLazyLoading()} returns true.
114 *
115 * @param payload request payload
116 * @param headers request parameters
117 * @return transaction response bundle
118 */
119 int handleSizeRequest(Object payload, Map<String, Object> headers);
120
121 /**
122 * @return returns true indicating that lazy loading of search results is supported, false otherwise.
123 */
124 boolean supportsLazyLoading();
125
126 default String getName() {
127 return getClass().getName();
128 }
129 }