]> source.dussan.org Git - archiva.git/commitdiff
[MRM-1269] restore pseudo-search by artifact in audit logs
authorBrett Porter <brett@apache.org>
Tue, 19 Jan 2010 11:21:10 +0000 (11:21 +0000)
committerBrett Porter <brett@apache.org>
Tue, 19 Jan 2010 11:21:10 +0000 (11:21 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/branches/MRM-1025@900726 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/reports/ViewAuditLogReportAction.java
archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/AuditManager.java
archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/DefaultAuditManager.java
archiva-modules/plugins/audit/src/test/java/org/apache/archiva/audit/AuditManagerTest.java

index 1b06ed1992e871938893e8b01e24ae6171138b31..26433e15efed5d18e8c00cfc402194c54680f66a 100644 (file)
@@ -44,7 +44,7 @@ import org.codehaus.redback.integration.interceptor.SecureActionException;
 
 /**
  * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="viewAuditLogReport"
- *                   instantiation-strategy="per-lookup"
+ * instantiation-strategy="per-lookup"
  */
 public class ViewAuditLogReportAction
     extends PlexusActionSupport
@@ -68,7 +68,7 @@ public class ViewAuditLogReportAction
     private String startDate;
 
     private String endDate;
-    
+
     private int rowCount = 30;
 
     private int page = 1;
@@ -84,18 +84,18 @@ public class ViewAuditLogReportAction
     private static final String ALL_REPOSITORIES = "all";
 
     protected int[] range = new int[2];
-    
+
     private String initial = "true";
-    
+
     private String headerName;
-    
+
     private static final String HEADER_LATEST_EVENTS = "Latest Events";
-    
+
     private static final String HEADER_RESULTS = "Results";
-    
-    private String[] datePatterns = new String[] { "MM/dd/yy", "MM/dd/yyyy", "MMMMM/dd/yyyy", "MMMMM/dd/yy", 
-        "dd MMMMM yyyy", "dd/MM/yy", "dd/MM/yyyy", "yyyy/MM/dd", "yyyy-MM-dd", "yyyy-dd-MM", "MM-dd-yyyy",
-        "MM-dd-yy" };
+
+    private String[] datePatterns =
+        new String[]{"MM/dd/yy", "MM/dd/yyyy", "MMMMM/dd/yyyy", "MMMMM/dd/yy", "dd MMMMM yyyy", "dd/MM/yy",
+            "dd/MM/yyyy", "yyyy/MM/dd", "yyyy-MM-dd", "yyyy-dd-MM", "MM-dd-yyyy", "MM-dd-yy"};
 
     /**
      * @plexus.requirement
@@ -119,7 +119,7 @@ public class ViewAuditLogReportAction
         this.request = request;
     }
 
-    @SuppressWarnings( "unchecked" )
+    @SuppressWarnings("unchecked")
     public void prepare()
         throws Exception
     {
@@ -132,8 +132,8 @@ public class ViewAuditLogReportAction
         groupId = "";
         artifactId = "";
         repository = "";
-                
-        if( Boolean.parseBoolean( initial ) )
+
+        if ( Boolean.parseBoolean( initial ) )
         {
             headerName = HEADER_LATEST_EVENTS;
         }
@@ -148,27 +148,14 @@ public class ViewAuditLogReportAction
     public String execute()
         throws Exception
     {
-        Date startDateInDF;
-        Date endDateInDF;
-        if ( startDate == null || "".equals( startDate ) )
-        {            
-            Calendar cal = Calendar.getInstance();
-            cal.set( Calendar.HOUR, 0 );
-            cal.set( Calendar.MINUTE, 0 );
-            cal.set( Calendar.SECOND, 0 );
-
-            startDateInDF = cal.getTime();
-        }
-        else
+        Date startDateInDF = null;
+        Date endDateInDF = null;
+        if ( !StringUtils.isEmpty( startDate ) )
         {
             startDateInDF = DateUtils.parseDate( startDate, datePatterns );
         }
 
-        if ( endDate == null || "".equals( endDate ) )
-        {
-            endDateInDF = Calendar.getInstance().getTime();
-        } 
-        else
+        if ( !StringUtils.isEmpty( endDate ) )
         {
             endDateInDF = DateUtils.parseDate( endDate, datePatterns );
             Calendar cal = Calendar.getInstance();
@@ -176,8 +163,8 @@ public class ViewAuditLogReportAction
             cal.set( Calendar.HOUR, 23 );
             cal.set( Calendar.MINUTE, 59 );
             cal.set( Calendar.SECOND, 59 );
-            
-            endDateInDF = cal.getTime();            
+
+            endDateInDF = cal.getTime();
         }
 
         range[0] = ( page - 1 ) * rowCount;
@@ -195,10 +182,30 @@ public class ViewAuditLogReportAction
                 repos = Collections.emptyList();
             }
         }
-        // TODO: query by artifact
-        auditLogs = auditManager.getAuditEventsInRange( repos, startDateInDF, endDateInDF );
 
-        if( auditLogs.isEmpty() )
+        if ( StringUtils.isEmpty( groupId ) && !StringUtils.isEmpty( artifactId ) )
+        {
+            // Until we store the full artifact metadata in the audit event, we can't query by these individually
+            addActionError( "If you specify an artifact ID, you must specify a group ID" );
+            return INPUT;
+        }
+
+        String resource = null;
+        if ( !StringUtils.isEmpty( groupId ) )
+        {
+            if ( StringUtils.isEmpty( artifactId ) )
+            {
+                resource = groupId;
+            }
+            else
+            {
+                resource = groupId + "/" + artifactId;
+            }
+        }
+
+        auditLogs = auditManager.getAuditEventsInRange( repos, resource, startDateInDF, endDateInDF );
+
+        if ( auditLogs.isEmpty() )
         {
             addActionError( "No audit logs found." );
             initial = "true";
@@ -213,7 +220,7 @@ public class ViewAuditLogReportAction
 
         return SUCCESS;
     }
-    
+
     private void paginate()
     {
         if ( auditLogs.size() <= rowCount )
@@ -221,21 +228,19 @@ public class ViewAuditLogReportAction
             isLastPage = true;
         }
         else
-        {   
+        {
             isLastPage = false;
             auditLogs.remove( rowCount );
         }
 
-        prev =
-            request.getRequestURL() + "?page=" + ( page - 1 ) + "&rowCount=" + rowCount + "&groupId=" + groupId +
-                "&artifactId=" + artifactId + "&repository=" + repository + "&startDate=" + startDate + "&endDate=" +
-                endDate;
-        
-        next =
-            request.getRequestURL() + "?page=" + ( page + 1 ) + "&rowCount=" + rowCount + "&groupId=" + groupId +
-                "&artifactId=" + artifactId + "&repository=" + repository + "&startDate=" + startDate + "&endDate=" +
-                endDate;
-        
+        prev = request.getRequestURL() + "?page=" + ( page - 1 ) + "&rowCount=" + rowCount + "&groupId=" + groupId +
+            "&artifactId=" + artifactId + "&repository=" + repository + "&startDate=" + startDate + "&endDate=" +
+            endDate;
+
+        next = request.getRequestURL() + "?page=" + ( page + 1 ) + "&rowCount=" + rowCount + "&groupId=" + groupId +
+            "&artifactId=" + artifactId + "&repository=" + repository + "&startDate=" + startDate + "&endDate=" +
+            endDate;
+
         prev = StringUtils.replace( prev, " ", "%20" );
         next = StringUtils.replace( next, " ", "%20" );
     }
@@ -355,7 +360,7 @@ public class ViewAuditLogReportAction
     {
         this.isLastPage = isLastPage;
     }
-    
+
     public String getPrev()
     {
         return prev;
@@ -375,7 +380,7 @@ public class ViewAuditLogReportAction
     {
         this.next = next;
     }
-    
+
     public String getInitial()
     {
         return initial;
index 137e0a83f38eefa42e7236f18f8c8cd72858ca09..3b3448efb360f6c8ac11c2933a2f3bd2851fc0e8 100644 (file)
@@ -33,5 +33,25 @@ public interface AuditManager
 
     void deleteAuditEvents( String repositoryId );
 
+    /**
+     * Get all audit events from the given repositories that match a certain range
+     *
+     * @param repositoryIds the repositories to retrieve events for
+     * @param startTime     find events only after this time
+     * @param endTime       find events only before this time
+     * @return the list of events found
+     */
     List<AuditEvent> getAuditEventsInRange( Collection<String> repositoryIds, Date startTime, Date endTime );
+
+    /**
+     * Get all audit events from the given repositories that match a certain range and resource pattern
+     *
+     * @param repositoryIds   the repositories to retrieve events for
+     * @param resourcePattern find all events whose resources start with this string
+     * @param startTime       find events only after this time
+     * @param endTime         find events only before this time
+     * @return the list of events found
+     */
+    List<AuditEvent> getAuditEventsInRange( Collection<String> repositoryIds, String resourcePattern, Date startTime,
+                                            Date endTime );
 }
