Search in sources :

Example 16 with ApplicationListItem

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

the class PermissionsResourceTest method init.

@Before
public void init() {
    resetAllMocks();
    ApiEntity mockApi = new ApiEntity();
    mockApi.setId(API);
    Set<ApiEntity> mockApis = new HashSet<>(Arrays.asList(mockApi));
    doReturn(mockApis).when(apiService).findPublishedByUser(any(), any());
    ApplicationListItem mockAppListItem = new ApplicationListItem();
    mockAppListItem.setId(APPLICATION);
    Set<ApplicationListItem> mockApps = new HashSet<>(Arrays.asList(mockAppListItem));
    doReturn(mockApps).when(applicationService).findByUser(any());
    ApplicationEntity mockAppEntity = new ApplicationEntity();
    mockAppEntity.setId(APPLICATION);
    doReturn(mockAppEntity).when(applicationService).findById(any());
}
Also used : ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Before(org.junit.Before)

Example 17 with ApplicationListItem

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

the class FilteringServiceTest method shouldGetApplicationsOrderByNbSubscriptionsDesc.

@Test
public void shouldGetApplicationsOrderByNbSubscriptionsDesc() {
    ApplicationListItem applicationListItem1 = new ApplicationListItem();
    applicationListItem1.setId("A");
    applicationListItem1.setName("A");
    ApplicationListItem applicationListItem2 = new ApplicationListItem();
    applicationListItem2.setId("B");
    applicationListItem2.setName("B");
    Set<FilterableItem> mockApplications = new HashSet<>(Arrays.asList(applicationListItem1, applicationListItem2));
    SubscriptionEntity subA1 = new SubscriptionEntity();
    subA1.setApplication("A");
    SubscriptionEntity subA2 = new SubscriptionEntity();
    subA2.setApplication("A");
    SubscriptionEntity subB1 = new SubscriptionEntity();
    subB1.setApplication("B");
    SubscriptionEntity subB2 = new SubscriptionEntity();
    subB2.setApplication("B");
    SubscriptionEntity subB3 = new SubscriptionEntity();
    subB3.setApplication("B");
    doReturn(Arrays.asList(subA1, subA2, subB1, subB2, subB3)).when(subscriptionService).search(any());
    FilteredEntities<FilterableItem> applicationListItemFilteredEntities = filteringService.getEntitiesOrderByNumberOfSubscriptions(mockApplications, false, false);
    List<FilterableItem> filteredItems = applicationListItemFilteredEntities.getFilteredItems();
    assertEquals("B", filteredItems.get(0).getId());
    assertEquals("A", filteredItems.get(1).getId());
    Map<String, Object> subscriptionsMetadata = applicationListItemFilteredEntities.getMetadata().get("subscriptions");
    assertEquals(3L, subscriptionsMetadata.get("B"));
    assertEquals(2L, subscriptionsMetadata.get("A"));
}
Also used : ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) FilterableItem(io.gravitee.rest.api.model.filtering.FilterableItem) Test(org.junit.Test)

Example 18 with ApplicationListItem

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

the class SubscriptionsResource method getSubscriptions.

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getSubscriptions(@QueryParam("apiId") String apiId, @QueryParam("applicationId") String applicationId, @QueryParam("statuses") List<SubscriptionStatus> statuses, @BeanParam PaginationParam paginationParam) {
    final boolean withoutPagination = paginationParam.getSize() != null && paginationParam.getSize().equals(-1);
    final SubscriptionQuery query = new SubscriptionQuery();
    query.setApi(apiId);
    query.setApplication(applicationId);
    final Map<String, Map<String, Object>> metadata = new HashMap<>();
    if (applicationId == null) {
        final Set<ApplicationListItem> applications = applicationService.findByUser(getAuthenticatedUser());
        if (applications == null || applications.isEmpty()) {
            return createListResponse(emptyList(), paginationParam, !withoutPagination);
        }
        query.setApplications(applications.stream().map(ApplicationListItem::getId).collect(toSet()));
        applications.forEach(application -> {
            final Map<String, Object> m = new HashMap<>();
            m.put("name", application.getName());
            metadata.put(application.getId(), m);
        });
    } else if (!hasPermission(RolePermission.APPLICATION_SUBSCRIPTION, applicationId, RolePermissionAction.READ)) {
        throw new ForbiddenAccessException();
    }
    if (statuses != null && !statuses.isEmpty()) {
        query.setStatuses(statuses);
    }
    final Collection<SubscriptionEntity> subscriptions;
    if (withoutPagination) {
        subscriptions = subscriptionService.search(query);
    } else {
        final Page<SubscriptionEntity> pagedSubscriptions = subscriptionService.search(query, new PageableImpl(paginationParam.getPage(), paginationParam.getSize()));
        if (pagedSubscriptions == null) {
            subscriptions = emptyList();
        } else {
            subscriptions = pagedSubscriptions.getContent();
        }
    }
    final List<Subscription> subscriptionList = subscriptions.stream().map(subscriptionMapper::convert).collect(Collectors.toList());
    subscriptionList.forEach(subscription -> {
        final ApiEntity api = apiService.findById(subscription.getApi());
        if (api != null) {
            final Map<String, Object> m = new HashMap<>();
            m.put("name", api.getName());
            m.put("pictureUrl", apiMapper.computeApiLinks(PortalApiLinkHelper.apisURL(uriInfo.getBaseUriBuilder(), api.getId()), api.getUpdatedAt()).getPicture());
            m.put("state", api.getLifecycleState());
            m.put("version", api.getVersion());
            m.put("entrypoints", api.getEntrypoints());
            metadata.put(api.getId(), m);
        }
        final PlanEntity plan = planService.findById(subscription.getPlan());
        if (plan != null) {
            final Map<String, Object> m = new HashMap<>();
            m.put("name", plan.getName());
            metadata.put(plan.getId(), m);
        }
        final UserEntity user = userService.findById(subscription.getSubscribedBy(), true);
        if (user != null) {
            final Map<String, Object> m = new HashMap<>();
            m.put("name", user.getDisplayName());
            metadata.put(user.getId(), m);
        }
    });
    return createListResponse(subscriptionList, paginationParam, metadata, !withoutPagination);
}
Also used : PageableImpl(io.gravitee.rest.api.model.common.PageableImpl) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) ForbiddenAccessException(io.gravitee.rest.api.service.exceptions.ForbiddenAccessException) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) Subscription(io.gravitee.rest.api.portal.rest.model.Subscription)

