Search in sources :

Example 76 with ApiEntity

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

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

the class ApiRatingAnswerResource method deleteApiRatingAnswer.

@DELETE
@Produces(MediaType.APPLICATION_JSON)
@Permissions({ @Permission(value = RolePermission.API_RATING_ANSWER, acls = RolePermissionAction.DELETE) })
public Response deleteApiRatingAnswer(@PathParam("apiId") String apiId, @PathParam("ratingId") String ratingId, @PathParam("answerId") String answerId) {
    final ApiQuery apiQuery = new ApiQuery();
    apiQuery.setIds(Collections.singletonList(apiId));
    Collection<ApiEntity> userApis = apiService.findPublishedByUser(getAuthenticatedUserOrNull(), apiQuery);
    if (userApis.stream().anyMatch(a -> a.getId().equals(apiId))) {
        RatingEntity ratingEntity = ratingService.findById(ratingId);
        if (ratingEntity != null && ratingEntity.getApi().equals(apiId)) {
            if (ratingEntity.getAnswers().stream().anyMatch(answer -> answer.getId().equals(answerId))) {
                ratingService.deleteAnswer(ratingId, answerId);
                return Response.status(Status.NO_CONTENT).build();
            }
            throw new RatingAnswerNotFoundException(answerId, ratingId, apiId);
        }
        throw new RatingNotFoundException(ratingId, apiId);
    }
    throw new ApiNotFoundException(apiId);
}
Also used : ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) RatingNotFoundException(io.gravitee.rest.api.service.exceptions.RatingNotFoundException) RatingAnswerNotFoundException(io.gravitee.rest.api.service.exceptions.RatingAnswerNotFoundException) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) RatingEntity(io.gravitee.rest.api.model.RatingEntity) DELETE(javax.ws.rs.DELETE) Produces(javax.ws.rs.Produces) Permissions(io.gravitee.rest.api.portal.rest.security.Permissions)

Example 78 with ApiEntity

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

the class ApiRatingResource method deleteApiRating.

@DELETE
@Produces(MediaType.APPLICATION_JSON)
@Permissions({ @Permission(value = RolePermission.API_RATING, acls = RolePermissionAction.DELETE) })
public Response deleteApiRating(@PathParam("apiId") String apiId, @PathParam("ratingId") String ratingId) {
    // FIXME: are we sure we need to fetch the api while the permission system alreay allowed the user to delete the rating ?
    final ApiQuery apiQuery = new ApiQuery();
    apiQuery.setIds(Collections.singletonList(apiId));
    Collection<ApiEntity> userApis = apiService.findPublishedByUser(getAuthenticatedUserOrNull(), apiQuery);
    if (userApis.stream().anyMatch(a -> a.getId().equals(apiId))) {
        RatingEntity ratingEntity = ratingService.findById(ratingId);
        if (ratingEntity != null && ratingEntity.getApi().equals(apiId)) {
            ratingService.delete(ratingId);
            return Response.status(Status.NO_CONTENT).build();
        }
        throw new RatingNotFoundException(ratingId, apiId);
    }
    throw new ApiNotFoundException(apiId);
}
Also used : ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) RatingNotFoundException(io.gravitee.rest.api.service.exceptions.RatingNotFoundException) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) UpdateRatingEntity(io.gravitee.rest.api.model.UpdateRatingEntity) RatingEntity(io.gravitee.rest.api.model.RatingEntity) Permissions(io.gravitee.rest.api.portal.rest.security.Permissions)

Example 79 with ApiEntity

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

the class ApiRatingResource method updateApiRating.

@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Permissions({ @Permission(value = RolePermission.API_RATING, acls = RolePermissionAction.UPDATE) })
public Response updateApiRating(@PathParam("apiId") String apiId, @PathParam("ratingId") String ratingId, @Valid RatingInput ratingInput) {
    if (ratingInput == null) {
        throw new BadRequestException("Input must not be null.");
    }
    final ApiQuery apiQuery = new ApiQuery();
    apiQuery.setIds(Collections.singletonList(apiId));
    Collection<ApiEntity> userApis = apiService.findPublishedByUser(getAuthenticatedUserOrNull(), apiQuery);
    if (userApis.stream().anyMatch(a -> a.getId().equals(apiId))) {
        RatingEntity ratingEntity = ratingService.findById(ratingId);
        if (ratingEntity != null && ratingEntity.getApi().equals(apiId)) {
            UpdateRatingEntity rating = new UpdateRatingEntity();
            rating.setId(ratingId);
            rating.setApi(apiId);
            rating.setComment(ratingInput.getComment());
            rating.setTitle(ratingInput.getTitle());
            rating.setRate(ratingInput.getValue().byteValue());
            RatingEntity updatedRating = ratingService.update(rating);
            return Response.status(Status.OK).entity(ratingMapper.convert(updatedRating, uriInfo)).build();
        }
        throw new RatingNotFoundException(ratingId, apiId);
    }
    throw new ApiNotFoundException(apiId);
}
Also used : ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) RatingNotFoundException(io.gravitee.rest.api.service.exceptions.RatingNotFoundException) UpdateRatingEntity(io.gravitee.rest.api.model.UpdateRatingEntity) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) UpdateRatingEntity(io.gravitee.rest.api.model.UpdateRatingEntity) RatingEntity(io.gravitee.rest.api.model.RatingEntity) Permissions(io.gravitee.rest.api.portal.rest.security.Permissions)

