From 3ffa7805cac73c720409185e5b977b2b050a5e0c Mon Sep 17 00:00:00 2001 From: DevSnaith <65865554+DevSnaith@users.noreply.github.com> Date: Wed, 19 Apr 2023 10:46:32 +0300 Subject: [PATCH] Make the code better & add error handler --- src/org/eu/lumiere/Lumiere.java | 95 ++++++++++++++++++++++----------- 1 file changed, 64 insertions(+), 31 deletions(-) diff --git a/src/org/eu/lumiere/Lumiere.java b/src/org/eu/lumiere/Lumiere.java index d367699..4e451fc 100644 --- a/src/org/eu/lumiere/Lumiere.java +++ b/src/org/eu/lumiere/Lumiere.java @@ -1,72 +1,96 @@ package org.eu.lumiere; import java.io.IOException; +import java.io.OutputStream; import java.net.Socket; import java.text.SimpleDateFormat; import java.util.Date; +import java.util.HashMap; import java.util.NoSuchElementException; import java.util.TimeZone; +import org.eu.lumiere.error.ErrorHandler; +import org.eu.lumiere.error.HTTPException; import org.eu.lumiere.loggers.GlobalLogger; import org.eu.lumiere.loggers.GlobalLogger.LogLevel; +import org.eu.lumiere.net.RequestHandler; import org.eu.lumiere.net.RequestListener; import org.eu.lumiere.net.ServerEvents; -import org.eu.lumiere.net.http.HttpRequest; -import org.eu.lumiere.net.http.HttpRequestHandler; -import org.eu.lumiere.net.http.HttpResponse; +import org.eu.lumiere.net.http.HTTPController; +import org.eu.lumiere.net.http.HTTPRequest; +import org.eu.lumiere.net.http.HTTPResponse; import org.eu.lumiere.utils.SimpleResponse; -public class Lumiere implements ServerEvents{ +public class Lumiere implements ServerEvents { private GlobalLogger l = GlobalLogger.getLogger(); + private HTTPException hEx; private int server_port = 8080; private RequestListener server; - private HttpResponse header; - private HttpRequestHandler request; + private HTTPResponse header; + private RequestHandler request; - public Lumiere(HttpRequestHandler rHandler) { + private HashMap requests; + + public Lumiere(RequestHandler rHandler) { + initialize(rHandler, null); + } + + public Lumiere(RequestHandler rHandler, HTTPException ex) { + initialize(rHandler, ex == null ? ErrorHandler.getHandler() : ex); + } + + private void initialize(RequestHandler rHandler, HTTPException ex) { + this.hEx = ex == null ? ErrorHandler.getHandler() : ex; if((this.request = rHandler) == null) { l.printf(LogLevel.ERROR, "Lumiere 'requestHandler' CANNOT BE NULL", null); return; } - header = new HttpResponse(null, "HTTP/1.1 200 OK", "text/html"); - header.setProperty("Server", "Lumiere Server"); + header = new HTTPResponse(null, "HTTP/1.1 200 OK", "text/html"); header.setProperty("Accept-Ranges", "bytes"); + ErrorHandler.getHandler().debug = false; + ErrorHandler.getHandler().initialize(); + requests = new HashMap<>(); } - + @Override public void onConnection(Socket socket) { + OutputStream s_os = null; + HTTPRequest http_in = null; + HTTPResponse http_out = null; - if(header == null) - return; + try { + s_os = socket.getOutputStream(); + http_out = new HTTPResponse(s_os, header.getStatus(), header.getContentType()); + for(String key : header.getKeySet()) { + http_out.setProperty(key, header.getProperty(key)); + } + SimpleDateFormat gmtDate = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z"); + gmtDate.setTimeZone(TimeZone.getTimeZone("GMT")); + http_out.setProperty("Date", gmtDate.format(new Date())); + + http_in = new HTTPRequest(socket.getInputStream()); + }catch(NoSuchElementException ex) { + hEx.onException(http_out, 400, "The request you sent is not a valid request."); + }catch(IOException ex) { + l.printf(LogLevel.ERROR, ex.getMessage(), null); + hEx.onException(http_out, 503, ""); + } - HttpResponse httpH = new HttpResponse(socket, header.getStatusLine(), header.getContentType()); - header.getKeySet().forEach(key -> { - httpH.setProperty(key, header.getProperty(key)); - }); - - // Add Date to the header - SimpleDateFormat gmtDate = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z"); - gmtDate.setTimeZone(TimeZone.getTimeZone("GMT")); - httpH.setProperty("Date", gmtDate.format(new Date())); - - try { - request.onRequestReceived(new HttpRequest(socket), httpH); - }catch (NoSuchElementException e) { - httpH.setStatus("HTTP/1.1 400 Bad Request"); - new SimpleResponse("Bad Request", false).onRequestReceived(null, httpH); - } - + if(http_in != null) + request.onRequestReceived(socket, http_in, http_out); + try { if(socket.isConnected()) socket.close(); }catch(IOException ex) { l.printf(LogLevel.ERROR, ex.getMessage(), null); } + } - + public void bootServer(int port) { if(server != null) { l.printf(LogLevel.ERROR, "Lumiere is already running", null); @@ -74,11 +98,20 @@ public class Lumiere implements ServerEvents{ } server_port = port; + double lTIme = System.nanoTime(); server = new RequestListener(this.getClass().getSimpleName(), this, server_port); server.start(); + + if(server.isRunning()) + l.printf(LogLevel.INFO, "Lumiere server restarted after %f seconds", null, (double) ((System.nanoTime() - lTIme) / 1000000000)); + } - public HttpResponse getSharedResponseHeader() { + public Socket getClient(HTTPRequest request) { + return requests.get(request); + } + + public HTTPResponse getSharedResponseHeader() { return header; }