use of org.apache.archiva.metadata.model.facets.AuditEvent in project archiva by apache.
the class DefaultAuditManager method getAuditEventsInRange.
@Override
public List<AuditEvent> getAuditEventsInRange(MetadataRepository metadataRepository, Collection<String> repositoryIds, String resource, Date startTime, Date endTime) throws MetadataRepositoryException {
List<AuditEvent> results = new ArrayList<>();
for (String repositoryId : repositoryIds) {
List<String> list = metadataRepository.getMetadataFacets(repositoryId, AuditEvent.FACET_ID);
for (String name : list) {
try {
Date date = createNameFormat().parse(name);
if ((startTime == null || !date.before(startTime)) && (endTime == null || !date.after(endTime))) {
AuditEvent event = (AuditEvent) metadataRepository.getMetadataFacet(repositoryId, AuditEvent.FACET_ID, name);
if (resource == null || event.getResource().startsWith(resource)) {
results.add(event);
}
}
} catch (ParseException e) {
log.error("Invalid audit event found in the metadata repository: {}", e.getMessage());
// continue and ignore this one
}
}
}
Collections.sort(results, new Comparator<AuditEvent>() {
@Override
public int compare(AuditEvent o1, AuditEvent o2) {
return o2.getTimestamp().compareTo(o1.getTimestamp());
}
});
return results;
}
use of org.apache.archiva.metadata.model.facets.AuditEvent in project archiva by apache.
the class AuditManagerTest method testGetEventsRangeMultipleRepositories.
@Test
public void testGetEventsRangeMultipleRepositories() throws Exception {
Date current = new Date();
Date ts1 = new Date(current.getTime() - 12345);
AuditEvent expectedEvent1 = createEvent(ts1);
Date expectedTimestamp = new Date(current.getTime() - 3000);
AuditEvent expectedEvent2 = createEvent(expectedTimestamp);
expectedEvent2.setRepositoryId(TEST_REPO_ID_2);
Date ts3 = new Date(current.getTime() - 1000);
AuditEvent expectedEvent3 = createEvent(ts3);
EasyMock.expect(metadataRepository.getMetadataFacets(TEST_REPO_ID, AuditEvent.FACET_ID)).andReturn(Arrays.asList(expectedEvent1.getName(), expectedEvent3.getName()));
EasyMock.expect(metadataRepository.getMetadataFacets(TEST_REPO_ID_2, AuditEvent.FACET_ID)).andReturn(Arrays.asList(expectedEvent2.getName()));
EasyMock.expect(metadataRepository.getMetadataFacet(TEST_REPO_ID, AuditEvent.FACET_ID, expectedEvent1.getName())).andReturn(expectedEvent1);
EasyMock.expect(metadataRepository.getMetadataFacet(TEST_REPO_ID_2, AuditEvent.FACET_ID, expectedEvent2.getName())).andReturn(expectedEvent2);
EasyMock.expect(metadataRepository.getMetadataFacet(TEST_REPO_ID, AuditEvent.FACET_ID, expectedEvent3.getName())).andReturn(expectedEvent3);
metadataRepositoryControl.replay();
List<AuditEvent> events = auditManager.getAuditEventsInRange(metadataRepository, Arrays.asList(TEST_REPO_ID, TEST_REPO_ID_2), new Date(current.getTime() - 20000), current);
assertEquals(3, events.size());
assertTestEvent(events.get(0), TEST_REPO_ID, TIMESTAMP_FORMAT.format(ts3), expectedEvent3.getResource());
assertTestEvent(events.get(1), TEST_REPO_ID_2, TIMESTAMP_FORMAT.format(expectedTimestamp), expectedEvent2.getResource());
assertTestEvent(events.get(2), TEST_REPO_ID, TIMESTAMP_FORMAT.format(ts1), expectedEvent1.getResource());
metadataRepositoryControl.verify();
}
use of org.apache.archiva.metadata.model.facets.AuditEvent in project archiva by apache.
the class AuditManagerTest method testGetEventsWithResource.
@Test
public void testGetEventsWithResource() throws Exception {
Date current = new Date();
Date ts1 = new Date(current.getTime() - 12345);
AuditEvent expectedEvent1 = createEvent(ts1);
Date expectedTimestamp = new Date(current.getTime() - 3000);
AuditEvent expectedEvent2 = createEvent(expectedTimestamp);
expectedEvent2.setResource("different-resource");
Date ts3 = new Date(current.getTime() - 1000);
AuditEvent expectedEvent3 = createEvent(ts3);
EasyMock.expect(metadataRepository.getMetadataFacets(TEST_REPO_ID, AuditEvent.FACET_ID)).andReturn(Arrays.asList(expectedEvent1.getName(), expectedEvent2.getName(), expectedEvent3.getName()));
EasyMock.expect(metadataRepository.getMetadataFacet(TEST_REPO_ID, AuditEvent.FACET_ID, expectedEvent1.getName())).andReturn(expectedEvent1);
EasyMock.expect(metadataRepository.getMetadataFacet(TEST_REPO_ID, AuditEvent.FACET_ID, expectedEvent2.getName())).andReturn(expectedEvent2);
EasyMock.expect(metadataRepository.getMetadataFacet(TEST_REPO_ID, AuditEvent.FACET_ID, expectedEvent3.getName())).andReturn(expectedEvent3);
metadataRepositoryControl.replay();
List<AuditEvent> events = auditManager.getAuditEventsInRange(metadataRepository, Collections.singletonList(TEST_REPO_ID), TEST_RESOURCE_BASE, new Date(current.getTime() - 20000), current);
assertEquals(2, events.size());
assertTestEvent(events.get(0), TIMESTAMP_FORMAT.format(ts3), expectedEvent3.getResource());
assertTestEvent(events.get(1), TIMESTAMP_FORMAT.format(ts1), expectedEvent1.getResource());
metadataRepositoryControl.verify();
}
use of org.apache.archiva.metadata.model.facets.AuditEvent in project archiva by apache.
the class AuditManagerTest method testGetEventsRangeLowerOutside.
@Test
public void testGetEventsRangeLowerOutside() throws Exception {
Date current = new Date();
Date ts1 = new Date(current.getTime() - 12345);
AuditEvent expectedEvent1 = createEvent(ts1);
Date expectedTimestamp = new Date(current.getTime() - 3000);
AuditEvent expectedEvent2 = createEvent(expectedTimestamp);
AuditEvent event3 = createEvent(new Date(current.getTime() - 1000));
EasyMock.expect(metadataRepository.getMetadataFacets(TEST_REPO_ID, AuditEvent.FACET_ID)).andReturn(Arrays.asList(expectedEvent1.getName(), expectedEvent2.getName(), event3.getName()));
EasyMock.expect(metadataRepository.getMetadataFacet(TEST_REPO_ID, AuditEvent.FACET_ID, expectedEvent1.getName())).andReturn(expectedEvent1);
EasyMock.expect(metadataRepository.getMetadataFacet(TEST_REPO_ID, AuditEvent.FACET_ID, expectedEvent2.getName())).andReturn(expectedEvent2);
metadataRepositoryControl.replay();
List<AuditEvent> events = auditManager.getAuditEventsInRange(metadataRepository, Collections.singletonList(TEST_REPO_ID), new Date(current.getTime() - 20000), new Date(current.getTime() - 2000));
assertEquals(2, events.size());
assertTestEvent(events.get(0), TIMESTAMP_FORMAT.format(expectedTimestamp), expectedEvent2.getResource());
assertTestEvent(events.get(1), TIMESTAMP_FORMAT.format(ts1), expectedEvent1.getResource());
metadataRepositoryControl.verify();
}
use of org.apache.archiva.metadata.model.facets.AuditEvent in project archiva by apache.
the class AuditManagerTest method createEvent.
private static AuditEvent createEvent(String repositoryId, String timestamp, String resource) throws ParseException {
AuditEvent event = new AuditEvent();
event.setTimestamp(TIMESTAMP_FORMAT.parse(timestamp));
event.setAction(AuditEvent.UPLOAD_FILE);
event.setRemoteIP(TEST_IP_ADDRESS);
event.setRepositoryId(repositoryId);
event.setUserId(TEST_USER);
event.setResource(resource);
return event;
}
Aggregations