View Javadoc
1   /*
2    * Copyright 2009 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  package org.openehealth.ipf.commons.ihe.core.atna;
17  
18  import lombok.AllArgsConstructor;
19  import lombok.Getter;
20  import lombok.NoArgsConstructor;
21  import lombok.Setter;
22  import org.apache.commons.lang3.StringUtils;
23  import org.openehealth.ipf.commons.audit.codes.EventOutcomeIndicator;
24  import org.openehealth.ipf.commons.audit.types.ActiveParticipantRoleId;
25  import org.openehealth.ipf.commons.audit.utils.AuditUtils;
26  
27  import java.io.Serializable;
28  import java.util.ArrayList;
29  import java.util.Arrays;
30  import java.util.Collection;
31  import java.util.List;
32  
33  /**
34   * A generic data structure used to store information pieces needed for auditing.
35   *
36   * @author Dmytro Rud
37   */
38  public abstract class AuditDataset implements Serializable {
39  
40      @NoArgsConstructor
41      public static class HumanUser {
42          /** ID, preferably in the format defined in the IHE XUA profile */
43          @Getter @Setter private String id;
44  
45          /** Real-world name */
46          @Getter @Setter private String name;
47  
48          /** Role codes */
49          @Getter private final List<ActiveParticipantRoleId> roles = new ArrayList<>();
50  
51          public HumanUser(String id, String name, Collection<ActiveParticipantRoleId> roles) {
52              this.id = id;
53              this.name = name;
54              this.roles.addAll(roles);
55          }
56  
57          public boolean isEmpty() {
58              return StringUtils.isAllBlank(id, name);
59          }
60      }
61  
62  
63      /**
64       * whether we audit on server (true) or on client (false)
65       */
66      @Getter
67      private final boolean serverSide;
68  
69      /**
70       * Overall outcome of the transaction that causes this audit event
71       */
72      @Getter
73      @Setter
74      private EventOutcomeIndicator eventOutcomeIndicator;
75  
76      /**
77       * Description of the overall outcome of the transaction that causes this audit event
78       */
79      @Getter
80      @Setter
81      private String eventOutcomeDescription;
82  
83      /**
84       * Source User Name, e.g. extracted from a client certificate
85       */
86      @Getter
87      @Setter
88      String sourceUserName;
89  
90      /**
91       * @param serverSide   specifies whether this audit dataset will be used on the
92       *                     server side (<code>true</code>) or on the client side
93       *                     (<code>false</code>)
94       */
95      public AuditDataset(boolean serverSide) {
96          this.serverSide = serverSide;
97      }
98  
99      @Override
100     public String toString() {
101         return "AuditDataset{" +
102                 "serverSide=" + serverSide +
103                 ", eventOutcomeIndicator=" + eventOutcomeIndicator +
104                 ", eventOutcomeDescription='" + eventOutcomeDescription + '\'' +
105                 ", sourceUserName='" + sourceUserName + '\'' +
106                 '}';
107     }
108 
109     /**
110      * @return the user ID of the transaction source
111      */
112     public abstract String getSourceUserId();
113 
114     /**
115      * @return the user ID of the transaction destination
116      */
117     public abstract String getDestinationUserId();
118 
119     /**
120      * Returns the local address. May fall back to {@link AuditUtils#getLocalIPAddress()} if not explicitly set
121      *
122      * @return the local address
123      */
124     public abstract String getLocalAddress();
125 
126     /**
127      * @return the remote address of the transaction
128      */
129     public abstract String getRemoteAddress();
130 
131     /**
132      * @return information about human user(s) participating in the transaction
133      */
134     public abstract List<HumanUser> getHumanUsers();
135 
136     /**
137      * @return true if the source user is the requestor of the event
138      */
139     public boolean isSourceUserIsRequestor() {
140         return getHumanUsers().isEmpty();
141     }
142 
143     /**
144      * @return true if the destination user is the requestor of the event
145      */
146     public boolean isDestinationUserIsRequestor() {
147         return false;
148     }
149 }