Search in sources :

Example 21 with ApiEntity

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

the class PlatformEventsResource method listEvents.

@GET
@Produces(MediaType.APPLICATION_JSON)
@Permissions({ @Permission(value = RolePermission.MANAGEMENT_PLATFORM, acls = RolePermissionAction.READ) })
public Page<EventEntity> listEvents(@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());
    }
    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());
            ApiEntity api = apiService.findById(apiId);
            properties1.put("api_name", api.getName());
            properties1.put("api_version", api.getVersion());
        }
    });
    return events;
}
Also used : HashMap(java.util.HashMap) EventEntity(io.gravitee.management.model.EventEntity) ApiEntity(io.gravitee.management.model.ApiEntity) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Permissions(io.gravitee.management.rest.security.Permissions)

Example 22 with ApiEntity

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

the class PermissionsFilter method filter.

protected void filter(Permissions permissions, ContainerRequestContext requestContext) {
    if (permissions != null && permissions.value().length > 0) {
        Principal principal = securityContext.getUserPrincipal();
        if (principal != null) {
            String username = principal.getName();
            for (Permission permission : permissions.value()) {
                RoleEntity role;
                Map<String, char[]> memberPermissions;
                switch(permission.value().getScope()) {
                    case MANAGEMENT:
                        role = membershipService.getRole(MembershipReferenceType.MANAGEMENT, MembershipDefaultReferenceId.DEFAULT.name(), username, RoleScope.MANAGEMENT);
                        if (roleService.hasPermission(role.getPermissions(), permission.value().getPermission(), permission.acls())) {
                            return;
                        }
                        break;
                    case PORTAL:
                        role = membershipService.getRole(MembershipReferenceType.PORTAL, MembershipDefaultReferenceId.DEFAULT.name(), username, RoleScope.PORTAL);
                        if (roleService.hasPermission(role.getPermissions(), permission.value().getPermission(), permission.acls())) {
                            return;
                        }
                        break;
                    case APPLICATION:
                        ApplicationEntity application = getApplication(requestContext);
                        memberPermissions = membershipService.getMemberPermissions(application, username);
                        if (roleService.hasPermission(memberPermissions, permission.value().getPermission(), permission.acls())) {
                            return;
                        }
                        break;
                    case API:
                        ApiEntity api = getApi(requestContext);
                        memberPermissions = membershipService.getMemberPermissions(api, username);
                        if (roleService.hasPermission(memberPermissions, permission.value().getPermission(), permission.acls())) {
                            return;
                        }
                        break;
                    default:
                        sendSecurityError();
                }
            }
        }
        sendSecurityError();
    }
}
Also used : RoleEntity(io.gravitee.management.model.RoleEntity) ApplicationEntity(io.gravitee.management.model.ApplicationEntity) Permission(io.gravitee.management.rest.security.Permission) ApiEntity(io.gravitee.management.model.ApiEntity) Principal(java.security.Principal)

Example 23 with ApiEntity

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

the class HealthCheckServiceImpl method getResponseTime.

@Override
public ApiMetrics getResponseTime(String api, String field) {
    logger.debug("Run health response-time query for API '{}'", api);
    try {
        ApiEntity apiEntity = apiService.findById(api);
        AverageResponseTimeResponse response = healthCheckRepository.query(QueryBuilders.responseTime().api(api).field(AverageResponseTimeQuery.Field.valueOf(field)).build());
        return convert(apiEntity, response.getEndpointResponseTimes(), field);
    } catch (Exception ex) {
        logger.error("An unexpected error occurs while searching for health data.", ex);
        return null;
    }
}
Also used : AverageResponseTimeResponse(io.gravitee.repository.healthcheck.query.responsetime.AverageResponseTimeResponse) ApiEntity(io.gravitee.management.model.ApiEntity) AnalyticsException(io.gravitee.repository.analytics.AnalyticsException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 24 with ApiEntity

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

the class ApiSynchronizationProcessor method processCheckSynchronization.

public boolean processCheckSynchronization(ApiEntity deployedApi, ApiEntity apiToDeploy) {
    Class<ApiEntity> cl = ApiEntity.class;
    List<Object> requiredFieldsDeployedApi = new ArrayList<Object>();
    List<Object> requiredFieldsApiToDeploy = new ArrayList<Object>();
    for (Field f : cl.getDeclaredFields()) {
        if (f.getAnnotation(DeploymentRequired.class) != null) {
            boolean previousAccessibleState = f.isAccessible();
            f.setAccessible(true);
            try {
                requiredFieldsDeployedApi.add(f.get(deployedApi));
                requiredFieldsApiToDeploy.add(f.get(apiToDeploy));
            } catch (Exception e) {
                LOGGER.error("Error access API required deployment fields", e);
            } finally {
                f.setAccessible(previousAccessibleState);
            }
        }
    }
    try {
        String requiredFieldsDeployedApiDefinition = objectMapper.writeValueAsString(requiredFieldsDeployedApi);
        String requiredFieldsApiToDeployDefinition = objectMapper.writeValueAsString(requiredFieldsApiToDeploy);
        return requiredFieldsDeployedApiDefinition.equals(requiredFieldsApiToDeployDefinition);
    } catch (Exception e) {
        LOGGER.error("Unexpected error while generating API deployment required fields definition", e);
        return false;
    }
}
Also used : Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) ApiEntity(io.gravitee.management.model.ApiEntity) DeploymentRequired(io.gravitee.management.model.DeploymentRequired)

