da2a90017271eb2268f794099979b979921b3e02
[portal/nonrtric-controlpanel.git] / webapp-backend / src / main / java / org / oransc / portal / nonrtric / controlpanel / controller / SimpleErrorController.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property
6  * Modifications Copyright (C) 2020 Nordix Foundation
7  * %%
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ========================LICENSE_END===================================
20  */
21
22 package org.oransc.portal.nonrtric.controlpanel.controller;
23
24 import java.lang.invoke.MethodHandles;
25 import java.util.Map;
26
27 import javax.servlet.http.HttpServletRequest;
28
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.boot.web.servlet.error.ErrorAttributes;
33 import org.springframework.boot.web.servlet.error.ErrorController;
34 import org.springframework.http.MediaType;
35 import org.springframework.stereotype.Controller;
36 import org.springframework.web.bind.annotation.GetMapping;
37 import org.springframework.web.bind.annotation.RequestMapping;
38 import org.springframework.web.context.request.ServletWebRequest;
39 import springfox.documentation.annotations.ApiIgnore;
40
41 /**
42  * Provides a controller which is invoked on any error within the Spring-managed
43  * context, including page not found, and redirects the caller to a custom error
44  * page. The caller is also redirected to this page if a REST controller takes
45  * an uncaught exception.
46  *
47  * If trace is requested via request parameter ("?trace=true") and available,
48  * adds stack trace information to the standard JSON error response.
49  *
50  * Excluded from Swagger API documentation.
51  *
52  * https://stackoverflow.com/questions/25356781/spring-boot-remove-whitelabel-error-page
53  * https://www.baeldung.com/spring-boot-custom-error-page
54  */
55
56 @ApiIgnore
57 @Controller
58 @RequestMapping(value = SimpleErrorController.ERROR_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
59 public class SimpleErrorController implements ErrorController {
60
61     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
62
63     @SuppressWarnings("squid:S1075") //URIs should not be hardcoded
64     public static final String ERROR_PATH = "/error";
65
66     private final ErrorAttributes errorAttributes;
67
68     @Autowired
69     public SimpleErrorController(ErrorAttributes errorAttributes) {
70         this.errorAttributes = errorAttributes;
71     }
72
73     @Override
74     public String getErrorPath() {
75         logger.warn("getErrorPath");
76         return ERROR_PATH;
77     }
78
79     @GetMapping
80     public String handleError(HttpServletRequest request) {
81         ServletWebRequest servletWebRequest = new ServletWebRequest(request);
82         Throwable t = errorAttributes.getError(servletWebRequest);
83         if (t != null)
84             logger.warn("handleError", t);
85         Map<String, Object> attributes = errorAttributes.getErrorAttributes(servletWebRequest, true);
86         attributes.forEach((attribute, value) -> logger.warn("handleError: {} -> {}", attribute, value));
87         // Return the name of the page INCLUDING suffix, which I guess is a "view" name.
88         // Just "error" is not enough, but don't seem to need a ModelAndView object.
89         return "error.html";
90     }
91
92 }