Search in sources :

Example 31 with HttpHeaders

use of javax.ws.rs.core.HttpHeaders 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 32 with HttpHeaders

use of javax.ws.rs.core.HttpHeaders 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 33 with HttpHeaders

use of javax.ws.rs.core.HttpHeaders 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 34 with HttpHeaders

use of javax.ws.rs.core.HttpHeaders in project com-liferay-apio-architect by liferay.

the class JSONLDErrorMessageMapperTest method testJSONLDErrorMessageMapper.

@Test
public void testJSONLDErrorMessageMapper() {
    APIError apiError = new APIError(new IllegalArgumentException(), "A title", "A description", "A type", 404);
    HttpHeaders httpHeaders = Mockito.mock(HttpHeaders.class);
    String error = ErrorWriter.writeError(_errorMessageMapper, apiError, httpHeaders);
    Conditions.Builder builder = new Conditions.Builder();
    Conditions conditions = builder.where("@type", is(aJsonString(equalTo("A type")))).where("description", is(aJsonString(equalTo("A description")))).where("statusCode", is(aJsonInt(equalTo(404)))).where("title", is(aJsonString(equalTo("A title")))).build();
    assertThat(error, is(aJsonObjectStringWith(conditions)));
}
Also used : HttpHeaders(javax.ws.rs.core.HttpHeaders) JsonMatchers.aJsonString(com.liferay.apio.architect.test.util.json.JsonMatchers.aJsonString) APIError(com.liferay.apio.architect.error.APIError) Conditions(com.liferay.apio.architect.test.util.json.Conditions) Test(org.junit.Test)

Example 35 with HttpHeaders

use of javax.ws.rs.core.HttpHeaders in project com-liferay-apio-architect by liferay.

the class HALPageMessageMapperTest method testHALPageMessageMapper.

@Test
public void testHALPageMessageMapper() {
    HttpHeaders httpHeaders = Mockito.mock(HttpHeaders.class);
    JsonObject jsonObject = MockPageWriter.write(httpHeaders, _pageMessageMapper);
    Conditions.Builder builder = new Conditions.Builder();
    Conditions conditions = builder.where("_embedded", _isAJsonObjectWithTheEmbedded).where("_links", _isAJsonObjectWithTheLinks).where("count", is(aJsonInt(equalTo(3)))).where("total", is(aJsonInt(equalTo(9)))).build();
    assertThat(jsonObject, is(aJsonObjectWith(conditions)));
}
Also used : HttpHeaders(javax.ws.rs.core.HttpHeaders) JsonObject(com.google.gson.JsonObject) Conditions(com.liferay.apio.architect.test.util.json.Conditions) Test(org.junit.Test)

Aggregations

HttpHeaders (javax.ws.rs.core.HttpHeaders)95 Test (org.junit.Test)57 Message (org.apache.cxf.message.Message)31 Optional (java.util.Optional)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)13 UriInfo (javax.ws.rs.core.UriInfo)12 CatalogFramework (ddf.catalog.CatalogFramework)10 Locale (java.util.Locale)10 Response (javax.ws.rs.core.Response)10 MultipartBody (org.apache.cxf.jaxrs.ext.multipart.MultipartBody)10 JsonObject (com.google.gson.JsonObject)9 MediaType (javax.ws.rs.core.MediaType)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 Map (java.util.Map)8 Cookie (javax.ws.rs.core.Cookie)8 IOException (java.io.IOException)7 Collections (java.util.Collections)7 WebApplicationException (javax.ws.rs.WebApplicationException)7 URI (java.net.URI)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6