Search in sources :

Example 1 with Page

use of com.liferay.apio.architect.pagination.Page in project com-liferay-apio-architect by liferay.

the class PageMessageBodyWriter method writeTo.

@Override
public void writeTo(Try.Success<Page<T>> success, Class<?> clazz, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(entityStream, StandardCharsets.UTF_8);
    PrintWriter printWriter = new PrintWriter(outputStreamWriter, true);
    Page<T> page = success.getValue();
    RequestInfo requestInfo = RequestInfo.create(builder -> builder.httpHeaders(_httpHeaders).httpServletRequest(_httpServletRequest).serverURL(_providerManager.provideMandatory(_httpServletRequest, ServerURL.class)).embedded(_providerManager.provideOptional(_httpServletRequest, Embedded.class).orElse(__ -> false)).fields(_providerManager.provideOptional(_httpServletRequest, Fields.class).orElse(__ -> string -> true)).language(_providerManager.provideOptional(_httpServletRequest, Language.class).orElse(Locale::getDefault)).build());
    Optional<PageMessageMapper<T>> optional = _pageMessageMapperManager.getPageMessageMapperOptional(_request);
    PageMessageMapper<T> pageMessageMapper = optional.orElseThrow(NotSupportedException::new);
    PageWriter<T> pageWriter = PageWriter.create(builder -> builder.page(page).pageMessageMapper(pageMessageMapper).pathFunction(_pathIdentifierMapperManager::mapToPath).resourceNameFunction(_nameManager::getNameOptional).representorFunction(name -> unsafeCast(_representableManager.getRepresentorOptional(name))).requestInfo(requestInfo).singleModelFunction(this::_getSingleModelOptional).build());
    httpHeaders.put(CONTENT_TYPE, Collections.singletonList(pageMessageMapper.getMediaType()));
    printWriter.println(pageWriter.write());
    printWriter.close();
}
Also used : CONTENT_TYPE(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE) Provider(javax.ws.rs.ext.Provider) PageMessageMapperManager(com.liferay.apio.architect.wiring.osgi.manager.message.json.PageMessageMapperManager) RequestInfo(com.liferay.apio.architect.request.RequestInfo) ItemRoutes(com.liferay.apio.architect.routes.ItemRoutes) Embedded(com.liferay.apio.architect.response.control.Embedded) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) Identifier(com.liferay.apio.architect.identifier.Identifier) HttpServletRequest(javax.servlet.http.HttpServletRequest) MediaType(javax.ws.rs.core.MediaType) Component(org.osgi.service.component.annotations.Component) Unsafe(com.liferay.apio.architect.unsafe.Unsafe) Locale(java.util.Locale) Page(com.liferay.apio.architect.pagination.Page) Fields(com.liferay.apio.architect.response.control.Fields) GenericUtil(com.liferay.apio.architect.wiring.osgi.util.GenericUtil) OutputStreamWriter(java.io.OutputStreamWriter) ProviderManager(com.liferay.apio.architect.wiring.osgi.manager.ProviderManager) SingleModel(com.liferay.apio.architect.single.model.SingleModel) NotSupportedException(javax.ws.rs.NotSupportedException) OutputStream(java.io.OutputStream) PrintWriter(java.io.PrintWriter) Try(com.liferay.apio.architect.functional.Try) PathIdentifierMapperManager(com.liferay.apio.architect.wiring.osgi.manager.PathIdentifierMapperManager) Context(javax.ws.rs.core.Context) NameManager(com.liferay.apio.architect.wiring.osgi.manager.representable.NameManager) RepresentableManager(com.liferay.apio.architect.wiring.osgi.manager.representable.RepresentableManager) ServerURL(com.liferay.apio.architect.url.ServerURL) IOException(java.io.IOException) StandardCharsets(java.nio.charset.StandardCharsets) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) PageMessageMapper(com.liferay.apio.architect.message.json.PageMessageMapper) Language(com.liferay.apio.architect.language.Language) PageWriter(com.liferay.apio.architect.writer.PageWriter) HttpHeaders(javax.ws.rs.core.HttpHeaders) Type(java.lang.reflect.Type) Unsafe.unsafeCast(com.liferay.apio.architect.unsafe.Unsafe.unsafeCast) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) Request(javax.ws.rs.core.Request) Reference(org.osgi.service.component.annotations.Reference) Collections(java.util.Collections) ItemRouterManager(com.liferay.apio.architect.wiring.osgi.manager.router.ItemRouterManager) RequestInfo(com.liferay.apio.architect.request.RequestInfo) PageMessageMapper(com.liferay.apio.architect.message.json.PageMessageMapper) Fields(com.liferay.apio.architect.response.control.Fields) OutputStreamWriter(java.io.OutputStreamWriter) NotSupportedException(javax.ws.rs.NotSupportedException) PrintWriter(java.io.PrintWriter)

