All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openl.rules.ruleservice.spring.WebApplicationExceptionMapper Maven / Gradle / Ivy

There is a newer version: 5.27.9
Show newest version
package org.openl.rules.ruleservice.spring;

import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper;
import jakarta.ws.rs.ext.Provider;

import org.apache.cxf.jaxrs.utils.JAXRSUtils;
import org.springframework.stereotype.Component;

import org.openl.rules.ruleservice.core.ExceptionType;
import org.openl.rules.ruleservice.publish.jaxrs.JAXRSErrorResponse;

/**
 * Wraps the response into the JSON structure.
 *
 * @author Yury Molchan
 */
@Component
@Provider
public class WebApplicationExceptionMapper implements ExceptionMapper {
    @Override
    public Response toResponse(WebApplicationException exception) {
        var response = exception.getResponse();
        if (response != null && response.hasEntity()) {
            return response;
        }
        if (response == null) {
            response = Response.serverError().build();
        }
        var originalMessage = exception.getMessage();
        var cause = exception.getCause();
        var message = cause != null ? cause.getMessage() : originalMessage;
        if (message == null) {
            message = cause != null ? cause.getClass().getName() : ((Throwable) exception).getClass().getName();
        }
        return JAXRSUtils.fromResponse(response, false)
                .type(MediaType.APPLICATION_JSON)
                .entity(new JAXRSErrorResponse(message, ExceptionType.SYSTEM))
                .build();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy