Search in sources :

Example 1 with Failure

use of com.liferay.apio.architect.functional.Try.Failure in project com-liferay-apio-architect by liferay.

the class FailureFilter method filter.

@Override
public void filter(ContainerRequestContext containerRequestContext, ContainerResponseContext containerResponseContext) throws IOException {
    Try<Object> objectTry = Try.fromFallible(containerResponseContext::getEntity);
    objectTry.map(Failure.class::cast).map(Failure::getException).map(exception -> _errorUtil.getErrorResponse(exception, _request, _httpHeaders)).ifSuccess(response -> {
        containerResponseContext.setStatus(response.getStatus());
        MultivaluedMap<String, Object> headers = containerResponseContext.getHeaders();
        headers.remove(CONTENT_TYPE);
        MediaType mediaType = response.getMediaType();
        if (mediaType != null) {
            headers.add(CONTENT_TYPE, mediaType.toString());
        }
        Object entity = response.getEntity();
        if (entity != null) {
            containerResponseContext.setEntity(entity);
        }
    });
}
Also used : CONTENT_TYPE(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE) Try(com.liferay.apio.architect.functional.Try) Context(javax.ws.rs.core.Context) IOException(java.io.IOException) Failure(com.liferay.apio.architect.functional.Try.Failure) ContainerResponseFilter(javax.ws.rs.container.ContainerResponseFilter) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) ErrorUtil(com.liferay.apio.architect.jaxrs.json.internal.util.ErrorUtil) MediaType(javax.ws.rs.core.MediaType) Component(org.osgi.service.component.annotations.Component) HttpHeaders(javax.ws.rs.core.HttpHeaders) Request(javax.ws.rs.core.Request) Reference(org.osgi.service.component.annotations.Reference) ContainerResponseContext(javax.ws.rs.container.ContainerResponseContext) MediaType(javax.ws.rs.core.MediaType) Failure(com.liferay.apio.architect.functional.Try.Failure)

Aggregations

Try (com.liferay.apio.architect.functional.Try)1 Failure (com.liferay.apio.architect.functional.Try.Failure)1 ErrorUtil (com.liferay.apio.architect.jaxrs.json.internal.util.ErrorUtil)1 IOException (java.io.IOException)1 ContainerRequestContext (javax.ws.rs.container.ContainerRequestContext)1 ContainerResponseContext (javax.ws.rs.container.ContainerResponseContext)1 ContainerResponseFilter (javax.ws.rs.container.ContainerResponseFilter)1 Context (javax.ws.rs.core.Context)1 HttpHeaders (javax.ws.rs.core.HttpHeaders)1 CONTENT_TYPE (javax.ws.rs.core.HttpHeaders.CONTENT_TYPE)1 MediaType (javax.ws.rs.core.MediaType)1 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)1 Request (javax.ws.rs.core.Request)1 Component (org.osgi.service.component.annotations.Component)1 Reference (org.osgi.service.component.annotations.Reference)1