Example 2 with Page

use of com.liferay.apio.architect.pagination.Page in project com-liferay-apio-architect by liferay.

the class PageWriter method write.

/**
 * Writes the handled {@link Page} to a string. This method uses a {@link
 * FieldsWriter} to write the different fields of its items' {@link
 * Representor}. If no {@code Representor} or {@code Path} exist for the
 * model, this method returns {@code Optional#empty()}.
 *
 * @return the representation of the {@code Page}, if the {@code
 *         Representor} and {@code Path} exist for the model; returns {@code
 *         Optional#empty()} otherwise
 */
@SuppressWarnings("Duplicates")
public String write() {
    _pageMessageMapper.onStart(_jsonObjectBuilder, _page, _requestInfo.getHttpHeaders());
    _pageMessageMapper.mapItemTotalCount(_jsonObjectBuilder, _page.getTotalCount());
    Collection<T> items = _page.getItems();
    _pageMessageMapper.mapPageCount(_jsonObjectBuilder, items.size());
    _writePageURLs();
    String url = _getCollectionURL();
    _pageMessageMapper.mapCollectionURL(_jsonObjectBuilder, url);
    String resourceName = _page.getResourceName();
    items.forEach(model -> _writeItem(new SingleModel<>(model, resourceName, Collections.emptyList())));
    List<Operation> operations = _page.getOperations();
    operations.forEach(operation -> {
        JSONObjectBuilder operationJSONObjectBuilder = new JSONObjectBuilder();
        _pageMessageMapper.onStartOperation(_jsonObjectBuilder, operationJSONObjectBuilder, operation);
        Optional<Form> formOptional = operation.getFormOptional();
        formOptional.map(form -> createFormURL(_requestInfo.getServerURL(), form)).ifPresent(formURL -> _pageMessageMapper.mapOperationFormURL(_jsonObjectBuilder, operationJSONObjectBuilder, formURL));
        _pageMessageMapper.mapOperationMethod(_jsonObjectBuilder, operationJSONObjectBuilder, operation.method);
        _pageMessageMapper.onFinishOperation(_jsonObjectBuilder, operationJSONObjectBuilder, operation);
    });
    _pageMessageMapper.onFinish(_jsonObjectBuilder, _page, _requestInfo.getHttpHeaders());
    JsonObject jsonObject = _jsonObjectBuilder.build();
    return jsonObject.toString();
}
Also used : SingleModel(com.liferay.apio.architect.single.model.SingleModel) JsonObject(com.google.gson.JsonObject) ResourceNameFunction(com.liferay.apio.architect.writer.alias.ResourceNameFunction) RequestInfo(com.liferay.apio.architect.request.RequestInfo) URLCreator.createCollectionPageURL(com.liferay.apio.architect.writer.url.URLCreator.createCollectionPageURL) WriterUtil.getFieldsWriter(com.liferay.apio.architect.writer.util.WriterUtil.getFieldsWriter) SingleModelFunction(com.liferay.apio.architect.writer.alias.SingleModelFunction) Function(java.util.function.Function) URLCreator.createCollectionURL(com.liferay.apio.architect.writer.url.URLCreator.createCollectionURL) Operation(com.liferay.apio.architect.operation.Operation) Representor(com.liferay.apio.architect.representor.Representor) WriterUtil.getPathOptional(com.liferay.apio.architect.writer.util.WriterUtil.getPathOptional) FunctionalList(com.liferay.apio.architect.list.FunctionalList) Page(com.liferay.apio.architect.pagination.Page) Map(java.util.Map) PageType(com.liferay.apio.architect.pagination.PageType) SingleModel(com.liferay.apio.architect.single.model.SingleModel) PathFunction(com.liferay.apio.architect.writer.alias.PathFunction) RepresentorFunction(com.liferay.apio.architect.writer.alias.RepresentorFunction) Collection(java.util.Collection) Form(com.liferay.apio.architect.form.Form) Path(com.liferay.apio.architect.uri.Path) JSONObjectBuilder(com.liferay.apio.architect.message.json.JSONObjectBuilder) PageMessageMapper(com.liferay.apio.architect.message.json.PageMessageMapper) List(java.util.List) URLCreator.createNestedCollectionURL(com.liferay.apio.architect.writer.url.URLCreator.createNestedCollectionURL) Unsafe.unsafeCast(com.liferay.apio.architect.unsafe.Unsafe.unsafeCast) Optional(java.util.Optional) Collections(java.util.Collections) URLCreator.createFormURL(com.liferay.apio.architect.writer.url.URLCreator.createFormURL) JSONObjectBuilder(com.liferay.apio.architect.message.json.JSONObjectBuilder) Form(com.liferay.apio.architect.form.Form) JsonObject(com.google.gson.JsonObject) Operation(com.liferay.apio.architect.operation.Operation)

