view src/nabble/view/lib/NabbleErrorHandler.java @ 19:18cf4872fd7f

remove anonymous posting
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 29 May 2020 22:58:25 -0600
parents 7ecd1a4ef557
children
line wrap: on
line source


package nabble.view.lib;

import fschmidt.util.java.HtmlUtils;
import fschmidt.util.servlet.ServletUtils;
import nabble.model.Init;
import nabble.model.Site;
import nabble.model.UpdatingException;
import nabble.model.User;
import org.eclipse.jetty.http.HttpGenerator;
import org.eclipse.jetty.server.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;


public final class NabbleErrorHandler extends org.eclipse.jetty.server.handler.ErrorHandler {

	private static final Logger logger = LoggerFactory.getLogger(NabbleErrorHandler.class);
	private HttpServletResponse response;

	public void handle(String string, Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException {
		this.response = httpServletResponse;
		super.handle(string, request, httpServletRequest, httpServletResponse);
	}

	protected void writeErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks)
        throws IOException
    {
		if (code == HttpServletResponse.SC_SERVICE_UNAVAILABLE) {
			serviceUnavailablePage(writer);
			return;
		}
		if (message == null)
			message = HttpGenerator.getReasonBuffer(code).toString();

		Throwable throwable = (Throwable)request.getAttribute(NabbleErrorFilter.WORK_AROUND_ERROR_EXCEPTION);
		if (UpdatingException.isIn(throwable)) {
			logger.error("Updating database: "+ServletUtils.getCurrentURL(request));
			databaseUpdatePage(writer);
			return;
		}
		try {
			writeMyErrorPage(request,writer,code,message,showStacks, throwable);
		} catch (ServletException e) {
			logger.error(ServletUtils.getCurrentURL(request), e);
			throw new RuntimeException(e);
		} catch (RuntimeException e) {
			logger.error(ServletUtils.getCurrentURL(request), e);
			throw e;
		}
	}

    private void writeMyErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks, Throwable throwable)
        throws IOException, ServletException
    {
		PrintWriter out = new PrintWriter(writer);
		boolean hasTweaks = false;
		boolean isAdmin = false;
		String homeLink;
		Site site = null;
		try {
			site = Jtp.getSite(request);
		} catch(RuntimeException e) {}
		if( site == null ) {
			homeLink = "<a href='" + Jtp.homePage() + "'>Nabble</a>";
		} else {
			homeLink = Jtp.link(site.getRootNode());
			hasTweaks = site.getCustomTweaks().size() > 0;
			User user = Jtp.getUser(request);
			isAdmin = user != null && (site.getRootNode().getOwner().equals(user) || Permissions.isInGroup(user, "Administrators"));
		}
		
		out.print( "\n<html>\n	<head>\n		" );
 writeErrorPageHead(request,writer,code,message); 
		out.print( "\n		<META NAME=\"robots\" CONTENT=\"noindex,nofollow\">\n		<link rel=\"stylesheet\" href=\"" );
		out.print( (Shared.getCssPath()) );
		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		" );

				String uri = request.getRequestURI();
		        if (uri!=null) {
					uri = HtmlUtils.htmlEncode(uri);
		        }
				
		out.print( "\n<div class=\"nabble\" id=\"nabble\">\n	<div class=\"top-bar\">\n		<div class=\"breadcrumbs\" style=\"float:left;\">\n			" );
		out.print( (homeLink) );
		out.print( "\n		</div>\n	</div>\n	" );

					if (throwable != null) {
						if( throwable instanceof ServletException ) {
							if( MinorServletException.isIn(throwable) ) {
								logger.info(ServletUtils.getCurrentURL(request), throwable);
							} else {
								logger.warn(ServletUtils.getCurrentURL(request), throwable);
							}
						} else {
							logger.error(ServletUtils.getCurrentURL(request), throwable);
						}
						
		out.print( "\n<h1>Oops... An error has occurred</h1>\n\nPlease contact <a href=\"" );
		out.print( (Jtp.supportUrl()) );
		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" );
 if (hasTweaks) { 
		out.print( "\n	" );
 if (isAdmin) { 
		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			" );
		out.print( (site.getRootNode().getSubjectHtml()) );
		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	" );
 } 
		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" );
 } 
		out.print( "\n\n<h2 style=\"margin-top:1em\">More Details</h2>\n<div class=\"error-details light-bg-color\">\n	<h3>Error " );
		out.print( (code) );
		out.print( "</h3>\n	<pre>" );
		out.print( (HtmlUtils.htmlEncode(message)) );
		out.print( "</pre>\n	" );
 if (throwable.getCause() != null) {
								String msg = throwable.getCause().getMessage();
								if( msg != null ) {
									int posBreak = msg.indexOf("\n\t");
									msg = posBreak > 0? msg.substring(0, posBreak) : msg;
									msg = HtmlUtils.htmlEncode(msg);
									msg = msg.replaceAll("\n", "<br/>").replaceAll("\t","&nbsp;&nbsp;&nbsp;&nbsp;");
									
		out.print( "\n<p><b>Message</b>: " );
		out.print( (msg) );
		out.print( "</p>\n" );

								}
							}
		out.print( "\n<p><b>RequestURI</b>: " );
		out.print( (uri) );
		out.print( "</p>\n<p><b>Server</b>: " );
		out.print( (Jtp.getDefaultHost()) );
		out.print( "</p>\n" );


							if (showStacks) {
								StringWriter sw = new StringWriter();
								PrintWriter pw = new PrintWriter(sw);
								throwable.printStackTrace(pw);
								pw.flush();
								
		out.print( "<h3 style=\"margin-top:1.2em\">Caused by:</h3><pre>" );
		out.print( (HtmlUtils.htmlEncode(sw.toString())) );
		out.print( "</pre>" );

							}
							
		out.print( "\n</div>\n" );

					} else {
						
		out.print( "\n<h2 style=\"padding:0;margin:1em 0\">" );
		out.print( (message) );
		out.print( "</h2>\n\nPlease contact <a href=\"" );
		out.print( (Jtp.supportUrl()) );
		out.print( "\" target=\"_new\">Nabble Support</a> if you need help.\n" );

					}
					{
						StringBuffer url = request.getRequestURL();
						String query = request.getQueryString();
						if( query != null )
							url.append( '?' ).append( query );
						
		out.print( "\n<p>URL = <a href=\"" );
		out.print( (url) );
		out.print( "\">" );
		out.print( (url) );
		out.print( "</a></p>\n" );

					}
					
		out.print( "\n<table class=\"footer-table shaded-bg-color\">\n	<tr>\n		<td class=\"footer-left\">\n			Powered by <a href=\"" );
		out.print( (Jtp.homePage()) );
		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" );

	}

	private void serviceUnavailablePage(Writer writer) {
		PrintWriter out = new PrintWriter(writer);
		
		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" );

	}

	private void databaseUpdatePage(Writer writer) {
		PrintWriter out = new PrintWriter(writer);
		
		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" );

	}
}