Search in sources :

Example 41 with PageEntity

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

the class PageMapper method convert.

public Page convert(UriBuilder baseUriBuilder, String apiId, PageEntity page) {
    final Page pageItem = new Page();
    if (page.getConfiguration() != null) {
        PageConfiguration pageConfiguration = convertPageConfiguration(page.getConfiguration());
        pageItem.setConfiguration(pageConfiguration);
    }
    pageItem.setId(page.getId());
    if (page.getMetadata() != null) {
        AtomicInteger counter = new AtomicInteger(0);
        List<Metadata> metadataList = page.getMetadata().entrySet().stream().map(e -> new Metadata().name(e.getKey()).value(e.getValue()).order(Integer.toString(counter.getAndIncrement()))).collect(Collectors.toList());
        pageItem.setMetadata(metadataList);
    }
    pageItem.setName(page.getName());
    pageItem.setOrder(page.getOrder());
    pageItem.setParent(page.getParentId());
    if (page.getType() != null) {
        pageItem.setType(TypeEnum.fromValue(page.getType()));
    }
    if (page.getLastModificationDate() != null) {
        pageItem.setUpdatedAt(page.getLastModificationDate().toInstant().atOffset(ZoneOffset.UTC));
    }
    if (page.getAttachedMedia() != null && !page.getAttachedMedia().isEmpty() && baseUriBuilder != null) {
        final String mediaUrl = PortalApiLinkHelper.mediaURL(baseUriBuilder, apiId);
        final List<PageMedia> pageMedia = mediaService.findAllWithoutContent(page.getAttachedMedia(), apiId).stream().map(media -> new PageMedia().name(media.getFileName()).link(mediaUrl + "/" + media.getHash()).type(media.getType())).collect(Collectors.toList());
        pageItem.setMedia(pageMedia);
    }
    if (page.getContentRevisionId() != null) {
        final PageRevisionId contentRevisionId = new PageRevisionId();
        // do not use the pageId because the content maybe a translation (so a different pageId in the revisionId)
        contentRevisionId.setPageId(page.getContentRevisionId().getPageId());
        contentRevisionId.setRevision(page.getContentRevisionId().getRevision());
        pageItem.setContentRevisionId(contentRevisionId);
    }
    return pageItem;
}
Also used : PageEntity(io.gravitee.rest.api.model.PageEntity) MediaService(io.gravitee.rest.api.service.MediaService) io.gravitee.rest.api.portal.rest.model(io.gravitee.rest.api.portal.rest.model) Collectors(java.util.stream.Collectors) Inject(javax.inject.Inject) List(java.util.List) Component(org.springframework.stereotype.Component) TypeEnum(io.gravitee.rest.api.portal.rest.model.Page.TypeEnum) PortalApiLinkHelper(io.gravitee.rest.api.portal.rest.utils.PortalApiLinkHelper) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DocExpansionEnum(io.gravitee.rest.api.portal.rest.model.PageConfiguration.DocExpansionEnum) Map(java.util.Map) UriBuilder(javax.ws.rs.core.UriBuilder) ZoneOffset(java.time.ZoneOffset) ViewerEnum(io.gravitee.rest.api.portal.rest.model.PageConfiguration.ViewerEnum) PageConfigurationKeys(io.gravitee.rest.api.model.PageConfigurationKeys) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 42 with PageEntity

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

the class PortalPagesResourceAdminTest method shouldCreatePortalPage.

@Test
public void shouldCreatePortalPage() {
    reset(pageService);
    NewPageEntity newPageEntity = new NewPageEntity();
    newPageEntity.setName("my-page-name");
    newPageEntity.setType(PageType.MARKDOWN);
    newPageEntity.setVisibility(Visibility.PUBLIC);
    PageEntity returnedPage = new PageEntity();
    returnedPage.setId("my-beautiful-page");
    doReturn(returnedPage).when(pageService).createPage(any(), eq(GraviteeContext.getCurrentEnvironment()));
    doReturn(0).when(pageService).findMaxPortalPageOrder(eq(GraviteeContext.getCurrentEnvironment()));
    final Response response = envTarget().request().post(Entity.json(newPageEntity));
    assertEquals(HttpStatusCode.CREATED_201, response.getStatus());
    assertEquals(envTarget().path("my-beautiful-page").getUri().toString(), response.getHeaders().getFirst(HttpHeaders.LOCATION));
}
Also used : PageEntity(io.gravitee.rest.api.model.PageEntity) UpdatePageEntity(io.gravitee.rest.api.model.UpdatePageEntity) NewPageEntity(io.gravitee.rest.api.model.NewPageEntity) Response(javax.ws.rs.core.Response) NewPageEntity(io.gravitee.rest.api.model.NewPageEntity) Test(org.junit.Test)

Example 43 with PageEntity

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

the class ApiPageMediaResource method getApiPageMedia.

