Search in sources :

Example 1 with EventQuery

use of org.keycloak.events.EventQuery in project keycloak by keycloak.

the class TestingResourceProvider method queryEvents.

/**
 * Query events
 * <p>
 * Returns all events, or filters them based on URL query parameters listed here
 *
 * @param realmId     The realm
 * @param types       The types of events to return
 * @param client      App or oauth client name
 * @param user        User id
 * @param dateFrom    From date
 * @param dateTo      To date
 * @param ipAddress   IP address
 * @param firstResult Paging offset
 * @param maxResults  Paging size
 * @return
 */
@Path("query-events")
@GET
@NoCache
@Produces(MediaType.APPLICATION_JSON)
public Stream<EventRepresentation> queryEvents(@QueryParam("realmId") String realmId, @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) {
    EventStoreProvider eventStore = session.getProvider(EventStoreProvider.class);
    EventQuery query = eventStore.createQuery();
    if (realmId != null) {
        query.realm(realmId);
    }
    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) {
        Date from = formatDate(dateFrom, "Date(From)");
        query.fromDate(from);
    }
    if (dateTo != null) {
        Date to = formatDate(dateTo, "Date(To)");
        query.toDate(to);
    }
    if (ipAddress != null) {
        query.ipAddress(ipAddress);
    }
    if (firstResult != null) {
        query.firstResult(firstResult);
    }
    if (maxResults != null) {
        query.maxResults(maxResults);
    }
    return query.getResultStream().map(ModelToRepresentation::toRepresentation);
}
Also used : EventType(org.keycloak.events.EventType) AdminEventQuery(org.keycloak.events.admin.AdminEventQuery) EventQuery(org.keycloak.events.EventQuery) ModelToRepresentation(org.keycloak.models.utils.ModelToRepresentation) Date(java.util.Date) EventStoreProvider(org.keycloak.events.EventStoreProvider) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) NoCache(org.jboss.resteasy.annotations.cache.NoCache)

Aggregations

Date (java.util.Date)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 NoCache (org.jboss.resteasy.annotations.cache.NoCache)1 EventQuery (org.keycloak.events.EventQuery)1 EventStoreProvider (org.keycloak.events.EventStoreProvider)1 EventType (org.keycloak.events.EventType)1 AdminEventQuery (org.keycloak.events.admin.AdminEventQuery)1 ModelToRepresentation (org.keycloak.models.utils.ModelToRepresentation)1