Example 19 with ApplicationListItem

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

the class ApplicationSubscribersResource method getSubscriberApisByApplicationId.

@GET
@Produces({ MediaType.APPLICATION_JSON })
public Response getSubscriberApisByApplicationId(@BeanParam PaginationParam paginationParam, @PathParam("applicationId") String applicationId, @QueryParam("statuses") List<SubscriptionStatus> statuses) {
    String currentUser = getAuthenticatedUserOrNull();
    Collection<ApplicationListItem> userApplications = applicationService.findByUser(currentUser);
    Optional<ApplicationListItem> optionalApplication = userApplications.stream().filter(a -> a.getId().equals(applicationId)).findFirst();
    if (optionalApplication.isPresent()) {
        SubscriptionQuery subscriptionQuery = new SubscriptionQuery();
        subscriptionQuery.setApplication(applicationId);
        subscriptionQuery.setStatuses(statuses);
        ApplicationListItem application = optionalApplication.get();
        if (!application.getPrimaryOwner().getId().equals(currentUser)) {
            Set<ApiEntity> userApis = this.apiService.findPublishedByUser(currentUser);
            if (userApis == null || userApis.isEmpty()) {
                return createListResponse(Collections.emptyList(), paginationParam);
            }
            subscriptionQuery.setApis(userApis.stream().map(ApiEntity::getId).collect(Collectors.toList()));
        }
        Map<String, Long> nbHitsByApp = getNbHitsByApplication(applicationId);
        Collection<SubscriptionEntity> subscriptions = subscriptionService.search(subscriptionQuery);
        List<Api> subscribersApis = subscriptions.stream().map(SubscriptionEntity::getApi).distinct().map(api -> apiService.findById(api)).map(apiMapper::convert).sorted((o1, o2) -> compareApp(nbHitsByApp, o1, o2)).collect(Collectors.toList());
        return createListResponse(subscribersApis, paginationParam);
    }
    throw new ApplicationNotFoundException(applicationId);
}
Also used : ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) java.util(java.util) ApiMapper(io.gravitee.rest.api.portal.rest.mapper.ApiMapper) Api(io.gravitee.rest.api.portal.rest.model.Api) SubscriptionStatus(io.gravitee.rest.api.model.SubscriptionStatus) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) GroupByQuery(io.gravitee.rest.api.model.analytics.query.GroupByQuery) Inject(javax.inject.Inject) SubscriptionService(io.gravitee.rest.api.service.SubscriptionService) MediaType(io.gravitee.common.http.MediaType) ChronoUnit(java.time.temporal.ChronoUnit) javax.ws.rs(javax.ws.rs) Response(javax.ws.rs.core.Response) PaginationParam(io.gravitee.rest.api.portal.rest.resource.param.PaginationParam) ApplicationService(io.gravitee.rest.api.service.ApplicationService) TopHitsAnalytics(io.gravitee.rest.api.model.analytics.TopHitsAnalytics) AnalyticsService(io.gravitee.rest.api.service.AnalyticsService) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) ApplicationNotFoundException(io.gravitee.rest.api.service.exceptions.ApplicationNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) ApplicationNotFoundException(io.gravitee.rest.api.service.exceptions.ApplicationNotFoundException) Api(io.gravitee.rest.api.portal.rest.model.Api)

Example 20 with ApplicationListItem

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

the class PlatformAnalyticsResource method getPlatformAnalytics.

