This project has retired. For details please refer to its Attic page.
DefaultAuditManager xref
View Javadoc
1   package org.apache.archiva.audit;
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.facets.AuditEvent;
23  import org.apache.archiva.metadata.repository.MetadataRepository;
24  import org.apache.archiva.metadata.repository.MetadataRepositoryException;
25  import org.apache.archiva.metadata.repository.RepositorySession;
26  import org.apache.archiva.metadata.repository.RepositorySessionFactory;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  import org.springframework.stereotype.Service;
30  
31  import javax.inject.Inject;
32  import java.text.ParseException;
33  import java.text.SimpleDateFormat;
34  import java.util.ArrayList;
35  import java.util.Collection;
36  import java.util.Collections;
37  import java.util.Comparator;
38  import java.util.Date;
39  import java.util.List;
40  import java.util.TimeZone;
41  
42  /**
43   *
44   */
45  @Service("auditManager#default")
46  public class DefaultAuditManager
47      implements AuditManager
48  {
49      private static final int NUM_RECENT_EVENTS = 10;
50  
51      private static final Logger log = LoggerFactory.getLogger( DefaultAuditManager.class );
52  
53      private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
54  
55      @Inject
56      RepositorySessionFactory repositorySessionFactory;
57  
58      @Override
59      public List<AuditEvent> getMostRecentAuditEvents( MetadataRepository metadataRepository,
60                                                        List<String> repositoryIds )
61          throws MetadataRepositoryException
62      {
63          try(RepositorySession session = repositorySessionFactory.createSession()) {
64              // TODO: consider a more efficient implementation that directly gets the last ten from the content repository
65              List<AuditRecord> records = new ArrayList<>();
66              for (String repositoryId : repositoryIds) {
67                  List<String> names = metadataRepository.getMetadataFacets(session, repositoryId, AuditEvent.FACET_ID);
68                  for (String name : names) {
69                      records.add(new AuditRecord(repositoryId, name));
70                  }
71              }
72              Collections.sort(records);
73              records = records.subList(0, records.size() < NUM_RECENT_EVENTS ? records.size() : NUM_RECENT_EVENTS);
74  
75              List<AuditEvent> events = new ArrayList<>(records.size());
76              for (AuditRecord record : records) {
77                  AuditEvent/../org/apache/archiva/metadata/model/facets/AuditEvent.html#AuditEvent">AuditEvent auditEvent = (AuditEvent) metadataRepository.getMetadataFacet(session,
78                          record.repositoryId,
79                          AuditEvent.FACET_ID, record.name);
80                  events.add(auditEvent);
81              }
82              return events;
83          }
84      }
85  
86      @Override
87      public void addAuditEvent( MetadataRepository repository, AuditEvent event )
88          throws MetadataRepositoryException
89      {
90          try(RepositorySession session = repositorySessionFactory.createSession()) {
91              // ignore those with no repository - they will still be logged to the textual audit log
92              if (event.getRepositoryId() != null) {
93                  repository.addMetadataFacet(session, event.getRepositoryId(), event);
94              }
95          }
96      }
97  
98      @Override
99      public void deleteAuditEvents( MetadataRepository metadataRepository, String repositoryId )
100         throws MetadataRepositoryException
101     {
102         try(RepositorySession session = repositorySessionFactory.createSession()) {
103             metadataRepository.removeMetadataFacets(session, repositoryId, AuditEvent.FACET_ID);
104         }
105     }
106 
107     @Override
108     public List<AuditEvent> getAuditEventsInRange( MetadataRepository metadataRepository,
109                                                    Collection<String> repositoryIds, Date startTime, Date endTime )
110         throws MetadataRepositoryException
111     {
112         return getAuditEventsInRange( metadataRepository, repositoryIds, null, startTime, endTime );
113     }
114 
115     @Override
116     public List<AuditEvent> getAuditEventsInRange( MetadataRepository metadataRepository,
117                                                    Collection<String> repositoryIds, String resource, Date startTime,
118                                                    Date endTime )
119         throws MetadataRepositoryException
120     {
121         try(RepositorySession session = repositorySessionFactory.createSession()) {
122             List<AuditEvent> results = new ArrayList<>();
123             for (String repositoryId : repositoryIds) {
124                 List<String> list = metadataRepository.getMetadataFacets(session, repositoryId, AuditEvent.FACET_ID);
125                 for (String name : list) {
126                     try {
127                         Date date = createNameFormat().parse(name);
128                         if ((startTime == null || !date.before(startTime)) && (endTime == null || !date.after(
129                                 endTime))) {
130                             AuditEvent../../../org/apache/archiva/metadata/model/facets/AuditEvent.html#AuditEvent">AuditEvent event = (AuditEvent) metadataRepository.getMetadataFacet(session,
131                                     repositoryId,
132                                     AuditEvent.FACET_ID, name);
133 
134                             if (resource == null || event.getResource().startsWith(resource)) {
135                                 results.add(event);
136                             }
137                         }
138                     } catch (ParseException e) {
139                         log.error("Invalid audit event found in the metadata repository: {}", e.getMessage());
140                         // continue and ignore this one
141                     }
142                 }
143             }
144             Collections.sort(results, new Comparator<AuditEvent>() {
145                 @Override
146                 public int compare(AuditEvent="../../../../org/apache/archiva/metadata/model/facets/AuditEvent.html#AuditEvent">AuditEvent o1, AuditEvent o2) {
147                     return o2.getTimestamp().compareTo(o1.getTimestamp());
148                 }
149             });
150             return results;
151         }
152     }
153 
154     private static SimpleDateFormat createNameFormat()
155     {
156         SimpleDateFormat fmt = new SimpleDateFormat( AuditEvent.TIMESTAMP_FORMAT );
157         fmt.setTimeZone( UTC_TIME_ZONE );
158         return fmt;
159     }
160 
161     private static final class AuditRecord
162         implements Comparable<AuditRecord>
163     {
164         private String repositoryId;
165 
166         private String name;
167 
168         public AuditRecord( String repositoryId, String name )
169         {
170             this.repositoryId = repositoryId;
171             this.name = name;
172         }
173 
174         @Override
175         public int compareTo( AuditRecord other )
176         {
177             // reverse ordering
178             return other.name.compareTo( name );
179         }
180     }
181 
182     public RepositorySessionFactory getRepositorySessionFactory( )
183     {
184         return repositorySessionFactory;
185     }
186 
187     public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
188     {
189         this.repositorySessionFactory = repositorySessionFactory;
190     }
191 }