Search in sources :

Example 1 with SubscriptionStatus

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

the class ApiSubscribersResource method getSubscriberApplicationsByApiId.

@GET
@Produces({ MediaType.APPLICATION_JSON })
public Response getSubscriberApplicationsByApiId(@BeanParam PaginationParam paginationParam, @PathParam("apiId") String apiId, @QueryParam("statuses") List<SubscriptionStatus> statuses) {
    String currentUser = getAuthenticatedUserOrNull();
    final ApiQuery apiQuery = new ApiQuery();
    apiQuery.setIds(Collections.singletonList(apiId));
    Collection<ApiEntity> userApis = apiService.findPublishedByUser(currentUser, apiQuery);
    Optional<ApiEntity> optionalApi = userApis.stream().filter(a -> a.getId().equals(apiId)).findFirst();
    if (optionalApi.isPresent()) {
        SubscriptionQuery subscriptionQuery = new SubscriptionQuery();
        subscriptionQuery.setApi(apiId);
        subscriptionQuery.setStatuses(statuses);
        ApiEntity api = optionalApi.get();
        if (!api.getPrimaryOwner().getId().equals(currentUser)) {
            Set<ApplicationListItem> userApplications = this.applicationService.findByUser(currentUser);
            if (userApplications == null || userApplications.isEmpty()) {
                return createListResponse(Collections.emptyList(), paginationParam);
            }
            subscriptionQuery.setApplications(userApplications.stream().map(ApplicationListItem::getId).collect(Collectors.toList()));
        }
        Map<String, Long> nbHitsByApp = getNbHitsByApplication(apiId);
        Collection<SubscriptionEntity> subscriptions = subscriptionService.search(subscriptionQuery);
        List<Application> subscribersApplication = subscriptions.stream().map(SubscriptionEntity::getApplication).distinct().map(application -> applicationService.findById(application)).map(application -> applicationMapper.convert(application, uriInfo)).sorted((o1, o2) -> compareApp(nbHitsByApp, o1, o2)).collect(Collectors.toList());
        return createListResponse(subscribersApplication, paginationParam);
    }
    throw new ApiNotFoundException(apiId);
}
Also used : ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) java.util(java.util) SubscriptionStatus(io.gravitee.rest.api.model.SubscriptionStatus) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) ApplicationMapper(io.gravitee.rest.api.portal.rest.mapper.ApplicationMapper) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Application(io.gravitee.rest.api.portal.rest.model.Application) 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) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) Application(io.gravitee.rest.api.portal.rest.model.Application)

Example 2 with SubscriptionStatus

use of io.gravitee.rest.api.model.SubscriptionStatus 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)

Aggregations

MediaType (io.gravitee.common.http.MediaType)2 SubscriptionEntity (io.gravitee.rest.api.model.SubscriptionEntity)2 SubscriptionStatus (io.gravitee.rest.api.model.SubscriptionStatus)2 TopHitsAnalytics (io.gravitee.rest.api.model.analytics.TopHitsAnalytics)2 GroupByQuery (io.gravitee.rest.api.model.analytics.query.GroupByQuery)2 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)2 ApplicationListItem (io.gravitee.rest.api.model.application.ApplicationListItem)2 SubscriptionQuery (io.gravitee.rest.api.model.subscription.SubscriptionQuery)2 PaginationParam (io.gravitee.rest.api.portal.rest.resource.param.PaginationParam)2 AnalyticsService (io.gravitee.rest.api.service.AnalyticsService)2 ApplicationService (io.gravitee.rest.api.service.ApplicationService)2 SubscriptionService (io.gravitee.rest.api.service.SubscriptionService)2 Instant (java.time.Instant)2 ChronoUnit (java.time.temporal.ChronoUnit)2 java.util (java.util)2 Collectors (java.util.stream.Collectors)2 Inject (javax.inject.Inject)2 javax.ws.rs (javax.ws.rs)2 Response (javax.ws.rs.core.Response)2 ApiQuery (io.gravitee.rest.api.model.api.ApiQuery)1