\ No newline at end of file
index 2ddd3a3eee56e84dadc26e069e19b18940245cfa..96065838eb70d49da013a333b47056763e40b691 100644 (file)
@@ -92,6 +92,12 @@ public class DefaultAuditManager
     }
 
     public List<AuditEvent> getAuditEventsInRange( Collection<String> repositoryIds, Date startTime, Date endTime )
+    {
+        return getAuditEventsInRange( repositoryIds, null, startTime, endTime );
+    }
+
+    public List<AuditEvent> getAuditEventsInRange( Collection<String> repositoryIds, String resource, Date startTime,
+                                                   Date endTime )
     {
         List<AuditEvent> results = new ArrayList<AuditEvent>();
         for ( String repositoryId : repositoryIds )
@@ -107,7 +113,11 @@ public class DefaultAuditManager
                     {
                         AuditEvent event =
                             (AuditEvent) metadataRepository.getMetadataFacet( repositoryId, AuditEvent.FACET_ID, name );
-                        results.add( event );
+
+                        if ( resource == null || event.getResource().startsWith( resource ) )
+                        {
+                            results.add( event );
+                        }
                     }
                 }
                 catch ( ParseException e )
index 152b868876927d69c90c515664a942406972bc42..2bb3399a87599dc6a909a201898348b9ed0176db 100644 (file)
@@ -68,7 +68,7 @@ public class AuditManagerTest
     private static SimpleDateFormat createTimestampFormat()
     {
         SimpleDateFormat fmt = new SimpleDateFormat( AuditEvent.TIMESTAMP_FORMAT );
-        fmt.setTimeZone( TimeZone.getTimeZone( "UTC" ));
+        fmt.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
         return fmt;
     }
 
