From 120f792ddf6047e64ceeb1f090ea18fc515d694b Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Mon, 25 Jul 2016 12:21:42 +0200 Subject: [PATCH] Added getUser, CSS, using HTML parsing for modifying pages --- pages/css/style.css | 7 ++++ pages/index.html | 14 ++++---- pages/login.html | 2 +- pages/register.html | 8 ++--- pom.xml | 6 ++++ .../norbipeti/chat/server/IOHelper.java | 30 ++++++++++++++--- .../chat/server/db/DataProvider.java | 7 ++++ .../norbipeti/chat/server/page/IndexPage.java | 14 +++++--- .../norbipeti/chat/server/page/LoginPage.java | 10 ++++-- .../chat/server/page/RegisterPage.java | 8 +++-- .../norbipeti/chat/server/page/StylePage.java | 33 +++++++++++++++++++ 11 files changed, 113 insertions(+), 26 deletions(-) create mode 100644 pages/css/style.css create mode 100644 src/io/github/norbipeti/chat/server/page/StylePage.java diff --git a/pages/css/style.css b/pages/css/style.css new file mode 100644 index 0000000..0b9ca84 --- /dev/null +++ b/pages/css/style.css @@ -0,0 +1,7 @@ +body { + background-color: #8888; +} + +.loginbox, .userbox, .errormsg, .successmsg { + display: none; +} diff --git a/pages/index.html b/pages/index.html index b1fe39e..e8e8be2 100644 --- a/pages/index.html +++ b/pages/index.html @@ -1,11 +1,13 @@ - ChatServer - - +ChatServer + + + - +

Index

-

Hello

+
Loginbox
+
Userbox
- + diff --git a/pages/login.html b/pages/login.html index 16279ef..c2e9b61 100644 --- a/pages/login.html +++ b/pages/login.html @@ -1,5 +1,5 @@

Login

-

Hello

+
diff --git a/pages/register.html b/pages/register.html index 1890d36..b8df795 100644 --- a/pages/register.html +++ b/pages/register.html @@ -1,12 +1,8 @@

Register

-
- -
-
- -
+
+
diff --git a/pom.xml b/pom.xml index 45974d1..65e6f5b 100644 --- a/pom.xml +++ b/pom.xml @@ -46,6 +46,12 @@ 0.9.10runtime + + + org.jsoup + jsoup + 1.9.2 + 1.8 diff --git a/src/io/github/norbipeti/chat/server/IOHelper.java b/src/io/github/norbipeti/chat/server/IOHelper.java index 541bc3d..2846423 100644 --- a/src/io/github/norbipeti/chat/server/IOHelper.java +++ b/src/io/github/norbipeti/chat/server/IOHelper.java @@ -13,10 +13,15 @@ import java.util.HashMap; import java.util.List; import java.util.Map.Entry; import java.util.UUID; +import java.util.function.Function; import org.apache.commons.io.IOUtils; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; + import com.sun.net.httpserver.HttpExchange; +import io.github.norbipeti.chat.server.db.DataProvider; import io.github.norbipeti.chat.server.db.domain.User; import io.github.norbipeti.chat.server.page.Page; @@ -70,12 +75,14 @@ public class IOHelper { } } - public static boolean SendModifiedPage(int code, Page page, String replace, String with, HttpExchange exchange) - throws IOException { + public static boolean SendModifiedPage(int code, Page page, Function modifyfunc, + HttpExchange exchange) throws IOException { String content = GetPage(page, exchange); if (content == null) return false; - SendResponse(200, content.replace(replace, with), exchange); + Document doc = Jsoup.parse(content); + doc = modifyfunc.apply(doc); + SendResponse(200, doc.html(), exchange); return true; } @@ -105,12 +112,27 @@ public class IOHelper { return new HashMap<>(); HashMap map = new HashMap<>(); // TODO for (String cheader : exchange.getRequestHeaders().get("Cookie")) { - String[] spl = cheader.split("\\;"); + String[] spl = cheader.split("\\;\\S"); for (String s : spl) { String[] kv = s.split("\\="); if (kv.length < 2) continue; + map.put(kv[0], kv[1]); } } + return map; + } + + public static User GetLoggedInUser(HttpExchange exchange) { + HashMap cookies = GetCookies(exchange); + if (!cookies.containsKey("user_id") || !cookies.containsKey("session_id")) + return null; + try (DataProvider provider = new DataProvider()) { + User user = provider.getUser(Long.parseLong(cookies.get("user_id"))); + if (user != null && cookies.get("session_id") != null + && cookies.get("session_id").equals(user.getSessionid())) + return user; + } + return null; } } diff --git a/src/io/github/norbipeti/chat/server/db/DataProvider.java b/src/io/github/norbipeti/chat/server/db/DataProvider.java index 0f16b4f..7d7fac0 100644 --- a/src/io/github/norbipeti/chat/server/db/DataProvider.java +++ b/src/io/github/norbipeti/chat/server/db/DataProvider.java @@ -57,6 +57,13 @@ public class DataProvider implements AutoCloseable { em.close(); } + public User getUser(Long id) { + EntityManager em = emf.createEntityManager(); + User managedUser = em.find(User.class, id); + em.close(); + return managedUser; + } + @Override public void close() { if (emf != null) diff --git a/src/io/github/norbipeti/chat/server/page/IndexPage.java b/src/io/github/norbipeti/chat/server/page/IndexPage.java index 39b53c4..f908d33 100644 --- a/src/io/github/norbipeti/chat/server/page/IndexPage.java +++ b/src/io/github/norbipeti/chat/server/page/IndexPage.java @@ -1,18 +1,22 @@ package io.github.norbipeti.chat.server.page; import java.io.IOException; - import com.sun.net.httpserver.HttpExchange; import io.github.norbipeti.chat.server.IOHelper; +import io.github.norbipeti.chat.server.db.domain.User; public class IndexPage extends Page { @Override - public void handlePage(HttpExchange exchange) throws IOException { - if () - System.out.println(exchange.getRequestHeaders().get("Cookie")); - IOHelper.SendPage(200, this, exchange); + public void handlePage(HttpExchange exchange) throws IOException { //TODO: Make a base HTML and insert all pages into that + User user = IOHelper.GetLoggedInUser(exchange); + if (user == null) + IOHelper.SendModifiedPage(200, this, + (doc) -> doc.getElementById("loginbox").attr("style", "display: block").ownerDocument(), exchange); + else + IOHelper.SendModifiedPage(200, this, + (doc) -> doc.getElementById("userbox").attr("style", "display: block").ownerDocument(), exchange); } @Override diff --git a/src/io/github/norbipeti/chat/server/page/LoginPage.java b/src/io/github/norbipeti/chat/server/page/LoginPage.java index 675d13d..3094398 100644 --- a/src/io/github/norbipeti/chat/server/page/LoginPage.java +++ b/src/io/github/norbipeti/chat/server/page/LoginPage.java @@ -3,6 +3,7 @@ package io.github.norbipeti.chat.server.page; import java.io.IOException; import java.util.HashMap; +import org.jsoup.nodes.Element; import org.mindrot.jbcrypt.BCrypt; import com.sun.net.httpserver.HttpExchange; @@ -29,8 +30,13 @@ public class LoginPage extends Page { } } if (loginuser == null || !BCrypt.checkpw(post.get("pass"), loginuser.getPassword())) { - IOHelper.SendModifiedPage(200, this, "", "

The E-mail or password is incorrect

", - exchange); + IOHelper.SendModifiedPage(200, this, (doc) -> { + Element errorelement = doc.getElementById("errormsg"); + errorelement.appendElement("p").text("The username or password is invalid."); + errorelement.attr("style", "display: block"); + return doc; // TODO: Automatically redirect on every + // request, load HTML file directly for login + }, exchange); return; } IOHelper.LoginUser(exchange, loginuser); diff --git a/src/io/github/norbipeti/chat/server/page/RegisterPage.java b/src/io/github/norbipeti/chat/server/page/RegisterPage.java index b11c84e..c4b1e85 100644 --- a/src/io/github/norbipeti/chat/server/page/RegisterPage.java +++ b/src/io/github/norbipeti/chat/server/page/RegisterPage.java @@ -18,7 +18,9 @@ public class RegisterPage extends Page { if (post.size() > 0) { String errormsg = CheckValues(post, "name", "email", "pass", "pass2"); if (errormsg.length() > 0) { - IOHelper.SendModifiedPage(200, this, "", errormsg, exchange); + final String msg = errormsg; + IOHelper.SendModifiedPage(200, this, (doc) -> doc.getElementById("errormsg").text(msg).ownerDocument(), + exchange); return; // TODO: Use JavaScript too, for error checks } try (DataProvider provider = new DataProvider()) { @@ -31,7 +33,9 @@ public class RegisterPage extends Page { if (!post.get("pass").equals(post.get("pass2"))) errormsg += "

The passwords don't match

"; if (errormsg.length() > 0) { - IOHelper.SendModifiedPage(200, this, "", errormsg, exchange); + final String msg = errormsg; + IOHelper.SendModifiedPage(200, this, + (doc) -> doc.getElementById("errormsg").text(msg).ownerDocument(), exchange); return; } User user = new User(); diff --git a/src/io/github/norbipeti/chat/server/page/StylePage.java b/src/io/github/norbipeti/chat/server/page/StylePage.java new file mode 100644 index 0000000..5d8a944 --- /dev/null +++ b/src/io/github/norbipeti/chat/server/page/StylePage.java @@ -0,0 +1,33 @@ +package io.github.norbipeti.chat.server.page; + +import java.io.File; +import java.io.IOException; + +import com.sun.net.httpserver.HttpExchange; + +import io.github.norbipeti.chat.server.IOHelper; + +public class StylePage extends Page { + + @Override + public boolean getDo404() { + return false; + } + + @Override + public String GetName() { + return "css/"; + } + + @Override + public void handlePage(HttpExchange exchange) throws IOException { + if (exchange.getRequestURI().getPath().startsWith("/css/")) { + File cssfile = new File("pages", exchange.getRequestURI().getPath()); + if (!cssfile.exists()) + IOHelper.SendResponse(404, "

CSS file not found

", exchange); + else + IOHelper.SendResponse(200, IOHelper.ReadFile(cssfile), exchange); + } else + System.out.println(exchange.getRequestURI().getPath()); + } +}