Make the code better & add error handler

This commit is contained in:
DevSnaith 2023-04-19 10:46:32 +03:00
parent b9036ac208
commit 3ffa7805ca

View File

@ -1,63 +1,86 @@
package org.eu.lumiere; package org.eu.lumiere;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket; import java.net.Socket;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.TimeZone; 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;
import org.eu.lumiere.loggers.GlobalLogger.LogLevel; import org.eu.lumiere.loggers.GlobalLogger.LogLevel;
import org.eu.lumiere.net.RequestHandler;
import org.eu.lumiere.net.RequestListener; import org.eu.lumiere.net.RequestListener;
import org.eu.lumiere.net.ServerEvents; import org.eu.lumiere.net.ServerEvents;
import org.eu.lumiere.net.http.HttpRequest; import org.eu.lumiere.net.http.HTTPController;
import org.eu.lumiere.net.http.HttpRequestHandler; import org.eu.lumiere.net.http.HTTPRequest;
import org.eu.lumiere.net.http.HttpResponse; import org.eu.lumiere.net.http.HTTPResponse;
import org.eu.lumiere.utils.SimpleResponse; import org.eu.lumiere.utils.SimpleResponse;
public class Lumiere implements ServerEvents{ public class Lumiere implements ServerEvents {
private GlobalLogger l = GlobalLogger.getLogger(); private GlobalLogger l = GlobalLogger.getLogger();
private HTTPException hEx;
private int server_port = 8080; private int server_port = 8080;
private RequestListener server; private RequestListener server;
private HttpResponse header; private HTTPResponse header;
private HttpRequestHandler request; private RequestHandler request;
public Lumiere(HttpRequestHandler rHandler) { private HashMap<HTTPRequest, Socket> 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) { if((this.request = rHandler) == null) {
l.printf(LogLevel.ERROR, "Lumiere 'requestHandler' CANNOT BE NULL", null); l.printf(LogLevel.ERROR, "Lumiere 'requestHandler' CANNOT BE NULL", null);
return; return;
} }
header = new HttpResponse(null, "HTTP/1.1 200 OK", "text/html"); header = new HTTPResponse(null, "HTTP/1.1 200 OK", "text/html");
header.setProperty("Server", "Lumiere Server");
header.setProperty("Accept-Ranges", "bytes"); header.setProperty("Accept-Ranges", "bytes");
ErrorHandler.getHandler().debug = false;
ErrorHandler.getHandler().initialize();
requests = new HashMap<>();
} }
@Override @Override
public void onConnection(Socket socket) { public void onConnection(Socket socket) {
OutputStream s_os = null;
HTTPRequest http_in = null;
HTTPResponse http_out = null;
if(header == null) try {
return; 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()));
HttpResponse httpH = new HttpResponse(socket, header.getStatusLine(), header.getContentType()); http_in = new HTTPRequest(socket.getInputStream());
header.getKeySet().forEach(key -> { }catch(NoSuchElementException ex) {
httpH.setProperty(key, header.getProperty(key)); 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, "");
}
// Add Date to the header if(http_in != null)
SimpleDateFormat gmtDate = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z"); request.onRequestReceived(socket, http_in, http_out);
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);
}
try { try {
if(socket.isConnected()) if(socket.isConnected())
@ -65,6 +88,7 @@ public class Lumiere implements ServerEvents{
}catch(IOException ex) { }catch(IOException ex) {
l.printf(LogLevel.ERROR, ex.getMessage(), null); l.printf(LogLevel.ERROR, ex.getMessage(), null);
} }
} }
public void bootServer(int port) { public void bootServer(int port) {
@ -74,11 +98,20 @@ public class Lumiere implements ServerEvents{
} }
server_port = port; server_port = port;
double lTIme = System.nanoTime();
server = new RequestListener(this.getClass().getSimpleName(), this, server_port); server = new RequestListener(this.getClass().getSimpleName(), this, server_port);
server.start(); 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; return header;
} }