Search in sources :

Example 1 with AuditEvent

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;
}
Also used : ArrayList(java.util.ArrayList) AuditEvent(org.apache.archiva.metadata.model.facets.AuditEvent) ParseException(java.text.ParseException) Date(java.util.Date)

Example 2 with AuditEvent

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();
}
Also used : AuditEvent(org.apache.archiva.metadata.model.facets.AuditEvent) Date(java.util.Date) Test(org.junit.Test)

Example 3 with AuditEvent

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();
}
Also used : AuditEvent(org.apache.archiva.metadata.model.facets.AuditEvent) Date(java.util.Date) Test(org.junit.Test)

Example 4 with AuditEvent

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();
}
Also used : AuditEvent(org.apache.archiva.metadata.model.facets.AuditEvent) Date(java.util.Date) Test(org.junit.Test)

Example 5 with AuditEvent

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;
}
Also used : AuditEvent(org.apache.archiva.metadata.model.facets.AuditEvent)

Aggregations

AuditEvent (org.apache.archiva.metadata.model.facets.AuditEvent)21 Test (org.junit.Test)13 Date (java.util.Date)11 ArrayList (java.util.ArrayList)5 AuditListener (org.apache.archiva.repository.events.AuditListener)5 ParseException (java.text.ParseException)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 AuditInformation (org.apache.archiva.admin.model.AuditInformation)1 User (org.apache.archiva.redback.users.User)1