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> <!DOCTYPE html>
<head> <head>
<title>ChatServer</title> <title>ChatServer</title>
<script src="js/jquery-3.1.0.js"></script> <script src="js/jquery-3.1.0.js"></script>
<script src="js/message.js"></script> <script src="js/message.js"></script>
<link rel="stylesheet" href="css/style.css" />
</head> </head>
<html> <body>
<h1>Index</h1> <h1>Index</h1>
<p>Hello</p> <div id="loginbox">Loginbox</div>
<div id="userbox">Userbox</div>
<input id="msginput" value="" /> <input id="msginput" value="" />
</html> </body>

View file

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

View file

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

View file

@ -46,6 +46,12 @@
<version>0.9.10</version> <version>0.9.10</version>
<scope>runtime</scope> <scope>runtime</scope>
</dependency> </dependency>
<dependency>
<!-- jsoup HTML parser library @ http://jsoup.org/ -->
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.9.2</version>
</dependency>
</dependencies> </dependencies>
<properties> <properties>
<maven.compiler.source>1.8</maven.compiler.source> <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.List;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.UUID; import java.util.UUID;
import java.util.function.Function;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import com.sun.net.httpserver.HttpExchange; 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.db.domain.User;
import io.github.norbipeti.chat.server.page.Page; 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) public static boolean SendModifiedPage(int code, Page page, Function<Document, Document> modifyfunc,
throws IOException { HttpExchange exchange) throws IOException {
String content = GetPage(page, exchange); String content = GetPage(page, exchange);
if (content == null) if (content == null)
return false; 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; return true;
} }
@ -105,12 +112,27 @@ public class IOHelper {
return new HashMap<>(); return new HashMap<>();
HashMap<String, String> map = new HashMap<>(); // TODO HashMap<String, String> map = new HashMap<>(); // TODO
for (String cheader : exchange.getRequestHeaders().get("Cookie")) { for (String cheader : exchange.getRequestHeaders().get("Cookie")) {
String[] spl = cheader.split("\\;"); String[] spl = cheader.split("\\;\\S");
for (String s : spl) { for (String s : spl) {
String[] kv = s.split("\\="); String[] kv = s.split("\\=");
if (kv.length < 2) if (kv.length < 2)
continue; 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(); em.close();
} }
public User getUser(Long id) {
EntityManager em = emf.createEntityManager();
User managedUser = em.find(User.class, id);
em.close();
return managedUser;
}
@Override @Override
public void close() { public void close() {
if (emf != null) if (emf != null)

View file

@ -1,18 +1,22 @@
package io.github.norbipeti.chat.server.page; package io.github.norbipeti.chat.server.page;
import java.io.IOException; import java.io.IOException;
import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpExchange;
import io.github.norbipeti.chat.server.IOHelper; import io.github.norbipeti.chat.server.IOHelper;
import io.github.norbipeti.chat.server.db.domain.User;
public class IndexPage extends Page { public class IndexPage extends Page {
@Override @Override
public void handlePage(HttpExchange exchange) throws IOException { public void handlePage(HttpExchange exchange) throws IOException { //TODO: Make a base HTML and insert all pages into that
if () User user = IOHelper.GetLoggedInUser(exchange);
System.out.println(exchange.getRequestHeaders().get("Cookie")); if (user == null)
IOHelper.SendPage(200, this, exchange); 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 @Override

View file

@ -3,6 +3,7 @@ package io.github.norbipeti.chat.server.page;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import org.jsoup.nodes.Element;
import org.mindrot.jbcrypt.BCrypt; import org.mindrot.jbcrypt.BCrypt;
import com.sun.net.httpserver.HttpExchange; 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())) { if (loginuser == null || !BCrypt.checkpw(post.get("pass"), loginuser.getPassword())) {
IOHelper.SendModifiedPage(200, this, "<errormsg />", "<p>The E-mail or password is incorrect</p>", IOHelper.SendModifiedPage(200, this, (doc) -> {
exchange); 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; return;
} }
IOHelper.LoginUser(exchange, loginuser); IOHelper.LoginUser(exchange, loginuser);

View file

@ -18,7 +18,9 @@ public class RegisterPage extends Page {
if (post.size() > 0) { if (post.size() > 0) {
String errormsg = CheckValues(post, "name", "email", "pass", "pass2"); String errormsg = CheckValues(post, "name", "email", "pass", "pass2");
if (errormsg.length() > 0) { 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 return; // TODO: Use JavaScript too, for error checks
} }
try (DataProvider provider = new DataProvider()) { try (DataProvider provider = new DataProvider()) {
@ -31,7 +33,9 @@ public class RegisterPage extends Page {
if (!post.get("pass").equals(post.get("pass2"))) if (!post.get("pass").equals(post.get("pass2")))
errormsg += "<p>The passwords don't match</p>"; errormsg += "<p>The passwords don't match</p>";
if (errormsg.length() > 0) { 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; return;
} }
User user = new User(); 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());
}
}