blob: 8e7169c775a758e4fed4e073f32beb1b3c325691 [file] [log] [blame]
Michael Hanlca740d72015-06-16 10:04:58 +02001package de.ids_mannheim.korap.auditing;
2
3import com.fasterxml.jackson.annotation.JsonIgnore;
4import com.fasterxml.jackson.databind.JsonNode;
5import de.ids_mannheim.korap.utils.JsonUtils;
6import de.ids_mannheim.korap.utils.TimeUtils;
7import lombok.Getter;
8import lombok.Setter;
9
10import java.util.Arrays;
Michael Hanl0f6ffd72015-08-27 19:23:15 +020011import java.util.Date;
Michael Hanlca740d72015-06-16 10:04:58 +020012
13/**
14 * @author hanl
15 * <p/>
16 * Record holder for auditing requests. Holds the data until it can be persisted to a database
17 */
18@Getter
19@Setter
20public class AuditRecord {
21
Michael Hanl0f6ffd72015-08-27 19:23:15 +020022 // todo: handle via status codes
Michael Hanlca740d72015-06-16 10:04:58 +020023 @Deprecated
24 public enum Operation {
25 GET, INSERT, UPDATE, DELETE, CREATE
26 }
27
28 public enum CATEGORY {
29 SECURITY, DATABASE, RESOURCE, QUERY, SERVICE
30 }
31
32 @JsonIgnore
33 private Integer id;
34 //security access describes changes in user authorities and access control permissions of resources
35 private String userid;
36 private String target;
37
38 //fixme: replace with more specific error codes
39 private CATEGORY category;
40 private String loc;
41 private Long timestamp;
42 private Integer status = -1;
43 private String args;
44 private String field_1 = "None";
45
46 private AuditRecord() {
47 this.timestamp = TimeUtils.getNow().getMillis();
48 }
49
50 public AuditRecord(CATEGORY category) {
51 this();
52 this.category = category;
53 }
54
55 public AuditRecord(CATEGORY cat, Object userID, Integer status) {
56 this(cat);
57 this.status = status;
58 if (userID != null) {
59 //todo: client info!
60 // this.loc = clientInfoToString(user.getTokenContext().getHostAddress(),
61 // user.getTokenContext().getUserAgent());
62 this.loc = clientInfoToString("null", "null");
63 userid = String.valueOf(userID);
64 }else {
65 this.loc = clientInfoToString("null", "null");
66 userid = "-1";
67 }
68 }
69
70 public static AuditRecord serviceRecord(Object user, Integer status,
71 String... args) {
72 AuditRecord r = new AuditRecord(CATEGORY.SERVICE);
73 r.setArgs(Arrays.asList(args).toString());
74 r.setUserid(String.valueOf(user));
75 r.setStatus(status);
76 return r;
77 }
78
79 public static AuditRecord dbRecord(Object user, Integer status,
80 String... args) {
81 AuditRecord r = new AuditRecord(CATEGORY.DATABASE);
82 r.setArgs(Arrays.asList(args).toString());
83 r.setUserid(String.valueOf(user));
84 r.setStatus(status);
85 return r;
86 }
87
88 public AuditRecord fromJson(String json) {
89 JsonNode n = JsonUtils.readTree(json);
90 AuditRecord r = new AuditRecord();
91 r.setCategory(CATEGORY.valueOf(n.path("category").asText()));
92 r.setTarget(n.path("target").asText());
93 r.setField_1(n.path("field_1").asText());
94 r.setUserid(n.path("account").asText());
95 r.setStatus(n.path("status").asInt());
96 r.setLoc(n.path("loc").asText());
97 return r;
98 }
99
100 private String clientInfoToString(String IP, String userAgent) {
101 return userAgent + "@" + IP;
102 }
103
Michael Hanl0f6ffd72015-08-27 19:23:15 +0200104 // fixme: add id, useragent
Michael Hanlca740d72015-06-16 10:04:58 +0200105 @Override
106 public String toString() {
Michael Hanl0f6ffd72015-08-27 19:23:15 +0200107 StringBuilder b = new StringBuilder();
108 b.append(category.toString().toLowerCase() + " audit : ")
109 .append(userid + "@" + new Date(timestamp)).append("\n")
110 .append("Status " + status).append("; ");
111
112 if (this.args != null)
113 b.append("Args " + field_1).append("; ");
114 if (this.loc != null)
115 b.append("Location " + loc).append("; ");
116 return b.toString();
Michael Hanlca740d72015-06-16 10:04:58 +0200117 }
118
119 @Override
120 public boolean equals(Object o) {
121 if (this == o)
122 return true;
123 if (o == null || getClass() != o.getClass())
124 return false;
125
126 AuditRecord that = (AuditRecord) o;
127
128 if (userid != null ? !userid.equals(that.userid) : that.userid != null)
129 return false;
130 if (category != that.category)
131 return false;
132 if (status != null ? !status.equals(that.status) : that.status != null)
133 return false;
134 if (field_1 != null ?
135 !field_1.equals(that.field_1) :
136 that.field_1 != null)
137 return false;
138 if (loc != null ? !loc.equals(that.loc) : that.loc != null)
139 return false;
140 if (target != null ? !target.equals(that.target) : that.target != null)
141 return false;
142 if (timestamp != null ?
143 !timestamp.equals(that.timestamp) :
144 that.timestamp != null)
145 return false;
146
147 return true;
148 }
149
150 @Override
151 public int hashCode() {
152 int result = userid != null ? userid.hashCode() : 0;
153 result = 31 * result + (target != null ? target.hashCode() : 0);
154 result = 31 * result + category.hashCode();
155 result = 31 * result + (loc != null ? loc.hashCode() : 0);
156 result = 31 * result + (timestamp != null ? timestamp.hashCode() : 0);
157 result = 31 * result + (status != null ? status.hashCode() : 0);
158 result = 31 * result + (field_1 != null ? field_1.hashCode() : 0);
159 return result;
160 }
161}