Search in sources :

Example 16 with ApiNotFoundException

use of io.gravitee.rest.api.service.exceptions.ApiNotFoundException in project gravitee-management-rest-api by gravitee-io.

the class ApiMetricsResource method getApiMetricsByApiId.

@GET
@Produces({ MediaType.APPLICATION_JSON })
@RequirePortalAuth
public Response getApiMetricsByApiId(@Context Request request, @PathParam("apiId") String apiId) {
    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))) {
        Number healthRatio = getHealthRatio(apiId);
        Number nbHits = getNbHits(apiId);
        Number subscribers = getApiNbSubscribers(apiId);
        ApiMetrics metrics = new ApiMetrics();
        metrics.setHealth(healthRatio);
        metrics.setHits(nbHits);
        metrics.setSubscribers(subscribers);
        return Response.ok(metrics).build();
    }
    throw new ApiNotFoundException(apiId);
}
Also used : ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) ApiMetrics(io.gravitee.rest.api.portal.rest.model.ApiMetrics) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) RequirePortalAuth(io.gravitee.rest.api.portal.rest.security.RequirePortalAuth)

Example 17 with ApiNotFoundException

use of io.gravitee.rest.api.service.exceptions.ApiNotFoundException in project gravitee-management-rest-api by gravitee-io.

the class ApiPageResource method getPageByApiIdAndPageId.

@GET
@Produces(MediaType.APPLICATION_JSON)
@RequirePortalAuth
public Response getPageByApiIdAndPageId(@HeaderParam("Accept-Language") String acceptLang, @PathParam("apiId") String apiId, @PathParam("pageId") String pageId, @QueryParam("include") List<String> include) {
    final ApiQuery apiQuery = new ApiQuery();
    apiQuery.setIds(Collections.singletonList(apiId));
    if (accessControlService.canAccessApiFromPortal(apiId)) {
        final String acceptedLocale = HttpHeadersUtil.getFirstAcceptedLocaleName(acceptLang);
        PageEntity pageEntity = pageService.findById(pageId, acceptedLocale);
        if (accessControlService.canAccessPageFromPortal(apiId, pageEntity)) {
            pageService.transformSwagger(pageEntity, apiId);
            if (!isAuthenticated() && pageEntity.getMetadata() != null) {
                pageEntity.getMetadata().clear();
            }
            Page page = pageMapper.convert(uriInfo.getBaseUriBuilder(), apiId, pageEntity);
            if (include.contains(INCLUDE_CONTENT)) {
                page.setContent(pageEntity.getContent());
            }
            page.setLinks(pageMapper.computePageLinks(PortalApiLinkHelper.apiPagesURL(uriInfo.getBaseUriBuilder(), apiId, pageId), PortalApiLinkHelper.apiPagesURL(uriInfo.getBaseUriBuilder(), apiId, page.getParent())));
            return Response.ok(page).build();
        } else {
            throw new UnauthorizedAccessException();
        }
    }
    throw new ApiNotFoundException(apiId);
}
Also used : PageEntity(io.gravitee.rest.api.model.PageEntity) ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) UnauthorizedAccessException(io.gravitee.rest.api.service.exceptions.UnauthorizedAccessException) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) Page(io.gravitee.rest.api.portal.rest.model.Page) RequirePortalAuth(io.gravitee.rest.api.portal.rest.security.RequirePortalAuth)

Example 18 with ApiNotFoundException

use of io.gravitee.rest.api.service.exceptions.ApiNotFoundException in project gravitee-management-rest-api by gravitee-io.

the class ApiRatingAnswersResource method createApiRatingAnswer.

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Permissions({ @Permission(value = RolePermission.API_RATING_ANSWER, acls = RolePermissionAction.CREATE) })
public Response createApiRatingAnswer(@PathParam("apiId") String apiId, @PathParam("ratingId") String ratingId, @Valid RatingAnswerInput ratingAnswerInput) {
    if (ratingAnswerInput == 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)) {
            NewRatingAnswerEntity ratingAnswerEntity = new NewRatingAnswerEntity();
            ratingAnswerEntity.setComment(ratingAnswerInput.getComment());
            ratingAnswerEntity.setRatingId(ratingId);
            RatingEntity updatedRating = ratingService.createAnswer(ratingAnswerEntity);
            return Response.status(Status.CREATED).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) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) NewRatingAnswerEntity(io.gravitee.rest.api.model.NewRatingAnswerEntity) RatingEntity(io.gravitee.rest.api.model.RatingEntity) Permissions(io.gravitee.rest.api.portal.rest.security.Permissions)

Example 19 with ApiNotFoundException

use of io.gravitee.rest.api.service.exceptions.ApiNotFoundException in project gravitee-management-rest-api by gravitee-io.

the class ApiRatingsResource method getApiRatingsByApiId.

@GET
@Produces(MediaType.APPLICATION_JSON)
@RequirePortalAuth
public Response getApiRatingsByApiId(@PathParam("apiId") String apiId, @BeanParam PaginationParam paginationParam, @QueryParam("mine") Boolean mine, @QueryParam("order") String order) {
    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))) {
        List<Rating> ratings;
        if (mine != null && mine == true) {
            RatingEntity ratingEntity = ratingService.findByApiForConnectedUser(apiId);
            if (ratingEntity != null) {
                ratings = Arrays.asList(ratingMapper.convert(ratingEntity, uriInfo));
            } else {
                ratings = Collections.emptyList();
            }
        } else {
            final List<RatingEntity> ratingEntities = ratingService.findByApi(apiId);
            Stream<Rating> ratingStream = ratingEntities.stream().map((RatingEntity ratingEntity) -> ratingMapper.convert(ratingEntity, uriInfo));
            if (order != null) {
                ratingStream = ratingStream.sorted(new RatingComparator(order));
            }
            ratings = ratingStream.collect(toList());
        }
        return createListResponse(ratings, paginationParam, true);
    }
    throw new ApiNotFoundException(apiId);
}
Also used : ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) Rating(io.gravitee.rest.api.portal.rest.model.Rating) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) RatingEntity(io.gravitee.rest.api.model.RatingEntity) NewRatingEntity(io.gravitee.rest.api.model.NewRatingEntity) RequirePortalAuth(io.gravitee.rest.api.portal.rest.security.RequirePortalAuth)

Aggregations

ApiNotFoundException (io.gravitee.rest.api.service.exceptions.ApiNotFoundException)19 ApiQuery (io.gravitee.rest.api.model.api.ApiQuery)18 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)16 RequirePortalAuth (io.gravitee.rest.api.portal.rest.security.RequirePortalAuth)9 Produces (javax.ws.rs.Produces)7 MediaType (io.gravitee.common.http.MediaType)6 RatingEntity (io.gravitee.rest.api.model.RatingEntity)6 Inject (javax.inject.Inject)6 GET (javax.ws.rs.GET)6 Response (javax.ws.rs.core.Response)6 Permissions (io.gravitee.rest.api.portal.rest.security.Permissions)5 Collectors (java.util.stream.Collectors)5 RatingNotFoundException (io.gravitee.rest.api.service.exceptions.RatingNotFoundException)4 Collections (java.util.Collections)4 PaginationParam (io.gravitee.rest.api.portal.rest.resource.param.PaginationParam)3 List (java.util.List)3 javax.ws.rs (javax.ws.rs)3 Context (javax.ws.rs.core.Context)3 NewRatingEntity (io.gravitee.rest.api.model.NewRatingEntity)2 PageEntity (io.gravitee.rest.api.model.PageEntity)2