Search in sources :

Example 1 with EventEntity

use of io.gravitee.rest.api.model.EventEntity in project gravitee-management-rest-api by gravitee-io.

the class PlatformEventsResource method getPlatformEvents.

@GET
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "List platform events", notes = "User must have the MANAGEMENT_PLATFORM[READ] permission to use this service")
@ApiResponses({ @ApiResponse(code = 200, message = "Platform events", response = EventEntity.class), @ApiResponse(code = 500, message = "Internal server error") })
@Permissions({ @Permission(value = RolePermission.ENVIRONMENT_PLATFORM, acls = RolePermissionAction.READ) })
public Page<EventEntity> getPlatformEvents(@BeanParam EventSearchParam eventSearchParam) {
    eventSearchParam.validate();
    Map<String, Object> properties = new HashMap<>();
    if (eventSearchParam.getApiIdsParam() != null && eventSearchParam.getApiIdsParam().getIds() != null && !eventSearchParam.getApiIdsParam().getIds().isEmpty()) {
        properties.put(Event.EventProperties.API_ID.getValue(), eventSearchParam.getApiIdsParam().getIds());
    } else if (!isAdmin()) {
        properties.put(Event.EventProperties.API_ID.getValue(), apiService.findByUser(getAuthenticatedUser(), null, false).stream().filter(api -> permissionService.hasPermission(API_ANALYTICS, api.getId(), READ)).map(ApiEntity::getId).collect(Collectors.joining(",")));
    }
    Page<EventEntity> events = eventService.search(eventSearchParam.getEventTypeListParam().getEventTypes(), properties, eventSearchParam.getFrom(), eventSearchParam.getTo(), eventSearchParam.getPage(), eventSearchParam.getSize());
    events.getContent().forEach(event -> {
        Map<String, String> properties1 = event.getProperties();
        // Event relative to API
        if (properties1 != null && properties1.containsKey(Event.EventProperties.API_ID.getValue())) {
            // Remove payload content from response since it's not required anymore
            event.setPayload(null);
            // Retrieve additional data
            String apiId = properties1.get(Event.EventProperties.API_ID.getValue());
            try {
                ApiEntity api = apiService.findById(apiId);
                properties1.put("api_name", api.getName());
                properties1.put("api_version", api.getVersion());
            } catch (ApiNotFoundException anfe) {
                properties1.put("deleted", Boolean.TRUE.toString());
                properties1.put("api_name", "Deleted API");
            }
        }
    });
    return events;
}
Also used : HashMap(java.util.HashMap) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) EventEntity(io.gravitee.rest.api.model.EventEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) Permissions(io.gravitee.rest.api.management.rest.security.Permissions) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with EventEntity

use of io.gravitee.rest.api.model.EventEntity in project gravitee-management-rest-api by gravitee-io.

the class ApiEventsResource method searchApiEvents.

