Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. <error-page>
  2. <location>/500</location>
  3. </error-page>
  4.  
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.boot.autoconfigure.web.AbstractErrorController;
  7. import org.springframework.boot.autoconfigure.web.ErrorAttributes;
  8. import org.springframework.http.ResponseEntity;
  9. import org.springframework.stereotype.Controller;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11.  
  12. import javax.servlet.http.HttpServletRequest;
  13.  
  14. @Controller
  15. public class CustomErrorController extends AbstractErrorController {
  16. public static final String ERROR_500 = "/500";
  17. private static final String ERROR_PATH= "/error";
  18.  
  19. @Autowired
  20. public CustomErrorController(ErrorAttributes errorAttributes) {
  21. super(errorAttributes);
  22. }
  23.  
  24.  
  25. /**
  26. * Responsible for handling all errors and throw especial exceptions
  27. * for some HTTP status codes. Otherwise, it will return a map that
  28. * ultimately will be converted to a json error.
  29. */
  30. @RequestMapping({ERROR_PATH,ERROR_500})
  31. public ResponseEntity<?> handleErrors(HttpServletRequest request) {
  32. return ResponseEntity.status(getStatus(request)).body(getErrorAttributes(request, false));
  33. }
  34.  
  35. @Override
  36. public String getErrorPath() {
  37. return ERROR_PATH;
  38. }
  39. }
  40.  
  41. /**
  42. * Process this request, publishing an event regardless of the outcome.
  43. * <p>The actual event handling is performed by the abstract
  44. * {@link #doService} template method.
  45. */
  46. protected final void processRequest(HttpServletRequest request, HttpServletResponse response)
  47. throws ServletException, IOException {
  48.  
  49. long startTime = System.currentTimeMillis();
  50. Throwable failureCause = null;
  51.  
  52. // Here the locale is determined
  53. LocaleContext previousLocaleContext = LocaleContextHolder.getLocaleContext();
  54. LocaleContext localeContext = buildLocaleContext(request);
  55.  
  56. RequestAttributes previousAttributes = RequestContextHolder.getRequestAttributes();
  57. ServletRequestAttributes requestAttributes = buildRequestAttributes(request, response, previousAttributes);
  58.  
  59. WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
  60. asyncManager.registerCallableInterceptor(FrameworkServlet.class.getName(), new RequestBindingInterceptor());
  61.  
  62. initContextHolders(request, localeContext, requestAttributes);
  63.  
  64. try {
  65. // here is where the WebMvc processing happens
  66. doService(request, response);
  67. }
  68. catch (ServletException ex) {
  69. failureCause = ex;
  70. throw ex;
  71. }
  72. catch (IOException ex) {
  73. failureCause = ex;
  74. throw ex;
  75. }
  76. catch (Throwable ex) {
  77. failureCause = ex;
  78. throw new NestedServletException("Request processing failed", ex);
  79. }
  80.  
  81. finally {
  82. resetContextHolders(request, previousLocaleContext, previousAttributes);
  83. if (requestAttributes != null) {
  84. requestAttributes.requestCompleted();
  85. }
  86.  
  87. if (logger.isDebugEnabled()) {
  88. if (failureCause != null) {
  89. this.logger.debug("Could not complete request", failureCause);
  90. }
  91. else {
  92. if (asyncManager.isConcurrentHandlingStarted()) {
  93. logger.debug("Leaving response open for concurrent processing");
  94. }
  95. else {
  96. this.logger.debug("Successfully completed request");
  97. }
  98. }
  99. }
  100.  
  101. publishRequestHandledEvent(request, response, startTime, failureCause);
  102. }
  103. }
  104.  
  105. /**
  106. * Build a LocaleContext for the given request, exposing the request's primary locale as current locale.
  107. * <p>The default implementation uses the dispatcher's LocaleResolver to obtain the current locale,
  108. * which might change during a request.
  109. * @param request current HTTP request
  110. * @return the corresponding LocaleContext
  111. */
  112. @Override
  113. protected LocaleContext buildLocaleContext(final HttpServletRequest request) {
  114. if (this.localeResolver instanceof LocaleContextResolver) {
  115. return ((LocaleContextResolver) this.localeResolver).resolveLocaleContext(request);
  116. }
  117. else {
  118. return new LocaleContext() {
  119. @Override
  120. public Locale getLocale() {
  121. return localeResolver.resolveLocale(request);
  122. }
  123. };
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement