mirror of
https://github.com/devsnaith/lumiere-server.git
synced 2024-11-23 17:13:14 +03:00
Make the code better & add error handler
This commit is contained in:
parent
b9036ac208
commit
3ffa7805ca
@ -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<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) {
|
||||
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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user