diff --git a/src/org/eu/lumiere/error/ErrorHandler.java b/src/org/eu/lumiere/error/ErrorHandler.java new file mode 100644 index 0000000..ac2deea --- /dev/null +++ b/src/org/eu/lumiere/error/ErrorHandler.java @@ -0,0 +1,75 @@ +package org.eu.lumiere.error; + +import java.io.ByteArrayOutputStream; +import java.io.OutputStream; +import java.util.HashMap; + +import org.eu.lumiere.loggers.GlobalLogger; +import org.eu.lumiere.loggers.GlobalLogger.LogLevel; +import org.eu.lumiere.net.http.HTTPResponse; + +public class ErrorHandler extends HTTPException { + + public boolean debug = true; + private static GlobalLogger l = GlobalLogger.getLogger(); + private final HashMap errors = new HashMap<>(); + private static ErrorHandler errorHandler = new ErrorHandler(); + private int defaultErrorCode = 500; + + public void setError(int errorCode, String statusLine) { + errors.put(errorCode, (String) (debug ? l.printf(LogLevel.INFO, "[%d:%s] added to ErrorHandler", statusLine, errorCode, statusLine) : statusLine)); + } + + public void initialize() { + setError(500, "Internal Server Error"); + setError(501, "Not Implemented"); + setError(503, "Service Unavailable"); + setError(400, "Bad Request"); + setError(401, "Unauthorized"); + setError(403, "Forbidden"); + setError(404, "Not Found"); + setError(411, "Length Required"); + setError(414, "URI Too Long"); + setError(423, "Locked"); + setError(429, "Too Many Requests"); + } + + @Override + public void onException(HTTPResponse res, int httpCode, String msg) { + String[] sLine = getStatusLine(httpCode).split(" ", 3); + StringBuilder htmlBuilder = new StringBuilder("\n\n"); + htmlBuilder.append("").append("\n"); + htmlBuilder.append("").append("\n"); + htmlBuilder.append("").append(String.format("%s %s", sLine[1], sLine[2])).append("").append("\n"); + htmlBuilder.append("").append("\n"); + htmlBuilder.append("").append(String.format("%s %s", sLine[1], sLine[2])).append("
").append(msg); + res.setStatus(String.format("%s %s %s", sLine[0], sLine[1], sLine[2])); + res.setContentType("text/html"); + res.push(htmlBuilder.append("").toString()); + } + + public void generateHTML(OutputStream os, int httpCode, String msg) { + HTTPResponse r = new HTTPResponse(os, null, null); + onException(r, httpCode, msg); + } + + public byte[] generateHTMLAsBytes(int httpCode, String msg) { + ByteArrayOutputStream byteArray = new ByteArrayOutputStream(); + generateHTML(byteArray, httpCode, msg); + return byteArray.toByteArray(); + } + + public String generateHTMLAsString(int httpCode, String msg) { + return new String(generateHTMLAsBytes(httpCode, msg)); + } + + public synchronized String getStatusLine(int httpCode) { + if(!errors.containsKey(httpCode)) + return errors.containsKey(defaultErrorCode) ? getStatusLine(defaultErrorCode) : "HTTP/1.0 200 OK"; + return String.format("HTTP/1.0 %d %s", httpCode, errors.get(httpCode)); + } + public static ErrorHandler getHandler() { + return errorHandler; + } + +} diff --git a/src/org/eu/lumiere/error/HTTPException.java b/src/org/eu/lumiere/error/HTTPException.java new file mode 100644 index 0000000..ea75b7f --- /dev/null +++ b/src/org/eu/lumiere/error/HTTPException.java @@ -0,0 +1,7 @@ +package org.eu.lumiere.error; + +import org.eu.lumiere.net.http.HTTPResponse; + +public abstract class HTTPException { + public abstract void onException(HTTPResponse os, int httpCode, String msg); +}