Search in sources :

Example 1 with OPTIONAL

use of org.osgi.service.component.annotations.ReferenceCardinality.OPTIONAL in project com-liferay-apio-architect by liferay.

the class ErrorUtil method getErrorResponse.

/**
 * Transforms an exception into a {@code Response}.
 *
 * @param  exception the exception
 * @param  request the current request
 * @param  httpHeaders the current HTTP headers
 * @return the response
 */
public Response getErrorResponse(Exception exception, Request request, HttpHeaders httpHeaders) {
    Optional<APIError> apiErrorOptional = _exceptionConverterManager.convert(exception);
    if (!apiErrorOptional.isPresent()) {
        Class<? extends Exception> exceptionClass = exception.getClass();
        if (_apioLogger != null) {
            _apioLogger.warning("No exception converter found for " + exceptionClass);
        }
        if (exceptionClass.isAssignableFrom(WebApplicationException.class)) {
            WebApplicationException webApplicationException = (WebApplicationException) exception;
            return webApplicationException.getResponse();
        }
        return Response.serverError().build();
    }
    APIError apiError = apiErrorOptional.get();
    if (_apioLogger != null) {
        _apioLogger.error(apiError);
    }
    int statusCode = apiError.getStatusCode();
    Optional<ErrorMessageMapper> errorMessageMapperOptional = _errorMessageMapperManager.getErrorMessageMapperOptional(request);
    return errorMessageMapperOptional.map(errorMessageMapper -> {
        String result = ErrorWriter.writeError(errorMessageMapper, apiError, httpHeaders);
        return Response.status(statusCode).type(errorMessageMapper.getMediaType()).entity(result).build();
    }).orElseGet(() -> Response.status(statusCode).build());
}
Also used : ErrorWriter(com.liferay.apio.architect.writer.ErrorWriter) ErrorMessageMapper(com.liferay.apio.architect.message.json.ErrorMessageMapper) ApioLogger(com.liferay.apio.architect.logger.ApioLogger) ErrorMessageMapperManager(com.liferay.apio.architect.wiring.osgi.manager.message.json.ErrorMessageMapperManager) Component(org.osgi.service.component.annotations.Component) APIError(com.liferay.apio.architect.error.APIError) HttpHeaders(javax.ws.rs.core.HttpHeaders) Response(javax.ws.rs.core.Response) OPTIONAL(org.osgi.service.component.annotations.ReferenceCardinality.OPTIONAL) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) GREEDY(org.osgi.service.component.annotations.ReferencePolicyOption.GREEDY) Request(javax.ws.rs.core.Request) Reference(org.osgi.service.component.annotations.Reference) ExceptionConverterManager(com.liferay.apio.architect.wiring.osgi.manager.ExceptionConverterManager) WebApplicationException(javax.ws.rs.WebApplicationException) ErrorMessageMapper(com.liferay.apio.architect.message.json.ErrorMessageMapper) APIError(com.liferay.apio.architect.error.APIError)

Aggregations

APIError (com.liferay.apio.architect.error.APIError)1 ApioLogger (com.liferay.apio.architect.logger.ApioLogger)1 ErrorMessageMapper (com.liferay.apio.architect.message.json.ErrorMessageMapper)1 ExceptionConverterManager (com.liferay.apio.architect.wiring.osgi.manager.ExceptionConverterManager)1 ErrorMessageMapperManager (com.liferay.apio.architect.wiring.osgi.manager.message.json.ErrorMessageMapperManager)1 ErrorWriter (com.liferay.apio.architect.writer.ErrorWriter)1 Optional (java.util.Optional)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 HttpHeaders (javax.ws.rs.core.HttpHeaders)1 Request (javax.ws.rs.core.Request)1 Response (javax.ws.rs.core.Response)1 Component (org.osgi.service.component.annotations.Component)1 Reference (org.osgi.service.component.annotations.Reference)1 OPTIONAL (org.osgi.service.component.annotations.ReferenceCardinality.OPTIONAL)1 GREEDY (org.osgi.service.component.annotations.ReferencePolicyOption.GREEDY)1