Add multi-layer RIC instance selector
[portal/ric-dashboard.git] / dashboard / 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
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
21 package org.oransc.ric.portal.dashboard.controller;
22
23 import java.lang.invoke.MethodHandles;
24 import java.util.Map;
25
26 import javax.servlet.http.HttpServletRequest;
27
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.boot.web.servlet.error.ErrorAttributes;
32 import org.springframework.boot.web.servlet.error.ErrorController;
33 import org.springframework.http.MediaType;
34 import org.springframework.stereotype.Controller;
35 import org.springframework.web.bind.annotation.GetMapping;
36 import org.springframework.web.bind.annotation.RequestMapping;
37 import org.springframework.web.context.request.ServletWebRequest;
38
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         public static final String ERROR_PATH = "/error";
64
65         private final ErrorAttributes errorAttributes;
66
67         @Autowired
68         public SimpleErrorController(ErrorAttributes errorAttributes) {
69                 this.errorAttributes = errorAttributes;
70         }
71
72         @Override
73         public String getErrorPath() {
74                 logger.warn("getErrorPath");
75                 return ERROR_PATH;
76         }
77
78         @GetMapping
79         public String handleError(HttpServletRequest request) {
80                 ServletWebRequest servletWebRequest = new ServletWebRequest(request);
81                 Throwable t = errorAttributes.getError(servletWebRequest);
82                 if (t != null)
83                         logger.warn("handleError", t);
84                 Map<String, Object> attributes = errorAttributes.getErrorAttributes(servletWebRequest, true);
85                 // use compact lambda syntax to silence Sonar complaint
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 }