Search in sources :

Example 11 with ApiNotFoundException

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

the class ApiResource method getPictureByApiId.

@GET
@Path("picture")
@Produces({ MediaType.WILDCARD, MediaType.APPLICATION_JSON })
@RequirePortalAuth
public Response getPictureByApiId(@Context Request request, @PathParam("apiId") String apiId) {
    final ApiQuery apiQuery = new ApiQuery();
    apiQuery.setIds(Collections.singletonList(apiId));
    // Do not filter on visibility to display the picture on subscription screen even if the API is no more published
    Collection<ApiEntity> userApis = apiService.findByUser(getAuthenticatedUserOrNull(), apiQuery, true);
    if (userApis.stream().anyMatch(a -> a.getId().equals(apiId))) {
        InlinePictureEntity image = apiService.getPicture(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) RequirePortalAuth(io.gravitee.rest.api.portal.rest.security.RequirePortalAuth)

Example 12 with ApiNotFoundException

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

the class ApiPagesResource method getPagesByApiId.

@GET
@Produces(MediaType.APPLICATION_JSON)
@RequirePortalAuth
public Response getPagesByApiId(@HeaderParam("Accept-Language") String acceptLang, @PathParam("apiId") String apiId, @BeanParam PaginationParam paginationParam, @QueryParam("homepage") Boolean homepage, @QueryParam("parent") String parent) {
    final ApiQuery apiQuery = new ApiQuery();
    apiQuery.setIds(Collections.singletonList(apiId));
    if (accessControlService.canAccessApiFromPortal(apiId)) {
        final String acceptedLocale = HttpHeadersUtil.getFirstAcceptedLocaleName(acceptLang);
        Stream<Page> pageStream = pageService.search(new PageQuery.Builder().api(apiId).homepage(homepage).published(true).build(), acceptedLocale, GraviteeContext.getCurrentEnvironment()).stream().filter(page -> accessControlService.canAccessPageFromPortal(apiId, page)).map(pageMapper::convert).map(page -> this.addPageLink(apiId, page));
        List<Page> pages;
        if (parent != null) {
            pages = new ArrayList<>();
            Map<String, Page> pagesMap = pageStream.collect(Collectors.toMap(Page::getId, page -> page));
            pagesMap.values().forEach(page -> {
                List<String> ancestors = this.getAncestors(pagesMap, page);
                if (ancestors.contains(parent)) {
                    pages.add(page);
                }
            });
        } else {
            pages = pageStream.collect(Collectors.toList());
        }
        return createListResponse(pages, paginationParam);
    }
    throw new ApiNotFoundException(apiId);
}
Also used : ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) Context(javax.ws.rs.core.Context) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) HttpHeadersUtil(io.gravitee.rest.api.portal.rest.utils.HttpHeadersUtil) GraviteeContext(io.gravitee.rest.api.service.common.GraviteeContext) Page(io.gravitee.rest.api.portal.rest.model.Page) RequirePortalAuth(io.gravitee.rest.api.portal.rest.security.RequirePortalAuth) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) AccessControlService(io.gravitee.rest.api.service.AccessControlService) List(java.util.List) MediaType(io.gravitee.common.http.MediaType) Stream(java.util.stream.Stream) 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) PaginationParam(io.gravitee.rest.api.portal.rest.resource.param.PaginationParam) Map(java.util.Map) PageQuery(io.gravitee.rest.api.model.documentation.PageQuery) PageService(io.gravitee.rest.api.service.PageService) Collections(java.util.Collections) PageMapper(io.gravitee.rest.api.portal.rest.mapper.PageMapper) ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) PageQuery(io.gravitee.rest.api.model.documentation.PageQuery) 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 13 with ApiNotFoundException

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

the class ApiMediaResource method getApiMedia.

@GET
@Path("{mediaHash}")
@Produces({ MediaType.WILDCARD, MediaType.APPLICATION_JSON })
public Response getApiMedia(@Context Request request, @PathParam("apiId") String apiId, @PathParam("mediaHash") String mediaHash) {
    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))) {
        MediaEntity mediaEntity = mediaService.findByHashAndApi(mediaHash, apiId, true);
        if (mediaEntity == null) {
            return Response.status(Response.Status.NOT_FOUND).build();
        }
        return createMediaResponse(request, mediaHash, mediaEntity);
    }
    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) MediaEntity(io.gravitee.rest.api.model.MediaEntity) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 14 with ApiNotFoundException

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

the class ApiPageResource method getPageContentByApiIdAndPageId.

@GET
@Path("content")
@Produces(MediaType.TEXT_PLAIN)
@RequirePortalAuth
public Response getPageContentByApiIdAndPageId(@PathParam("apiId") String apiId, @PathParam("pageId") String pageId) {
    final ApiQuery apiQuery = new ApiQuery();
    apiQuery.setIds(Collections.singletonList(apiId));
    if (accessControlService.canAccessApiFromPortal(apiId)) {
        PageEntity pageEntity = pageService.findById(pageId, null);
        if (accessControlService.canAccessPageFromPortal(apiId, pageEntity)) {
            pageService.transformSwagger(pageEntity, apiId);
            return Response.ok(pageEntity.getContent()).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) RequirePortalAuth(io.gravitee.rest.api.portal.rest.security.RequirePortalAuth)

Example 15 with ApiNotFoundException

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

the class ApiInformationsResource method getApiInformations.

@GET
@Produces({ MediaType.APPLICATION_JSON })
@RequirePortalAuth
public Response getApiInformations(@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))) {
        List<ApiHeaderEntity> all = apiService.getPortalHeaders(apiId);
        List<ApiInformation> information = all.stream().map(apiHeaderEntity -> {
            ApiInformation ai = new ApiInformation();
            ai.setName(apiHeaderEntity.getName());
            ai.setValue(apiHeaderEntity.getValue());
            return ai;
        }).collect(Collectors.toList());
        return Response.ok(information).build();
    }
    throw new ApiNotFoundException(apiId);
}
Also used : ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) PathParam(javax.ws.rs.PathParam) Context(javax.ws.rs.core.Context) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Collection(java.util.Collection) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) RequirePortalAuth(io.gravitee.rest.api.portal.rest.security.RequirePortalAuth) Collectors(java.util.stream.Collectors) ApiService(io.gravitee.rest.api.service.ApiService) Inject(javax.inject.Inject) List(java.util.List) MediaType(io.gravitee.common.http.MediaType) ApiInformation(io.gravitee.rest.api.portal.rest.model.ApiInformation) Response(javax.ws.rs.core.Response) Request(javax.ws.rs.core.Request) ApiHeaderEntity(io.gravitee.rest.api.model.api.header.ApiHeaderEntity) Collections(java.util.Collections) ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApiHeaderEntity(io.gravitee.rest.api.model.api.header.ApiHeaderEntity) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) ApiInformation(io.gravitee.rest.api.portal.rest.model.ApiInformation) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) 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