View Javadoc
1   /*
2    * Copyright 2018 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.support;
18  
19  import ca.uhn.fhir.rest.param.TokenParam;
20  import org.openehealth.ipf.commons.ihe.fhir.Constants;
21  import org.openehealth.ipf.commons.ihe.fhir.FhirSearchParameters;
22  import org.openehealth.ipf.commons.ihe.fhir.audit.FhirQueryAuditDataset;
23  
24  import java.io.UnsupportedEncodingException;
25  import java.net.URLDecoder;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.stream.Collectors;
29  
30  import static org.openehealth.ipf.commons.ihe.fhir.Constants.HTTP_QUERY;
31  import static org.openehealth.ipf.commons.ihe.fhir.Constants.HTTP_URL;
32  
33  /**
34   * Generic Audit Strategy for FHIR query transactions
35   *
36   * @author Christian Ohr
37   * @since 3.4
38   */
39  public abstract class FhirQueryAuditStrategy extends FhirAuditStrategy<FhirQueryAuditDataset> {
40  
41      protected FhirQueryAuditStrategy(boolean serverSide) {
42          super(serverSide);
43      }
44  
45      /**
46       * Further enrich the audit dataset: add query string and patient IDs in the search parameter
47       * (if available).
48       *
49       * @param auditDataset audit dataset
50       * @param request      request object
51       * @param parameters   request parameters
52       * @return enriched audit dataset
53       */
54      @Override
55      public FhirQueryAuditDataset enrichAuditDatasetFromRequest(FhirQueryAuditDataset auditDataset, Object request, Map<String, Object> parameters) {
56          FhirQueryAuditDataset dataset = super.enrichAuditDatasetFromRequest(auditDataset, request, parameters);
57  
58          String url = (String) parameters.get(HTTP_URL);
59          String query = (String) parameters.get(HTTP_QUERY);
60          try {
61              dataset.setQueryString(URLDecoder.decode(String.format("%s?%s", url, query), "UTF-8"));
62          } catch (UnsupportedEncodingException ignored) {
63              // should never occur
64          }
65  
66          FhirSearchParameters searchParameter = (FhirSearchParameters) parameters.get(Constants.FHIR_REQUEST_PARAMETERS);
67          if (searchParameter != null) {
68              List<TokenParam> tokenParams = searchParameter.getPatientIdParam();
69              if (tokenParams != null) {
70                  dataset.getPatientIds().addAll(
71                          tokenParams.stream()
72                                  .map(t -> t.getValueAsQueryToken(searchParameter.getFhirContext()))
73                                  .collect(Collectors.toList()));
74              }
75          }
76  
77          return dataset;
78      }
79  
80      @Override
81      public FhirQueryAuditDataset createAuditDataset() {
82          return new FhirQueryAuditDataset(isServerSide());
83      }
84  }