Search in sources :

Example 6 with Page

use of io.gravitee.rest.api.portal.rest.model.Page in project gravitee-management-rest-api by gravitee-io.

the class ApiPageResourceNotAuthenticatedTest method init.

@Before
public void init() {
    resetAllMocks();
    mockApi = new ApiEntity();
    mockApi.setId(API);
    doReturn(mockApi).when(apiService).findById(API);
    Set<ApiEntity> mockApis = new HashSet<>(Arrays.asList(mockApi));
    doReturn(mockApis).when(apiService).findPublishedByUser(isNull(), argThat(q -> singletonList(API).equals(q.getIds())));
    mockPage = new PageEntity();
    mockPage.setPublished(true);
    mockPage.setVisibility(Visibility.PUBLIC);
    doReturn(mockPage).when(pageService).findById(PAGE, null);
    mockAnotherPage = new PageEntity();
    mockAnotherPage.setPublished(true);
    mockAnotherPage.setVisibility(Visibility.PUBLIC);
    Map<String, String> metadataMap = new HashMap<>();
    metadataMap.put(ANOTHER_PAGE, ANOTHER_PAGE);
    mockAnotherPage.setMetadata(metadataMap);
    doReturn(mockAnotherPage).when(pageService).findById(ANOTHER_PAGE, null);
    doReturn(new Page()).when(pageMapper).convert(any(), any(), any());
}
Also used : PageEntity(io.gravitee.rest.api.model.PageEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) java.util(java.util) ArgumentMatchers(org.mockito.ArgumentMatchers) OK_200(io.gravitee.common.http.HttpStatusCode.OK_200) SecurityContext(javax.ws.rs.core.SecurityContext) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Page(io.gravitee.rest.api.portal.rest.model.Page) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter) Collections.singletonList(java.util.Collections.singletonList) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) Priority(javax.annotation.Priority) Principal(java.security.Principal) Response(javax.ws.rs.core.Response) Visibility(io.gravitee.rest.api.model.Visibility) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Mockito.doReturn(org.mockito.Mockito.doReturn) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) PageEntity(io.gravitee.rest.api.model.PageEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Page(io.gravitee.rest.api.portal.rest.model.Page) Before(org.junit.Before)

Example 7 with Page

use of io.gravitee.rest.api.portal.rest.model.Page 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 8 with Page

use of io.gravitee.rest.api.portal.rest.model.Page in project gravitee-management-rest-api by gravitee-io.

the class PagesResource method getAncestors.

private List<String> getAncestors(Map<String, Page> pages, Page page) {
    final List<String> ancestors = new ArrayList<>();
    final String parentId = page.getParent();
    if (parentId == null) {
        return ancestors;
    }
    ancestors.add(parentId);
    final Page parentPage = pages.get(parentId);
    if (parentPage != null) {
        ancestors.addAll(getAncestors(pages, parentPage));
    }
    return ancestors;
}
Also used : ArrayList(java.util.ArrayList) Page(io.gravitee.rest.api.portal.rest.model.Page)

Example 9 with Page

use of io.gravitee.rest.api.portal.rest.model.Page in project gravitee-management-rest-api by gravitee-io.

the class PageResourceTest method shouldGetPageWithInclude.

@Test
public void shouldGetPageWithInclude() {
    final Response response = target(PUBLISHED_PAGE).queryParam("include", "content").request().get();
    assertEquals(OK_200, response.getStatus());
    final Page pageResponse = response.readEntity(Page.class);
    assertNotNull(pageResponse);
    assertEquals(PAGE_CONTENT, pageResponse.getContent());
    assertNotNull(pageResponse.getLinks());
}
Also used : ErrorResponse(io.gravitee.rest.api.portal.rest.model.ErrorResponse) Response(javax.ws.rs.core.Response) Page(io.gravitee.rest.api.portal.rest.model.Page) Test(org.junit.Test)

Example 10 with Page

use of io.gravitee.rest.api.portal.rest.model.Page in project gravitee-management-rest-api by gravitee-io.

the class PageResourceTest method init.

@Before
public void init() {
    resetAllMocks();
    PageEntity publishedPage = new PageEntity();
    publishedPage.setPublished(true);
    publishedPage.setVisibility(Visibility.PUBLIC);
    publishedPage.setContent(PAGE_CONTENT);
    doReturn(publishedPage).when(pageService).findById(PUBLISHED_PAGE, null);
    PageEntity unPublishedPage = new PageEntity();
    unPublishedPage.setPublished(false);
    publishedPage.setVisibility(Visibility.PUBLIC);
    unPublishedPage.setContent(PAGE_CONTENT);
    doReturn(unPublishedPage).when(pageService).findById(UNPUBLISHED_PAGE, null);
    mockAnotherPage = new PageEntity();
    mockAnotherPage.setPublished(true);
    publishedPage.setVisibility(Visibility.PUBLIC);
    Map<String, String> metadataMap = new HashMap<>();
    metadataMap.put(ANOTHER_PAGE, ANOTHER_PAGE);
    mockAnotherPage.setMetadata(metadataMap);
    doReturn(mockAnotherPage).when(pageService).findById(ANOTHER_PAGE, null);
    doThrow(new PageNotFoundException(UNKNOWN_PAGE)).when(pageService).findById(UNKNOWN_PAGE, null);
    doReturn(new Page()).when(pageMapper).convert(any(), any(), any());
    doReturn(new PageLinks()).when(pageMapper).computePageLinks(any(), any());
    doReturn(true).when(accessControlService).canAccessApiFromPortal(anyString());
    doReturn(true).when(accessControlService).canAccessPageFromPortal(any());
}
Also used : PageEntity(io.gravitee.rest.api.model.PageEntity) PageNotFoundException(io.gravitee.rest.api.service.exceptions.PageNotFoundException) HashMap(java.util.HashMap) Page(io.gravitee.rest.api.portal.rest.model.Page) PageLinks(io.gravitee.rest.api.portal.rest.model.PageLinks) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Before(org.junit.Before)

Aggregations

Page (io.gravitee.rest.api.portal.rest.model.Page)24 Response (javax.ws.rs.core.Response)14 Test (org.junit.Test)14 PageEntity (io.gravitee.rest.api.model.PageEntity)13 ErrorResponse (io.gravitee.rest.api.portal.rest.model.ErrorResponse)7 Before (org.junit.Before)6 PagesResponse (io.gravitee.rest.api.portal.rest.model.PagesResponse)4 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)3 PageLinks (io.gravitee.rest.api.portal.rest.model.PageLinks)3 RequirePortalAuth (io.gravitee.rest.api.portal.rest.security.RequirePortalAuth)3 ArrayList (java.util.ArrayList)3 Visibility (io.gravitee.rest.api.model.Visibility)2 ApiQuery (io.gravitee.rest.api.model.api.ApiQuery)2 Api (io.gravitee.rest.api.portal.rest.model.Api)2 Metadata (io.gravitee.rest.api.portal.rest.model.Metadata)2 PageConfiguration (io.gravitee.rest.api.portal.rest.model.PageConfiguration)2 Plan (io.gravitee.rest.api.portal.rest.model.Plan)2 ApiNotFoundException (io.gravitee.rest.api.service.exceptions.ApiNotFoundException)2 PageNotFoundException (io.gravitee.rest.api.service.exceptions.PageNotFoundException)2 UnauthorizedAccessException (io.gravitee.rest.api.service.exceptions.UnauthorizedAccessException)2