Example 3 with Page

use of com.liferay.apio.architect.pagination.Page in project com-liferay-apio-architect by liferay.

the class URLCreatorTest method testCreateCollectionPageURL.

@Test
public void testCreateCollectionPageURL() {
    Pagination pagination = Mockito.mock(Pagination.class);
    Mockito.when(pagination.getItemsPerPage()).thenReturn(30);
    Mockito.when(pagination.getPageNumber()).thenReturn(1);
    PageItems<String> pageItems = new PageItems<>(emptyList(), 0);
    Page page = new Page<>("", pageItems, pagination, null);
    String firstPageURL = URLCreator.createCollectionPageURL("www.liferay.com", page, PageType.FIRST);
    assertThat(firstPageURL, is("www.liferay.com?page=1&per_page=30"));
}
Also used : Pagination(com.liferay.apio.architect.pagination.Pagination) Page(com.liferay.apio.architect.pagination.Page) PageItems(com.liferay.apio.architect.pagination.PageItems) Test(org.junit.Test)

Example 4 with Page

use of com.liferay.apio.architect.pagination.Page in project com-liferay-apio-architect by liferay.

the class CollectionRoutesTest method _testCollectionRoutes.

private void _testCollectionRoutes(CollectionRoutes<String> collectionRoutes) {
    Optional<CollectionRoutes<String>> optional = Optional.of(collectionRoutes);
    Map map = optional.flatMap(CollectionRoutes::getFormOptional).map(form -> {
        assertThat(form.id, is("c/name"));
        return (Map) form.get(_body);
    }).get();
    Optional<String> valueOptional = _body.getValueOptional("key");
    assertThat(map.get("key"), is(valueOptional.get()));
    SingleModel<String> singleModel = optional.flatMap(CollectionRoutes::getCreateItemFunctionOptional).get().apply(null).apply(_body).getUnchecked();
    assertThat(singleModel.getResourceName(), is("name"));
    assertThat(singleModel.getModel(), is("Apio"));
    Page<String> page = optional.flatMap(CollectionRoutes::getGetPageFunctionOptional).get().apply(null).getUnchecked();
    assertThat(page.getItems(), hasSize(1));
    assertThat(page.getItems(), hasItem("Apio"));
    assertThat(page.getTotalCount(), is(1));
    List<Operation> operations = page.getOperations();
    assertThat(operations, hasSize(1));
    Operation operation = operations.get(0);
    assertThat(operation.getFormOptional(), is(optionalWithValue()));
    assertThat(operation.method, is(POST));
    assertThat(operation.name, is("name/create"));
}
Also used : CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) GetPageFunction(com.liferay.apio.architect.alias.routes.GetPageFunction) Body(com.liferay.apio.architect.form.Body) OptionalMatchers.emptyOptional(com.spotify.hamcrest.optional.OptionalMatchers.emptyOptional) Operation(com.liferay.apio.architect.operation.Operation) TreeSet(java.util.TreeSet) CreateItemFunction(com.liferay.apio.architect.alias.routes.CreateItemFunction) Builder(com.liferay.apio.architect.routes.CollectionRoutes.Builder) Pagination(com.liferay.apio.architect.pagination.Pagination) Page(com.liferay.apio.architect.pagination.Page) Map(java.util.Map) POST(com.liferay.apio.architect.operation.Method.POST) Is.is(org.hamcrest.core.Is.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) SingleModel(com.liferay.apio.architect.single.model.SingleModel) OptionalMatchers.optionalWithValue(com.spotify.hamcrest.optional.OptionalMatchers.optionalWithValue) Set(java.util.Set) Test(org.junit.Test) PageItems(com.liferay.apio.architect.pagination.PageItems) FORM_BUILDER_FUNCTION(com.liferay.apio.architect.routes.RoutesTestUtil.FORM_BUILDER_FUNCTION) REQUEST_PROVIDE_FUNCTION(com.liferay.apio.architect.routes.RoutesTestUtil.REQUEST_PROVIDE_FUNCTION) IsCollectionWithSize.hasSize(org.hamcrest.collection.IsCollectionWithSize.hasSize) List(java.util.List) PAGINATION(com.liferay.apio.architect.routes.RoutesTestUtil.PAGINATION) Matchers.contains(org.hamcrest.Matchers.contains) Optional(java.util.Optional) COLLECTION_PERMISSION_FUNCTION(com.liferay.apio.architect.routes.RoutesTestUtil.COLLECTION_PERMISSION_FUNCTION) Collections(java.util.Collections) Operation(com.liferay.apio.architect.operation.Operation) Map(java.util.Map)

