comparison src/nabble/view/lib/NabbleErrorHandler.java @ 0:7ecd1a4ef557

add content
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 21 Mar 2019 19:15:52 -0600
parents
children 18cf4872fd7f
comparison
equal deleted inserted replaced
-1:000000000000 0:7ecd1a4ef557
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;
77 User user = Jtp.getUser(request, response);
78 isAdmin = user != null && (site.getRootNode().getOwner().equals(user) || Permissions.isInGroup(user, "Administrators"));
79 }
80
81 out.print( "\n<html>\n <head>\n " );
82 writeErrorPageHead(request,writer,code,message);
83 out.print( "\n <META NAME=\"robots\" CONTENT=\"noindex,nofollow\">\n <link rel=\"stylesheet\" href=\"" );
84 out.print( (Shared.getCssPath()) );
85 out.print( "\" type=\"text/css\" />\n <style type=\"text/css\">\n p { margin: .3em 0 0; }\n .error-details {\n border: 1px solid gray;\n padding: .5em;\n width:90%;\n margin-left:2.5%;\n position: relative;\n overflow-x:scroll;\n }\n </style>\n </head>\n <body>\n " );
86
87 String uri = request.getRequestURI();
88 if (uri!=null) {
89 uri = HtmlUtils.htmlEncode(uri);
90 }
91
92 out.print( "\n<div class=\"nabble\" id=\"nabble\">\n <div class=\"top-bar\">\n <div class=\"breadcrumbs\" style=\"float:left;\">\n " );
93 out.print( (homeLink) );
94 out.print( "\n </div>\n </div>\n " );
95
96 if (throwable != null) {
97 if( throwable instanceof ServletException ) {
98 if( MinorServletException.isIn(throwable) ) {
99 logger.info(ServletUtils.getCurrentURL(request), throwable);
100 } else {
101 logger.warn(ServletUtils.getCurrentURL(request), throwable);
102 }
103 } else {
104 logger.error(ServletUtils.getCurrentURL(request), throwable);
105 }
106
107 out.print( "\n<h1>Oops... An error has occurred</h1>\n\nPlease contact <a href=\"" );
108 out.print( (Jtp.supportUrl()) );
109 out.print( "\" target=\"_new\">Nabble Support</a> and explain what you did to cause this error.\nYour feedback is very important to us.\n\n" );
110 if (hasTweaks) {
111 out.print( "\n " );
112 if (isAdmin) {
113 out.print( "\n <div class=\"info-message rounded\" style=\"margin-top:1em;padding:.5em .3em\">\n <h3 style=\"margin-top:0;padding-top:0\">Modified NAML Code</h3>\n " );
114 out.print( (site.getRootNode().getSubjectHtml()) );
115 out.print( " has modified NAML code and this could be the cause of the error below.\n Please take a careful look at your changes and try to make sure your code is not broken.<br/>\n </div>\n " );
116 }
117 out.print( "\n <div style=\"margin:1.2em 0;font-weight:bold\">\n <img src=\"/images/tool.png\" width=\"16\" height=\"17\" style=\"vertical-align:-25%\"/>\n <a href=\"/template/NamlEditor.jtp\">Go to NAML Editor</a>\n </div>\n" );
118 }
119 out.print( "\n\n<h2 style=\"margin-top:1em\">More Details</h2>\n<div class=\"error-details light-bg-color\">\n <h3>Error " );
120 out.print( (code) );
121 out.print( "</h3>\n <pre>" );
122 out.print( (HtmlUtils.htmlEncode(message)) );
123 out.print( "</pre>\n " );
124 if (throwable.getCause() != null) {
125 String msg = throwable.getCause().getMessage();
126 if( msg != null ) {
127 int posBreak = msg.indexOf("\n\t");
128 msg = posBreak > 0? msg.substring(0, posBreak) : msg;
129 msg = HtmlUtils.htmlEncode(msg);
130 msg = msg.replaceAll("\n", "<br/>").replaceAll("\t","&nbsp;&nbsp;&nbsp;&nbsp;");
131
132 out.print( "\n<p><b>Message</b>: " );
133 out.print( (msg) );
134 out.print( "</p>\n" );
135
136 }
137 }
138 out.print( "\n<p><b>RequestURI</b>: " );
139 out.print( (uri) );
140 out.print( "</p>\n<p><b>Server</b>: " );
141 out.print( (Jtp.getDefaultHost()) );
142 out.print( "</p>\n" );
143
144
145 if (showStacks) {
146 StringWriter sw = new StringWriter();
147 PrintWriter pw = new PrintWriter(sw);
148 throwable.printStackTrace(pw);
149 pw.flush();
150
151 out.print( "<h3 style=\"margin-top:1.2em\">Caused by:</h3><pre>" );
152 out.print( (HtmlUtils.htmlEncode(sw.toString())) );
153 out.print( "</pre>" );
154
155 }
156
157 out.print( "\n</div>\n" );
158
159 } else {
160
161 out.print( "\n<h2 style=\"padding:0;margin:1em 0\">" );
162 out.print( (message) );
163 out.print( "</h2>\n\nPlease contact <a href=\"" );
164 out.print( (Jtp.supportUrl()) );
165 out.print( "\" target=\"_new\">Nabble Support</a> if you need help.\n" );
166
167 }
168 {
169 StringBuffer url = request.getRequestURL();
170 String query = request.getQueryString();
171 if( query != null )
172 url.append( '?' ).append( query );
173
174 out.print( "\n<p>URL = <a href=\"" );
175 out.print( (url) );
176 out.print( "\">" );
177 out.print( (url) );
178 out.print( "</a></p>\n" );
179
180 }
181
182 out.print( "\n<table class=\"footer-table shaded-bg-color\">\n <tr>\n <td class=\"footer-left\">\n Powered by <a href=\"" );
183 out.print( (Jtp.homePage()) );
184 out.print( "\" target=\"_top\" title=\"Free forum and other embeddable web apps\">Nabble</a>\n </td>\n <td class=\"footer-right\"></td>\n </tr>\n</table>\n</div>\n</body>\n</html>\n" );
185
186 }
187
188 private void serviceUnavailablePage(Writer writer) {
189 PrintWriter out = new PrintWriter(writer);
190
191 out.print( "\n<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n<html>\n <head>\n <title>Service Unavailable</title>\n <META NAME=\"robots\" CONTENT=\"noindex,nofollow\">\n </head>\n <body style=\"text-align:center;font-family: Verdana,sans-serif;font-size:.84em;padding-top:2em\">\n <img src=\"https://www.nabble.com/assets/images/logo_nabble_home.png\"/>\n <h1 style=\"margin-top:1.5em\">This Nabble server is over capacity.</h1>\n <div style=\"margin:0 15% 2em;text-align:center;background:#eee;padding:1em 0\">\n Too many requests... Please wait a moment and try again.\n </div>\n\n Powered by <a href=\"https://www.nabble.com/\">Nabble</a>\n </div>\n </body>\n</html>\n" );
192
193 }
194
195 private void databaseUpdatePage(Writer writer) {
196 PrintWriter out = new PrintWriter(writer);
197
198 out.print( "\n<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n<html>\n <head>\n <title>Updating Database</title>\n <META NAME=\"robots\" CONTENT=\"noindex,nofollow\">\n </head>\n <body style=\"text-align:center;font-family: Verdana,sans-serif;font-size:.84em;padding-top:2em\">\n <h1 style=\"margin-top:3em\">Updating Database</h1>\n <div style=\"margin:0 25% 2em;text-align:center;background:#eee;padding:1em 0\">\n This database is being updated. Please try again in a few seconds.\n </div>\n <h2>Reloading in <span id=\"sec\">60</span> seconds</h2>\n <script type=\"text/javascript\">\n var seconds = 60;\n function countDown() {\n if (seconds == 0) {\n window.location.reload();\n return;\n }\n document.getElementById('sec').innerHTML = seconds;\n seconds--;\n setTimeout(countDown, 1000);\n };\n countDown();\n </script>\n\n Powered by <a href=\"https://www.nabble.com/\">Nabble</a>\n </div>\n </body>\n</html>\n" );
199
200 }
201 }
202