Project

General

Profile

1
package eu.dnetlib.uoaorcidservice.controllers;
2
import eu.dnetlib.uoaorcidservice.responses.ExceptionResponse;
3
import org.apache.log4j.Logger;
4
import org.springframework.beans.factory.annotation.Autowired;
5
import org.springframework.boot.autoconfigure.web.ErrorAttributes;
6
import org.springframework.boot.autoconfigure.web.ErrorController;
7
import org.springframework.http.HttpStatus;
8
import org.springframework.http.ResponseEntity;
9
import org.springframework.util.Assert;
10
import org.springframework.web.bind.annotation.CrossOrigin;
11
import org.springframework.web.bind.annotation.RequestMapping;
12
import org.springframework.web.bind.annotation.RestController;
13
import org.springframework.web.context.request.RequestAttributes;
14
import org.springframework.web.context.request.ServletRequestAttributes;
15

    
16
import javax.servlet.RequestDispatcher;
17
import javax.servlet.http.HttpServletRequest;
18
import java.util.Map;
19

    
20
//public class SimpleErrorController {}
21

    
22
@RestController
23
@CrossOrigin(origins = "*")
24
@RequestMapping("/error")
25
public class SimpleErrorController implements ErrorController {
26
    private final Logger log = Logger.getLogger(this.getClass());
27
    private final Logger orcid_log = Logger.getLogger("ORCID-"+this.getClass().getName());
28

    
29
    private final ErrorAttributes errorAttributes;
30

    
31
    @Autowired
32
    public SimpleErrorController(ErrorAttributes errorAttributes) {
33
        Assert.notNull(errorAttributes, "ErrorAttributes must not be null");
34
        this.errorAttributes = errorAttributes;
35
    }
36

    
37
    @Override
38
    public String getErrorPath() {
39
        return "/error";
40
    }
41

    
42
    @RequestMapping
43
    public ResponseEntity<ExceptionResponse> error(HttpServletRequest aRequest){
44
        Map<String, Object> body = getErrorAttributes(aRequest,getTraceParameter(aRequest));
45
//        String trace = (String) body.get("trace");
46
//        log.debug(trace);
47
//        if(trace != null){
48
//            String[] lines = trace.split("\n\t");
49
//            log.debug(lines);
50
//            body.put("trace", lines);
51
//        }
52
//        log.debug(body);
53

    
54
//        {timestamp=Mon Jan 04 14:29:25 EET 2021,
55
//                status=500,
56
//                error=Internal Server Error,
57
//                exception=org.springframework.web.client.UnknownHttpStatusCodeException,
58
//                message=Unknown status code [525] Origin SSL Handshake Error,
59
//            path=/uoa-orcid-service/orcid/work/save}
60

    
61
        String path = (String)body.get("path");
62
        if(path.contains("/uoa-orcid-service/orcid")) {
63
            orcid_log.error(body);
64
        } else {
65
            log.error(body);
66
        }
67

    
68
        Integer status = (Integer)body.get("status");
69

    
70
        HttpStatus statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
71
        if (status != null) {
72
            statusCode = HttpStatus.valueOf(status);
73
        }
74

    
75
        ExceptionResponse response = new ExceptionResponse();
76
        response.setErrorCode((String)body.get("error"));
77
        response.setErrorMessage((String)body.get("exception"));
78
        response.setErrors((String)body.get("message"));
79
        response.setStatus(statusCode);
80
//        log.error((String)body.get("exception")+" : "+ (String)body.get("message"));
81
        return new ResponseEntity<ExceptionResponse>(response, statusCode);
82

    
83
//        return body;
84
    }
85

    
86
    private boolean getTraceParameter(HttpServletRequest request) {
87
        String parameter = request.getParameter("trace");
88
        if (parameter == null) {
89
            return false;
90
        }
91
        return !"false".equals(parameter.toLowerCase());
92
    }
93

    
94
    private Map<String, Object> getErrorAttributes(HttpServletRequest aRequest, boolean includeStackTrace) {
95
        RequestAttributes requestAttributes = new ServletRequestAttributes(aRequest);
96
        return errorAttributes.getErrorAttributes(requestAttributes, includeStackTrace);
97
    }
98
}
(2-2/4)