diff --git a/src/org/eu/lumiere/net/http/HttpController.java b/src/org/eu/lumiere/net/http/HttpController.java deleted file mode 100644 index e62e570..0000000 --- a/src/org/eu/lumiere/net/http/HttpController.java +++ /dev/null @@ -1,43 +0,0 @@ -package org.eu.lumiere.net.http; - -import java.util.HashMap; - -import org.eu.lumiere.loggers.GlobalLogger; -import org.eu.lumiere.loggers.GlobalLogger.LogLevel; - -public class HttpController implements HttpRequestHandler { - - private GlobalLogger l = GlobalLogger.getLogger(); - private HashMap handlers = new HashMap<>();; - - public boolean addHandler(String path, HttpRequestHandler handler) { - if(handler == null) - return (boolean) l.printf(LogLevel.WARNING, "The handler ["+path+"] is null", false); - - if(handler.equals(this)) - return (boolean) l.printf(LogLevel.WARNING, "The handler cannot be the HttpController", false); - l.printf(LogLevel.INFO, "A Handler for " + path + " has been created", null); - handlers.put(path, handler); - return true; - } - - @Override - public void onRequestReceived(HttpRequest request, HttpResponse response) { - String clientUrl = request.getURL(); - if(handlers.containsKey(clientUrl)) - handlers.get(clientUrl).onRequestReceived(request, response); - } - - public HttpRequestHandler getHandler(String path) { - return handlers.get(path); - } - - public HttpRequestHandler[] getHandlers() { - return handlers.values().toArray(new HttpRequestHandler[0]); - } - - public String[] getPaths() { - return handlers.keySet().toArray(new String[0]); - } - -} diff --git a/src/org/eu/lumiere/net/http/HttpRequest.java b/src/org/eu/lumiere/net/http/HttpRequest.java deleted file mode 100644 index 0a5f99d..0000000 --- a/src/org/eu/lumiere/net/http/HttpRequest.java +++ /dev/null @@ -1,61 +0,0 @@ -package org.eu.lumiere.net.http; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.Socket; -import java.util.Properties; -import java.util.StringTokenizer; - -import org.eu.lumiere.loggers.GlobalLogger; -import org.eu.lumiere.loggers.GlobalLogger.LogLevel; - -public class HttpRequest { - private GlobalLogger l = GlobalLogger.getLogger(); - private String reqMethod, reqPath, reqVersion; - private Properties properties; - public HttpRequest(Socket client) { - try { - InputStreamReader stream = new InputStreamReader(client.getInputStream()); - BufferedReader reader = new BufferedReader(stream); - StringTokenizer t = new StringTokenizer(reader.readLine()); - reqMethod = t.nextToken(); - reqPath = t.nextToken(); - reqVersion = t.nextToken(); - - String line; - properties = new Properties(); - while((line = reader.readLine()) != null) { - String[] split; - if((split = line.split(":", 2)).length >= 2) { - properties.setProperty(split[0], split[1].replaceFirst ("^ *", "")); - }else { - break; - } - } - - } catch (IOException e) { - l.printf(LogLevel.ERROR, e.getMessage(), null); - } - } - - public String[] getKeys() { - return properties.keySet().toArray(new String[0]); - } - - public String getProperty(String key) { - return properties.getProperty(key); - } - - public String getMethod() { - return reqMethod; - } - - public String getURL() { - return reqPath; - } - - public String getHttpVersion() { - return reqVersion; - } -} diff --git a/src/org/eu/lumiere/net/http/HttpRequestHandler.java b/src/org/eu/lumiere/net/http/HttpRequestHandler.java deleted file mode 100644 index d49d86d..0000000 --- a/src/org/eu/lumiere/net/http/HttpRequestHandler.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.eu.lumiere.net.http; - -public interface HttpRequestHandler{ - public void onRequestReceived(HttpRequest request, HttpResponse response); -} diff --git a/src/org/eu/lumiere/net/http/HttpResponse.java b/src/org/eu/lumiere/net/http/HttpResponse.java deleted file mode 100644 index dcce73e..0000000 --- a/src/org/eu/lumiere/net/http/HttpResponse.java +++ /dev/null @@ -1,72 +0,0 @@ -package org.eu.lumiere.net.http; - -import java.io.IOException; -import java.net.Socket; -import java.util.LinkedHashMap; -import java.util.Set; - -import org.eu.lumiere.loggers.GlobalLogger; -import org.eu.lumiere.loggers.GlobalLogger.LogLevel; - -public class HttpResponse { - - private GlobalLogger l = GlobalLogger.getLogger(); - private String status_line = "HTTP/1.0 200 OK"; - private String content_type = "text/html"; - private LinkedHashMap httph; - private Socket client; - - public HttpResponse(Socket client, String status_line, String content_type) { - this.content_type = content_type == null ? this.content_type : content_type; - if(status_line != null && !status_line.isEmpty()) - this.status_line = status_line; - httph = httph == null ? new LinkedHashMap<>() : httph; - this.client = client; - } - - public void push(String body) { - try { - StringBuilder build = new StringBuilder(status_line+"\r\n"); - for(String key : httph.keySet()) { - build.append(String.format("%s: %s\r\n", key, getProperty(key))); - } - client.getOutputStream().write(build.append("\r\n").append(body).toString().getBytes()); - } catch (IOException e) { - l.printf(LogLevel.ERROR, e.getMessage(), null); - } - } - - public void setProperty(String key, String value) { - if(!httph.containsKey(key)) - httph.put(key, value); - } - - public void setStatus(String sl) { - this.status_line = sl; - } - - public void setContentType(String ct) { - this.content_type = ct; - } - - public String getStatus() { - return this.status_line; - } - - public String getContentType() { - return this.content_type; - } - - public String getProperty(String key) { - return httph.get(key); - } - - public Set getKeySet() { - return httph.keySet(); - } - - public String getStatusLine() { - return status_line; - } - -}