Example 80 with ApiEntity

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

the class ApiResource method getApiByApiId.

@GET
@Produces({ MediaType.APPLICATION_JSON })
@RequirePortalAuth
public Response getApiByApiId(@PathParam("apiId") String apiId, @QueryParam("include") List<String> include) {
    String username = getAuthenticatedUserOrNull();
    if (accessControlService.canAccessApiFromPortal(apiId)) {
        ApiEntity apiEntity = apiService.findById(apiId);
        Api api = apiMapper.convert(apiEntity);
        if (include.contains(INCLUDE_PAGES)) {
            List<Page> pages = pageService.search(new PageQuery.Builder().api(apiId).published(true).build(), GraviteeContext.getCurrentEnvironment()).stream().filter(page -> accessControlService.canAccessPageFromPortal(page)).map(pageMapper::convert).collect(Collectors.toList());
            api.setPages(pages);
        }
        if (include.contains(INCLUDE_PLANS)) {
            List<Plan> plans = planService.findByApi(apiId).stream().filter(plan -> PlanStatus.PUBLISHED.equals(plan.getStatus())).filter(plan -> groupService.isUserAuthorizedToAccessApiData(apiEntity, plan.getExcludedGroups(), username)).sorted(Comparator.comparingInt(PlanEntity::getOrder)).map(p -> planMapper.convert(p)).collect(Collectors.toList());
            api.setPlans(plans);
        }
        api.links(apiMapper.computeApiLinks(PortalApiLinkHelper.apisURL(uriInfo.getBaseUriBuilder(), api.getId()), apiEntity.getUpdatedAt()));
        if (!parameterService.findAsBoolean(Key.PORTAL_APIS_SHOW_TAGS_IN_APIHEADER, ParameterReferenceType.ENVIRONMENT)) {
            api.setLabels(new ArrayList<>());
        }
        if (!parameterService.findAsBoolean(Key.PORTAL_APIS_SHOW_CATEGORIES_IN_APIHEADER, ParameterReferenceType.ENVIRONMENT)) {
            api.setCategories(new ArrayList<>());
        }
        return Response.ok(api).build();
    }
    throw new ApiNotFoundException(apiId);
}
Also used : PlanMapper(io.gravitee.rest.api.portal.rest.mapper.PlanMapper) io.gravitee.rest.api.service(io.gravitee.rest.api.service) java.util(java.util) ApiMapper(io.gravitee.rest.api.portal.rest.mapper.ApiMapper) GraviteeContext(io.gravitee.rest.api.service.common.GraviteeContext) RequirePortalAuth(io.gravitee.rest.api.portal.rest.security.RequirePortalAuth) Inject(javax.inject.Inject) PageQuery(io.gravitee.rest.api.model.documentation.PageQuery) io.gravitee.rest.api.model(io.gravitee.rest.api.model) ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Context(javax.ws.rs.core.Context) ParameterReferenceType(io.gravitee.rest.api.model.parameters.ParameterReferenceType) ResourceTypeEnum(io.gravitee.rest.api.portal.rest.model.Link.ResourceTypeEnum) io.gravitee.rest.api.portal.rest.model(io.gravitee.rest.api.portal.rest.model) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) HttpHeadersUtil(io.gravitee.rest.api.portal.rest.utils.HttpHeadersUtil) Collectors(java.util.stream.Collectors) Key(io.gravitee.rest.api.model.parameters.Key) MediaType(io.gravitee.common.http.MediaType) PortalApiLinkHelper(io.gravitee.rest.api.portal.rest.utils.PortalApiLinkHelper) javax.ws.rs(javax.ws.rs) Response(javax.ws.rs.core.Response) ResourceContext(javax.ws.rs.container.ResourceContext) Request(javax.ws.rs.core.Request) PageMapper(io.gravitee.rest.api.portal.rest.mapper.PageMapper) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) RequirePortalAuth(io.gravitee.rest.api.portal.rest.security.RequirePortalAuth)

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