Added JSON communication and proper error display

This commit is contained in:
Norbi Peti 2016-07-27 10:12:16 +02:00
parent d619811a34
commit 0308228e4a
7 changed files with 183 additions and 70 deletions

View file

@ -2,16 +2,10 @@
<project version="4">
<component name="ChangeListManager">
<list default="true" id="588ba1ca-220a-4913-8e8c-5657fa1c845f" name="Default" comment="">
<change type="NEW" beforePath="" afterPath="$PROJECT_DIR$/js/login.js" />
<change type="DELETED" beforePath="$PROJECT_DIR$/js/userbox.js" afterPath="" />
<change type="DELETED" beforePath="$PROJECT_DIR$/login.html" afterPath="" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/../.settings/org.eclipse.jdt.core.prefs" afterPath="$PROJECT_DIR$/../.settings/org.eclipse.jdt.core.prefs" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/.idea/workspace.xml" afterPath="$PROJECT_DIR$/.idea/workspace.xml" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/css/style.css" afterPath="$PROJECT_DIR$/css/style.css" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/index.html" afterPath="$PROJECT_DIR$/index.html" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/js/message.js" afterPath="$PROJECT_DIR$/js/message.js" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/../src/io/github/norbipeti/chat/server/IOHelper.java" afterPath="$PROJECT_DIR$/../src/io/github/norbipeti/chat/server/IOHelper.java" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/../src/io/github/norbipeti/chat/server/Main.java" afterPath="$PROJECT_DIR$/../src/io/github/norbipeti/chat/server/Main.java" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/../src/io/github/norbipeti/chat/server/page/LoginPage.java" afterPath="$PROJECT_DIR$/../src/io/github/norbipeti/chat/server/page/LoginPage.java" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/js/login.js" afterPath="$PROJECT_DIR$/js/login.js" />
</list>
<ignored path="pages.iws" />
<ignored path=".idea/workspace.xml" />
@ -35,25 +29,47 @@
<file leaf-file-name="index.html" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/index.html">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="255">
<caret line="15" column="59" selection-start-line="15" selection-start-column="59" selection-end-line="15" selection-end-column="59" />
<state relative-caret-position="238">
<caret line="14" column="19" selection-start-line="14" selection-start-column="19" selection-end-line="14" selection-end-column="19" />
<folding />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="login.js" pinned="false" current-in-tab="false">
<file leaf-file-name="jquery-3.1.0.js" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/js/jquery-3.1.0.js">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="0">
<caret line="0" column="0" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
<folding>
<marker date="1469189245325" expanded="true" signature="26510:26515" placeholder="..." />
</folding>
</state>
</provider>
</entry>
</file>
<file leaf-file-name="login.js" pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/js/login.js">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="34">
<caret line="4" column="26" selection-start-line="4" selection-start-column="26" selection-end-line="4" selection-end-column="26" />
<state relative-caret-position="306">
<caret line="20" column="44" selection-start-line="20" selection-start-column="44" selection-end-line="20" selection-end-column="44" />
<folding>
<element signature="n#!!doc;n#check#0" expanded="false" />
<element signature="n#!!doc;n#getFormData#0" expanded="false" />
</folding>
</state>
</provider>
</entry>
</file>
<file leaf-file-name="style.css" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/css/style.css">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="17">
<caret line="1" column="27" selection-start-line="1" selection-start-column="27" selection-end-line="1" selection-end-column="27" />
<folding />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="register.html" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/register.html">
<provider selected="true" editor-type-id="text-editor">
@ -74,7 +90,7 @@
</provider>
</entry>
</file>
<file leaf-file-name="message.js" pinned="false" current-in-tab="true">
<file leaf-file-name="message.js" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/js/message.js">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="68">
@ -103,6 +119,7 @@
<option value="$PROJECT_DIR$/userbox.html" />
<option value="$PROJECT_DIR$/js/userbox.js" />
<option value="$PROJECT_DIR$/js/message.js" />
<option value="$PROJECT_DIR$/css/style.css" />
<option value="$PROJECT_DIR$/index.html" />
<option value="$PROJECT_DIR$/js/login.js" />
</list>
@ -164,7 +181,6 @@
</navigator>
<panes>
<pane id="Scope" />
<pane id="Scratches" />
<pane id="ProjectPane">
<subPane>
<PATH>
@ -197,8 +213,23 @@
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
</PATH_ELEMENT>
</PATH>
<PATH>
<PATH_ELEMENT>
<option name="myItemId" value="pages" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.ProjectViewProjectNode" />
</PATH_ELEMENT>
<PATH_ELEMENT>
<option name="myItemId" value="pages" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
</PATH_ELEMENT>
<PATH_ELEMENT>
<option name="myItemId" value="css" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode" />
</PATH_ELEMENT>
</PATH>
</subPane>
</pane>
<pane id="Scratches" />
</panes>
</component>
<component name="PropertiesComponent">
@ -316,15 +347,16 @@
<updated>1469522327377</updated>
<workItem from="1469522328775" duration="6392000" />
<workItem from="1469535953166" duration="2597000" />
<workItem from="1469602650261" duration="4137000" />
</task>
<servers />
</component>
<component name="TimeTrackingManager">
<option name="totallyTimeSpent" value="8989000" />
<option name="totallyTimeSpent" value="13126000" />
</component>
<component name="ToolWindowManager">
<frame x="-8" y="-8" width="1936" height="1056" extended-state="6" />
<editor active="false" />
<editor active="true" />
<layout>
<window_info id="Project" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.18020834" sideWeight="0.5" order="1" side_tool="false" content_ui="combo" />
<window_info id="TODO" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="9" side_tool="false" content_ui="tabs" />
@ -362,17 +394,19 @@
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/index.html">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="0">
<caret line="0" column="0" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
<state relative-caret-position="255">
<caret line="15" column="59" selection-start-line="15" selection-start-column="59" selection-end-line="15" selection-end-column="59" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/js/userbox.js">
<entry file="file://$PROJECT_DIR$/js/login.js">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="119">
<caret line="7" column="5" selection-start-line="7" selection-start-column="5" selection-end-line="7" selection-end-column="5" />
<folding />
<state relative-caret-position="34">
<caret line="4" column="26" selection-start-line="4" selection-start-column="26" selection-end-line="4" selection-end-column="26" />
<folding>
<element signature="n#!!doc;n#getFormData#0" expanded="false" />
</folding>
</state>
</provider>
</entry>
@ -394,20 +428,42 @@
</entry>
<entry file="file://$PROJECT_DIR$/js/message.js">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="68">
<caret line="4" column="0" selection-start-line="4" selection-start-column="0" selection-end-line="4" selection-end-column="0" />
<state relative-caret-position="0">
<caret line="0" column="0" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/js/jquery-3.1.0.js">
<entry file="file://$PROJECT_DIR$/index.html">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="0">
<caret line="0" column="0" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
<folding>
<element signature="n#!!doc" expanded="true" />
<marker date="1469189245325" expanded="true" signature="26510:26515" placeholder="..." />
</folding>
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/js/userbox.js" />
<entry file="file://$PROJECT_DIR$/register.html">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="272">
<caret line="16" column="55" selection-start-line="16" selection-start-column="55" selection-end-line="16" selection-end-column="55" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/notfound.html">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="0">
<caret line="0" column="0" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/js/message.js">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="68">
<caret line="4" column="0" selection-start-line="4" selection-start-column="0" selection-end-line="4" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
@ -428,32 +484,7 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/js/userbox.js">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="119">
<caret line="7" column="5" selection-start-line="7" selection-start-column="5" selection-end-line="7" selection-end-column="5" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/index.html">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="255">
<caret line="15" column="59" selection-start-line="15" selection-start-column="59" selection-end-line="15" selection-end-column="59" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/js/login.js">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="34">
<caret line="4" column="26" selection-start-line="4" selection-start-column="26" selection-end-line="4" selection-end-column="26" />
<folding>
<element signature="n#!!doc;n#check#0" expanded="false" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/js/userbox.js" />
<entry file="file://$PROJECT_DIR$/js/message.js">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="68">
@ -462,5 +493,41 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/css/style.css">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="17">
<caret line="1" column="27" selection-start-line="1" selection-start-column="27" selection-end-line="1" selection-end-column="27" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/js/jquery-3.1.0.js">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="0">
<caret line="0" column="0" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
<folding>
<marker date="1469189245325" expanded="true" signature="26510:26515" placeholder="..." />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/index.html">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="238">
<caret line="14" column="19" selection-start-line="14" selection-start-column="19" selection-end-line="14" selection-end-column="19" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/js/login.js">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="306">
<caret line="20" column="44" selection-start-line="20" selection-start-column="44" selection-end-line="20" selection-end-column="44" />
<folding>
<element signature="n#!!doc;n#getFormData#0" expanded="false" />
</folding>
</state>
</provider>
</entry>
</component>
</project>

View file

@ -1,5 +1,5 @@
body {
background-color: #8888;
background-color: #EEE;
}
#errormsg, #successmsg {

View file

@ -12,11 +12,13 @@
Logged in as
<username/>
</div>
<div id="errormsg">
</div>
<div id="loginbox">
<form action="login" method="POST" onsubmit="check(this)">
<form action="login" method="POST">
E-mail: <input type="email" name="email">
Password: <input type="password" name="pass">
<input type="submit" value="Login">
<input type="button" value="Login" onclick="check(this.form)">
</form>
</div>
<input id="msginput" value=""/>

View file

@ -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";
}
}
});
}

View file

@ -64,6 +64,12 @@
<artifactId>log4j-api</artifactId>
<version>2.6.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160212</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>

View file

@ -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<String, String> 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<Document, Document> 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<Document, Document> action, HttpExchange exchange)
public static void SendResponse(int code, Function<Document, Document> action, HttpExchange exchange)
throws IOException {
Document doc = new Document("");
doc = action.apply(doc);

View file

@ -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<String, String> 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;
}