lumiere-server/src/org/eu/lumiere/Lumiere.java

82 lines
2.2 KiB
Java
Raw Normal View History

2023-04-13 06:27:32 +03:00
package org.eu.lumiere;
import java.io.IOException;
import java.net.Socket;
2023-04-14 02:32:17 +03:00
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
2023-04-13 06:27:32 +03:00
import org.eu.lumiere.loggers.GlobalLogger;
import org.eu.lumiere.loggers.GlobalLogger.LogLevel;
2023-04-14 02:32:17 +03:00
import org.eu.lumiere.net.RequestListener;
2023-04-13 06:27:32 +03:00
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;
public class Lumiere implements ServerEvents{
private GlobalLogger l = GlobalLogger.getLogger();
private int server_port = 8080;
2023-04-14 02:32:17 +03:00
private RequestListener server;
2023-04-13 06:27:32 +03:00
private HttpResponse header;
private HttpRequestHandler request;
public Lumiere(HttpRequestHandler rHandler) {
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");
2023-04-14 02:32:17 +03:00
header.setProperty("Accept-Ranges", "bytes");
2023-04-13 06:27:32 +03:00
}
@Override
public void onConnection(Socket socket) {
if(header == null)
return;
HttpResponse httpH = new HttpResponse(socket, header.getStatusLine(), header.getContentType());
header.getKeySet().forEach(key -> {
httpH.setProperty(key, header.getProperty(key));
});
2023-04-14 02:32:17 +03:00
// 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()));
2023-04-13 06:27:32 +03:00
request.onRequestReceived(new HttpRequest(socket), httpH);
2023-04-14 02:32:17 +03:00
2023-04-13 06:27:32 +03:00
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);
return;
}
server_port = port;
2023-04-14 02:32:17 +03:00
server = new RequestListener(this.getClass().getSimpleName(), this, server_port);
2023-04-13 06:27:32 +03:00
server.start();
}
public HttpResponse getSharedResponseHeader() {
return header;
}
public int getCurrentPort() {
return server_port;
}
}