Search in sources :

Example 1 with PageWriter

use of com.liferay.apio.architect.writer.PageWriter 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 PageWriter

use of com.liferay.apio.architect.writer.PageWriter in project com-liferay-apio-architect by liferay.

the class MockPageWriter method write.

/**
 * Writes a Collection of {@link RootModel}, with the hierarchy of embedded
 * models and multiple fields.
 *
 * @param httpHeaders the request's {@code HttpHeaders}
 * @param pageMessageMapper the {@link PageMessageMapper} to use for writing
 *        the JSON object
 */
public static JsonObject write(HttpHeaders httpHeaders, PageMessageMapper<RootModel> pageMessageMapper) {
    RequestInfo requestInfo = getRequestInfo(httpHeaders);
    Collection<RootModel> items = Arrays.asList(() -> "1", () -> "2", () -> "3");
    PageItems<RootModel> pageItems = new PageItems<>(items, 9);
    Pagination pagination = new Pagination(3, 2);
    Path path = new Path("name", "id");
    List<Operation> operations = Collections.singletonList(new Operation(createForm("c", "p"), POST, "create-operation"));
    Page<RootModel> page = new Page<>("root", pageItems, pagination, path, operations);
    PageWriter<RootModel> pageWriter = PageWriter.create(builder -> builder.page(page).pageMessageMapper(pageMessageMapper).pathFunction(MockWriterUtil::identifierToPath).resourceNameFunction(__ -> Optional.of("models")).representorFunction(MockWriterUtil::getRepresentorOptional).requestInfo(requestInfo).singleModelFunction(MockWriterUtil::getSingleModel).build());
    return new Gson().fromJson(pageWriter.write(), JsonObject.class);
}
Also used : Path(com.liferay.apio.architect.uri.Path) JsonObject(com.google.gson.JsonObject) Arrays(java.util.Arrays) MockFormCreator.createForm(com.liferay.apio.architect.test.util.form.MockFormCreator.createForm) RequestInfo(com.liferay.apio.architect.request.RequestInfo) Collection(java.util.Collection) PageItems(com.liferay.apio.architect.pagination.PageItems) Path(com.liferay.apio.architect.uri.Path) Operation(com.liferay.apio.architect.operation.Operation) PageMessageMapper(com.liferay.apio.architect.message.json.PageMessageMapper) List(java.util.List) PageWriter(com.liferay.apio.architect.writer.PageWriter) HttpHeaders(javax.ws.rs.core.HttpHeaders) Pagination(com.liferay.apio.architect.pagination.Pagination) Gson(com.google.gson.Gson) RootModel(com.liferay.apio.architect.test.util.model.RootModel) Page(com.liferay.apio.architect.pagination.Page) POST(com.liferay.apio.architect.operation.Method.POST) Optional(java.util.Optional) MockWriterUtil.getRequestInfo(com.liferay.apio.architect.test.util.writer.MockWriterUtil.getRequestInfo) Collections(java.util.Collections) RootModel(com.liferay.apio.architect.test.util.model.RootModel) Gson(com.google.gson.Gson) Page(com.liferay.apio.architect.pagination.Page) Operation(com.liferay.apio.architect.operation.Operation) RequestInfo(com.liferay.apio.architect.request.RequestInfo) MockWriterUtil.getRequestInfo(com.liferay.apio.architect.test.util.writer.MockWriterUtil.getRequestInfo) Pagination(com.liferay.apio.architect.pagination.Pagination) PageItems(com.liferay.apio.architect.pagination.PageItems)

Aggregations

PageMessageMapper (com.liferay.apio.architect.message.json.PageMessageMapper)2 Page (com.liferay.apio.architect.pagination.Page)2 RequestInfo (com.liferay.apio.architect.request.RequestInfo)2 PageWriter (com.liferay.apio.architect.writer.PageWriter)2 Collections (java.util.Collections)2 Optional (java.util.Optional)2 HttpHeaders (javax.ws.rs.core.HttpHeaders)2 Gson (com.google.gson.Gson)1 JsonObject (com.google.gson.JsonObject)1 Try (com.liferay.apio.architect.functional.Try)1 Identifier (com.liferay.apio.architect.identifier.Identifier)1 Language (com.liferay.apio.architect.language.Language)1 POST (com.liferay.apio.architect.operation.Method.POST)1 Operation (com.liferay.apio.architect.operation.Operation)1 PageItems (com.liferay.apio.architect.pagination.PageItems)1 Pagination (com.liferay.apio.architect.pagination.Pagination)1 Embedded (com.liferay.apio.architect.response.control.Embedded)1 Fields (com.liferay.apio.architect.response.control.Fields)1 ItemRoutes (com.liferay.apio.architect.routes.ItemRoutes)1 SingleModel (com.liferay.apio.architect.single.model.SingleModel)1