Search in sources :

Example 6 with ApiNotFoundException

use of io.gravitee.rest.api.service.exceptions.ApiNotFoundException 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 7 with ApiNotFoundException

use of io.gravitee.rest.api.service.exceptions.ApiNotFoundException 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 8 with ApiNotFoundException

use of io.gravitee.rest.api.service.exceptions.ApiNotFoundException 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 9 with ApiNotFoundException

use of io.gravitee.rest.api.service.exceptions.ApiNotFoundException 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)

Example 10 with ApiNotFoundException

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

the class ApiResource method getBackgroundByApiId.

@GET
@Path("background")
@Produces({ MediaType.WILDCARD, MediaType.APPLICATION_JSON })
public Response getBackgroundByApiId(@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))) {
        InlinePictureEntity image = apiService.getBackground(apiId);
        return createPictureResponse(request, image);
    }
    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)

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