Project

General

Profile

1
package eu.dnetlib.uoaadmintoolslibrary.controllers;
2
import org.springframework.beans.factory.annotation.Autowired;
3
import org.springframework.boot.autoconfigure.web.ErrorAttributes;
4
import org.springframework.boot.autoconfigure.web.ErrorController;
5
import org.springframework.util.Assert;
6
import org.springframework.web.bind.annotation.CrossOrigin;
7
import org.springframework.web.bind.annotation.RequestMapping;
8
import org.springframework.web.bind.annotation.RestController;
9
import org.springframework.web.context.request.RequestAttributes;
10
import org.springframework.web.context.request.ServletRequestAttributes;
11

    
12
import javax.servlet.http.HttpServletRequest;
13
import java.util.Map;
14

    
15
/**
16
 * Created by argirok on 8/3/2018.
17
 */
18

    
19
@RestController
20
@CrossOrigin(origins = "*")
21
@RequestMapping("/error")
22
public class SimpleErrorController implements ErrorController {
23

    
24
    private final ErrorAttributes errorAttributes;
25

    
26
    @Autowired
27
    public SimpleErrorController(ErrorAttributes errorAttributes) {
28
        Assert.notNull(errorAttributes, "ErrorAttributes must not be null");
29
        this.errorAttributes = errorAttributes;
30
    }
31

    
32
    @Override
33
    public String getErrorPath() {
34
        return "/error";
35
    }
36

    
37
    @RequestMapping
38
    public Map<String, Object> error(HttpServletRequest aRequest){
39
        Map<String, Object> body = getErrorAttributes(aRequest,getTraceParameter(aRequest));
40
        String trace = (String) body.get("trace");
41
        if(trace != null){
42
            String[] lines = trace.split("\n\t");
43
            body.put("trace", lines);
44
        }
45
        return body;
46
    }
47

    
48
    private boolean getTraceParameter(HttpServletRequest request) {
49
        String parameter = request.getParameter("trace");
50
        if (parameter == null) {
51
            return false;
52
        }
53
        return !"false".equals(parameter.toLowerCase());
54
    }
55

    
56
    private Map<String, Object> getErrorAttributes(HttpServletRequest aRequest, boolean includeStackTrace) {
57
        RequestAttributes requestAttributes = new ServletRequestAttributes(aRequest);
58
        return errorAttributes.getErrorAttributes(requestAttributes, includeStackTrace);
59
    }
60
}
(9-9/9)