Mercurial Hosting > nabble
annotate src/nabble/view/lib/NabbleErrorHandler.jtp @ 62:4674ed7d56df default tip
remove n2
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sat, 30 Sep 2023 20:25:29 -0600 |
parents | 18cf4872fd7f |
children |
rev | line source |
---|---|
0 | 1 <% |
2 package nabble.view.lib; | |
3 | |
4 import fschmidt.util.java.HtmlUtils; | |
5 import fschmidt.util.servlet.ServletUtils; | |
6 import nabble.model.Init; | |
7 import nabble.model.Site; | |
8 import nabble.model.UpdatingException; | |
9 import nabble.model.User; | |
10 import org.eclipse.jetty.http.HttpGenerator; | |
11 import org.eclipse.jetty.server.Request; | |
12 import org.slf4j.Logger; | |
13 import org.slf4j.LoggerFactory; | |
14 | |
15 import javax.servlet.ServletException; | |
16 import javax.servlet.http.HttpServletRequest; | |
17 import javax.servlet.http.HttpServletResponse; | |
18 import java.io.IOException; | |
19 import java.io.PrintWriter; | |
20 import java.io.StringWriter; | |
21 import java.io.Writer; | |
22 | |
23 | |
24 public final class NabbleErrorHandler extends org.eclipse.jetty.server.handler.ErrorHandler { | |
25 | |
26 private static final Logger logger = LoggerFactory.getLogger(NabbleErrorHandler.class); | |
27 private HttpServletResponse response; | |
28 | |
29 public void handle(String string, Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException { | |
30 this.response = httpServletResponse; | |
31 super.handle(string, request, httpServletRequest, httpServletResponse); | |
32 } | |
33 | |
34 protected void writeErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks) | |
35 throws IOException | |
36 { | |
37 if (code == HttpServletResponse.SC_SERVICE_UNAVAILABLE) { | |
38 serviceUnavailablePage(writer); | |
39 return; | |
40 } | |
41 if (message == null) | |
42 message = HttpGenerator.getReasonBuffer(code).toString(); | |
43 | |
44 Throwable throwable = (Throwable)request.getAttribute(NabbleErrorFilter.WORK_AROUND_ERROR_EXCEPTION); | |
45 if (UpdatingException.isIn(throwable)) { | |
46 logger.error("Updating database: "+ServletUtils.getCurrentURL(request)); | |
47 databaseUpdatePage(writer); | |
48 return; | |
49 } | |
50 try { | |
51 writeMyErrorPage(request,writer,code,message,showStacks, throwable); | |
52 } catch (ServletException e) { | |
53 logger.error(ServletUtils.getCurrentURL(request), e); | |
54 throw new RuntimeException(e); | |
55 } catch (RuntimeException e) { | |
56 logger.error(ServletUtils.getCurrentURL(request), e); | |
57 throw e; | |
58 } | |
59 } | |
60 | |
61 private void writeMyErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks, Throwable throwable) | |
62 throws IOException, ServletException | |
63 { | |
64 PrintWriter out = new PrintWriter(writer); | |
65 boolean hasTweaks = false; | |
66 boolean isAdmin = false; | |
67 String homeLink; | |
68 Site site = null; | |
69 try { | |
70 site = Jtp.getSite(request); | |
71 } catch(RuntimeException e) {} | |
72 if( site == null ) { | |
73 homeLink = "<a href='" + Jtp.homePage() + "'>Nabble</a>"; | |
74 } else { | |
75 homeLink = Jtp.link(site.getRootNode()); | |
76 hasTweaks = site.getCustomTweaks().size() > 0; | |
19
18cf4872fd7f
remove anonymous posting
Franklin Schmidt <fschmidt@gmail.com>
parents:
0
diff
changeset
|
77 User user = Jtp.getUser(request); |
0 | 78 isAdmin = user != null && (site.getRootNode().getOwner().equals(user) || Permissions.isInGroup(user, "Administrators")); |
79 } | |
80 %> | |
81 <html> | |
82 <head> | |
83 <% writeErrorPageHead(request,writer,code,message); %> | |
84 <META NAME="robots" CONTENT="noindex,nofollow"> | |
85 <link rel="stylesheet" href="<%=Shared.getCssPath()%>" type="text/css" /> | |
86 <style type="text/css"> | |
87 p { margin: .3em 0 0; } | |
88 .error-details { | |
89 border: 1px solid gray; | |
90 padding: .5em; | |
91 width:90%; | |
92 margin-left:2.5%; | |
93 position: relative; | |
94 overflow-x:scroll; | |
95 } | |
96 </style> | |
97 </head> | |
98 <body> | |
99 <% | |
100 String uri = request.getRequestURI(); | |
101 if (uri!=null) { | |
102 uri = HtmlUtils.htmlEncode(uri); | |
103 } | |
104 %> | |
105 <div class="nabble" id="nabble"> | |
106 <div class="top-bar"> | |
107 <div class="breadcrumbs" style="float:left;"> | |
108 <%=homeLink%> | |
109 </div> | |
110 </div> | |
111 <% | |
112 if (throwable != null) { | |
113 if( throwable instanceof ServletException ) { | |
114 if( MinorServletException.isIn(throwable) ) { | |
115 logger.info(ServletUtils.getCurrentURL(request), throwable); | |
116 } else { | |
117 logger.warn(ServletUtils.getCurrentURL(request), throwable); | |
118 } | |
119 } else { | |
120 logger.error(ServletUtils.getCurrentURL(request), throwable); | |
121 } | |
122 %> | |
123 <h1>Oops... An error has occurred</h1> | |
124 | |
125 Please contact <a href="<%=Jtp.supportUrl()%>" target="_new">Nabble Support</a> and explain what you did to cause this error. | |
126 Your feedback is very important to us. | |
127 | |
128 <% if (hasTweaks) { %> | |
129 <% if (isAdmin) { %> | |
130 <div class="info-message rounded" style="margin-top:1em;padding:.5em .3em"> | |
131 <h3 style="margin-top:0;padding-top:0">Modified NAML Code</h3> | |
132 <%=site.getRootNode().getSubjectHtml()%> has modified NAML code and this could be the cause of the error below. | |
133 Please take a careful look at your changes and try to make sure your code is not broken.<br/> | |
134 </div> | |
135 <% } %> | |
136 <div style="margin:1.2em 0;font-weight:bold"> | |
137 <img src="/images/tool.png" width="16" height="17" style="vertical-align:-25%"/> | |
138 <a href="/template/NamlEditor.jtp">Go to NAML Editor</a> | |
139 </div> | |
140 <% } %> | |
141 | |
142 <h2 style="margin-top:1em">More Details</h2> | |
143 <div class="error-details light-bg-color"> | |
144 <h3>Error <%=code%></h3> | |
145 <pre><%=HtmlUtils.htmlEncode(message)%></pre> | |
146 <% if (throwable.getCause() != null) { | |
147 String msg = throwable.getCause().getMessage(); | |
148 if( msg != null ) { | |
149 int posBreak = msg.indexOf("\n\t"); | |
150 msg = posBreak > 0? msg.substring(0, posBreak) : msg; | |
151 msg = HtmlUtils.htmlEncode(msg); | |
152 msg = msg.replaceAll("\n", "<br/>").replaceAll("\t"," "); | |
153 %> | |
154 <p><b>Message</b>: <%=msg%></p> | |
155 <% | |
156 } | |
157 }%> | |
158 <p><b>RequestURI</b>: <%=uri%></p> | |
159 <p><b>Server</b>: <%=Jtp.getDefaultHost()%></p> | |
160 <% | |
161 | |
162 if (showStacks) { | |
163 StringWriter sw = new StringWriter(); | |
164 PrintWriter pw = new PrintWriter(sw); | |
165 throwable.printStackTrace(pw); | |
166 pw.flush(); | |
167 %><h3 style="margin-top:1.2em">Caused by:</h3><pre><%=HtmlUtils.htmlEncode(sw.toString())%></pre><% | |
168 } | |
169 %> | |
170 </div> | |
171 <% | |
172 } else { | |
173 %> | |
174 <h2 style="padding:0;margin:1em 0"><%=message%></h2> | |
175 | |
176 Please contact <a href="<%=Jtp.supportUrl()%>" target="_new">Nabble Support</a> if you need help. | |
177 <% | |
178 } | |
179 { | |
180 StringBuffer url = request.getRequestURL(); | |
181 String query = request.getQueryString(); | |
182 if( query != null ) | |
183 url.append( '?' ).append( query ); | |
184 %> | |
185 <p>URL = <a href="<%=url%>"><%=url%></a></p> | |
186 <% | |
187 } | |
188 %> | |
189 <table class="footer-table shaded-bg-color"> | |
190 <tr> | |
191 <td class="footer-left"> | |
192 Powered by <a href="<%=Jtp.homePage()%>" target="_top" title="Free forum and other embeddable web apps">Nabble</a> | |
193 </td> | |
194 <td class="footer-right"></td> | |
195 </tr> | |
196 </table> | |
197 </div> | |
198 </body> | |
199 </html> | |
200 <% | |
201 } | |
202 | |
203 private void serviceUnavailablePage(Writer writer) { | |
204 PrintWriter out = new PrintWriter(writer); | |
205 %> | |
206 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> | |
207 <html> | |
208 <head> | |
209 <title>Service Unavailable</title> | |
210 <META NAME="robots" CONTENT="noindex,nofollow"> | |
211 </head> | |
212 <body style="text-align:center;font-family: Verdana,sans-serif;font-size:.84em;padding-top:2em"> | |
213 <img src="https://www.nabble.com/assets/images/logo_nabble_home.png"/> | |
214 <h1 style="margin-top:1.5em">This Nabble server is over capacity.</h1> | |
215 <div style="margin:0 15% 2em;text-align:center;background:#eee;padding:1em 0"> | |
216 Too many requests... Please wait a moment and try again. | |
217 </div> | |
218 | |
219 Powered by <a href="https://www.nabble.com/">Nabble</a> | |
220 </div> | |
221 </body> | |
222 </html> | |
223 <% | |
224 } | |
225 | |
226 private void databaseUpdatePage(Writer writer) { | |
227 PrintWriter out = new PrintWriter(writer); | |
228 %> | |
229 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> | |
230 <html> | |
231 <head> | |
232 <title>Updating Database</title> | |
233 <META NAME="robots" CONTENT="noindex,nofollow"> | |
234 </head> | |
235 <body style="text-align:center;font-family: Verdana,sans-serif;font-size:.84em;padding-top:2em"> | |
236 <h1 style="margin-top:3em">Updating Database</h1> | |
237 <div style="margin:0 25% 2em;text-align:center;background:#eee;padding:1em 0"> | |
238 This database is being updated. Please try again in a few seconds. | |
239 </div> | |
240 <h2>Reloading in <span id="sec">60</span> seconds</h2> | |
241 <script type="text/javascript"> | |
242 var seconds = 60; | |
243 function countDown() { | |
244 if (seconds == 0) { | |
245 window.location.reload(); | |
246 return; | |
247 } | |
248 document.getElementById('sec').innerHTML = seconds; | |
249 seconds--; | |
250 setTimeout(countDown, 1000); | |
251 }; | |
252 countDown(); | |
253 </script> | |
254 | |
255 Powered by <a href="https://www.nabble.com/">Nabble</a> | |
256 </div> | |
257 </body> | |
258 </html> | |
259 <% | |
260 } | |
261 } | |
262 %> |