@@ -434,6 +434,79 @@ public class AuditManagerTest
         metadataRepositoryControl.verify();
     }
 
+    public void testGetEventsWithResource()
+        throws ParseException
+    {
+        Date current = new Date();
+
+        String name1 = TIMESTAMP_FORMAT.format( new Date( current.getTime() - 12345 ) );
+        AuditEvent expectedEvent1 = createTestEvent( name1 );
+        Date expectedTimestamp = new Date( current.getTime() - 3000 );
+        String name2 = TIMESTAMP_FORMAT.format( expectedTimestamp );
+        AuditEvent expectedEvent2 = createTestEvent( name2 );
+        expectedEvent2.setResource( "different-resource" );
+        String name3 = TIMESTAMP_FORMAT.format( new Date( current.getTime() - 1000 ) );
+        AuditEvent expectedEvent3 = createTestEvent( name3 );
+
+        metadataRepositoryControl.expectAndReturn(
+            metadataRepository.getMetadataFacets( TEST_REPO_ID, AuditEvent.FACET_ID ),
+            Arrays.asList( name1, name2, name3 ) );
+
+        metadataRepositoryControl.expectAndReturn(
+            metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, name1 ), expectedEvent1 );
+        metadataRepositoryControl.expectAndReturn(
+            metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, name2 ), expectedEvent2 );
+        metadataRepositoryControl.expectAndReturn(
+            metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, name3 ), expectedEvent3 );
+
+        metadataRepositoryControl.replay();
+
+        List<AuditEvent> events =
+            auditManager.getAuditEventsInRange( Collections.singletonList( TEST_REPO_ID ), TEST_RESOURCE_BASE,
+                                                new Date( current.getTime() - 20000 ), current );
+
+        assertEquals( 2, events.size() );
+        assertEvent( events.get( 0 ), name3, expectedEvent3.getResource() );
+        assertEvent( events.get( 1 ), name1, expectedEvent1.getResource() );
+
+        metadataRepositoryControl.verify();
+    }
+
+    public void testGetEventsWithNonExistantResource()
+        throws ParseException
+    {
+        Date current = new Date();
+
+        String name1 = TIMESTAMP_FORMAT.format( new Date( current.getTime() - 12345 ) );
+        AuditEvent expectedEvent1 = createTestEvent( name1 );
+        Date expectedTimestamp = new Date( current.getTime() - 3000 );
+        String name2 = TIMESTAMP_FORMAT.format( expectedTimestamp );
+        AuditEvent expectedEvent2 = createTestEvent( name2 );
+        expectedEvent2.setResource( "different-resource" );
+        String name3 = TIMESTAMP_FORMAT.format( new Date( current.getTime() - 1000 ) );
+        AuditEvent expectedEvent3 = createTestEvent( name3 );
+
+        metadataRepositoryControl.expectAndReturn(
+            metadataRepository.getMetadataFacets( TEST_REPO_ID, AuditEvent.FACET_ID ),
+            Arrays.asList( name1, name2, name3 ) );
+
+        metadataRepositoryControl.expectAndReturn(
+            metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, name1 ), expectedEvent1 );
+        metadataRepositoryControl.expectAndReturn(
+            metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, name2 ), expectedEvent2 );
+        metadataRepositoryControl.expectAndReturn(
+            metadataRepository.getMetadataFacet( TEST_REPO_ID, AuditEvent.FACET_ID, name3 ), expectedEvent3 );
+
+        metadataRepositoryControl.replay();
+
+        List<AuditEvent> events = auditManager.getAuditEventsInRange( Collections.singletonList( TEST_REPO_ID ), "foo",
+                                                                      new Date( current.getTime() - 20000 ), current );
+
+        assertEquals( 0, events.size() );
+
+        metadataRepositoryControl.verify();
+    }
+
     public void testGetEventsRangeMultipleRepositories()
         throws ParseException
     {
@@ -478,12 +551,9 @@ public class AuditManagerTest
         Date current = new Date();
 
         String name1 = TIMESTAMP_FORMAT.format( new Date( current.getTime() - 12345 ) );
-        AuditEvent expectedEvent1 = createTestEvent( name1 );
         Date expectedTimestamp = new Date( current.getTime() - 3000 );
         String name2 = TIMESTAMP_FORMAT.format( expectedTimestamp );
-        AuditEvent expectedEvent2 = createTestEvent( name2 );
         String name3 = TIMESTAMP_FORMAT.format( new Date( current.getTime() - 1000 ) );
-        AuditEvent expectedEvent3 = createTestEvent( name3 );
 
         metadataRepositoryControl.expectAndReturn(
             metadataRepository.getMetadataFacets( TEST_REPO_ID, AuditEvent.FACET_ID ),