Example 5 with Page

use of com.liferay.apio.architect.pagination.Page in project com-liferay-apio-architect by liferay.

the class NestedCollectionRoutesTest method _testNestedCollectionRoutes.

private void _testNestedCollectionRoutes(NestedCollectionRoutes<String, Long> nestedCollectionRoutes) {
    Optional<NestedCollectionRoutes<String, Long>> optional = Optional.of(nestedCollectionRoutes);
    Map map = optional.flatMap(NestedCollectionRoutes::getFormOptional).map(form -> {
        assertThat(form.id, is("c/name/nested"));
        return (Map) form.get(_body);
    }).get();
    Optional<String> valueOptional = _body.getValueOptional("key");
    assertThat(map.get("key"), is(valueOptional.get()));
    SingleModel<String> singleModel = optional.flatMap(NestedCollectionRoutes::getNestedCreateItemFunctionOptional).get().apply(null).apply(42L).apply(_body).getUnchecked();
    assertThat(singleModel.getResourceName(), is("nested"));
    assertThat(singleModel.getModel(), is("Apio"));
    Path path = new Path("name", "42");
    Page<String> page = optional.flatMap(NestedCollectionRoutes::getNestedGetPageFunctionOptional).get().apply(null).apply(path).apply(42L).getUnchecked();
    assertThat(page.getItems(), hasSize(1));
    assertThat(page.getItems(), hasItem("Apio"));
    assertThat(page.getPathOptional(), optionalWithValue(equalTo(path)));
    assertThat(page.getTotalCount(), is(1));
    List<Operation> operations = page.getOperations();
    assertThat(operations, hasSize(1));
    Operation secondOperation = operations.get(0);
    assertThat(secondOperation.getFormOptional(), is(optionalWithValue()));
    assertThat(secondOperation.method, is(POST));
    assertThat(secondOperation.name, is("name/nested/create"));
}
Also used : CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) Body(com.liferay.apio.architect.form.Body) OptionalMatchers.emptyOptional(com.spotify.hamcrest.optional.OptionalMatchers.emptyOptional) Operation(com.liferay.apio.architect.operation.Operation) TreeSet(java.util.TreeSet) Pagination(com.liferay.apio.architect.pagination.Pagination) Page(com.liferay.apio.architect.pagination.Page) Map(java.util.Map) POST(com.liferay.apio.architect.operation.Method.POST) Is.is(org.hamcrest.core.Is.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) SingleModel(com.liferay.apio.architect.single.model.SingleModel) RoutesTestUtil.getNestedCollectionPermissionFunction(com.liferay.apio.architect.routes.RoutesTestUtil.getNestedCollectionPermissionFunction) OptionalMatchers.optionalWithValue(com.spotify.hamcrest.optional.OptionalMatchers.optionalWithValue) NestedGetPageFunction(com.liferay.apio.architect.alias.routes.NestedGetPageFunction) Set(java.util.Set) Test(org.junit.Test) PageItems(com.liferay.apio.architect.pagination.PageItems) Path(com.liferay.apio.architect.uri.Path) FORM_BUILDER_FUNCTION(com.liferay.apio.architect.routes.RoutesTestUtil.FORM_BUILDER_FUNCTION) REQUEST_PROVIDE_FUNCTION(com.liferay.apio.architect.routes.RoutesTestUtil.REQUEST_PROVIDE_FUNCTION) IsCollectionWithSize.hasSize(org.hamcrest.collection.IsCollectionWithSize.hasSize) NestedCreateItemFunction(com.liferay.apio.architect.alias.routes.NestedCreateItemFunction) List(java.util.List) PAGINATION(com.liferay.apio.architect.routes.RoutesTestUtil.PAGINATION) Matchers.contains(org.hamcrest.Matchers.contains) Optional(java.util.Optional) Builder(com.liferay.apio.architect.routes.NestedCollectionRoutes.Builder) Collections(java.util.Collections) Path(com.liferay.apio.architect.uri.Path) Operation(com.liferay.apio.architect.operation.Operation) Map(java.util.Map)

