Search in sources :

Example 11 with RequirePortalAuth

use of io.gravitee.rest.api.portal.rest.security.RequirePortalAuth 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)

Example 12 with RequirePortalAuth

use of io.gravitee.rest.api.portal.rest.security.RequirePortalAuth in project gravitee-management-rest-api by gravitee-io.

the class PageResource method getPageByPageId.

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

Aggregations

RequirePortalAuth (io.gravitee.rest.api.portal.rest.security.RequirePortalAuth)12 ApiQuery (io.gravitee.rest.api.model.api.ApiQuery)9 ApiNotFoundException (io.gravitee.rest.api.service.exceptions.ApiNotFoundException)9 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)7 MediaType (io.gravitee.common.http.MediaType)4 Collectors (java.util.stream.Collectors)4 Inject (javax.inject.Inject)4 GET (javax.ws.rs.GET)4 Response (javax.ws.rs.core.Response)4 PageEntity (io.gravitee.rest.api.model.PageEntity)3 Page (io.gravitee.rest.api.portal.rest.model.Page)3 UnauthorizedAccessException (io.gravitee.rest.api.service.exceptions.UnauthorizedAccessException)3 Collections (java.util.Collections)3 List (java.util.List)3 Produces (javax.ws.rs.Produces)3 Context (javax.ws.rs.core.Context)3 PageQuery (io.gravitee.rest.api.model.documentation.PageQuery)2 PageMapper (io.gravitee.rest.api.portal.rest.mapper.PageMapper)2 PlanMapper (io.gravitee.rest.api.portal.rest.mapper.PlanMapper)2 PaginationParam (io.gravitee.rest.api.portal.rest.resource.param.PaginationParam)2