2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 AT&T Intellectual Property and Nokia
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
20 package org.oransc.ric.portal.dashboard.controller;
22 import java.lang.invoke.MethodHandles;
25 import javax.servlet.http.HttpServletRequest;
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.GetMapping;
34 import org.springframework.web.bind.annotation.RestController;
35 import org.springframework.web.context.request.ServletWebRequest;
36 import org.springframework.web.context.request.WebRequest;
38 import springfox.documentation.annotations.ApiIgnore;
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.
45 * If trace is requested via request parameter ("?trace=true") and available,
46 * adds stack trace information to the standard JSON error response.
48 * Excluded from Swagger API documentation.
50 * https://stackoverflow.com/questions/25356781/spring-boot-remove-whitelabel-error-page
51 * https://www.baeldung.com/spring-boot-custom-error-page
55 public class SimpleErrorController implements ErrorController {
57 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
59 public static final String ERROR_PATH = "/error";
60 private static final String TRACE = "trace";
61 private final ErrorAttributes errorAttributes;
66 * @param errorAttributes
67 * error attributes must not be null
70 public SimpleErrorController(ErrorAttributes errorAttributes) {
71 Assert.notNull(errorAttributes, "ErrorAttributes must not be null");
72 this.errorAttributes = errorAttributes;
76 public String getErrorPath() {
81 * Builds a map with error details
85 * @return Map of String to Object
87 @GetMapping(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")));
96 private boolean getTraceParameter(HttpServletRequest request) {
97 String parameter = request.getParameter(TRACE);
98 if (parameter == null)
100 return !"false".equalsIgnoreCase(parameter);
103 private Map<String, Object> getErrorAttributes(HttpServletRequest aRequest, boolean includeStackTrace) {
104 WebRequest webRequest = new ServletWebRequest(aRequest);
105 return errorAttributes.getErrorAttributes(webRequest, includeStackTrace);