Search in sources :

Example 1 with SingleModelWriter

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

the class SingleModelMessageBodyWriter method writeTo.

@Override
public void writeTo(Try.Success<SingleModel<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);
    SingleModel<T> singleModel = 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<SingleModelMessageMapper<T>> optional = _singleModelMessageMapperManager.getSingleModelMessageMapperOptional(_request);
    SingleModelMessageMapper<T> singleModelMessageMapper = optional.orElseThrow(NotSupportedException::new);
    SingleModelWriter<T> singleModelWriter = SingleModelWriter.create(builder -> builder.singleModel(singleModel).modelMessageMapper(singleModelMessageMapper).pathFunction(_pathIdentifierMapperManager::mapToPath).resourceNameFunction(_nameManager::getNameOptional).representorFunction(name -> unsafeCast(_representableManager.getRepresentorOptional(name))).requestInfo(requestInfo).singleModelFunction(this::_getSingleModelOptional).build());
    httpHeaders.put(CONTENT_TYPE, Collections.singletonList(singleModelMessageMapper.getMediaType()));
    Optional<String> resultOptional = singleModelWriter.write();
    resultOptional.ifPresent(printWriter::write);
    printWriter.close();
}
Also used : CONTENT_TYPE(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE) Provider(javax.ws.rs.ext.Provider) 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) SingleModelMessageMapperManager(com.liferay.apio.architect.wiring.osgi.manager.message.json.SingleModelMessageMapperManager) 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) SingleModelWriter(com.liferay.apio.architect.writer.SingleModelWriter) Locale(java.util.Locale) 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) SingleModelMessageMapper(com.liferay.apio.architect.message.json.SingleModelMessageMapper) ServerURL(com.liferay.apio.architect.url.ServerURL) IOException(java.io.IOException) StandardCharsets(java.nio.charset.StandardCharsets) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) IdentifierClassManager(com.liferay.apio.architect.wiring.osgi.manager.representable.IdentifierClassManager) Language(com.liferay.apio.architect.language.Language) 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) SingleModelMessageMapper(com.liferay.apio.architect.message.json.SingleModelMessageMapper) RequestInfo(com.liferay.apio.architect.request.RequestInfo) Fields(com.liferay.apio.architect.response.control.Fields) OutputStreamWriter(java.io.OutputStreamWriter) NotSupportedException(javax.ws.rs.NotSupportedException) PrintWriter(java.io.PrintWriter)

Example 2 with SingleModelWriter

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

the class MockSingleModelWriter method write.

/**
 * Writes a {@link RootModel}, with the hierarchy of embedded models and
 * multiple fields.
 *
 * @param httpHeaders the request's {@code HttpHeaders}
 * @param singleModelMessageMapper the {@link SingleModelMessageMapper} to
 *        use for writing the JSON object
 */
public static JsonObject write(HttpHeaders httpHeaders, SingleModelMessageMapper<RootModel> singleModelMessageMapper) {
    RequestInfo requestInfo = getRequestInfo(httpHeaders);
    Operation deleteOperation = new Operation(DELETE, "delete-operation");
    Operation putOperation = new Operation(createForm("u", "r"), PUT, "update-operation");
    SingleModel<RootModel> singleModel = new SingleModel<>(() -> "first", "root", asList(deleteOperation, putOperation));
    SingleModelWriter<RootModel> singleModelWriter = SingleModelWriter.create(builder -> builder.singleModel(singleModel).modelMessageMapper(singleModelMessageMapper).pathFunction(MockWriterUtil::identifierToPath).resourceNameFunction(__ -> Optional.of("models")).representorFunction(MockWriterUtil::getRepresentorOptional).requestInfo(requestInfo).singleModelFunction(MockWriterUtil::getSingleModel).build());
    Optional<String> optional = singleModelWriter.write();
    if (!optional.isPresent()) {
        throw new AssertionError("Writer failed to write");
    }
    return new Gson().fromJson(optional.get(), JsonObject.class);
}
Also used : SingleModel(com.liferay.apio.architect.single.model.SingleModel) JsonObject(com.google.gson.JsonObject) MockFormCreator.createForm(com.liferay.apio.architect.test.util.form.MockFormCreator.createForm) RequestInfo(com.liferay.apio.architect.request.RequestInfo) SingleModelMessageMapper(com.liferay.apio.architect.message.json.SingleModelMessageMapper) Operation(com.liferay.apio.architect.operation.Operation) HttpHeaders(javax.ws.rs.core.HttpHeaders) SingleModelWriter(com.liferay.apio.architect.writer.SingleModelWriter) Arrays.asList(java.util.Arrays.asList) Gson(com.google.gson.Gson) RootModel(com.liferay.apio.architect.test.util.model.RootModel) DELETE(com.liferay.apio.architect.operation.Method.DELETE) Optional(java.util.Optional) MockWriterUtil.getRequestInfo(com.liferay.apio.architect.test.util.writer.MockWriterUtil.getRequestInfo) SingleModel(com.liferay.apio.architect.single.model.SingleModel) PUT(com.liferay.apio.architect.operation.Method.PUT) RootModel(com.liferay.apio.architect.test.util.model.RootModel) Gson(com.google.gson.Gson) 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)

Aggregations

SingleModelMessageMapper (com.liferay.apio.architect.message.json.SingleModelMessageMapper)2 RequestInfo (com.liferay.apio.architect.request.RequestInfo)2 SingleModel (com.liferay.apio.architect.single.model.SingleModel)2 SingleModelWriter (com.liferay.apio.architect.writer.SingleModelWriter)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 DELETE (com.liferay.apio.architect.operation.Method.DELETE)1 PUT (com.liferay.apio.architect.operation.Method.PUT)1 Operation (com.liferay.apio.architect.operation.Operation)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 MockFormCreator.createForm (com.liferay.apio.architect.test.util.form.MockFormCreator.createForm)1 RootModel (com.liferay.apio.architect.test.util.model.RootModel)1 MockWriterUtil.getRequestInfo (com.liferay.apio.architect.test.util.writer.MockWriterUtil.getRequestInfo)1