Search in sources :

Example 1 with ServerURL

use of com.liferay.apio.architect.url.ServerURL in project com-liferay-apio-architect by liferay.

the class DocumentationMessageBodyWriter method writeTo.

@Override
public void writeTo(Documentation documentation, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream outputStream) throws IOException, WebApplicationException {
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
    PrintWriter printWriter = new PrintWriter(outputStreamWriter, true);
    Optional<DocumentationMessageMapper> optional = _documentationMessageMapperManager.getDocumentationMessageMapperOptional(_request);
    DocumentationMessageMapper documentationMessageMapper = optional.orElseThrow(NotSupportedException::new);
    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());
    DocumentationWriter documentationWriter = DocumentationWriter.create(builder -> builder.documentation(documentation).documentationMessageMapper(documentationMessageMapper).requestInfo(requestInfo).build());
    httpHeaders.put(CONTENT_TYPE, Collections.singletonList(documentationMessageMapper.getMediaType()));
    printWriter.println(documentationWriter.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) Embedded(com.liferay.apio.architect.response.control.Embedded) DocumentationMessageMapperManager(com.liferay.apio.architect.wiring.osgi.manager.message.json.DocumentationMessageMapperManager) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) HttpServletRequest(javax.servlet.http.HttpServletRequest) MediaType(javax.ws.rs.core.MediaType) Component(org.osgi.service.component.annotations.Component) Locale(java.util.Locale) Documentation(com.liferay.apio.architect.documentation.Documentation) Fields(com.liferay.apio.architect.response.control.Fields) OutputStreamWriter(java.io.OutputStreamWriter) ProviderManager(com.liferay.apio.architect.wiring.osgi.manager.ProviderManager) NotSupportedException(javax.ws.rs.NotSupportedException) OutputStream(java.io.OutputStream) PrintWriter(java.io.PrintWriter) Context(javax.ws.rs.core.Context) DocumentationMessageMapper(com.liferay.apio.architect.message.json.DocumentationMessageMapper) ServerURL(com.liferay.apio.architect.url.ServerURL) IOException(java.io.IOException) StandardCharsets(java.nio.charset.StandardCharsets) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Language(com.liferay.apio.architect.language.Language) HttpHeaders(javax.ws.rs.core.HttpHeaders) Type(java.lang.reflect.Type) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) DocumentationWriter(com.liferay.apio.architect.writer.DocumentationWriter) Request(javax.ws.rs.core.Request) Reference(org.osgi.service.component.annotations.Reference) Collections(java.util.Collections) Fields(com.liferay.apio.architect.response.control.Fields) DocumentationWriter(com.liferay.apio.architect.writer.DocumentationWriter) OutputStreamWriter(java.io.OutputStreamWriter) NotSupportedException(javax.ws.rs.NotSupportedException) RequestInfo(com.liferay.apio.architect.request.RequestInfo) PrintWriter(java.io.PrintWriter) DocumentationMessageMapper(com.liferay.apio.architect.message.json.DocumentationMessageMapper)

Example 2 with ServerURL

use of com.liferay.apio.architect.url.ServerURL in project com-liferay-apio-architect by liferay.

the class FormMessageBodyWriter method writeTo.