Example 25 with ApiEntity

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

the class ApiService_CreateTest method shouldCreateWithDefaultPath.

@Test
public void shouldCreateWithDefaultPath() throws TechnicalException {
    when(api.getId()).thenReturn(API_ID);
    when(api.getName()).thenReturn(API_NAME);
    when(api.getVisibility()).thenReturn(Visibility.PRIVATE);
    when(apiRepository.findById(anyString())).thenReturn(Optional.empty());
    when(apiRepository.create(any())).thenReturn(api);
    when(newApi.getName()).thenReturn(API_NAME);
    when(newApi.getVersion()).thenReturn("v1");
    when(newApi.getDescription()).thenReturn("Ma description");
    when(newApi.getContextPath()).thenReturn("/context");
    UserEntity admin = new UserEntity();
    admin.setUsername(USER_NAME);
    when(userService.findById(admin.getUsername())).thenReturn(admin);
    final ApiEntity apiEntity = apiService.create(newApi, USER_NAME);
    assertNotNull(apiEntity);
    assertEquals(API_NAME, apiEntity.getName());
    assertNotNull(apiEntity.getPaths());
/*assertTrue("paths not empty", !apiEntity.getPaths().isEmpty());
        assertEquals("paths.size == 1", apiEntity.getPaths().size(), 1);
        assertEquals("path == /* ", apiEntity.getPaths().get(0).getPath(), "/*");*/
}
Also used : ApiEntity(io.gravitee.management.model.ApiEntity) NewApiEntity(io.gravitee.management.model.NewApiEntity) UserEntity(io.gravitee.management.model.UserEntity) Test(org.junit.Test)

Aggregations

ApiEntity (io.gravitee.management.model.ApiEntity)50 Test (org.junit.Test)34 Response (javax.ws.rs.core.Response)19 UpdateApiEntity (io.gravitee.management.model.UpdateApiEntity)13 PageEntity (io.gravitee.management.model.PageEntity)5 NewApiEntity (io.gravitee.management.model.NewApiEntity)4 NewTopApiEntity (io.gravitee.management.model.NewTopApiEntity)3 TopApiEntity (io.gravitee.management.model.TopApiEntity)3 UpdateTopApiEntity (io.gravitee.management.model.UpdateTopApiEntity)3 Permission (io.gravitee.management.rest.security.Permission)3 HashMap (java.util.HashMap)3 Collectors (java.util.stream.Collectors)3 Proxy (io.gravitee.definition.model.Proxy)2 ApplicationEntity (io.gravitee.management.model.ApplicationEntity)2 MemberEntity (io.gravitee.management.model.MemberEntity)2 SubscriptionEntity (io.gravitee.management.model.SubscriptionEntity)2 UserEntity (io.gravitee.management.model.UserEntity)2 RolePermission (io.gravitee.management.model.permissions.RolePermission)2 RolePermissionAction (io.gravitee.management.model.permissions.RolePermissionAction)2 SubscriptionQuery (io.gravitee.management.model.subscription.SubscriptionQuery)2