Aggregations

Page (com.liferay.apio.architect.pagination.Page)6 Collections (java.util.Collections)5 Optional (java.util.Optional)5 Operation (com.liferay.apio.architect.operation.Operation)4 PageItems (com.liferay.apio.architect.pagination.PageItems)4 Pagination (com.liferay.apio.architect.pagination.Pagination)4 SingleModel (com.liferay.apio.architect.single.model.SingleModel)4 PageMessageMapper (com.liferay.apio.architect.message.json.PageMessageMapper)3 POST (com.liferay.apio.architect.operation.Method.POST)3 RequestInfo (com.liferay.apio.architect.request.RequestInfo)3 Path (com.liferay.apio.architect.uri.Path)3 List (java.util.List)3 JsonObject (com.google.gson.JsonObject)2 Body (com.liferay.apio.architect.form.Body)2 FORM_BUILDER_FUNCTION (com.liferay.apio.architect.routes.RoutesTestUtil.FORM_BUILDER_FUNCTION)2 PAGINATION (com.liferay.apio.architect.routes.RoutesTestUtil.PAGINATION)2 REQUEST_PROVIDE_FUNCTION (com.liferay.apio.architect.routes.RoutesTestUtil.REQUEST_PROVIDE_FUNCTION)2 Unsafe.unsafeCast (com.liferay.apio.architect.unsafe.Unsafe.unsafeCast)2 OptionalMatchers.emptyOptional (com.spotify.hamcrest.optional.OptionalMatchers.emptyOptional)2 OptionalMatchers.optionalWithValue (com.spotify.hamcrest.optional.OptionalMatchers.optionalWithValue)2