Fix formatting in the dashboard
[nonrtric.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 import springfox.documentation.annotations.ApiIgnore;
39
40 /**
41  * Provides a controller which is invoked on any error within the Spring-managed
42  * context, including page not found, and redirects the caller to a custom error
43  * page. The caller is also redirected to this page if a REST controller takes
44  * an uncaught exception.
45  *
46  * If trace is requested via request parameter ("?trace=true") and available,
47  * adds stack trace information to the standard JSON error response.
48  *
49  * Excluded from Swagger API documentation.
50  *
51  * https://stackoverflow.com/questions/25356781/spring-boot-remove-whitelabel-error-page
52  * https://www.baeldung.com/spring-boot-custom-error-page
53  */
54
55 @ApiIgnore
56 @Controller
57 @RequestMapping(value = SimpleErrorController.ERROR_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
58 public class SimpleErrorController implements ErrorController {
59
60     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
61
62     public static final String ERROR_PATH = "/error";
63
64     private final ErrorAttributes errorAttributes;
65
66     @Autowired
67     public SimpleErrorController(ErrorAttributes errorAttributes) {
68         this.errorAttributes = errorAttributes;
69     }
70
71     @Override
72     public String getErrorPath() {
73         logger.warn("getErrorPath");
74         return ERROR_PATH;
75     }
76
77     @GetMapping
78     public String handleError(HttpServletRequest request) {
79         ServletWebRequest servletWebRequest = new ServletWebRequest(request);
80         Throwable t = errorAttributes.getError(servletWebRequest);
81         if (t != null)
82             logger.warn("handleError", t);
83         Map<String, Object> attributes = errorAttributes.getErrorAttributes(servletWebRequest, true);
84         attributes.forEach((attribute, value) -> {
85             logger.warn("handleError: {} -> {}", attribute, value);
86         });
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 }