Line data Source code
1 : // Copyright (C) 2012 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server; 16 : 17 : import static java.util.Objects.requireNonNull; 18 : 19 : import com.google.auto.value.AutoValue; 20 : import com.google.common.base.MoreObjects; 21 : import com.google.common.collect.ImmutableListMultimap; 22 : import com.google.common.collect.ListMultimap; 23 : import com.google.gerrit.server.util.time.TimeUtil; 24 : 25 : public class AuditEvent { 26 : 27 : public static final String UNKNOWN_SESSION_ID = "000000000000000000000000000"; 28 47 : protected static final ImmutableListMultimap<String, ?> EMPTY_PARAMS = ImmutableListMultimap.of(); 29 : 30 : public final String sessionId; 31 : public final CurrentUser who; 32 : public final long when; 33 : public final String what; 34 : public final ListMultimap<String, ?> params; 35 : public final Object result; 36 : public final long timeAtStart; 37 : public final long elapsed; 38 : public final UUID uuid; 39 : 40 : @AutoValue 41 47 : public abstract static class UUID { 42 : private static UUID create() { 43 47 : return new AutoValue_AuditEvent_UUID( 44 47 : String.format("audit:%s", java.util.UUID.randomUUID().toString())); 45 : } 46 : 47 : public abstract String uuid(); 48 : } 49 : 50 : /** 51 : * Creates a new audit event with results 52 : * 53 : * @param sessionId session id the event belongs to 54 : * @param who principal that has generated the event 55 : * @param what object of the event 56 : * @param when time-stamp of when the event started 57 : * @param params parameters of the event 58 : * @param result result of the event 59 : */ 60 : public AuditEvent( 61 : String sessionId, 62 : CurrentUser who, 63 : String what, 64 : long when, 65 : ListMultimap<String, ?> params, 66 47 : Object result) { 67 47 : requireNonNull(what, "what is a mandatory not null param !"); 68 : 69 47 : this.sessionId = MoreObjects.firstNonNull(sessionId, UNKNOWN_SESSION_ID); 70 47 : this.who = who; 71 47 : this.what = what; 72 47 : this.when = when; 73 47 : this.timeAtStart = this.when; 74 47 : this.params = MoreObjects.firstNonNull(params, EMPTY_PARAMS); 75 47 : this.uuid = UUID.create(); 76 47 : this.result = result; 77 47 : this.elapsed = TimeUtil.nowMs() - timeAtStart; 78 47 : } 79 : 80 : @Override 81 : public int hashCode() { 82 0 : return uuid.hashCode(); 83 : } 84 : 85 : // This is a value class that allows adding attributes by subclassing. 86 : // Doing this is discouraged and using composition rather than inheritance to add fields to value 87 : // types is preferred. However this class is part of the plugin API (used in the AuditListener 88 : // extension point), hence we cannot change it without breaking plugins. Hence suppress the 89 : // EqualsGetClass warning here. 90 : @SuppressWarnings("EqualsGetClass") 91 : @Override 92 : public boolean equals(Object obj) { 93 0 : if (this == obj) { 94 0 : return true; 95 : } 96 0 : if (obj == null) { 97 0 : return false; 98 : } 99 0 : if (getClass() != obj.getClass()) { 100 0 : return false; 101 : } 102 : 103 0 : AuditEvent other = (AuditEvent) obj; 104 0 : return this.uuid.equals(other.uuid); 105 : } 106 : 107 : @Override 108 : public String toString() { 109 0 : return String.format( 110 : "AuditEvent UUID:%s, SID:%s, TS:%d, who:%s, what:%s", 111 0 : uuid.uuid(), sessionId, when, who, what); 112 : } 113 : }