vendredi 17 juillet 2015

Customer header is not coming in the response

In my Jax-Rs(Jersey + Jetty) application, I've added some custom headers to the response so that I can use those at the client side. I'm using ResponseBuilder to achieve this.

     @Provider
public class CustomExceptionMapper implements ExceptionMapper<CustomException> {

    private static final Logger LOGGER = LoggerFactory.getLogger(CustomExceptionMapper.class);

    @Context
    private HttpHeaders headers;


    @Override
    public Response toResponse(CustomException exception) {

        String errorKey = null;
        String errorArgs[] = null;

        // log stack trace
        LOGGER.error("An error occurred during the operation.", exception);
        if (exception instanceof MetadataServerException) {
                errorKey = ExceptionMapperUtil.checkForDBException(exception.getCause());
            if (errorKey != null) {
                return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
                        .entity(MessageSourceUtils.getMessage(errorKey, errorArgs, ExceptionMapperUtil.getLocale(headers))).type(MediaType.TEXT_PLAIN).build();
            }
        }

        if(ErrorCodes.INTERNAL_SERVER_ERROR.equals(exception.getMessageKey()) && 
                (exception.getMessageArguments() == null || exception.getMessageArguments().length==0) ){
            exception.setMessageArguments(new Object[]{"while executing the operation"}); 

        }
        // prepare response to be sent to client
        return Response.status(ResponseCodeMapper.mapToStatusCode(exception)).
                entity(exception.getMessage(ExceptionMapperUtil.getLocale(headers))).
                type(MediaType.TEXT_PLAIN).header("errorCode", exception.getMessageKey()).
                build();
    }
}

And also, I've confirmed that that the response object is getting built properly. However, for some reason, the returned response doesn't contain my custom header. Any clues as to to what is going wrong ?

Aucun commentaire:

Enregistrer un commentaire