1
|
package eu.dnetlib.repo.manager.controllers;
|
2
|
|
3
|
|
4
|
import eu.dnetlib.api.functionality.ValidatorServiceException;
|
5
|
import eu.dnetlib.repo.manager.exception.EndPointException;
|
6
|
import eu.dnetlib.repo.manager.exception.ResourceNotFoundException;
|
7
|
import eu.dnetlib.repo.manager.exception.ServerError;
|
8
|
import eu.dnetlib.repo.manager.shared.BrokerException;
|
9
|
import org.apache.log4j.LogManager;
|
10
|
import org.apache.log4j.Logger;
|
11
|
import org.json.JSONException;
|
12
|
import org.springframework.core.Ordered;
|
13
|
import org.springframework.core.annotation.Order;
|
14
|
import org.springframework.http.HttpStatus;
|
15
|
import org.springframework.security.access.AccessDeniedException;
|
16
|
import org.springframework.web.bind.annotation.*;
|
17
|
|
18
|
import javax.servlet.http.HttpServletRequest;
|
19
|
import java.net.UnknownHostException;
|
20
|
|
21
|
|
22
|
@ControllerAdvice
|
23
|
@Order(Ordered.HIGHEST_PRECEDENCE)
|
24
|
public class GenericControllerAdvice {
|
25
|
|
26
|
private Logger logger = LogManager.getLogger(GenericControllerAdvice.class);
|
27
|
|
28
|
|
29
|
@ResponseStatus(HttpStatus.NOT_FOUND)
|
30
|
@ExceptionHandler(ResourceNotFoundException.class)
|
31
|
@ResponseBody
|
32
|
public ServerError securityException(HttpServletRequest req, Exception ex) {
|
33
|
return new ServerError(req.getRequestURL().toString(),ex);
|
34
|
}
|
35
|
|
36
|
@ResponseStatus(HttpStatus.FORBIDDEN)
|
37
|
@ExceptionHandler(AccessDeniedException.class)
|
38
|
@ResponseBody
|
39
|
public ServerError accessDeniedException(HttpServletRequest req, Exception ex) {
|
40
|
return new ServerError(req.getRequestURL().toString(),ex);
|
41
|
}
|
42
|
|
43
|
@ResponseStatus(HttpStatus.NOT_FOUND)
|
44
|
@ExceptionHandler(UnknownHostException.class)
|
45
|
@ResponseBody
|
46
|
public ServerError unknownHostException(HttpServletRequest req, Exception ex) {
|
47
|
return new ServerError(req.getRequestURL().toString(),ex);
|
48
|
}
|
49
|
|
50
|
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
51
|
@ExceptionHandler({JSONException.class,BrokerException.class,ValidatorServiceException.class})
|
52
|
@ResponseBody
|
53
|
public ServerError internalException(HttpServletRequest req, Exception ex) {
|
54
|
return new ServerError(req.getRequestURL().toString(),ex);
|
55
|
}
|
56
|
|
57
|
@ResponseStatus(HttpStatus.GATEWAY_TIMEOUT)
|
58
|
@ExceptionHandler(EndPointException.class)
|
59
|
@ResponseBody
|
60
|
public ServerError endPointException(HttpServletRequest req, Exception ex) {
|
61
|
return new ServerError(req.getRequestURL().toString(),ex);
|
62
|
}
|
63
|
|
64
|
}
|