Search in sources :

Example 1 with ApiEntity

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

the class ApiPagesResource method getApiPages.

@GET
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "List pages", notes = "User must have the READ permission to use this service")
@ApiResponses({ @ApiResponse(code = 200, message = "List of pages", response = PageEntity.class, responseContainer = "List"), @ApiResponse(code = 500, message = "Internal server error") })
public List<PageEntity> getApiPages(@HeaderParam("Accept-Language") String acceptLang, @QueryParam("homepage") Boolean homepage, @QueryParam("type") PageType type, @QueryParam("parent") String parent, @QueryParam("name") String name, @QueryParam("root") Boolean rootParent, @QueryParam("translated") boolean translated) {
    final String acceptedLocale = HttpHeadersUtil.getFirstAcceptedLocaleName(acceptLang);
    final ApiEntity apiEntity = apiService.findById(api);
    if (Visibility.PUBLIC.equals(apiEntity.getVisibility()) || hasPermission(RolePermission.API_DOCUMENTATION, api, RolePermissionAction.READ)) {
        return pageService.search(new PageQuery.Builder().api(api).homepage(homepage).type(type).parent(parent).name(name).rootParent(rootParent).build(), translated ? acceptedLocale : null, GraviteeContext.getCurrentEnvironment()).stream().filter(page -> isDisplayable(apiEntity, page)).map(page -> {
            // check if the page is used as GeneralCondition by an active Plan
            // and update the PageEntity to transfer the information to the FrontEnd
            page.setGeneralConditions(pageService.isPageUsedAsGeneralConditions(page, api));
            return page;
        }).collect(Collectors.toList());
    }
    throw new ForbiddenAccessException();
}
Also used : GraviteeContext(io.gravitee.rest.api.service.common.GraviteeContext) ApiService(io.gravitee.rest.api.service.ApiService) Inject(javax.inject.Inject) Valid(javax.validation.Valid) PageMarkdownTemplateActionException(io.gravitee.rest.api.service.exceptions.PageMarkdownTemplateActionException) PageQuery(io.gravitee.rest.api.model.documentation.PageQuery) io.gravitee.rest.api.model(io.gravitee.rest.api.model) PageService(io.gravitee.rest.api.service.PageService) io.swagger.annotations(io.swagger.annotations) HttpHeadersUtil(io.gravitee.rest.api.management.rest.utils.HttpHeadersUtil) RolePermissionAction(io.gravitee.rest.api.model.permissions.RolePermissionAction) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Context(javax.ws.rs.core.Context) ForbiddenAccessException(io.gravitee.rest.api.service.exceptions.ForbiddenAccessException) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) Permission(io.gravitee.rest.api.management.rest.security.Permission) AccessControlService(io.gravitee.rest.api.service.AccessControlService) List(java.util.List) MediaType(io.gravitee.common.http.MediaType) javax.ws.rs(javax.ws.rs) Response(javax.ws.rs.core.Response) ResourceContext(javax.ws.rs.container.ResourceContext) PageSystemFolderActionException(io.gravitee.rest.api.service.exceptions.PageSystemFolderActionException) Permissions(io.gravitee.rest.api.management.rest.security.Permissions) RolePermission(io.gravitee.rest.api.model.permissions.RolePermission) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) ForbiddenAccessException(io.gravitee.rest.api.service.exceptions.ForbiddenAccessException)

Example 2 with ApiEntity

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

the class ApplicationSubscriptionsResource method convert.

