package eu.dnetlib.repo.manager.controllers; import eu.dnetlib.api.functionality.ValidatorServiceException; import eu.dnetlib.repo.manager.exception.BrokerException; import eu.dnetlib.repo.manager.exception.EndPointException; import eu.dnetlib.repo.manager.exception.ResourceNotFoundException; import eu.dnetlib.repo.manager.exception.ServerError; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import org.json.JSONException; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.http.HttpStatus; import org.springframework.security.access.AccessDeniedException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; import javax.servlet.http.HttpServletRequest; import java.net.UnknownHostException; @ControllerAdvice @Order(Ordered.HIGHEST_PRECEDENCE) public class GenericControllerAdvice { private Logger logger = LogManager.getLogger(GenericControllerAdvice.class); @ResponseStatus(HttpStatus.NOT_FOUND) @ExceptionHandler(ResourceNotFoundException.class) @ResponseBody public ServerError securityException(HttpServletRequest req, Exception ex) { return new ServerError(req.getRequestURL().toString(), ex); } @ResponseStatus(HttpStatus.FORBIDDEN) @ExceptionHandler(AccessDeniedException.class) @ResponseBody public ServerError accessDeniedException(HttpServletRequest req, Exception ex) { return new ServerError(req.getRequestURL().toString(), ex); } @ResponseStatus(HttpStatus.NOT_FOUND) @ExceptionHandler(UnknownHostException.class) @ResponseBody public ServerError unknownHostException(HttpServletRequest req, Exception ex) { return new ServerError(req.getRequestURL().toString(), ex); } @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ExceptionHandler({JSONException.class, BrokerException.class, ValidatorServiceException.class}) @ResponseBody public ServerError internalException(HttpServletRequest req, Exception ex) { return new ServerError(req.getRequestURL().toString(), ex); } @ResponseStatus(HttpStatus.GATEWAY_TIMEOUT) @ExceptionHandler(EndPointException.class) @ResponseBody public ServerError endPointException(HttpServletRequest req, Exception ex) { return new ServerError(req.getRequestURL().toString(), ex); } }