334a88cb954e7276eca0fa2ee3cf86668b896e64
[portal/ric-dashboard.git] / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / controller / SimpleErrorController.java
1 /*-
2  * ========================LICENSE_START=================================
3  * ORAN-OSC
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.util.Map;
23
24 import javax.servlet.http.HttpServletRequest;
25
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.boot.web.servlet.error.ErrorAttributes;
28 import org.springframework.boot.web.servlet.error.ErrorController;
29 import org.springframework.util.Assert;
30 import org.springframework.web.bind.annotation.RequestMapping;
31 import org.springframework.web.bind.annotation.RestController;
32 import org.springframework.web.context.request.ServletWebRequest;
33 import org.springframework.web.context.request.WebRequest;
34
35 import springfox.documentation.annotations.ApiIgnore;
36
37 /**
38  * Returns JSON on error within the Spring-managed context. Does not fire for
39  * anything else; e.g., resource not found outside the context. If trace is
40  * requested via request parameter ("?trace=true") and available, adds stack
41  * trace information to the standard JSON error response.
42  * 
43  * Excluded from Swagger API documentation.
44  * 
45  * https://stackoverflow.com/questions/25356781/spring-boot-remove-whitelabel-error-page
46  * https://www.baeldung.com/spring-boot-custom-error-page
47  */
48 @ApiIgnore
49 @RestController
50 public class SimpleErrorController implements ErrorController {
51
52         private static final String ERROR_PATH = "/error";
53         private static final String TRACE = "trace";
54         private final ErrorAttributes errorAttributes;
55
56         /**
57          * Constructor
58          * 
59          * @param errorAttributes
60          *                            error attributes must not be null
61          */
62         @Autowired
63         public SimpleErrorController(ErrorAttributes errorAttributes) {
64                 Assert.notNull(errorAttributes, "ErrorAttributes must not be null");
65                 this.errorAttributes = errorAttributes;
66         }
67
68         @Override
69         public String getErrorPath() {
70                 return ERROR_PATH;
71         }
72
73         /**
74          * Builds a map with error details
75          * 
76          * @param aRequest
77          *                     HttpServletRequest
78          * @return Map of String to Object
79          */
80         @RequestMapping(ERROR_PATH)
81         public Map<String, Object> error(HttpServletRequest aRequest) {
82                 Map<String, Object> body = getErrorAttributes(aRequest, getTraceParameter(aRequest));
83                 body.put("decorated-by", SimpleErrorController.class.getName());
84                 body.computeIfPresent(TRACE, (key, value) -> body.put(TRACE, ((String) value).split("\n\t")));
85                 return body;
86         }
87
88         private boolean getTraceParameter(HttpServletRequest request) {
89                 String parameter = request.getParameter(TRACE);
90                 if (parameter == null)
91                         return false;
92                 return !"false".equalsIgnoreCase(parameter);
93         }
94
95         private Map<String, Object> getErrorAttributes(HttpServletRequest aRequest, boolean includeStackTrace) {
96                 WebRequest webRequest = new ServletWebRequest(aRequest);
97                 return errorAttributes.getErrorAttributes(webRequest, includeStackTrace);
98         }
99
100 }