@GET
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Get platform analytics", notes = "User must have the MANAGEMENT_PLATFORM[READ] permission to use this service")
@ApiResponses({ @ApiResponse(code = 200, message = "Platform analytics"), @ApiResponse(code = 500, message = "Internal server error") })
@Permissions({ @Permission(value = ENVIRONMENT_PLATFORM, acls = READ) })
public Response getPlatformAnalytics(@BeanParam AnalyticsParam analyticsParam) {
    analyticsParam.validate();
    Analytics analytics = null;
    // add filter by Apis or Applications
    String extraFilter = null;
    if (!isAdmin()) {
        String fieldName;
        List<String> ids;
        if ("application".equals(analyticsParam.getField())) {
            fieldName = "application";
            ids = applicationService.findByUser(getAuthenticatedUser()).stream().filter(app -> permissionService.hasPermission(APPLICATION_ANALYTICS, app.getId(), READ)).map(ApplicationListItem::getId).collect(Collectors.toList());
        } else {
            fieldName = "api";
            ids = apiService.findByUser(getAuthenticatedUser(), null, false).stream().filter(api -> permissionService.hasPermission(API_ANALYTICS, api.getId(), READ)).map(ApiEntity::getId).collect(Collectors.toList());
        }
        if (ids.isEmpty()) {
            return Response.noContent().build();
        }
        extraFilter = getExtraFilter(fieldName, ids);
    }
    if (analyticsParam.getQuery() != null) {
        analyticsParam.setQuery(analyticsParam.getQuery().replaceAll("\\?", "1"));
    }
    switch(analyticsParam.getTypeParam().getValue()) {
        case DATE_HISTO:
            analytics = executeDateHisto(analyticsParam, extraFilter);
            break;
        case GROUP_BY:
            analytics = executeGroupBy(analyticsParam, extraFilter);
            break;
        case COUNT:
            analytics = executeCount(analyticsParam, extraFilter);
            break;
        case STATS:
            analytics = executeStats(analyticsParam, extraFilter);
            break;
    }
    return Response.ok(analytics).build();
}
Also used : Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiResponses(io.swagger.annotations.ApiResponses) Function(java.util.function.Function) ApiService(io.gravitee.rest.api.service.ApiService) Inject(javax.inject.Inject) ApiOperation(io.swagger.annotations.ApiOperation) Aggregation(io.gravitee.rest.api.management.rest.resource.param.Aggregation) Map(java.util.Map) PermissionService(io.gravitee.rest.api.service.PermissionService) Analytics(io.gravitee.rest.api.model.analytics.Analytics) Api(io.swagger.annotations.Api) io.gravitee.rest.api.model.analytics.query(io.gravitee.rest.api.model.analytics.query) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) READ(io.gravitee.rest.api.model.permissions.RolePermissionAction.READ) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Collectors(java.util.stream.Collectors) BeanParam(javax.ws.rs.BeanParam) Permission(io.gravitee.rest.api.management.rest.security.Permission) List(java.util.List) MediaType(io.gravitee.common.http.MediaType) Response(javax.ws.rs.core.Response) ApiResponse(io.swagger.annotations.ApiResponse) AnalyticsParam(io.gravitee.rest.api.management.rest.resource.param.AnalyticsParam) ApplicationService(io.gravitee.rest.api.service.ApplicationService) Range(io.gravitee.rest.api.management.rest.resource.param.Range) AnalyticsService(io.gravitee.rest.api.service.AnalyticsService) Permissions(io.gravitee.rest.api.management.rest.security.Permissions) RolePermission(io.gravitee.rest.api.model.permissions.RolePermission) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Analytics(io.gravitee.rest.api.model.analytics.Analytics) 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)

Aggregations

ApplicationListItem (io.gravitee.rest.api.model.application.ApplicationListItem)29 Test (org.junit.Test)16 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)11 Response (javax.ws.rs.core.Response)9 SubscriptionEntity (io.gravitee.rest.api.model.SubscriptionEntity)8 SubscriptionQuery (io.gravitee.rest.api.model.subscription.SubscriptionQuery)7 ApplicationEntity (io.gravitee.rest.api.model.ApplicationEntity)6 Collectors (java.util.stream.Collectors)6 ApplicationService (io.gravitee.rest.api.service.ApplicationService)5 java.util (java.util)5 MediaType (io.gravitee.common.http.MediaType)4 FilterableItem (io.gravitee.rest.api.model.filtering.FilterableItem)4 Inject (javax.inject.Inject)4 Before (org.junit.Before)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 SimpleEvent (io.gravitee.common.event.impl.SimpleEvent)3 UserEntity (io.gravitee.rest.api.model.UserEntity)3 AlertTriggerEntity (io.gravitee.rest.api.model.alert.AlertTriggerEntity)3 ApiQuery (io.gravitee.rest.api.model.api.ApiQuery)3 Application (io.gravitee.rest.api.portal.rest.model.Application)3