From: Brett Porter Date: Tue, 19 Jan 2010 11:21:10 +0000 (+0000) Subject: [MRM-1269] restore pseudo-search by artifact in audit logs X-Git-Tag: archiva-1.4-M1~1017^2~36 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=93f9e26408d1fb72a22e31cb19640cdca2de2e9f;p=archiva.git [MRM-1269] restore pseudo-search by artifact in audit logs git-svn-id: https://svn.apache.org/repos/asf/archiva/branches/MRM-1025@900726 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/reports/ViewAuditLogReportAction.java b/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/reports/ViewAuditLogReportAction.java index 1b06ed199..26433e15e 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/reports/ViewAuditLogReportAction.java +++ b/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/reports/ViewAuditLogReportAction.java @@ -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; diff --git a/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/AuditManager.java b/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/AuditManager.java index 137e0a83f..3b3448efb 100644 --- a/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/AuditManager.java +++ b/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/AuditManager.java @@ -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 getAuditEventsInRange( Collection 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 getAuditEventsInRange( Collection repositoryIds, String resourcePattern, Date startTime, + Date endTime ); } \ No newline at end of file diff --git a/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/DefaultAuditManager.java b/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/DefaultAuditManager.java index 2ddd3a3ee..96065838e 100644 --- a/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/DefaultAuditManager.java +++ b/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/DefaultAuditManager.java @@ -92,6 +92,12 @@ public class DefaultAuditManager } public List getAuditEventsInRange( Collection repositoryIds, Date startTime, Date endTime ) + { + return getAuditEventsInRange( repositoryIds, null, startTime, endTime ); + } + + public List getAuditEventsInRange( Collection repositoryIds, String resource, Date startTime, + Date endTime ) { List results = new ArrayList(); 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 ) diff --git a/archiva-modules/plugins/audit/src/test/java/org/apache/archiva/audit/AuditManagerTest.java b/archiva-modules/plugins/audit/src/test/java/org/apache/archiva/audit/AuditManagerTest.java index 152b86887..2bb3399a8 100644 --- a/archiva-modules/plugins/audit/src/test/java/org/apache/archiva/audit/AuditManagerTest.java +++ b/archiva-modules/plugins/audit/src/test/java/org/apache/archiva/audit/AuditManagerTest.java @@ -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 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 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 ),