use of io.gravitee.rest.api.model.api.ApiQuery in project gravitee-management-rest-api by gravitee-io.
the class ApisResource method createQueryFromParam.
private ApiQuery createQueryFromParam(ApisParam apisParam) {
final ApiQuery apiQuery = new ApiQuery();
if (apisParam != null) {
apiQuery.setContextPath(apisParam.getContextPath());
apiQuery.setLabel(apisParam.getLabel());
apiQuery.setName(apisParam.getName());
apiQuery.setTag(apisParam.getTag());
apiQuery.setVersion(apisParam.getVersion());
if (apisParam.getCategory() != null) {
apiQuery.setCategory(apisParam.getCategory());
}
}
return apiQuery;
}
use of io.gravitee.rest.api.model.api.ApiQuery 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);
}
use of io.gravitee.rest.api.model.api.ApiQuery 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);
}
use of io.gravitee.rest.api.model.api.ApiQuery 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);
}
use of io.gravitee.rest.api.model.api.ApiQuery in project gravitee-management-rest-api by gravitee-io.
the class PortalApisResource method searchPortalApis.
@POST
@Path("_search")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Search for API using the search engine")
@ApiResponses({ @ApiResponse(code = 200, message = "List accessible APIs for current user", response = ApiListItem.class, responseContainer = "List"), @ApiResponse(code = 500, message = "Internal server error") })
public Response searchPortalApis(@ApiParam(name = "q", required = true) @NotNull @QueryParam("q") String query) {
try {
final Collection<ApiEntity> apis;
final ApiQuery apiQuery = new ApiQuery();
if (isAdmin()) {
apis = apiService.search(apiQuery);
} else {
apiQuery.setLifecycleStates(singletonList(PUBLISHED));
if (isAuthenticated()) {
apis = apiService.findByUser(getAuthenticatedUser(), apiQuery, true);
} else if (configService.portalLoginForced()) {
// if portal requires login, this endpoint should hide the APIS even PUBLIC ones
return Response.ok().entity(emptyList()).build();
} else {
apiQuery.setVisibility(PUBLIC);
apis = apiService.search(apiQuery);
}
}
Map<String, Object> filters = new HashMap<>();
filters.put("api", apis.stream().map(ApiEntity::getId).collect(Collectors.toSet()));
return Response.ok().entity(apiService.search(query, filters).stream().map(this::convert).collect(toList())).build();
} catch (TechnicalException te) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(te).build();
}
}
Aggregations