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