/**
* @plexus.component role="com.opensymphony.xwork2.Action" role-hint="viewAuditLogReport"
- * instantiation-strategy="per-lookup"
+ * instantiation-strategy="per-lookup"
*/
public class ViewAuditLogReportAction
extends PlexusActionSupport
private String startDate;
private String endDate;
-
+
private int rowCount = 30;
private int page = 1;
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
this.request = request;
}
- @SuppressWarnings( "unchecked" )
+ @SuppressWarnings("unchecked")
public void prepare()
throws Exception
{
groupId = "";
artifactId = "";
repository = "";
-
- if( Boolean.parseBoolean( initial ) )
+
+ if ( Boolean.parseBoolean( initial ) )
{
headerName = HEADER_LATEST_EVENTS;
}
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();
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;
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";
return SUCCESS;
}
-
+
private void paginate()
{
if ( auditLogs.size() <= rowCount )
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" );
}
{
this.isLastPage = isLastPage;
}
-
+
public String getPrev()
{
return prev;
{
this.next = next;
}
-
+
public String getInitial()
{
return initial;
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
}
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 )
{
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 )
private static SimpleDateFormat createTimestampFormat()
{
SimpleDateFormat fmt = new SimpleDateFormat( AuditEvent.TIMESTAMP_FORMAT );
- fmt.setTimeZone( TimeZone.getTimeZone( "UTC" ));
+ fmt.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
return fmt;
}
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
{
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 ),