@GET
@Path("search")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Get API's events", notes = "User must have the API_EVENT[READ] permission to use this service")
@ApiResponses({ @ApiResponse(code = 200, message = "Page of API events", response = Page.class), @ApiResponse(code = 500, message = "Internal server error") })
@Permissions({ @Permission(value = RolePermission.API_EVENT, acls = RolePermissionAction.READ) })
public Page<EventEntity> searchApiEvents(@ApiParam @BeanParam EventSearchParam eventSearchParam) {
    ApiEntity apiEntity = apiService.findById(api);
    Map<String, Object> properties = new HashMap<>();
    properties.put(Event.EventProperties.API_ID.getValue(), Arrays.asList(api));
    final Page<EventEntity> apiEvents = eventService.search(eventSearchParam.getEventTypeListParam().getEventTypes(), properties, eventSearchParam.getFrom(), eventSearchParam.getTo(), eventSearchParam.getPage(), eventSearchParam.getSize());
    apiEvents.getContent().forEach(event -> {
        Map<String, String> properties1 = event.getProperties();
        // Remove payload content from response since it's not required anymore
        event.setPayload(null);
        // complete event with API info
        properties1.put("api_name", apiEntity.getName());
        properties1.put("api_version", apiEntity.getVersion());
    });
    return apiEvents;
}
Also used : HashMap(java.util.HashMap) EventEntity(io.gravitee.rest.api.model.EventEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Permissions(io.gravitee.rest.api.management.rest.security.Permissions)

Example 3 with EventEntity

use of io.gravitee.rest.api.model.EventEntity in project gravitee-management-rest-api by gravitee-io.

the class EventServiceTest method shouldFindById.

@Test
public void shouldFindById() throws TechnicalException {
    when(event.getType()).thenReturn(EventType.PUBLISH_API);
    when(event.getPayload()).thenReturn(EVENT_PAYLOAD);
    when(event.getProperties()).thenReturn(EVENT_PROPERTIES);
    when(eventRepository.findById(EVENT_ID)).thenReturn(Optional.of(event));
    final EventEntity eventEntity = eventService.findById(EVENT_ID);
    assertNotNull(eventEntity);
}
Also used : EventEntity(io.gravitee.rest.api.model.EventEntity) NewEventEntity(io.gravitee.rest.api.model.NewEventEntity) Test(org.junit.Test)

Example 4 with EventEntity

use of io.gravitee.rest.api.model.EventEntity in project gravitee-management-rest-api by gravitee-io.

the class ApiEventsResource method getApiEventsEvents.

@GET
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Get API's events", notes = "User must have the MANAGE_LIFECYCLE permission to use this service")
@ApiResponses({ @ApiResponse(code = 200, message = "API's events"), @ApiResponse(code = 500, message = "Internal server error") })
@Permissions({ @Permission(value = RolePermission.API_EVENT, acls = RolePermissionAction.READ) })
public List<EventEntity> getApiEventsEvents(@ApiParam @DefaultValue("all") @QueryParam("type") EventTypeListParam eventTypeListParam) {
    final EventQuery query = new EventQuery();
    query.setApi(api);
    return eventService.search(query).stream().filter(event -> eventTypeListParam.getEventTypes().contains(event.getType())).sorted((e1, e2) -> e2.getCreatedAt().compareTo(e1.getCreatedAt())).collect(Collectors.toList());
}
Also used : RolePermissionAction(io.gravitee.rest.api.model.permissions.RolePermissionAction) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Arrays(java.util.Arrays) Event(io.gravitee.repository.management.model.Event) Page(io.gravitee.common.data.domain.Page) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) Permission(io.gravitee.rest.api.management.rest.security.Permission) EventService(io.gravitee.rest.api.service.EventService) Inject(javax.inject.Inject) EventEntity(io.gravitee.rest.api.model.EventEntity) List(java.util.List) MediaType(io.gravitee.common.http.MediaType) javax.ws.rs(javax.ws.rs) EventQuery(io.gravitee.rest.api.model.EventQuery) Map(java.util.Map) io.swagger.annotations(io.swagger.annotations) EventTypeListParam(io.gravitee.rest.api.management.rest.resource.param.EventTypeListParam) Permissions(io.gravitee.rest.api.management.rest.security.Permissions) EventSearchParam(io.gravitee.rest.api.management.rest.resource.param.EventSearchParam) RolePermission(io.gravitee.rest.api.model.permissions.RolePermission) EventQuery(io.gravitee.rest.api.model.EventQuery) Permissions(io.gravitee.rest.api.management.rest.security.Permissions)

Example 5 with EventEntity

use of io.gravitee.rest.api.model.EventEntity in project gravitee-management-rest-api by gravitee-io.

the class EventServiceTest method shouldCreateEventWithPublishApiEventType.

@Test
public void shouldCreateEventWithPublishApiEventType() throws TechnicalException {
    when(event.getType()).thenReturn(EventType.PUBLISH_API);
    when(event.getPayload()).thenReturn(EVENT_PAYLOAD);
    when(event.getProperties()).thenReturn(EVENT_PROPERTIES);
    when(eventRepository.create(any())).thenReturn(event);
    when(newEvent.getType()).thenReturn(io.gravitee.rest.api.model.EventType.PUBLISH_API);
    when(newEvent.getPayload()).thenReturn(EVENT_PAYLOAD);
    when(newEvent.getProperties()).thenReturn(EVENT_PROPERTIES);
    final EventEntity eventEntity = eventService.create(newEvent);
    assertNotNull(eventEntity);
    assertEquals(EventType.PUBLISH_API.toString(), eventEntity.getType().toString());
    assertEquals(EVENT_PAYLOAD, eventEntity.getPayload());
    assertEquals(EVENT_USERNAME, eventEntity.getProperties().get(Event.EventProperties.USER.getValue()));
}
Also used : EventEntity(io.gravitee.rest.api.model.EventEntity) NewEventEntity(io.gravitee.rest.api.model.NewEventEntity) Test(org.junit.Test)

Aggregations

EventEntity (io.gravitee.rest.api.model.EventEntity)5 Permissions (io.gravitee.rest.api.management.rest.security.Permissions)3 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)3 HashMap (java.util.HashMap)3 NewEventEntity (io.gravitee.rest.api.model.NewEventEntity)2 ApiNotFoundException (io.gravitee.rest.api.service.exceptions.ApiNotFoundException)2 Test (org.junit.Test)2 Page (io.gravitee.common.data.domain.Page)1 MediaType (io.gravitee.common.http.MediaType)1 Event (io.gravitee.repository.management.model.Event)1 EventSearchParam (io.gravitee.rest.api.management.rest.resource.param.EventSearchParam)1 EventTypeListParam (io.gravitee.rest.api.management.rest.resource.param.EventTypeListParam)1 Permission (io.gravitee.rest.api.management.rest.security.Permission)1 EventQuery (io.gravitee.rest.api.model.EventQuery)1 RolePermission (io.gravitee.rest.api.model.permissions.RolePermission)1 RolePermissionAction (io.gravitee.rest.api.model.permissions.RolePermissionAction)1 EventService (io.gravitee.rest.api.service.EventService)1 io.swagger.annotations (io.swagger.annotations)1 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1