From e48c318e4294f509681c4099e3f788fc1066522a Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Tue, 19 Jul 2016 10:01:02 +0200 Subject: [PATCH] Added methods and a not found page --- .../norbipeti/chat/server/IOHelper.java | 21 +++++++++++++- src/io/github/norbipeti/chat/server/Main.java | 20 ++++++------- .../norbipeti/chat/server/page/IndexPage.java | 9 ++++-- .../norbipeti/chat/server/page/LoginPage.java | 18 +++++++++++- .../chat/server/page/NotFoundPage.java | 28 +++++++++++++++++++ .../norbipeti/chat/server/page/Page.java | 26 +++++++++++++++++ .../chat/server/page/RegisterPage.java | 10 +++++-- 7 files changed, 114 insertions(+), 18 deletions(-) create mode 100644 src/io/github/norbipeti/chat/server/page/NotFoundPage.java diff --git a/src/io/github/norbipeti/chat/server/IOHelper.java b/src/io/github/norbipeti/chat/server/IOHelper.java index e8496b7..83c46b2 100644 --- a/src/io/github/norbipeti/chat/server/IOHelper.java +++ b/src/io/github/norbipeti/chat/server/IOHelper.java @@ -1,14 +1,33 @@ package io.github.norbipeti.chat.server; +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; +import java.nio.charset.StandardCharsets; import org.apache.commons.io.IOUtils; import com.sun.net.httpserver.HttpExchange; +import io.github.norbipeti.chat.server.page.Page; + 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()); + IOUtils.write(content, exchange.getResponseBody(), StandardCharsets.UTF_8); exchange.getResponseBody().close(); } + + public static boolean SendPage(int code, Page page, HttpExchange exchange) throws IOException { + File file = new File(page.GetHTMLPath()); + if (!file.exists()) { + SendResponse(501, + "

501 Not Implemented

The page \"" + page.GetName() + "\" cannot be found on disk.", + exchange); + return false; + } + FileInputStream inputStream = new FileInputStream(file); + String content = IOUtils.toString(inputStream, StandardCharsets.UTF_8); + SendResponse(code, content, exchange); + return true; + } } diff --git a/src/io/github/norbipeti/chat/server/Main.java b/src/io/github/norbipeti/chat/server/Main.java index ae4c681..6f42b26 100644 --- a/src/io/github/norbipeti/chat/server/Main.java +++ b/src/io/github/norbipeti/chat/server/Main.java @@ -2,18 +2,11 @@ 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; import io.github.norbipeti.chat.server.db.DataProvider; -import io.github.norbipeti.chat.server.page.IndexPage; -import io.github.norbipeti.chat.server.page.RegisterPage; +import io.github.norbipeti.chat.server.page.*; public class Main { // public static final HashMap Pages = new HashMapIndex", exchange); + public void handlePage(HttpExchange exchange) throws IOException { + IOHelper.SendPage(200, this, exchange); + } + + @Override + public String GetName() { + return ""; } } diff --git a/src/io/github/norbipeti/chat/server/page/LoginPage.java b/src/io/github/norbipeti/chat/server/page/LoginPage.java index da17a27..7e8f080 100644 --- a/src/io/github/norbipeti/chat/server/page/LoginPage.java +++ b/src/io/github/norbipeti/chat/server/page/LoginPage.java @@ -1,5 +1,21 @@ package io.github.norbipeti.chat.server.page; -public class LoginPage { +import java.io.IOException; + +import com.sun.net.httpserver.HttpExchange; + +import io.github.norbipeti.chat.server.IOHelper; + +public class LoginPage extends Page { + + @Override + public void handlePage(HttpExchange exchange) throws IOException { + IOHelper.SendPage(200, this, exchange); + } + + @Override + public String GetName() { + return "login"; + } } diff --git a/src/io/github/norbipeti/chat/server/page/NotFoundPage.java b/src/io/github/norbipeti/chat/server/page/NotFoundPage.java new file mode 100644 index 0000000..c33e979 --- /dev/null +++ b/src/io/github/norbipeti/chat/server/page/NotFoundPage.java @@ -0,0 +1,28 @@ +package io.github.norbipeti.chat.server.page; + +import java.io.IOException; + +import com.sun.net.httpserver.HttpExchange; + +import io.github.norbipeti.chat.server.IOHelper; + +public class NotFoundPage extends Page { + + @Override + public String GetName() { + return "notfound"; + } + + @Override + public void handlePage(HttpExchange exchange) throws IOException { + IOHelper.SendPage(404, this, exchange); + } + + public NotFoundPage() { + if (Instance != null) + throw new UnsupportedOperationException("There can only be one instance of a page."); + Instance = this; + } + + public static NotFoundPage Instance; +} diff --git a/src/io/github/norbipeti/chat/server/page/Page.java b/src/io/github/norbipeti/chat/server/page/Page.java index da94056..1132045 100644 --- a/src/io/github/norbipeti/chat/server/page/Page.java +++ b/src/io/github/norbipeti/chat/server/page/Page.java @@ -1,7 +1,10 @@ package io.github.norbipeti.chat.server.page; +import java.io.IOException; + import com.sun.net.httpserver.*; +import io.github.norbipeti.chat.server.IOHelper; import io.github.norbipeti.chat.server.Main; /** @@ -9,4 +12,27 @@ import io.github.norbipeti.chat.server.Main; * */ public abstract class Page implements HttpHandler { + public abstract String GetName(); + + public final String GetHTMLPath() { + if (GetName().length() == 0) + return "pages/index.html"; + return new StringBuilder("pages/").append(GetName().length() == 0).append(".html").toString(); + } + + @Override + public void handle(HttpExchange exchange) throws IOException { + if (exchange.getRequestURI().getPath().equals("/" + GetName())) + handlePage(exchange); + else { + if (!IOHelper.SendPage(404, NotFoundPage.Instance, exchange)) + ; + } + } + + public abstract void handlePage(HttpExchange exchange) throws IOException; + + public boolean getDo404() { + return true; + } } diff --git a/src/io/github/norbipeti/chat/server/page/RegisterPage.java b/src/io/github/norbipeti/chat/server/page/RegisterPage.java index 85bf0d5..d1fc90e 100644 --- a/src/io/github/norbipeti/chat/server/page/RegisterPage.java +++ b/src/io/github/norbipeti/chat/server/page/RegisterPage.java @@ -8,9 +8,13 @@ import io.github.norbipeti.chat.server.IOHelper; public class RegisterPage extends Page { @Override - public void handle(HttpExchange exchange) throws IOException { - //exchange.getRequestURI().getPath() - IOHelper.SendResponse(200, "

Register

", exchange); + public void handlePage(HttpExchange exchange) throws IOException { + IOHelper.SendPage(200, this, exchange); + } + + @Override + public String GetName() { + return "register"; } }