Search in sources :

Example 1 with FormMessageMapper

use of com.liferay.apio.architect.message.json.FormMessageMapper 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)

Aggregations

Form (com.liferay.apio.architect.form.Form)1 Try (com.liferay.apio.architect.functional.Try)1 Success (com.liferay.apio.architect.functional.Try.Success)1 Language (com.liferay.apio.architect.language.Language)1 FormMessageMapper (com.liferay.apio.architect.message.json.FormMessageMapper)1 RequestInfo (com.liferay.apio.architect.request.RequestInfo)1 Embedded (com.liferay.apio.architect.response.control.Embedded)1 Fields (com.liferay.apio.architect.response.control.Fields)1 ServerURL (com.liferay.apio.architect.url.ServerURL)1 ProviderManager (com.liferay.apio.architect.wiring.osgi.manager.ProviderManager)1 FormMessageMapperManager (com.liferay.apio.architect.wiring.osgi.manager.message.json.FormMessageMapperManager)1 GenericUtil (com.liferay.apio.architect.wiring.osgi.util.GenericUtil)1 FormWriter (com.liferay.apio.architect.writer.FormWriter)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 Annotation (java.lang.annotation.Annotation)1 Type (java.lang.reflect.Type)1 StandardCharsets (java.nio.charset.StandardCharsets)1