@GET
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Retrieve all media for an API page", notes = "User must have the API_DOCUMENTATION[READ] permission to use this service")
@Permissions({ @Permission(value = RolePermission.API_DOCUMENTATION, acls = RolePermissionAction.READ) })
public Response getApiPageMedia() {
    final PageEntity currentPage = pageService.findById(page);
    List<MediaEntity> pageMedia = mediaService.findAllWithoutContent(currentPage.getAttachedMedia(), api);
    if (pageMedia != null && !pageMedia.isEmpty()) {
        return Response.ok(pageMedia).build();
    }
    return Response.noContent().build();
}
Also used : PageEntity(io.gravitee.rest.api.model.PageEntity) MediaEntity(io.gravitee.rest.api.model.MediaEntity) Permissions(io.gravitee.rest.api.management.rest.security.Permissions)

Example 44 with PageEntity

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

the class PageMapperTest method testMinimalConvert.

@Test
public void testMinimalConvert() {
    // init
    pageEntity = new PageEntity();
    pageEntity.setType(PAGE_TYPE);
    Instant now = Instant.now();
    pageEntity.setLastModificationDate(Date.from(now));
    // Test
    Page responsePage = pageMapper.convert(pageEntity);
    assertNotNull(responsePage);
    PageConfiguration pageConfiguration = responsePage.getConfiguration();
    assertNull(pageConfiguration);
    List<Metadata> metadatas = responsePage.getMetadata();
    assertNull(metadatas);
    assertEquals(TypeEnum.SWAGGER, responsePage.getType());
    assertEquals(now.toEpochMilli(), responsePage.getUpdatedAt().toInstant().toEpochMilli());
}
Also used : PageEntity(io.gravitee.rest.api.model.PageEntity) PageConfiguration(io.gravitee.rest.api.portal.rest.model.PageConfiguration) Instant(java.time.Instant) Metadata(io.gravitee.rest.api.portal.rest.model.Metadata) Page(io.gravitee.rest.api.portal.rest.model.Page) Test(org.junit.Test)

Example 45 with PageEntity

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

the class ApiPageResource method getPageByApiIdAndPageId.

@GET
@Produces(MediaType.APPLICATION_JSON)
@RequirePortalAuth
public Response getPageByApiIdAndPageId(@HeaderParam("Accept-Language") String acceptLang, @PathParam("apiId") String apiId, @PathParam("pageId") String pageId, @QueryParam("include") List<String> include) {
    final ApiQuery apiQuery = new ApiQuery();
    apiQuery.setIds(Collections.singletonList(apiId));
    if (accessControlService.canAccessApiFromPortal(apiId)) {
        final String acceptedLocale = HttpHeadersUtil.getFirstAcceptedLocaleName(acceptLang);
        PageEntity pageEntity = pageService.findById(pageId, acceptedLocale);
        if (accessControlService.canAccessPageFromPortal(apiId, pageEntity)) {
            pageService.transformSwagger(pageEntity, apiId);
            if (!isAuthenticated() && pageEntity.getMetadata() != null) {
                pageEntity.getMetadata().clear();
            }
            Page page = pageMapper.convert(uriInfo.getBaseUriBuilder(), apiId, pageEntity);
            if (include.contains(INCLUDE_CONTENT)) {
                page.setContent(pageEntity.getContent());
            }
            page.setLinks(pageMapper.computePageLinks(PortalApiLinkHelper.apiPagesURL(uriInfo.getBaseUriBuilder(), apiId, pageId), PortalApiLinkHelper.apiPagesURL(uriInfo.getBaseUriBuilder(), apiId, page.getParent())));
            return Response.ok(page).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) Page(io.gravitee.rest.api.portal.rest.model.Page) RequirePortalAuth(io.gravitee.rest.api.portal.rest.security.RequirePortalAuth)

Aggregations

PageEntity (io.gravitee.rest.api.model.PageEntity)67 Test (org.junit.Test)48 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)15 OAIDescriptor (io.gravitee.rest.api.service.swagger.OAIDescriptor)15 Page (io.gravitee.rest.api.portal.rest.model.Page)13 Response (javax.ws.rs.core.Response)13 UpdatePageEntity (io.gravitee.rest.api.model.UpdatePageEntity)9 PageConfigurationOAITransformer (io.gravitee.rest.api.service.impl.swagger.transformer.page.PageConfigurationOAITransformer)8 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 Permissions (io.gravitee.rest.api.management.rest.security.Permissions)6 EntrypointsOAITransformer (io.gravitee.rest.api.service.impl.swagger.transformer.entrypoints.EntrypointsOAITransformer)6 SwaggerDescriptor (io.gravitee.rest.api.service.swagger.SwaggerDescriptor)5 Before (org.junit.Before)5 Page (io.gravitee.repository.management.model.Page)4 NewPageEntity (io.gravitee.rest.api.model.NewPageEntity)4 PageNotFoundException (io.gravitee.rest.api.service.exceptions.PageNotFoundException)4 ArgumentMatchers (org.mockito.ArgumentMatchers)4 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)3 PageRepository (io.gravitee.repository.management.api.PageRepository)3 PageReferenceType (io.gravitee.repository.management.model.PageReferenceType)3