From 0308228e4aa4b990019d0d8bb3b9b33a2a9785f0 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Wed, 27 Jul 2016 10:12:16 +0200 Subject: [PATCH] Added JSON communication and proper error display --- pages/.idea/workspace.xml | 181 ++++++++++++------ pages/css/style.css | 2 +- pages/index.html | 6 +- pages/js/login.js | 29 ++- pom.xml | 6 + .../norbipeti/chat/server/IOHelper.java | 17 +- .../norbipeti/chat/server/page/LoginPage.java | 12 +- 7 files changed, 183 insertions(+), 70 deletions(-) diff --git a/pages/.idea/workspace.xml b/pages/.idea/workspace.xml index 80ddb89..a357d56 100644 --- a/pages/.idea/workspace.xml +++ b/pages/.idea/workspace.xml @@ -2,16 +2,10 @@ - - - - + - - - - + @@ -35,25 +29,47 @@ - - + + - + + + + + + + + + + + + + - - + + - + + + + + + + + + + + @@ -74,7 +90,7 @@ - + @@ -103,6 +119,7 @@ @@ -316,15 +347,16 @@ 1469522327377 + - - + @@ -362,17 +394,19 @@ - - + + - + - - - + + + + + @@ -394,20 +428,42 @@ - - + + - + - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -428,32 +484,7 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -462,5 +493,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pages/css/style.css b/pages/css/style.css index e713f1b..564c1b1 100644 --- a/pages/css/style.css +++ b/pages/css/style.css @@ -1,5 +1,5 @@ body { - background-color: #8888; + background-color: #EEE; } #errormsg, #successmsg { diff --git a/pages/index.html b/pages/index.html index 6b0113f..f3b1e4e 100644 --- a/pages/index.html +++ b/pages/index.html @@ -12,11 +12,13 @@ Logged in as +
+
-
+ E-mail: Password: - +
diff --git a/pages/js/login.js b/pages/js/login.js index 746f451..29885a1 100644 --- a/pages/js/login.js +++ b/pages/js/login.js @@ -1,3 +1,26 @@ -/** - * Created by Norbert_Szatmari on 2016-07-26. - */ +/** + * Created by Norbert_Szatmari on 2016-07-26. + */ +function getFormData($form) { + var unindexed_array = $form.serializeArray(); + var indexed_array = {}; + + $.map(unindexed_array, function (n, i) { + indexed_array[n['name']] = n['value']; + }); + + return indexed_array; +} + +function check(form) { + var json = JSON.stringify(getFormData($(form))); + $.ajax({ + url: "/login", data: json, method: "POST", success: function (result) { + if (result != "Success") { + var errormsg = document.getElementById("errormsg"); + errormsg.innerHTML = result; + errormsg.style = "display: block"; + } + } + }); +} diff --git a/pom.xml b/pom.xml index d93e648..fdd526e 100644 --- a/pom.xml +++ b/pom.xml @@ -64,6 +64,12 @@ log4j-api 2.6.2 + + + org.json + json + 20160212 + 1.8 diff --git a/src/io/github/norbipeti/chat/server/IOHelper.java b/src/io/github/norbipeti/chat/server/IOHelper.java index 02ded4e..cdb33be 100644 --- a/src/io/github/norbipeti/chat/server/IOHelper.java +++ b/src/io/github/norbipeti/chat/server/IOHelper.java @@ -16,6 +16,7 @@ import java.util.UUID; import java.util.function.Function; import org.apache.commons.io.IOUtils; +import org.json.JSONObject; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; @@ -75,6 +76,20 @@ public class IOHelper { } } + public static JSONObject GetPOSTJSON(HttpExchange exchange) throws IOException { + if (exchange.getRequestBody().available() == 0) + return null; + try { + String content = IOUtils.toString(exchange.getRequestBody(), StandardCharsets.ISO_8859_1); + HashMap vars = new HashMap<>(); + JSONObject obj = new JSONObject(content); + return obj; + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + public static boolean SendModifiedPage(int code, Page page, Function modifyfunc, HttpExchange exchange) throws IOException { String content = GetPage(page, exchange); @@ -156,7 +171,7 @@ public class IOHelper { return null; } - public static void SendResponse(int code, Page page, Function action, HttpExchange exchange) + public static void SendResponse(int code, Function action, HttpExchange exchange) throws IOException { Document doc = new Document(""); doc = action.apply(doc); diff --git a/src/io/github/norbipeti/chat/server/page/LoginPage.java b/src/io/github/norbipeti/chat/server/page/LoginPage.java index 67cdcdd..e682d69 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.json.JSONObject; import org.jsoup.nodes.Element; import org.mindrot.jbcrypt.BCrypt; @@ -16,8 +17,8 @@ public class LoginPage extends Page { @Override public void handlePage(HttpExchange exchange) throws IOException { - HashMap post = IOHelper.GetPOST(exchange); - if (post.size() == 0 || !post.containsKey("email") || !post.containsKey("pass")) { + JSONObject post = IOHelper.GetPOSTJSON(exchange); + if (post == null || !post.has("email") || !post.has("pass")) { IOHelper.Redirect("/", exchange); return; } @@ -29,12 +30,11 @@ public class LoginPage extends Page { break; } } - if (loginuser == null || !BCrypt.checkpw(post.get("pass"), loginuser.getPassword())) { - IOHelper.SendResponse(200, this, (doc) -> { + if (loginuser == null || !BCrypt.checkpw(post.getString("pass"), loginuser.getPassword())) { + IOHelper.SendResponse(200, (doc) -> { doc.appendElement("p").text("The username or password is invalid."); doc.attr("style", "display: block"); - return doc; // TODO: Automatically redirect on every - // request, load HTML file directly for login + return doc; }, exchange); return; }