Search in sources :

Example 6 with Path

use of com.liferay.apio.architect.uri.Path 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 7 with Path

use of com.liferay.apio.architect.uri.Path 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)

Example 8 with Path

use of com.liferay.apio.architect.uri.Path in project com-liferay-apio-architect by liferay.

the class PageEndpointImpl method deleteCollectionItem.

@Override
public Response deleteCollectionItem(String id) throws Exception {
    Try<ItemRoutes<T, S>> itemRoutesTry = Try.fromOptional(_itemRoutesSupplier::get, notFound(_name));
    ThrowableConsumer<S> throwableConsumer = itemRoutesTry.mapOptional(ItemRoutes::getDeleteConsumerOptional, notAllowed(DELETE, _name, id)).map(function -> function.apply(_httpServletRequest)).getUnchecked();
    Path path = new Path(_name, id);
    throwableConsumer.accept(_identifierFunction.apply(path));
    return noContent().build();
}
Also used : Body(com.liferay.apio.architect.form.Body) ItemRoutes(com.liferay.apio.architect.routes.ItemRoutes) ExceptionSupplierUtil.notFound(com.liferay.apio.architect.endpoint.ExceptionSupplierUtil.notFound) NestedCollectionRoutes(com.liferay.apio.architect.routes.NestedCollectionRoutes) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ExceptionSupplierUtil.notAllowed(com.liferay.apio.architect.endpoint.ExceptionSupplierUtil.notAllowed) PageEndpoint(com.liferay.apio.architect.endpoint.PageEndpoint) Representor(com.liferay.apio.architect.representor.Representor) Identifier(com.liferay.apio.architect.identifier.Identifier) ThrowableFunction(com.liferay.apio.architect.function.throwable.ThrowableFunction) HttpServletRequest(javax.servlet.http.HttpServletRequest) Response.noContent(javax.ws.rs.core.Response.noContent) Page(com.liferay.apio.architect.pagination.Page) CollectionRoutes(com.liferay.apio.architect.routes.CollectionRoutes) POST(com.liferay.apio.architect.operation.Method.POST) DELETE(com.liferay.apio.architect.operation.Method.DELETE) RelatedCollection(com.liferay.apio.architect.related.RelatedCollection) SingleModel(com.liferay.apio.architect.single.model.SingleModel) NoSuchElementException(java.util.NoSuchElementException) PUT(com.liferay.apio.architect.operation.Method.PUT) Try(com.liferay.apio.architect.functional.Try) Predicate(java.util.function.Predicate) Path(com.liferay.apio.architect.uri.Path) Response(javax.ws.rs.core.Response) ThrowableConsumer(com.liferay.apio.architect.consumer.throwable.ThrowableConsumer) Optional(java.util.Optional) Path(com.liferay.apio.architect.uri.Path) ItemRoutes(com.liferay.apio.architect.routes.ItemRoutes)

Example 9 with Path

use of com.liferay.apio.architect.uri.Path in project com-liferay-apio-architect by liferay.

the class PathLongIdentifierMapperTest method testMapThrowsExceptionIfNoLongPathId.

@Test(expected = BadRequestException.class)
public void testMapThrowsExceptionIfNoLongPathId() {
    PathLongIdentifierMapper pathLongIdentifierMapper = new PathLongIdentifierMapper();
    pathLongIdentifierMapper.map(new Path("name", "wrong"));
}
Also used : Path(com.liferay.apio.architect.uri.Path) Test(org.junit.Test)

Example 10 with Path

use of com.liferay.apio.architect.uri.Path in project com-liferay-apio-architect by liferay.

the class PathLongIdentifierMapperTest method testMapReturnsPath.

@Test
public void testMapReturnsPath() {
    Path path = _pathIdentifierMapper.map("name", 42L);
    assertThat(path.getName(), is("name"));
    assertThat(path.getId(), is("42"));
}
Also used : Path(com.liferay.apio.architect.uri.Path) Test(org.junit.Test)

Aggregations

Path (com.liferay.apio.architect.uri.Path)14 Test (org.junit.Test)9 Optional (java.util.Optional)6 Operation (com.liferay.apio.architect.operation.Operation)5 SingleModel (com.liferay.apio.architect.single.model.SingleModel)5 Page (com.liferay.apio.architect.pagination.Page)4 Collections (java.util.Collections)4 List (java.util.List)4 POST (com.liferay.apio.architect.operation.Method.POST)3 Representor (com.liferay.apio.architect.representor.Representor)3 RequestInfo (com.liferay.apio.architect.request.RequestInfo)3 JsonObject (com.google.gson.JsonObject)2 Body (com.liferay.apio.architect.form.Body)2 Identifier (com.liferay.apio.architect.identifier.Identifier)2 FunctionalList (com.liferay.apio.architect.list.FunctionalList)2 PageMessageMapper (com.liferay.apio.architect.message.json.PageMessageMapper)2 DELETE (com.liferay.apio.architect.operation.Method.DELETE)2 PageItems (com.liferay.apio.architect.pagination.PageItems)2 Pagination (com.liferay.apio.architect.pagination.Pagination)2 Function (java.util.function.Function)2