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.audit;
18  
19  import org.hl7.fhir.instance.model.api.IBaseOperationOutcome;
20  import org.hl7.fhir.instance.model.api.IBaseResource;
21  import org.openehealth.ipf.commons.audit.AuditContext;
22  import org.openehealth.ipf.commons.audit.codes.EventOutcomeIndicator;
23  import org.openehealth.ipf.commons.ihe.core.atna.AuditStrategySupport;
24  
25  import java.util.Map;
26  
27  import static org.openehealth.ipf.commons.ihe.fhir.Constants.*;
28  
29  /**
30   * Generic Audit Strategy for FHIR transactions
31   *
32   * @author Christian Ohr
33   * @since 3.2
34   */
35  public abstract class AbstractFhirAuditStrategy<T extends FhirAuditDataset, O extends IBaseOperationOutcome> extends AuditStrategySupport<T> {
36  
37  
38      protected AbstractFhirAuditStrategy(boolean serverSide) {
39          super(serverSide);
40      }
41  
42      @Override
43      public T enrichAuditDatasetFromRequest(T auditDataset, Object request, Map<String, Object> parameters) {
44  
45          if (parameters.get(HTTP_URI) != null) {
46              auditDataset.setSourceUserId((String) parameters.get(HTTP_URI));
47          }
48          if (parameters.get(HTTP_URL) != null) {
49              auditDataset.setDestinationUserId((String) parameters.get(HTTP_URL));
50          }
51          if (parameters.get(HTTP_CLIENT_IP_ADDRESS) != null) {
52              auditDataset.setRemoteAddress((String) parameters.get(HTTP_CLIENT_IP_ADDRESS));
53          }
54          return auditDataset;
55      }
56  
57      @Override
58      public boolean enrichAuditDatasetFromResponse(T auditDataset, Object response, AuditContext auditContext) {
59          if (response instanceof IBaseResource) {
60              EventOutcomeIndicator eventOutcomeIndicator = getEventOutcomeIndicator(response);
61              auditDataset.setEventOutcomeIndicator(eventOutcomeIndicator);
62              auditDataset.setEventOutcomeDescription(getEventOutcomeDescription(response));
63              return eventOutcomeIndicator == EventOutcomeIndicator.Success;
64          }
65          return true;
66      }
67  
68  
69      @Override
70      public EventOutcomeIndicator getEventOutcomeIndicator(Object response) {
71          return getEventOutcomeCodeFromResource((IBaseResource) response);
72      }
73  
74      /**
75       * A resource is returned from the business logic. This may usually success unless it's OperationOutcome
76       *
77       * @param resource FHIR resource
78       * @return event outcome code
79       */
80      protected EventOutcomeIndicator getEventOutcomeCodeFromResource(IBaseResource resource) {
81          return resource instanceof IBaseOperationOutcome ?
82                  getEventOutcomeCodeFromOperationOutcome((O)resource) :
83                  EventOutcomeIndicator.Success;
84      }
85  
86      @Override
87      public String getEventOutcomeDescription(Object response) {
88          return response instanceof IBaseOperationOutcome ?
89                  getEventOutcomeDescriptionFromOperationOutcome((O)response) :
90                  null;
91      }
92  
93  
94      /**
95       * Operation Outcomes are sets of error, warning and information messages that provide detailed information
96       * about the outcome of some attempted system operation. They are provided as a direct system response,
97       * or component of one, where they provide information about the outcome of the operation.
98       *
99       * @param response {@link IBaseOperationOutcome} to be analyzed
100      * @return ATNA outcome code
101      */
102     public abstract EventOutcomeIndicator getEventOutcomeCodeFromOperationOutcome(O response);
103 
104     public abstract String getEventOutcomeDescriptionFromOperationOutcome(O response);
105 
106 }