private Subscription convert(SubscriptionEntity subscriptionEntity) {
    Subscription subscription = new Subscription();
    subscription.setId(subscriptionEntity.getId());
    subscription.setCreatedAt(subscriptionEntity.getCreatedAt());
    subscription.setUpdatedAt(subscriptionEntity.getUpdatedAt());
    subscription.setStartingAt(subscriptionEntity.getStartingAt());
    subscription.setEndingAt(subscriptionEntity.getEndingAt());
    subscription.setProcessedAt(subscriptionEntity.getProcessedAt());
    subscription.setProcessedBy(subscriptionEntity.getProcessedBy());
    subscription.setReason(subscriptionEntity.getReason());
    subscription.setRequest(subscriptionEntity.getRequest());
    subscription.setStatus(subscriptionEntity.getStatus());
    subscription.setSubscribedBy(new Subscription.User(subscriptionEntity.getSubscribedBy(), userService.findById(subscriptionEntity.getSubscribedBy(), true).getDisplayName()));
    PlanEntity plan = planService.findById(subscriptionEntity.getPlan());
    subscription.setPlan(new Subscription.Plan(plan.getId(), plan.getName()));
    subscription.getPlan().setSecurity(plan.getSecurity());
    ApiEntity api = apiService.findById(subscriptionEntity.getApi());
    subscription.setApi(new Subscription.Api(api.getId(), api.getName(), api.getVersion(), new Subscription.User(api.getPrimaryOwner().getId(), api.getPrimaryOwner().getDisplayName())));
    subscription.setClosedAt(subscriptionEntity.getClosedAt());
    subscription.setPausedAt(subscriptionEntity.getPausedAt());
    return subscription;
}
Also used : PlanEntity(io.gravitee.rest.api.model.PlanEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Subscription(io.gravitee.rest.api.management.rest.model.Subscription)

Example 3 with ApiEntity

use of io.gravitee.rest.api.model.api.ApiEntity 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 4 with ApiEntity

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

the class TaskServiceImpl method getMetadata.

@Override
public Metadata getMetadata(List<TaskEntity> tasks) {
    final Metadata metadata = new Metadata();
    tasks.forEach(task -> {
        final Object data = task.getData();
        if (data instanceof SubscriptionEntity) {
            final SubscriptionEntity subscription = (SubscriptionEntity) data;
            if (!metadata.containsKey(subscription.getApplication())) {
                ApplicationEntity applicationEntity = applicationService.findById(subscription.getApplication());
                metadata.put(subscription.getApplication(), "name", applicationEntity.getName());
            }
            if (!metadata.containsKey(subscription.getPlan())) {
                PlanEntity planEntity = planService.findById(subscription.getPlan());
                String apiId = planEntity.getApi();
                ApiEntity api = apiService.findById(apiId);
                metadata.put(subscription.getPlan(), "name", planEntity.getName());
                metadata.put(subscription.getPlan(), "api", apiId);
                metadata.put(apiId, "name", api.getName());
            }
        } else if (data instanceof Workflow) {
            final Workflow workflow = (Workflow) data;
            if (API.name().equals(workflow.getReferenceType()) && !metadata.containsKey(workflow.getReferenceId())) {
                ApiEntity api = apiService.findById(workflow.getReferenceId());
                metadata.put(api.getId(), "name", api.getName());
            }
        }
    });
    return metadata;
}
Also used : Metadata(io.gravitee.rest.api.model.pagedresult.Metadata) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Workflow(io.gravitee.repository.management.model.Workflow)

Example 5 with ApiEntity

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

the class FilteringServiceImpl method getRatedApis.

private FilteredEntities<ApiEntity> getRatedApis(Collection<ApiEntity> apis, boolean excluded) {
    // keep apis with ratings
    Map<ApiEntity, RatingSummaryEntity> ratings = new HashMap<>();
    // APIPortal: should create a specific service to retrieve all the information
    // in one call to the repository
    apis.forEach(api -> {
        RatingSummaryEntity apiRatingSummary = ratingService.findSummaryByApi(api.getId());
        if (apiRatingSummary != null && apiRatingSummary.getNumberOfRatings() > 0) {
            ratings.put(api, apiRatingSummary);
        }
    });
    if (excluded) {
        return new FilteredEntities<>(apis.stream().filter(api -> !ratings.containsKey(api)).collect(Collectors.toList()), null);
    } else {
        // sort apis by ratings, nb of ratings, and name
        return new FilteredEntities<>(ratings.entrySet().stream().sorted((e1, e2) -> {
            RatingSummaryEntity o1 = e1.getValue();
            RatingSummaryEntity o2 = e2.getValue();
            int averageRateComparaison = Double.compare(o2.getAverageRate(), o1.getAverageRate());
            if (averageRateComparaison != 0) {
                return averageRateComparaison;
            }
            int nbRatingsComparaison = Integer.compare(o2.getNumberOfRatings(), o1.getNumberOfRatings());
            if (nbRatingsComparaison != 0) {
                return nbRatingsComparaison;
            }
            return String.CASE_INSENSITIVE_ORDER.compare(e1.getKey().getName(), e2.getKey().getName());
        }).map(Map.Entry::getKey).collect(Collectors.toList()), null);
    }
}
Also used : ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) java.util(java.util) TopApiService(io.gravitee.rest.api.service.TopApiService) SubscriptionStatus(io.gravitee.rest.api.model.SubscriptionStatus) RatingService(io.gravitee.rest.api.service.RatingService) Autowired(org.springframework.beans.factory.annotation.Autowired) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) FilterableItem(io.gravitee.rest.api.model.filtering.FilterableItem) SubscriptionService(io.gravitee.rest.api.service.SubscriptionService) Component(org.springframework.stereotype.Component) RatingSummaryEntity(io.gravitee.rest.api.model.RatingSummaryEntity) ApplicationService(io.gravitee.rest.api.service.ApplicationService) AbstractService(io.gravitee.rest.api.service.impl.AbstractService) TopApiEntity(io.gravitee.rest.api.model.TopApiEntity) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) FilteringService(io.gravitee.rest.api.service.filtering.FilteringService) FilteredEntities(io.gravitee.rest.api.model.filtering.FilteredEntities) RatingSummaryEntity(io.gravitee.rest.api.model.RatingSummaryEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) TopApiEntity(io.gravitee.rest.api.model.TopApiEntity) FilteredEntities(io.gravitee.rest.api.model.filtering.FilteredEntities)

Aggregations

ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)273 Test (org.junit.Test)180 Response (javax.ws.rs.core.Response)65 Before (org.junit.Before)46 ApiQuery (io.gravitee.rest.api.model.api.ApiQuery)35 Error (io.gravitee.rest.api.portal.rest.model.Error)32 java.util (java.util)30 Collectors (java.util.stream.Collectors)27 Mockito.doReturn (org.mockito.Mockito.doReturn)27 Mockito (org.mockito.Mockito)25 ArgumentMatchers (org.mockito.ArgumentMatchers)24 UpdateApiEntity (io.gravitee.rest.api.model.api.UpdateApiEntity)23 Assert (org.junit.Assert)22 Collections.singletonList (java.util.Collections.singletonList)21 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)20 IOException (java.io.IOException)20 HttpStatusCode (io.gravitee.common.http.HttpStatusCode)19 Entity (javax.ws.rs.client.Entity)19 HashSet (java.util.HashSet)18 Api (io.gravitee.repository.management.model.Api)17