Beispiel #1
0
  @Test
  public void shouldFilterLogEntriesOnEventCategories() throws Exception {
    DocumentModel doc = RestServerInit.getFile(1, session);

    List<LogEntry> logEntries = new ArrayList<>();
    LogEntry logEntry = auditLogger.newLogEntry();
    logEntry.setDocUUID(doc.getRef());
    logEntry.setCategory("One");
    logEntry.setEventId("firstEvent");
    logEntries.add(logEntry);
    logEntry = auditLogger.newLogEntry();
    logEntry.setDocUUID(doc.getRef());
    logEntry.setCategory("One");
    logEntry.setEventId("secondEvent");
    logEntries.add(logEntry);
    logEntry = auditLogger.newLogEntry();
    logEntry.setDocUUID(doc.getRef());
    logEntry.setCategory("Two");
    logEntry.setEventId("firstEvent");
    logEntries.add(logEntry);
    auditLogger.addLogEntries(logEntries);

    TransactionHelper.commitOrRollbackTransaction();
    TransactionHelper.startTransaction();

    MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
    queryParams.add("category", "One");
    queryParams.add("category", "Two");
    ClientResponse response =
        getResponse(
            BaseTest.RequestType.GET, "id/" + doc.getId() + "/@" + AuditAdapter.NAME, queryParams);
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
    JsonNode node = mapper.readTree(response.getEntityInputStream());
    List<JsonNode> nodes = getLogEntries(node);
    assertEquals(3, nodes.size());

    queryParams = new MultivaluedMapImpl();
    queryParams.add("category", "Two");
    response =
        getResponse(
            BaseTest.RequestType.GET, "id/" + doc.getId() + "/@" + AuditAdapter.NAME, queryParams);
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
    node = mapper.readTree(response.getEntityInputStream());
    nodes = getLogEntries(node);
    assertEquals(1, nodes.size());
  }
  @Test
  public void canLogMultipleLifecycleTransitionsInSameTx() throws ClientException {
    // generate events
    DocumentModel doc = repo.createDocumentModel("/", "a-file", "File");
    doc = repo.createDocument(doc);
    String initialLifeCycle = doc.getCurrentLifeCycleState();
    doc.followTransition(LifeCycleConstants.DELETE_TRANSITION);
    String deletedLifeCycle = doc.getCurrentLifeCycleState();
    doc.followTransition(LifeCycleConstants.UNDELETE_TRANSITION);
    String undeletedLifeCycle = doc.getCurrentLifeCycleState();
    TransactionHelper.commitOrRollbackTransaction();
    Framework.getLocalService(EventService.class).waitForAsyncCompletion();

    // test audit trail
    AuditReader reader = Framework.getLocalService(AuditReader.class);
    List<LogEntry> trail = reader.getLogEntriesFor(doc.getId());

    assertThat(trail, notNullValue());
    assertThat(trail.size(), is(3));

    boolean seenDocCreated = false;
    boolean seenDocDeleted = false;
    boolean seenDocUndeleted = false;

    for (LogEntry entry : trail) {
      String lifeCycle = entry.getDocLifeCycle();
      String id = entry.getEventId();
      if (DocumentEventTypes.DOCUMENT_CREATED.equals(id)) {
        if (initialLifeCycle.equals(lifeCycle)) {
          seenDocCreated = true;
        }
      } else if (LifeCycleConstants.TRANSITION_EVENT.equals(id)) {
        if (undeletedLifeCycle.equals(lifeCycle)) {
          seenDocUndeleted = true;
        } else if (deletedLifeCycle.equals(lifeCycle)) {
          seenDocDeleted = true;
        }
      }
    }

    assertThat(seenDocUndeleted, is(true));
    assertThat(seenDocDeleted, is(true));
    assertThat(seenDocCreated, is(true));
  }
  protected LogEntry doCreateEntry(String docId, String eventId, String category) {
    LogEntry createdEntry = new LogEntryImpl();
    createdEntry.setEventId(eventId);
    createdEntry.setCategory(category);
    createdEntry.setDocUUID(docId);
    createdEntry.setEventDate(new Date());
    createdEntry.setDocPath("/" + docId);
    createdEntry.setRepositoryId("test");
    createdEntry.setExtendedInfos(createExtendedInfos());

    return createdEntry;
  }
