859c9134bf29155e40d44e88a93c68ff3af0addd
[portal/ric-dashboard.git] / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / controller / SimpleErrorController.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property and Nokia
6  * %%
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20 package org.oransc.ric.portal.dashboard.controller;
21
22 import java.lang.invoke.MethodHandles;
23 import java.util.Map;
24
25 import javax.servlet.http.HttpServletRequest;
26
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.boot.web.servlet.error.ErrorAttributes;
31 import org.springframework.boot.web.servlet.error.ErrorController;
32 import org.springframework.util.Assert;
33 import org.springframework.web.bind.annotation.RequestMapping;
34 import org.springframework.web.bind.annotation.RestController;
35 import org.springframework.web.context.request.ServletWebRequest;
36 import org.springframework.web.context.request.WebRequest;
37
38 import springfox.documentation.annotations.ApiIgnore;
39
40 /**
41  * Provides an endpoint that returns JSON, which is invoked following any error
42  * within the Spring-managed context. This is NOT called for errors outside the
43  * context; e.g., resource not found.
44  * 
45  * If trace is requested via request parameter ("?trace=true") and available,
46  * adds stack trace information to the standard JSON error response.
47  * 
48  * Excluded from Swagger API documentation.
49  * 
50  * https://stackoverflow.com/questions/25356781/spring-boot-remove-whitelabel-error-page
51  * https://www.baeldung.com/spring-boot-custom-error-page
52  */
53 @ApiIgnore
54 @RestController
55 public class SimpleErrorController implements ErrorController {
56
57         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
58
59         private static final String ERROR_PATH = "/error";
60         private static final String TRACE = "trace";
61         private final ErrorAttributes errorAttributes;
62
63         /**
64          * Constructor
65          * 
66          * @param errorAttributes
67          *                            error attributes must not be null
68          */
69         @Autowired
70         public SimpleErrorController(ErrorAttributes errorAttributes) {
71                 Assert.notNull(errorAttributes, "ErrorAttributes must not be null");
72                 this.errorAttributes = errorAttributes;
73         }
74
75         @Override
76         public String getErrorPath() {
77                 return ERROR_PATH;
78         }
79
80         /**
81          * Builds a map with error details
82          * 
83          * @param aRequest
84          *                     HttpServletRequest
85          * @return Map of String to Object
86          */
87         @RequestMapping(ERROR_PATH)
88         public Map<String, Object> error(HttpServletRequest aRequest) {
89                 Map<String, Object> body = getErrorAttributes(aRequest, getTraceParameter(aRequest));
90                 logger.warn("Failed in request for {}", body.get("path"));
91                 body.put("decorated-by", SimpleErrorController.class.getName());
92                 body.computeIfPresent(TRACE, (key, value) -> body.put(TRACE, ((String) value).split("\n\t")));
93                 return body;
94         }
95
96         private boolean getTraceParameter(HttpServletRequest request) {
97                 String parameter = request.getParameter(TRACE);
98                 if (parameter == null)
99                         return false;
100                 return !"false".equalsIgnoreCase(parameter);
101         }
102
103         private Map<String, Object> getErrorAttributes(HttpServletRequest aRequest, boolean includeStackTrace) {
104                 WebRequest webRequest = new ServletWebRequest(aRequest);
105                 return errorAttributes.getErrorAttributes(webRequest, includeStackTrace);
106         }
107
108 }