/*
* $Id: HtmlTemplates.java,v 1.13 2002/09/16 08:05:06 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.server;
import java.io.IOException;
import java.io.Writer;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URL;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import anvil.Location;
import anvil.ErrorListener;
import anvil.ErrorEvent;
import anvil.ForgingException;
import anvil.script.ScriptException;
import anvil.util.Conversions;
/**
* class HtmlTemplates
*
* @author: Jani Lehtim�ki
*/
public class HtmlTemplates
{
public static final void message(HttpServletRequest request,
HttpServletResponse response, int errorCode, String errorMessage) throws IOException
{
String path = request.getPathInfo();
path = (path != null) ? Conversions.encodeEntities(path): "";
String host = request.getHeader("Host");
host = (host != null) ? Conversions.encodeEntities(host) : "";
PrintWriter out = new PrintWriter(response.getOutputStream());
response.setStatus(errorCode);
out.print(
"<html>\n" +
" <head>\n" +
" <title>");
out.print(errorCode);
out.print(" - ");
out.print(errorMessage);
out.print(
"</title>\n" +
" </head>\n" +
" <body bgcolor=\"#ffffff\">\n" +
" <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td bgcolor=\"#b0b0d0\">\n" +
" <table width=\"100%\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\">\n" +
" <tr>\n" +
" <td bgcolor=\"#f0f0ff\" >\n" +
" <font face=\"verdana,lucida,arial,helvetica\" size=4>\n" +
" <strong>");
out.print(errorCode);
out.print(" - ");
out.print(errorMessage);
out.print(
"</strong>\n" +
" </font>\n" +
" </td>\n" +
" </tr>\n" +
" <tr bgcolor=\"#f0f0ff\">\n" +
" <td>\n" +
" <code>\n" +
" Resource\n" +
" <strong>");
out.print(path);
out.print("</strong> at <strong>");
out.print(host);
out.print("</strong>\n" +
" </code>\n" +
" </td>\n" +
" </tr>\n" +
" <tr bgcolor=\"#f0f0ff\">\n" +
" <td>\n" +
" <code>");
out.print(anvil.server.Server.VERSION);
out.print("</code>\n" +
" </td>\n" +
" </table>\n" +
" </td></tr></table>\n" +
" </body>\n" +
"</html>");
out.flush();
}
public static final void exception(Context context, Exception exception) throws IOException
{
PrintWriter out = new PrintWriter(context.getOutputStream());
out.println("</table></table></table></table></table></table>");
String title;
if (exception instanceof ForgingException) {
title = "Parsing errors";
} else if (exception instanceof ScriptException) {
title = "Uncaught exception";
} else if (exception instanceof IOException) {
title = "I/O error";
} else {
title = "Error";
}
out.print(
"<html>\n" +
" <head>\n" +
" <title>");
out.print(title);
out.print("</title>\n" +
" </head>\n" +
" <body bgcolor=\"#ffffff\">\n" +
" <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td bgcolor=\"#b0b0d0\">\n" +
" <table width=\"100%\" border=\"0\" cellpadding=\"4\" cellspacing=\"1\">\n" +
" <tr>\n" +
" <td bgcolor=\"#f0f0ff\" colspan=\"3\">\n" +
" <font face=\"verdana,lucida,arial,helvetica\" size=4>");
out.print(title);
out.print("</font>\n" +
" </td>\n" +
" </tr>");
if (exception instanceof ForgingException) {
ErrorListener listener = ((ForgingException)exception).getErrorListener();
Enumeration e = listener.getEvents();
while(e.hasMoreElements()) {
ErrorEvent evt = (ErrorEvent)e.nextElement();
Location loc = evt.getLocation();
out.print(
" <tr bgcolor=\"#f0f0ff\">\n" +
" <td><code><strong>");
if (loc != null) {
out.print("<a href=\"");
String url = (loc.getURL() != null) ? loc.getURL().toString() : "";
out.print(url);
out.print("\">");
out.print(url);
out.print("</a>");
} else {
out.println("Unknown location");
}
out.print(
"<strong></code></td>" +
" <td><code><strong>");
if (loc != null) {
int line = loc.getLine();
if (line > 0) {
out.print(line);
} else {
out.print(" ");
}
} else {
out.print(" ");
}
out.print(
"</strong></code></td>\n" +
" <td><code><strong>");
if (loc != null) {
int column = loc.getColumn();
if (column > 0) {
out.print(column);
} else {
out.print(" ");
}
}else {
out.print(" ");
}
out.print(
"</strong></code></td>\n" +
" </tr> \n" +
" <tr bgcolor=\"#f0f0ff\">\n" +
" <td colspan=\"3\">\n" +
" <font face=\"verdana,lucida,arial,helvetica\" size=2>");
String msg = evt.getMessage();
if (msg != null) {
out.print(Conversions.encodeEntities(msg));
} else {
out.println(" ");
}
out.print(
" </font>\n" +
" </td>\n" +
" </tr>\n");
}
} else if (exception instanceof ScriptException) {
out.print(
" <tr bgcolor=\"#f0f0ff\">\n" +
" <td colspan=\"3\"><pre>");
out.print(((ScriptException)exception).getData().toString());
out.print("</pre>\n" +
" </td>\n" +
" </tr>\n");
} else if (exception instanceof IOException) {
out.print(
" <tr bgcolor=\"#f0f0ff\" colspan=\"3\">\n" +
" <td><strong><code>");
out.print(context.getPathinfo());
out.print("</code></strong>\n" +
" </td>\n" +
" </tr>\n");
out.print(
" <tr bgcolor=\"#f0f0ff\">\n" +
" <td colspan=\"3\"><pre>");
out.print(exception.getMessage());
out.print("</pre>\n" +
" </td>\n" +
" </tr>\n");
} else {
out.print(
" <tr bgcolor=\"#f0f0ff\">\n" +
" <td colspan=\"3\"><strong><code>");
out.print(exception.getMessage());
out.print("</code></strong>\n" +
" </td>\n" +
" </tr>\n");
out.print(
" <tr bgcolor=\"#f0f0ff\">\n" +
" <td colspan=\"3\"><pre>");
exception.printStackTrace(out);
out.print("</pre>\n" +
" </td>\n" +
" </tr>\n");
}
out.print(
" </table>\n" +
" </td></tr></table>\n" +
" <code>");
out.print(anvil.server.Server.VERSION);
out.print(
"</code>\n" +
" </body>\n" +
"</html>");
out.flush();
}
}