Beispiel #4
0
  @WebMethod
  public ModifiedDocumentDescriptorPage listDeletedDocumentsByPage(
      @WebParam(name = "sessionId") String sessionId,
      @WebParam(name = "dataRangeQuery") String dateRangeQuery,
      @WebParam(name = "docPath") String path,
      @WebParam(name = "pageIndex") int page,
      @WebParam(name = "pageSize") int pageSize) {
    initSession(sessionId);

    String[] eventIds = {"documentRemoved"};

    List<LogEntry> logEntries =
        getLogsBean()
            .queryLogsByPage(
                eventIds, dateRangeQuery, "eventDocumentCategory", path, page, pageSize);

    boolean hasMorePage = logEntries.size() >= pageSize;

    List<ModifiedDocumentDescriptor> ldocs = new ArrayList<ModifiedDocumentDescriptor>();
    Set<String> uuids = new HashSet<String>();
    for (LogEntry logEntry : logEntries) {
      if (!uuids.contains(logEntry.getDocUUID())) {
        uuids.add(logEntry.getDocUUID());
        ldocs.add(
            new ModifiedDocumentDescriptor(
                logEntry.getEventDate(), logEntry.getDocType(), logEntry.getDocUUID()));
      }
    }

    ModifiedDocumentDescriptor[] docs = new ModifiedDocumentDescriptor[ldocs.size()];
    ldocs.toArray(docs);

    return new ModifiedDocumentDescriptorPage(docs, page, hasMorePage);
  }
Beispiel #5
0
 protected void writeExtendedInfos(JsonGenerator jg, LogEntry logEntry) throws IOException {
   Map<String, ExtendedInfo> extended = logEntry.getExtendedInfos();
   jg.writeObjectFieldStart("extended");
   for (String key : extended.keySet()) {
     ExtendedInfo ei = extended.get(key);
     if (ei != null && ei.getSerializableValue() != null) {
       writeExtendedInfo(jg, key, ei.getSerializableValue());
     } else {
       jg.writeNullField(key);
     }
   }
   jg.writeEndObject();
 }
Beispiel #6
0
  @WebMethod
  public ModifiedDocumentDescriptor[] listModifiedDocuments(
      @WebParam(name = "sessionId") String sessionId,
      @WebParam(name = "dataRangeQuery") String dateRangeQuery) {
    initSession(sessionId);

    BatchInfo batchInfo = BatchHelper.getBatchInfo(sessionId, dateRangeQuery);

    List<LogEntry> logEntries =
        getLogsBean()
            .queryLogsByPage(
                null,
                batchInfo.getPageDateRange(),
                EVENT_DOCUMENT_CATEGORY,
                null,
                batchInfo.getNextPage(),
                batchInfo.getPageSize());
    if (logEntries.size() < batchInfo.getPageSize()) {
      // we are at the end of the batch
      // ==> reset the batch
      BatchHelper.resetBatchInfo(sessionId);
    } else {
      // set the batchInfo ready for next call
      batchInfo.prepareNextCall();
    }

    List<ModifiedDocumentDescriptor> ldocs = new ArrayList<ModifiedDocumentDescriptor>();
    Set<String> uuids = new HashSet<String>();
    for (LogEntry logEntry : logEntries) {
      if (!uuids.contains(logEntry.getDocUUID())) {
        uuids.add(logEntry.getDocUUID());
        ldocs.add(
            new ModifiedDocumentDescriptor(
                logEntry.getEventDate(), logEntry.getDocType(), logEntry.getDocUUID()));
      }
    }

    ModifiedDocumentDescriptor[] docs = new ModifiedDocumentDescriptor[ldocs.size()];
    ldocs.toArray(docs);

    return docs;
  }
Beispiel #7
0
  @Test
  public void shouldHandlePagination() throws Exception {
    DocumentModel doc = RestServerInit.getFile(1, session);

    List<LogEntry> logEntries = new ArrayList<>();
    LogEntry logEntry = auditLogger.newLogEntry();
    logEntry.setDocUUID(doc.getRef());
    logEntry.setCategory("One");
    logEntry.setEventId("firstEvent");
    logEntries.add(logEntry);
    logEntry = auditLogger.newLogEntry();
    logEntry.setDocUUID(doc.getRef());
    logEntry.setCategory("One");
    logEntry.setEventId("secondEvent");
    logEntries.add(logEntry);
    logEntry = auditLogger.newLogEntry();
    logEntry.setDocUUID(doc.getRef());
    logEntry.setCategory("One");
    logEntry.setEventId("thirdEvent");
    logEntries.add(logEntry);
    logEntry = auditLogger.newLogEntry();
    logEntry.setDocUUID(doc.getRef());
    logEntry.setCategory("One");
    logEntry.setEventId("fourthEvent");
    logEntries.add(logEntry);
    logEntry = auditLogger.newLogEntry();
    logEntry.setDocUUID(doc.getRef());
    logEntry.setCategory("One");
    logEntry.setEventId("fifthEvent");
    logEntries.add(logEntry);
    logEntry = auditLogger.newLogEntry();
    logEntry.setDocUUID(doc.getRef());
    logEntry.setCategory("One");
    logEntry.setEventId("sixthEvent");
    logEntries.add(logEntry);
    auditLogger.addLogEntries(logEntries);

    TransactionHelper.commitOrRollbackTransaction();
    TransactionHelper.startTransaction();

    MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
    queryParams.putSingle("category", "One");
    ClientResponse response =
        getResponse(
            BaseTest.RequestType.GET, "id/" + doc.getId() + "/@" + AuditAdapter.NAME, queryParams);
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
    JsonNode node = mapper.readTree(response.getEntityInputStream());
    List<JsonNode> nodes = getLogEntries(node);
    assertEquals(6, nodes.size());

    queryParams = new MultivaluedMapImpl();
    queryParams.putSingle("category", "One");
    queryParams.putSingle("currentPageIndex", "0");
    queryParams.putSingle("pageSize", "2");
    response =
        getResponse(
            BaseTest.RequestType.GET, "id/" + doc.getId() + "/@" + AuditAdapter.NAME, queryParams);
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
    node = mapper.readTree(response.getEntityInputStream());
    assertTrue(node.get("isPaginable").getBooleanValue());
    assertEquals(0, node.get("currentPageIndex").getIntValue());
    assertEquals(2, node.get("pageSize").getIntValue());
    assertEquals(3, node.get("numberOfPages").getIntValue());
    nodes = getLogEntries(node);
    assertEquals(2, nodes.size());
    assertEquals("sixthEvent", nodes.get(0).get("eventId").getValueAsText());
    assertEquals("fifthEvent", nodes.get(1).get("eventId").getValueAsText());

    queryParams = new MultivaluedMapImpl();
    queryParams.putSingle("category", "One");
    queryParams.putSingle("currentPageIndex", "1");
    queryParams.putSingle("pageSize", "3");
    response =
        getResponse(
            BaseTest.RequestType.GET, "id/" + doc.getId() + "/@" + AuditAdapter.NAME, queryParams);
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
    node = mapper.readTree(response.getEntityInputStream());
    assertTrue(node.get("isPaginable").getBooleanValue());
    assertEquals(1, node.get("currentPageIndex").getIntValue());
    assertEquals(3, node.get("pageSize").getIntValue());
    assertEquals(2, node.get("numberOfPages").getIntValue());
    nodes = getLogEntries(node);
    assertEquals(3, nodes.size());
    assertEquals("thirdEvent", nodes.get(0).get("eventId").getValueAsText());
    assertEquals("secondEvent", nodes.get(1).get("eventId").getValueAsText());
    assertEquals("firstEvent", nodes.get(2).get("eventId").getValueAsText());

    queryParams = new MultivaluedMapImpl();
    queryParams.putSingle("category", "One");
    queryParams.putSingle("currentPageIndex", "2");
    queryParams.putSingle("pageSize", "3");
    response =
        getResponse(
            BaseTest.RequestType.GET, "id/" + doc.getId() + "/@" + AuditAdapter.NAME, queryParams);
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
    node = mapper.readTree(response.getEntityInputStream());
    nodes = getLogEntries(node);
    assertTrue(node.get("isPaginable").getBooleanValue());
    assertEquals(0, nodes.size());
  }
