Esempio n. 1
0
 @Path("log")
 @GET
 public Response logPage() {
   if (auth != null) {
     List<Event> events =
         eventStore
             .createQuery()
             .type(LOG_EVENTS)
             .user(auth.getUser().getId())
             .maxResults(30)
             .getResultList();
     for (Event e : events) {
       if (e.getDetails() != null) {
         Iterator<Map.Entry<String, String>> itr = e.getDetails().entrySet().iterator();
         while (itr.hasNext()) {
           if (!LOG_DETAILS.contains(itr.next().getKey())) {
             itr.remove();
           }
         }
       }
     }
     account.setEvents(events);
   }
   return forwardToPage("log", AccountPages.LOG);
 }
  @Test
  public void clearOld() {
    eventStore.onEvent(
        create(
            System.currentTimeMillis() - 30000,
            EventType.LOGIN,
            "realmId",
            "clientId",
            "userId",
            "127.0.0.1",
            "error"));
    eventStore.onEvent(
        create(
            System.currentTimeMillis() - 20000,
            EventType.LOGIN,
            "realmId",
            "clientId",
            "userId",
            "127.0.0.1",
            "error"));
    eventStore.onEvent(
        create(
            System.currentTimeMillis(),
            EventType.LOGIN,
            "realmId",
            "clientId",
            "userId",
            "127.0.0.1",
            "error"));
    eventStore.onEvent(
        create(
            System.currentTimeMillis(),
            EventType.LOGIN,
            "realmId",
            "clientId",
            "userId",
            "127.0.0.1",
            "error"));
    eventStore.onEvent(
        create(
            System.currentTimeMillis() - 30000,
            EventType.LOGIN,
            "realmId2",
            "clientId",
            "userId",
            "127.0.0.1",
            "error"));

    resetSession();

    eventStore.clear("realmId", System.currentTimeMillis() - 10000);

    Assert.assertEquals(3, eventStore.createQuery().getResultList().size());
  }
  @Test
  public void query() {
    long oldest = System.currentTimeMillis() - 30000;
    long newest = System.currentTimeMillis() + 30000;

    eventStore.onEvent(
        create(EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error"));
    eventStore.onEvent(
        create(newest, EventType.REGISTER, "realmId", "clientId", "userId", "127.0.0.1", "error"));
    eventStore.onEvent(
        create(newest, EventType.REGISTER, "realmId", "clientId", "userId2", "127.0.0.1", "error"));
    eventStore.onEvent(
        create(EventType.LOGIN, "realmId2", "clientId", "userId", "127.0.0.1", "error"));
    eventStore.onEvent(
        create(oldest, EventType.LOGIN, "realmId", "clientId2", "userId", "127.0.0.1", "error"));
    eventStore.onEvent(
        create(EventType.LOGIN, "realmId", "clientId", "userId2", "127.0.0.1", "error"));

    resetSession();

    Assert.assertEquals(5, eventStore.createQuery().client("clientId").getResultList().size());
    Assert.assertEquals(5, eventStore.createQuery().realm("realmId").getResultList().size());
    Assert.assertEquals(4, eventStore.createQuery().type(EventType.LOGIN).getResultList().size());
    Assert.assertEquals(
        6,
        eventStore.createQuery().type(EventType.LOGIN, EventType.REGISTER).getResultList().size());
    Assert.assertEquals(4, eventStore.createQuery().user("userId").getResultList().size());

    Assert.assertEquals(
        1, eventStore.createQuery().user("userId").type(EventType.REGISTER).getResultList().size());

    Assert.assertEquals(2, eventStore.createQuery().maxResults(2).getResultList().size());
    Assert.assertEquals(1, eventStore.createQuery().firstResult(5).getResultList().size());

    Assert.assertEquals(
        newest, eventStore.createQuery().maxResults(1).getResultList().get(0).getTime());
    Assert.assertEquals(
        oldest,
        eventStore.createQuery().firstResult(5).maxResults(1).getResultList().get(0).getTime());

    eventStore.clear("realmId");
    eventStore.clear("realmId2");

    Assert.assertEquals(0, eventStore.createQuery().getResultList().size());

    String d1 = new String("2015-03-04");
    String d2 = new String("2015-03-05");
    String d3 = new String("2015-03-06");
    String d4 = new String("2015-03-07");

    String d5 = new String("2015-03-01");
    String d6 = new String("2015-03-03");
    String d7 = new String("2015-03-08");
    String d8 = new String("2015-03-10");

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    Date date1 = null, date2 = null, date3 = null, date4 = null;
    Date date5 = null, date6 = null, date7 = null, date8 = null;

    try {
      date1 = formatter.parse(d1);
      date2 = formatter.parse(d2);
      date3 = formatter.parse(d3);
      date4 = formatter.parse(d4);

      date5 = formatter.parse(d5);
      date6 = formatter.parse(d6);
      date7 = formatter.parse(d7);
      date8 = formatter.parse(d8);
    } catch (ParseException e) {
      e.printStackTrace();
    }

    eventStore.onEvent(
        create(date1, EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error"));
    eventStore.onEvent(
        create(date1, EventType.LOGIN, "realmId", "clientId", "userId", "127.0.0.1", "error"));
    eventStore.onEvent(
        create(date2, EventType.REGISTER, "realmId", "clientId", "userId", "127.0.0.1", "error"));
    eventStore.onEvent(
        create(date2, EventType.REGISTER, "realmId", "clientId", "userId", "127.0.0.1", "error"));
    eventStore.onEvent(
        create(
            date3,
            EventType.CODE_TO_TOKEN,
            "realmId",
            "clientId",
            "userId2",
            "127.0.0.1",
            "error"));
    eventStore.onEvent(
        create(date3, EventType.LOGOUT, "realmId", "clientId", "userId2", "127.0.0.1", "error"));
    eventStore.onEvent(
        create(
            date4,
            EventType.UPDATE_PROFILE,
            "realmId2",
            "clientId2",
            "userId2",
            "127.0.0.1",
            "error"));
    eventStore.onEvent(
        create(
            date4,
            EventType.UPDATE_EMAIL,
            "realmId2",
            "clientId2",
            "userId2",
            "127.0.0.1",
            "error"));

    resetSession();

    Assert.assertEquals(6, eventStore.createQuery().client("clientId").getResultList().size());
    Assert.assertEquals(2, eventStore.createQuery().client("clientId2").getResultList().size());

    Assert.assertEquals(6, eventStore.createQuery().realm("realmId").getResultList().size());
    Assert.assertEquals(2, eventStore.createQuery().realm("realmId2").getResultList().size());

    Assert.assertEquals(4, eventStore.createQuery().user("userId").getResultList().size());
    Assert.assertEquals(4, eventStore.createQuery().user("userId2").getResultList().size());

    Assert.assertEquals(2, eventStore.createQuery().type(EventType.LOGIN).getResultList().size());
    Assert.assertEquals(
        2, eventStore.createQuery().type(EventType.REGISTER).getResultList().size());
    Assert.assertEquals(
        4,
        eventStore.createQuery().type(EventType.LOGIN, EventType.REGISTER).getResultList().size());
    Assert.assertEquals(
        1, eventStore.createQuery().type(EventType.CODE_TO_TOKEN).getResultList().size());
    Assert.assertEquals(1, eventStore.createQuery().type(EventType.LOGOUT).getResultList().size());
    Assert.assertEquals(
        1, eventStore.createQuery().type(EventType.UPDATE_PROFILE).getResultList().size());
    Assert.assertEquals(
        1, eventStore.createQuery().type(EventType.UPDATE_EMAIL).getResultList().size());

    Assert.assertEquals(8, eventStore.createQuery().fromDate(date1).getResultList().size());
    Assert.assertEquals(8, eventStore.createQuery().toDate(date4).getResultList().size());

    Assert.assertEquals(4, eventStore.createQuery().fromDate(date3).getResultList().size());
    Assert.assertEquals(4, eventStore.createQuery().toDate(date2).getResultList().size());

    Assert.assertEquals(0, eventStore.createQuery().fromDate(date7).getResultList().size());
    Assert.assertEquals(0, eventStore.createQuery().toDate(date6).getResultList().size());

    Assert.assertEquals(
        8, eventStore.createQuery().fromDate(date1).toDate(date4).getResultList().size());
    Assert.assertEquals(
        6, eventStore.createQuery().fromDate(date2).toDate(date4).getResultList().size());
    Assert.assertEquals(
        4, eventStore.createQuery().fromDate(date1).toDate(date2).getResultList().size());
    Assert.assertEquals(
        4, eventStore.createQuery().fromDate(date3).toDate(date4).getResultList().size());

    Assert.assertEquals(
        0, eventStore.createQuery().fromDate(date5).toDate(date6).getResultList().size());
    Assert.assertEquals(
        0, eventStore.createQuery().fromDate(date7).toDate(date8).getResultList().size());
  }
Esempio n. 4
0
  /**
   * Get events
   *
   * <p>Returns all events, or filters them based on URL query parameters listed here
   *
   * @param types The types of events to return
   * @param client App or oauth client name
   * @param user User id
   * @param ipAddress IP address
   * @param dateTo To date
   * @param dateFrom From date
   * @param firstResult Paging offset
   * @param maxResults Paging size
   * @return
   */
  @Path("events")
  @GET
  @NoCache
  @Produces(MediaType.APPLICATION_JSON)
  public List<EventRepresentation> getEvents(
      @QueryParam("type") List<String> types,
      @QueryParam("client") String client,
      @QueryParam("user") String user,
      @QueryParam("dateFrom") String dateFrom,
      @QueryParam("dateTo") String dateTo,
      @QueryParam("ipAddress") String ipAddress,
      @QueryParam("first") Integer firstResult,
      @QueryParam("max") Integer maxResults) {
    auth.init(RealmAuth.Resource.EVENTS).requireView();

    EventStoreProvider eventStore = session.getProvider(EventStoreProvider.class);

    EventQuery query = eventStore.createQuery().realm(realm.getId());
    if (client != null) {
      query.client(client);
    }

    if (types != null & !types.isEmpty()) {
      EventType[] t = new EventType[types.size()];
      for (int i = 0; i < t.length; i++) {
        t[i] = EventType.valueOf(types.get(i));
      }
      query.type(t);
    }

    if (user != null) {
      query.user(user);
    }

    if (dateFrom != null) {
      SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
      Date from = null;
      try {
        from = df.parse(dateFrom);
      } catch (ParseException e) {
        throw new BadRequestException(
            "Invalid value for 'Date(From)', expected format is yyyy-MM-dd");
      }
      query.fromDate(from);
    }

    if (dateTo != null) {
      SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
      Date to = null;
      try {
        to = df.parse(dateTo);
      } catch (ParseException e) {
        throw new BadRequestException(
            "Invalid value for 'Date(To)', expected format is yyyy-MM-dd");
      }
      query.toDate(to);
    }

    if (ipAddress != null) {
      query.ipAddress(ipAddress);
    }
    if (firstResult != null) {
      query.firstResult(firstResult);
    }
    if (maxResults != null) {
      query.maxResults(maxResults);
    }

    return toEventListRep(query.getResultList());
  }