diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..21f4084 --- /dev/null +++ b/pom.xml @@ -0,0 +1,36 @@ + + 4.0.0 + ChatServer + ChatServer + 0.0.1-SNAPSHOT + + src + + + maven-compiler-plugin + 3.5.1 + + 1.8 + 1.8 + + + + + + + org.apache.derby + derby + 10.12.1.1 + + + org.apache.derby + derbyclient + 10.12.1.1 + + + + 1.8 + 1.8 + + \ No newline at end of file diff --git a/src/io/github/norbipeti/chat/server/IOHelper.java b/src/io/github/norbipeti/chat/server/IOHelper.java new file mode 100644 index 0000000..e8496b7 --- /dev/null +++ b/src/io/github/norbipeti/chat/server/IOHelper.java @@ -0,0 +1,14 @@ +package io.github.norbipeti.chat.server; + +import java.io.IOException; + +import org.apache.commons.io.IOUtils; +import com.sun.net.httpserver.HttpExchange; + +public class IOHelper { + public static void SendResponse(int code, String content, HttpExchange exchange) throws IOException { + exchange.sendResponseHeaders(code, content.length()); + IOUtils.write(content, exchange.getResponseBody()); + exchange.getResponseBody().close(); + } +} diff --git a/src/io/github/norbipeti/chat/server/Index.java b/src/io/github/norbipeti/chat/server/Index.java new file mode 100644 index 0000000..d2ab22e --- /dev/null +++ b/src/io/github/norbipeti/chat/server/Index.java @@ -0,0 +1,14 @@ +package io.github.norbipeti.chat.server; + +import java.io.IOException; + +import com.sun.net.httpserver.HttpExchange; + +public class Index extends Page { + + @Override + public void handle(HttpExchange exchange) throws IOException { + + } + +} diff --git a/src/io/github/norbipeti/chat/server/Main.java b/src/io/github/norbipeti/chat/server/Main.java new file mode 100644 index 0000000..2e3fa64 --- /dev/null +++ b/src/io/github/norbipeti/chat/server/Main.java @@ -0,0 +1,71 @@ +package io.github.norbipeti.chat.server; + +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.sql.*; +import java.util.List; +import java.util.Map.Entry; +import org.apache.commons.io.IOUtils; + +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpHandler; +import com.sun.net.httpserver.HttpServer; + +public class Main { + // public static final HashMap Pages = new HashMap(); + + public static void main(String[] args) { // http://stackoverflow.com/questions/9266632/access-restriction-is-not-accessible-due-to-restriction-on-required-library/10642163#10642163 + try { // rt.jar Javadoc: + // https://docs.oracle.com/javase/8/docs/jre/api/net/httpserver/spec/ + // https://docs.oracle.com/javase/8/docs/api/ + System.out.println("Loading database..."); + Connection conn = DriverManager.getConnection("jdbc:derby:memory:chatserver;create=true"); + Statement statement = conn.createStatement(); + if (statement.execute("CREATE TABLE users ( username varchar(255), password varchar(255), id int )")) + System.out.println("Created users table"); + else + System.out.println("Failed to create users table!"); + ResultSet results = statement.executeQuery("SELECT * FROM users"); + while (results.next()) + results.getString(0); + System.out.println("Starting webserver..."); + HttpServer server = HttpServer.create(new InetSocketAddress(InetAddress.getLocalHost(), 8080), 10); + server.createContext("/").setHandler(new HttpHandler() { + + @Override + public void handle(HttpExchange exchange) { + try { + System.out.println("Received request from " + exchange.getRemoteAddress()); + System.out.println("Body: " + IOUtils.toString(exchange.getRequestBody(), "ASCII")); + System.out.println("Headers: "); + for (Entry> entry : exchange.getRequestHeaders().entrySet()) + System.out.println(entry.getKey() + " - " + entry.getValue()); + System.out.println(exchange.getRequestURI().getPath()); + /* + * String resp = + * Pages.get(exchange.getRequestURI().getPath()).Run( + * exchange); exchange.sendResponseHeaders(200, + * resp.length()); IOUtils.write(resp, + * exchange.getResponseBody()); + * exchange.getResponseBody().close(); + */ + exchange.sendResponseHeaders(404, 0); + exchange.getResponseBody().close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + }); + server.createContext("/register").setHandler(new RegisterPage()); + server.start(); + System.out.println("Ready... Press Enter to stop."); + System.in.read(); + System.out.println("Stopping..."); + server.stop(1); + } catch (Exception e) { + e.printStackTrace(); + } + System.out.println("Stopped"); + } +} \ No newline at end of file diff --git a/src/io/github/norbipeti/chat/server/Page.java b/src/io/github/norbipeti/chat/server/Page.java new file mode 100644 index 0000000..a1ed330 --- /dev/null +++ b/src/io/github/norbipeti/chat/server/Page.java @@ -0,0 +1,10 @@ +package io.github.norbipeti.chat.server; + +import com.sun.net.httpserver.*; + +/** + * Add to {@link Main}.Pages + * + */ +public abstract class Page implements HttpHandler { +} diff --git a/src/io/github/norbipeti/chat/server/RegisterPage.java b/src/io/github/norbipeti/chat/server/RegisterPage.java new file mode 100644 index 0000000..73b1056 --- /dev/null +++ b/src/io/github/norbipeti/chat/server/RegisterPage.java @@ -0,0 +1,14 @@ +package io.github.norbipeti.chat.server; + +import java.io.IOException; + +import com.sun.net.httpserver.HttpExchange; + +public class RegisterPage extends Page { + @Override + public void handle(HttpExchange exchange) throws IOException { + //exchange.getRequestURI().getPath() + IOHelper.SendResponse(200, "

Register

", exchange); + } + +}