Package: InitializationDiagnosticsDispatcherServletDecorator
InitializationDiagnosticsDispatcherServletDecorator
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
InitializationDiagnosticsDispatcherServletDecorator() |
|
|
|
|
|
||||||||||||||||||||
findUpperCauseWithMessage(Throwable) |
|
|
|
|
|
||||||||||||||||||||
init(ServletConfig) |
|
|
|
|
|
||||||||||||||||||||
sendProcessingError(Throwable, HttpServletResponse) |
|
|
|
|
|
||||||||||||||||||||
service(HttpServletRequest, HttpServletResponse) |
|
|
|
|
|
Coverage
1: /*
2: * #%L
3: * *********************************************************************************************************************
4: *
5: * NorthernWind - lightweight CMS
6: * http://northernwind.tidalwave.it - git clone https://bitbucket.org/tidalwave/northernwind-src.git
7: * %%
8: * Copyright (C) 2011 - 2023 Tidalwave s.a.s. (http://tidalwave.it)
9: * %%
10: * *********************************************************************************************************************
11: *
12: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
13: * the License. You may obtain a copy of the License at
14: *
15: * http://www.apache.org/licenses/LICENSE-2.0
16: *
17: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
18: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
19: * specific language governing permissions and limitations under the License.
20: *
21: * *********************************************************************************************************************
22: *
23: *
24: * *********************************************************************************************************************
25: * #L%
26: */
27: package it.tidalwave.northernwind.frontend.util;
28:
29: import javax.annotation.Nonnull;
30: import javax.annotation.Nullable;
31: import java.io.IOException;
32: import java.io.PrintStream;
33: import java.io.PrintWriter;
34: import javax.servlet.ServletConfig;
35: import javax.servlet.ServletException;
36: import javax.servlet.http.HttpServlet;
37: import javax.servlet.http.HttpServletRequest;
38: import javax.servlet.http.HttpServletResponse;
39: import org.springframework.web.servlet.DispatcherServlet;
40: import static java.nio.charset.StandardCharsets.UTF_8;
41: import static it.tidalwave.northernwind.frontend.util.NorthernWindContextLoaderListener.ATTRIBUTE_BOOT_THROWABLE;
42:
43: /***********************************************************************************************************************
44: *
45: * A decorator for a {@link HttpServlet} that returns an error diagnostic page when there are problems during the boot.
46: *
47: * A simple {@code Filter} wouldn't accomplish the job, since we need to prevent the delegate servlet from initializing
48: * in case of error.
49: *
50: * @author Fabrizio Giudici
51: *
52: **********************************************************************************************************************/
53: public class InitializationDiagnosticsDispatcherServletDecorator extends HttpServlet
54: {
55: private final DispatcherServlet delegate = new DispatcherServlet();
56:
57: @Nullable
58: private Throwable bootThrowable;
59:
60: /*******************************************************************************************************************
61: *
62: * {@inheritDoc}
63: *
64: ******************************************************************************************************************/
65: @Override
66: public void init (@Nonnull final ServletConfig config)
67: throws ServletException
68: {
69: super.init(config);
70:
71: bootThrowable = (Throwable)getServletContext().getAttribute(ATTRIBUTE_BOOT_THROWABLE);
72:
73:• if (bootThrowable == null)
74: {
75: delegate.init(config);
76: }
77: }
78:
79: /*******************************************************************************************************************
80: *
81: * {@inheritDoc}
82: *
83: ******************************************************************************************************************/
84: @Override
85: protected void service (@Nonnull final HttpServletRequest request, @Nonnull final HttpServletResponse response)
86: throws ServletException, IOException
87: {
88:• if (bootThrowable == null)
89: {
90: delegate.service(request, response);
91: }
92: else
93: {
94: sendProcessingError(findUpperCauseWithMessage(bootThrowable), response);
95: }
96: }
97:
98: /*******************************************************************************************************************
99: *
100: *
101: ******************************************************************************************************************/
102: @Nonnull
103: private static Throwable findUpperCauseWithMessage (@Nonnull final Throwable throwable)
104: {
105: var cause = throwable;
106:
107:• for (var parent = cause.getCause(); parent != null; parent = parent.getCause())
108: {
109: final var message = parent.getMessage();
110:
111:• if ((message != null) && !"".equals(message.trim()))
112: {
113: cause = parent;
114: }
115: }
116:
117: return cause;
118: }
119:
120: /*******************************************************************************************************************
121: *
122: *
123: ******************************************************************************************************************/
124: private static void sendProcessingError (@Nonnull final Throwable t, @Nonnull final HttpServletResponse response)
125: throws IOException
126: {
127: response.setStatus(500);
128: response.setContentType("text/html");
129: final var pw = new PrintWriter(new PrintStream(response.getOutputStream(), true, UTF_8));
130: pw.print("<html>\n<head>\n<title>Configuration Error</title>\n</head>\n<body>\n");
131: pw.print("<h1>Configuration Error</h1>\n<pre>\n");
132: pw.print(t);
133: // t.printStackTrace(pw);
134: pw.print("</pre>\n");
135: pw.print("<h2>Boot log</h2>\n<pre>\n");
136: pw.print(BootLogger.getLogContent());
137: pw.print("</pre></body>\n</html>");
138: pw.close();
139: response.getOutputStream().close();
140: }
141: }