Project

General

Profile

1
package eu.dnetlib.repo.manager.controllers;
2

    
3

    
4
import eu.dnetlib.api.functionality.ValidatorServiceException;
5
import eu.dnetlib.repo.manager.domain.BrokerException;
6
import eu.dnetlib.repo.manager.exception.EndPointException;
7
import eu.dnetlib.repo.manager.exception.ResourceNotFoundException;
8
import eu.dnetlib.repo.manager.exception.ServerError;
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.ControllerAdvice;
17
import org.springframework.web.bind.annotation.ExceptionHandler;
18
import org.springframework.web.bind.annotation.ResponseBody;
19
import org.springframework.web.bind.annotation.ResponseStatus;
20

    
21
import javax.servlet.http.HttpServletRequest;
22
import java.net.UnknownHostException;
23

    
24

    
25
@ControllerAdvice
26
@Order(Ordered.HIGHEST_PRECEDENCE)
27
public class GenericControllerAdvice {
28

    
29
    private Logger logger = LogManager.getLogger(GenericControllerAdvice.class);
30

    
31

    
32
    @ResponseStatus(HttpStatus.NOT_FOUND)
33
    @ExceptionHandler(ResourceNotFoundException.class)
34
    @ResponseBody
35
    public ServerError securityException(HttpServletRequest req, Exception ex) {
36
        return new ServerError(req.getRequestURL().toString(),ex);
37
    }
38

    
39
    @ResponseStatus(HttpStatus.FORBIDDEN)
40
    @ExceptionHandler(AccessDeniedException.class)
41
    @ResponseBody
42
    public ServerError accessDeniedException(HttpServletRequest req, Exception ex) {
43
        return new ServerError(req.getRequestURL().toString(),ex);
44
    }
45

    
46
    @ResponseStatus(HttpStatus.NOT_FOUND)
47
    @ExceptionHandler(UnknownHostException.class)
48
    @ResponseBody
49
    public ServerError unknownHostException(HttpServletRequest req, Exception ex) {
50
        return new ServerError(req.getRequestURL().toString(),ex);
51
    }
52

    
53
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
54
    @ExceptionHandler({JSONException.class,BrokerException.class,ValidatorServiceException.class})
55
    @ResponseBody
56
    public ServerError internalException(HttpServletRequest req, Exception ex) {
57
        return new ServerError(req.getRequestURL().toString(),ex);
58
    }
59

    
60
    @ResponseStatus(HttpStatus.GATEWAY_TIMEOUT)
61
    @ExceptionHandler(EndPointException.class)
62
    @ResponseBody
63
    public ServerError endPointException(HttpServletRequest req, Exception ex) {
64
        return new ServerError(req.getRequestURL().toString(),ex);
65
    }
66

    
67
}
(3-3/12)