@Override
public void writeTo(Success<Form> success, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream outputStream) throws IOException, WebApplicationException {
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
    PrintWriter printWriter = new PrintWriter(outputStreamWriter, true);
    Form form = success.getValue();
    Optional<FormMessageMapper> optional = _formMessageMapperManager.getFormMessageMapperOptional(_request);
    FormMessageMapper formMessageMapper = optional.orElseThrow(NotSupportedException::new);
    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());
    FormWriter formWriter = FormWriter.create(builder -> builder.form(form).formMessageMapper(formMessageMapper).requestInfo(requestInfo).build());
    httpHeaders.put(CONTENT_TYPE, Collections.singletonList(formMessageMapper.getMediaType()));
    printWriter.println(formWriter.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) FormMessageMapperManager(com.liferay.apio.architect.wiring.osgi.manager.message.json.FormMessageMapperManager) Embedded(com.liferay.apio.architect.response.control.Embedded) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) HttpServletRequest(javax.servlet.http.HttpServletRequest) MediaType(javax.ws.rs.core.MediaType) Component(org.osgi.service.component.annotations.Component) Locale(java.util.Locale) Fields(com.liferay.apio.architect.response.control.Fields) Success(com.liferay.apio.architect.functional.Try.Success) GenericUtil(com.liferay.apio.architect.wiring.osgi.util.GenericUtil) OutputStreamWriter(java.io.OutputStreamWriter) ProviderManager(com.liferay.apio.architect.wiring.osgi.manager.ProviderManager) NotSupportedException(javax.ws.rs.NotSupportedException) OutputStream(java.io.OutputStream) PrintWriter(java.io.PrintWriter) Try(com.liferay.apio.architect.functional.Try) Context(javax.ws.rs.core.Context) FormWriter(com.liferay.apio.architect.writer.FormWriter) Form(com.liferay.apio.architect.form.Form) ServerURL(com.liferay.apio.architect.url.ServerURL) IOException(java.io.IOException) FormMessageMapper(com.liferay.apio.architect.message.json.FormMessageMapper) StandardCharsets(java.nio.charset.StandardCharsets) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Language(com.liferay.apio.architect.language.Language) HttpHeaders(javax.ws.rs.core.HttpHeaders) Type(java.lang.reflect.Type) 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) Fields(com.liferay.apio.architect.response.control.Fields) Form(com.liferay.apio.architect.form.Form) FormMessageMapper(com.liferay.apio.architect.message.json.FormMessageMapper) OutputStreamWriter(java.io.OutputStreamWriter) NotSupportedException(javax.ws.rs.NotSupportedException) RequestInfo(com.liferay.apio.architect.request.RequestInfo) FormWriter(com.liferay.apio.architect.writer.FormWriter) PrintWriter(java.io.PrintWriter)

Example 3 with ServerURL

use of com.liferay.apio.architect.url.ServerURL 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 4 with ServerURL

use of com.liferay.apio.architect.url.ServerURL 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 5 with ServerURL

use of com.liferay.apio.architect.url.ServerURL in project com-liferay-apio-architect by liferay.

the class RootEndpointImpl method home.

@Override
public Response home() {
    List<String> resourceNames = _collectionRouterManager.getResourceNames();
    ServerURL serverURL = _providerManager.provideMandatory(_httpServletRequest, ServerURL.class);
    JsonObject resourcesJsonObject = new JsonObject();
    resourceNames.forEach(name -> {
        String url = serverURL.get() + "/p/" + name;
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("href", url);
        resourcesJsonObject.add(name, jsonObject);
    });
    JsonObject rootJsonObject = new JsonObject();
    rootJsonObject.add("resources", resourcesJsonObject);
    return Response.ok(rootJsonObject.toString()).type(MediaType.valueOf("application/json")).build();
}
Also used : ServerURL(com.liferay.apio.architect.url.ServerURL) JsonObject(com.google.gson.JsonObject)

Aggregations

ServerURL (com.liferay.apio.architect.url.ServerURL)5 Language (com.liferay.apio.architect.language.Language)4 RequestInfo (com.liferay.apio.architect.request.RequestInfo)4 Embedded (com.liferay.apio.architect.response.control.Embedded)4 Fields (com.liferay.apio.architect.response.control.Fields)4 ProviderManager (com.liferay.apio.architect.wiring.osgi.manager.ProviderManager)4 IOException (java.io.IOException)4 OutputStream (java.io.OutputStream)4 OutputStreamWriter (java.io.OutputStreamWriter)4 PrintWriter (java.io.PrintWriter)4 Annotation (java.lang.annotation.Annotation)4 Type (java.lang.reflect.Type)4 StandardCharsets (java.nio.charset.StandardCharsets)4 Collections (java.util.Collections)4 Locale (java.util.Locale)4 Optional (java.util.Optional)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 NotSupportedException (javax.ws.rs.NotSupportedException)4 WebApplicationException (javax.ws.rs.WebApplicationException)4 Context (javax.ws.rs.core.Context)4