Added getUser, CSS, using HTML parsing for modifying pages

This commit is contained in:
Norbi Peti 2016-07-25 12:21:42 +02:00
parent 14592db41f
commit 120f792ddf
11 changed files with 113 additions and 26 deletions

7
pages/css/style.css Normal file
View file

@ -0,0 +1,7 @@
body {
background-color: #8888;
}
.loginbox, .userbox, .errormsg, .successmsg {
display: none;
}

View file

@ -1,11 +1,13 @@
<!DOCTYPE html>
<head>
<title>ChatServer</title>
<script src="js/jquery-3.1.0.js"></script>
<script src="js/message.js"></script>
<title>ChatServer</title>
<script src="js/jquery-3.1.0.js"></script>
<script src="js/message.js"></script>
<link rel="stylesheet" href="css/style.css" />
</head>
<html>
<body>
<h1>Index</h1>
<p>Hello</p>
<div id="loginbox">Loginbox</div>
<div id="userbox">Userbox</div>
<input id="msginput" value="" />
</html>
</body>

View file

@ -1,5 +1,5 @@
<!DOCTYPE html>
<html>
<h1>Login</h1>
<p>Hello</p>
<div id="errormsg"></div>
</html>

View file

@ -1,12 +1,8 @@
<!DOCTYPE html>
<html>
<h1>Register</h1>
<div class="errormsg">
<errormsg />
</div>
<div class="successmsg">
<successmsg />
</div>
<div id="errormsg"></div>
<div id="successmsg"></div>
<table>
<form action="register" method="POST">
<tr>

View file

@ -46,6 +46,12 @@
<version>0.9.10</version>
<scope>runtime</scope>
</dependency>
<dependency>
<!-- jsoup HTML parser library @ http://jsoup.org/ -->
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.9.2</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>

View file

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

View file

@ -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)

View file

@ -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

View file

@ -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, "<errormsg />", "<p>The E-mail or password is incorrect</p>",
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);

View file

@ -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 />", 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 += "<p>The passwords don't match</p>";
if (errormsg.length() > 0) {
IOHelper.SendModifiedPage(200, this, "<errormsg />", errormsg, exchange);
final String msg = errormsg;
IOHelper.SendModifiedPage(200, this,
(doc) -> doc.getElementById("errormsg").text(msg).ownerDocument(), exchange);
return;
}
User user = new User();

View file

@ -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, "<h1>CSS file not found</h1>", exchange);
else
IOHelper.SendResponse(200, IOHelper.ReadFile(cssfile), exchange);
} else
System.out.println(exchange.getRequestURI().getPath());
}
}