This project has retired. For details please refer to its Attic page.
AuditEvent xref
View Javadoc
1   package org.apache.archiva.metadata.model.facets;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.archiva.metadata.model.MetadataFacet;
23  
24  import java.text.ParseException;
25  import java.text.SimpleDateFormat;
26  import java.util.Calendar;
27  import java.util.Date;
28  import java.util.HashMap;
29  import java.util.Map;
30  import java.util.TimeZone;
31  
32  /**
33   * AuditEvent
34   *
35   *
36   */
37  public class AuditEvent
38      implements MetadataFacet
39  {
40      public static final String TIMESTAMP_FORMAT = "yyyy/MM/dd/HHmmss.SSS";
41  
42      public static final String CREATE_DIR = "Created Directory";
43  
44      public static final String CREATE_FILE = "Created File";
45  
46      public static final String REMOVE_DIR = "Removed Directory";
47  
48      public static final String REMOVE_FILE = "Removed File";
49  
50      public static final String MODIFY_FILE = "Modified File";
51  
52      public static final String MOVE_FILE = "Moved File";
53  
54      public static final String MOVE_DIRECTORY = "Moved Directory";
55  
56      public static final String COPY_DIRECTORY = "Copied Directory";
57  
58      public static final String COPY_FILE = "Copied File";
59  
60      public static final String UPLOAD_FILE = "Uploaded File";
61  
62      public static final String ADD_LEGACY_PATH = "Added Legacy Artifact Path";
63  
64      public static final String REMOVE_LEGACY_PATH = "Removed Legacy Artifact Path";
65  
66      public static final String PURGE_ARTIFACT = "Purged Artifact";
67  
68      public static final String PURGE_FILE = "Purged Support File";
69  
70      public static final String REMOVE_SCANNED = "Removed in Filesystem";
71  
72      public static final String MERGING_REPOSITORIES = "Merged Artifact";
73  
74      // configuration events
75  
76      public static final String ADD_MANAGED_REPO = "Added Managed Repository";
77  
78      public static final String MODIFY_MANAGED_REPO = "Updated Managed Repository";
79  
80      public static final String DELETE_MANAGED_REPO = "Deleted Managed Repository";
81  
82      public static final String ADD_REMOTE_REPO = "Added Remote Repository";
83  
84      public static final String MODIFY_REMOTE_REPO = "Updated Remote Repository";
85  
86      public static final String DELETE_REMOTE_REPO = "Deleted Remote Repository";
87  
88      public static final String ADD_REPO_GROUP = "Added Repository Group";
89  
90      public static final String DELETE_REPO_GROUP = "Deleted Repository Group";
91  
92      public static final String MODIFY_REPO_GROUP = "Modify Repository Group";
93  
94      public static final String ADD_REPO_TO_GROUP = "Added Repository to Group";
95  
96      public static final String DELETE_REPO_FROM_GROUP = "Deleted Repository from Group";
97  
98      public static final String ENABLE_REPO_CONSUMER = "Enabled Content Consumer";
99  
100     public static final String DISABLE_REPO_CONSUMER = "Disabled Content Consumer";
101 
102     public static final String ADD_PATTERN = "Added File Type Pattern";
103 
104     public static final String REMOVE_PATTERN = "Removed File Type Pattern";
105 
106     public static final String MERGE_REPO_REMOTE = "Merged Staging Repository Triggered Remotely";
107 
108     public static final String ADD_PROXY_CONNECTOR = "Added Proxy Connector";
109 
110     public static final String DELETE_PROXY_CONNECTOR = "Deleted Proxy Connector";
111 
112     public static final String MODIFY_PROXY_CONNECTOR = "Updated Proxy Connector";
113 
114     public static final String ADD_NETWORK_PROXY = "Added Network Proxy";
115 
116     public static final String DELETE_NETWORK_PROXY = "Deleted Network Proxy";
117 
118     public static final String MODIFY_NETWORK_PROXY = "Updated Network Proxy";
119     
120     private String repositoryId;
121 
122     private String userId;
123 
124     private String remoteIP;
125 
126     // TODO: change to artifact reference? does it ever refer to just a path?
127 
128     private String resource;
129 
130     private String action;
131 
132     private Date timestamp;
133 
134     public static final String FACET_ID = "org.apache.archiva.audit";
135 
136     private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
137 
138     private static final int TS_LENGTH = TIMESTAMP_FORMAT.length();
139 
140     public AuditEvent()
141     {
142         /* do nothing */
143     }
144 
145     public AuditEvent( String name, String repositoryId )
146     {
147         String ts = name.substring( 0, TS_LENGTH );
148         try
149         {
150             timestamp = createNameFormat().parse( ts );
151         }
152         catch ( ParseException e )
153         {
154             throw new IllegalArgumentException( "Improperly formatted timestamp for audit log event: " + ts, e);
155         }
156 
157         if ( name.length() > TS_LENGTH )
158         {
159             if ( name.charAt( TS_LENGTH ) != '/' )
160             {
161                 throw new IllegalArgumentException(
162                     "Improperly formatted name for audit log event, no / separator between timestamp and resource: " +
163                         name );
164             }
165         }
166 
167         this.repositoryId = repositoryId;
168     }
169 
170     public AuditEvent( String repoId, String user, String resource, String action )
171     {
172         this.repositoryId = repoId;
173         this.userId = user;
174         this.resource = resource;
175         this.action = action;
176         this.timestamp = Calendar.getInstance().getTime();
177     }
178 
179     public String getRepositoryId()
180     {
181         return repositoryId;
182     }
183 
184     public void setRepositoryId( String repositoryId )
185     {
186         this.repositoryId = repositoryId;
187     }
188 
189     public String getUserId()
190     {
191         return userId;
192     }
193 
194     public void setUserId( String userId )
195     {
196         this.userId = userId;
197     }
198 
199     public String getResource()
200     {
201         return resource;
202     }
203 
204     public void setResource( String resource )
205     {
206         this.resource = resource;
207     }
208 
209     public String getAction()
210     {
211         return action;
212     }
213 
214     public void setAction( String action )
215     {
216         this.action = action;
217     }
218 
219     public String getRemoteIP()
220     {
221         return remoteIP;
222     }
223 
224     public void setRemoteIP( String remoteIP )
225     {
226         this.remoteIP = remoteIP;
227     }
228 
229     public Date getTimestamp()
230     {
231         return timestamp;
232     }
233 
234     public void setTimestamp( Date timestamp )
235     {
236         this.timestamp = timestamp;
237     }
238 
239     @Override
240     public String getFacetId()
241     {
242         return FACET_ID;
243     }
244 
245     @Override
246     public String getName()
247     {
248         // use the hashCode here to make it unique if multiple events occur at a certain timestamp. None of the other
249         // fields is unique on its own
250         return createNameFormat().format( timestamp ) + "/" + Integer.toHexString( hashCode() );
251         // TODO: a simple incremental counter might be better since it will retain ordering, but then we need to do a
252         //  bit of locking...
253     }
254 
255     private static SimpleDateFormat createNameFormat()
256     {
257         SimpleDateFormat fmt = new SimpleDateFormat( TIMESTAMP_FORMAT );
258         fmt.setTimeZone( UTC_TIME_ZONE );
259         return fmt;
260     }
261 
262     @Override
263     public Map<String, String> toProperties()
264     {
265         Map<String, String> properties = new HashMap<>();
266         properties.put( "action", this.action );
267         if ( this.userId != null )
268         {
269             properties.put( "user", this.userId );
270         }
271         if ( this.remoteIP != null )
272         {
273             properties.put( "remoteIP", this.remoteIP );
274         }
275         if ( this.resource != null )
276         {
277             properties.put( "resource", this.resource );
278         }
279         return properties;
280     }
281 
282     @Override
283     public void fromProperties( Map<String, String> properties )
284     {
285         this.action = properties.get( "action" );
286         this.remoteIP = properties.get( "remoteIP" );
287         this.userId = properties.get( "user" );
288         this.resource = properties.get( "resource" );
289     }
290 
291     @Override
292     public boolean equals( Object o )
293     {
294         if ( this == o )
295         {
296             return true;
297         }
298         if ( o == null || getClass() != o.getClass() )
299         {
300             return false;
301         }
302 
303         AuditEvent/../../../../../org/apache/archiva/metadata/model/facets/AuditEvent.html#AuditEvent">AuditEvent that = (AuditEvent) o;
304 
305         if ( !action.equals( that.action ) )
306         {
307             return false;
308         }
309         if ( remoteIP != null ? !remoteIP.equals( that.remoteIP ) : that.remoteIP != null )
310         {
311             return false;
312         }
313         if ( repositoryId != null ? !repositoryId.equals( that.repositoryId ) : that.repositoryId != null )
314         {
315             return false;
316         }
317         if ( resource != null ? !resource.equals( that.resource ) : that.resource != null )
318         {
319             return false;
320         }
321         if ( !timestamp.equals( that.timestamp ) )
322         {
323             return false;
324         }
325         if ( userId != null ? !userId.equals( that.userId ) : that.userId != null )
326         {
327             return false;
328         }
329 
330         return true;
331     }
332 
333     @Override
334     public int hashCode()
335     {
336         int result = repositoryId != null ? repositoryId.hashCode() : 0;
337         result = 31 * result + ( userId != null ? userId.hashCode() : 0 );
338         result = 31 * result + ( remoteIP != null ? remoteIP.hashCode() : 0 );
339         result = 31 * result + ( resource != null ? resource.hashCode() : 0 );
340         result = 31 * result + action.hashCode();
341         result = 31 * result + timestamp.hashCode();
342         return result;
343     }
344 
345     @Override
346     public String toString()
347     {
348         final StringBuilder sb = new StringBuilder();
349         sb.append( "AuditEvent" );
350         sb.append( "{repositoryId='" ).append( repositoryId ).append( '\'' );
351         sb.append( ", userId='" ).append( userId ).append( '\'' );
352         sb.append( ", remoteIP='" ).append( remoteIP ).append( '\'' );
353         sb.append( ", resource='" ).append( resource ).append( '\'' );
354         sb.append( ", action='" ).append( action ).append( '\'' );
355         sb.append( ", timestamp=" ).append( timestamp );
356         sb.append( '}' );
357         return sb.toString();
358     }
359 
360 
361 }