![JAR search and dependency download from the Maven repository](/logo.png)
com.sap.cloud.yaas.servicesdk.springboot.jersey.ResourceNotFoundExceptionMapper Maven / Gradle / Ivy
/*
* © 2017 SAP SE or an SAP affiliate company.
* All rights reserved.
* Please see http://www.sap.com/corporate-en/legal/copyright/index.epx for additional trademark information and
* notices.
*/
package com.sap.cloud.yaas.servicesdk.springboot.jersey;
import javax.annotation.Priority;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import com.sap.cloud.yaas.servicesdk.patternsupport.common.ErrorResponses;
import com.sap.cloud.yaas.servicesdk.patternsupport.schemas.ErrorMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collections;
/**
* Serializes a {@link ResourceNotFoundException} to a 404 response with an empty body.
*
* This is useful, if further processing outside of JAX-RS is intended.
* In particular, when Jersey runs as a Filter and passes requests that it cannot handle itself up the Filter chain.
*
* @see org.glassfish.jersey.servlet.ServletProperties#FILTER_FORWARD_ON_404
*/
@Produces({MediaType.APPLICATION_JSON})
@Priority(ResourceNotFoundExceptionMapper.THIS_PRIORITY)
public class ResourceNotFoundExceptionMapper implements ExceptionMapper
{
/**
* Defines the priority of the {@link ExceptionMapper}.
*/
protected static final int THIS_PRIORITY = 10;
private static final Logger LOG = LoggerFactory.getLogger(ResourceNotFoundExceptionMapper.class);
private static final String RESPONSE_BODY_MESSAGE = "The requested resource was not found on the server.";
@Override
public Response toResponse(final ResourceNotFoundException exception)
{
final int statusCode = Response.Status.NOT_FOUND.getStatusCode();
LOG.debug(ResourceNotFoundException.class.getSimpleName() + " occured: " + exception.getMessage() + ", responding with "
+ statusCode);
LOG.trace(ResourceNotFoundException.class.getSimpleName() + " occured", exception);
final ErrorMessage errorMessage = new ErrorMessage();
errorMessage.setStatus(statusCode);
errorMessage.setMessage(RESPONSE_BODY_MESSAGE);
errorMessage.setType(ErrorResponses.ErrorTypes.NOT_FOUND_ELEMENT);
errorMessage.setMoreInfo(ErrorResponses.DOCUMENTATION_LINK);
errorMessage.setDetails(Collections.emptyList());
return Response
.status(statusCode)
.type(MediaType.APPLICATION_JSON_TYPE)
.entity(errorMessage)
.build();
}
}