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;
24 import javax.servlet.http.HttpServletRequest;
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;
35 import springfox.documentation.annotations.ApiIgnore;
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.
43 * Excluded from Swagger API documentation.
45 * https://stackoverflow.com/questions/25356781/spring-boot-remove-whitelabel-error-page
46 * https://www.baeldung.com/spring-boot-custom-error-page
50 public class SimpleErrorController implements ErrorController {
52 private static final String ERROR_PATH = "/error";
53 private static final String TRACE = "trace";
54 private final ErrorAttributes errorAttributes;
59 * @param errorAttributes
60 * error attributes must not be null
63 public SimpleErrorController(ErrorAttributes errorAttributes) {
64 Assert.notNull(errorAttributes, "ErrorAttributes must not be null");
65 this.errorAttributes = errorAttributes;
69 public String getErrorPath() {
74 * Builds a map with error details
78 * @return Map of String to Object
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")));
88 private boolean getTraceParameter(HttpServletRequest request) {
89 String parameter = request.getParameter(TRACE);
90 if (parameter == null)
92 return !"false".equalsIgnoreCase(parameter);
95 private Map<String, Object> getErrorAttributes(HttpServletRequest aRequest, boolean includeStackTrace) {
96 WebRequest webRequest = new ServletWebRequest(aRequest);
97 return errorAttributes.getErrorAttributes(webRequest, includeStackTrace);