Beispiel #8
0
  @Test
  public void shouldFilterLogEntriesOnMultipleCriteria() throws Exception {
    DocumentModel doc = RestServerInit.getFile(1, session);

    DateTime firstDate = new DateTime();
    DateTime secondDate = firstDate.plusDays(10);

    List<LogEntry> logEntries = new ArrayList<>();
    LogEntry logEntry = auditLogger.newLogEntry();
    logEntry.setDocUUID(doc.getRef());
    logEntry.setCategory("One");
    logEntry.setEventId("firstEvent");
    logEntry.setPrincipalName("bender");
    logEntry.setEventDate(firstDate.toDate());
    logEntries.add(logEntry);
    logEntry = auditLogger.newLogEntry();
    logEntry.setDocUUID(doc.getRef());
    logEntry.setCategory("One");
    logEntry.setEventId("secondEvent");
    logEntry.setPrincipalName("leela");
    logEntry.setEventDate(firstDate.toDate());
    logEntries.add(logEntry);
    logEntry = auditLogger.newLogEntry();
    logEntry.setDocUUID(doc.getRef());
    logEntry.setCategory("One");
    logEntry.setEventId("firstEvent");
    logEntry.setPrincipalName("leela");
    logEntry.setEventDate(secondDate.toDate());
    logEntries.add(logEntry);
    logEntry = auditLogger.newLogEntry();
    logEntry.setDocUUID(doc.getRef());
    logEntry.setCategory("One");
    logEntry.setEventId("thirdEvent");
    logEntry.setPrincipalName("leela");
    logEntry.setEventDate(secondDate.toDate());
    logEntries.add(logEntry);
    auditLogger.addLogEntries(logEntries);

    TransactionHelper.commitOrRollbackTransaction();
    TransactionHelper.startTransaction();

    MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
    queryParams.putSingle("category", "One");
    queryParams.add("principalName", "leela");
    ClientResponse response =
        getResponse(
            BaseTest.RequestType.GET, "id/" + doc.getId() + "/@" + AuditAdapter.NAME, queryParams);
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
    JsonNode node = mapper.readTree(response.getEntityInputStream());
    List<JsonNode> nodes = getLogEntries(node);
    assertEquals(3, nodes.size());

    queryParams = new MultivaluedMapImpl();
    queryParams.putSingle("category", "One");
    queryParams.add("principalName", "leela");
    queryParams.add("eventId", "thirdEvent");
    response =
        getResponse(
            BaseTest.RequestType.GET, "id/" + doc.getId() + "/@" + AuditAdapter.NAME, queryParams);
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
    node = mapper.readTree(response.getEntityInputStream());
    nodes = getLogEntries(node);
    assertEquals(1, nodes.size());

    queryParams = new MultivaluedMapImpl();
    queryParams.putSingle("category", "One");
    queryParams.add("principalName", "leela");
    queryParams.add("eventId", "thirdEvent");
    queryParams.add("startEventDate", ISODateTimeFormat.date().print(firstDate.plusDays(1)));
    queryParams.add("endEventDate", ISODateTimeFormat.date().print(secondDate.minus(1)));
    response =
        getResponse(
            BaseTest.RequestType.GET, "id/" + doc.getId() + "/@" + AuditAdapter.NAME, queryParams);
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
    node = mapper.readTree(response.getEntityInputStream());
    nodes = getLogEntries(node);
    assertEquals(0, nodes.size());
  }
Beispiel #9
0
 @Override
 protected void writeEntityBody(JsonGenerator jg, LogEntry logEntry) throws IOException {
   jg.writeStringField("entity-type", "logEntry");
   jg.writeStringField("category", logEntry.getCategory());
   jg.writeStringField("principalName", logEntry.getPrincipalName());
   jg.writeStringField("comment", logEntry.getComment());
   jg.writeStringField("docLifeCycle", logEntry.getDocLifeCycle());
   jg.writeStringField("docPath", logEntry.getDocPath());
   jg.writeStringField("docType", logEntry.getDocType());
   jg.writeStringField("docUUID", logEntry.getDocUUID());
   jg.writeStringField("eventId", logEntry.getEventId());
   jg.writeStringField("repositoryId", logEntry.getRepositoryId());
   jg.writeStringField(
       "eventDate", ISODateTimeFormat.dateTime().print(new DateTime(logEntry.getEventDate())));
   jg.writeNumberField("id", logEntry.getId());
   jg.writeStringField(
       "logDate", ISODateTimeFormat.dateTime().print(new DateTime(logEntry.getLogDate())));
   writeExtendedInfos(jg, logEntry);
 }