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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations