From d64b6150207ae1d644ee977de0de9fd525506f6d Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Mon, 1 Aug 2016 10:17:21 +0200 Subject: [PATCH] Fixed sending messages and CSS improvements --- main/resources/log4j2.xml | 2 +- pages/css/style.css | 99 +++++++++------- .../norbipeti/chat/server/IOHelper.java | 2 +- src/io/github/norbipeti/chat/server/Main.java | 15 +-- .../norbipeti/chat/server/page/IndexPage.java | 7 +- .../chat/server/page/MessageAjaxPage.java | 1 + .../norbipeti/chat/server/page/Page.java | 110 +++++++++--------- 7 files changed, 126 insertions(+), 110 deletions(-) diff --git a/main/resources/log4j2.xml b/main/resources/log4j2.xml index 9467053..8d278b7 100644 --- a/main/resources/log4j2.xml +++ b/main/resources/log4j2.xml @@ -10,7 +10,7 @@ - + diff --git a/pages/css/style.css b/pages/css/style.css index 0fca267..f32557e 100644 --- a/pages/css/style.css +++ b/pages/css/style.css @@ -1,44 +1,57 @@ -body { - background-color: #EEE; -} - -#errormsg, #successmsg { - display: none; -} - -#errormsg { - background-color: #8c0000; - border-color: #ff0000; - border-style: solid; - border-width: 2px; - color: #ff0000; -} - -#successmsg { - background-color: #188c00; - border-color: #00ff00; - border-style: solid; - border-width: 2px; - color: #00ff00; -} - -#sidebar { - float: right; - width: 300px; -} - -#loginregisterbox { - margin: auto; - width: 300px; -} - -#usercontent { - margin: auto; - width: 75%; -} - -#msginput { - width: 100%; - height: 30px; - max-width: 100%; +body { + background-color: #EEE; +} + +#errormsg, #successmsg { + display: none; +} + +#errormsg { + background-color: #8c0000; + border-color: #ff0000; + border-style: solid; + border-width: 2px; + color: #ff0000; +} + +#successmsg { + background-color: #188c00; + border-color: #00ff00; + border-style: solid; + border-width: 2px; + color: #00ff00; +} + +#sidebar { + float: right; + width: 300px; +} + +#loginregisterbox { + margin: auto; + width: 300px; +} + +#usercontent { + margin: auto; + width: 75%; + height: 100%; +} + +#msginput { + width: 100%; + height: 30px; + max-width: 100%; +} + +#channelmessages { + width: 100%; + height: -moz-calc(100% - 50px); + height: -webkit-calc(100% - 50px); + height: calc(100% - 50px); +} + +html, body { + height: 100%; + margin: 0; } \ No newline at end of file diff --git a/src/io/github/norbipeti/chat/server/IOHelper.java b/src/io/github/norbipeti/chat/server/IOHelper.java index d4c1445..736d0d4 100644 --- a/src/io/github/norbipeti/chat/server/IOHelper.java +++ b/src/io/github/norbipeti/chat/server/IOHelper.java @@ -81,7 +81,7 @@ public class IOHelper { if (exchange.getRequestBody().available() == 0) return null; try { - String content = IOUtils.toString(exchange.getRequestBody(), StandardCharsets.ISO_8859_1); + String content = IOUtils.toString(exchange.getRequestBody(), StandardCharsets.ISO_8859_1).trim(); JSONObject obj = new JSONObject(content); return obj; } catch (Exception e) { diff --git a/src/io/github/norbipeti/chat/server/Main.java b/src/io/github/norbipeti/chat/server/Main.java index 716e70d..aff36e4 100644 --- a/src/io/github/norbipeti/chat/server/Main.java +++ b/src/io/github/norbipeti/chat/server/Main.java @@ -29,13 +29,14 @@ public class Main { try { // rt.jar Javadoc: // https://docs.oracle.com/javase/8/docs/jre/api/net/httpserver/spec/ // https://docs.oracle.com/javase/8/docs/api/ - LogManager.getLogger().log(Level.DEBUG, "Loading database..."); + LogManager.getLogger().log(Level.INFO, "Loading database..."); try (DataProvider provider = new DataProvider()) { User user = new User(); user.setName("asd"); user.setEmail("test@test.com"); User user2 = new User(); - user2.setName("Teszt"); //TODO: http://www.journaldev.com/3524/spring-hibernate-integration-example-tutorial + user2.setName("Teszt"); // TODO: + // http://www.journaldev.com/3524/spring-hibernate-integration-example-tutorial user2.setEmail("test2@test.com"); user2.getContacts().add(user); provider.save(user); @@ -69,14 +70,14 @@ public class Main { provider.save(user2); User loggedinuser = new User(); loggedinuser.setName("NorbiPeti"); - loggedinuser.setSessionid("2ed6e2cd-33ad-416e-92c2-7365510b8b31"); + loggedinuser.setSessionid("8b148304-5dd6-48dd-a1a3-c8e47bcfc44b"); loggedinuser.setEmail("sznp@asd.com"); convo.getUsers().add(loggedinuser); loggedinuser.getConversations().add(convo); provider.save(loggedinuser); provider.save(convo); } - LogManager.getLogger().log(Level.DEBUG, "Starting webserver..."); + LogManager.getLogger().log(Level.INFO, "Starting webserver..."); HttpServer server = HttpServer.create(new InetSocketAddress(InetAddress.getLocalHost(), 8080), 10); Reflections rf = new Reflections( new ConfigurationBuilder().setUrls(ClasspathHelper.forClassLoader(Page.class.getClassLoader())) @@ -96,14 +97,14 @@ public class Main { } } server.start(); - LogManager.getLogger().log(Level.DEBUG, "Ready... Press Enter to stop."); + LogManager.getLogger().log(Level.INFO, "Ready... Press Enter to stop."); System.in.read(); - LogManager.getLogger().log(Level.DEBUG, "Stopping..."); + LogManager.getLogger().log(Level.INFO, "Stopping..."); server.stop(1); } catch (Exception e) { e.printStackTrace(); } - LogManager.getLogger().log(Level.DEBUG, "Stopped"); + LogManager.getLogger().log(Level.INFO, "Stopped"); } private static void addPage(HttpServer server, Page page) { diff --git a/src/io/github/norbipeti/chat/server/page/IndexPage.java b/src/io/github/norbipeti/chat/server/page/IndexPage.java index 37822bc..897d32c 100644 --- a/src/io/github/norbipeti/chat/server/page/IndexPage.java +++ b/src/io/github/norbipeti/chat/server/page/IndexPage.java @@ -36,9 +36,10 @@ public class IndexPage extends Page { userbox.html(userbox.html().replace("", user.getName())); Element channelmessages = doc.getElementById("channelmessages"); try (DataProvider provider = new DataProvider()) { - LogManager.getLogger().log(Level.DEBUG, "Conversations: " + provider.getConversations().size()); - Conversation convo = provider.getConversations().get(0); - LogManager.getLogger().log(Level.DEBUG, "Messages: " + convo.getMesssages().size()); + LogManager.getLogger().log(Level.INFO, "Conversations: " + provider.getConversations().size()); + LogManager.getLogger().log(Level.INFO, "User conversations: " + user.getConversations().size()); + Conversation convo = user.getConversations().iterator().next(); + LogManager.getLogger().log(Level.INFO, "Messages: " + convo.getMesssages().size()); for (Message message : convo.getMesssages()) { Element msgelement = channelmessages.appendElement("div"); Element header = msgelement.appendElement("p"); diff --git a/src/io/github/norbipeti/chat/server/page/MessageAjaxPage.java b/src/io/github/norbipeti/chat/server/page/MessageAjaxPage.java index 224fcdc..708c9a2 100644 --- a/src/io/github/norbipeti/chat/server/page/MessageAjaxPage.java +++ b/src/io/github/norbipeti/chat/server/page/MessageAjaxPage.java @@ -60,6 +60,7 @@ public class MessageAjaxPage extends Page { msg.setSender(user); msg.setMessage(message); msg.setTime(new Date()); + msg.setConversation(conv); provider.save(msg); conv.getMesssages().add(msg); provider.save(conv); diff --git a/src/io/github/norbipeti/chat/server/page/Page.java b/src/io/github/norbipeti/chat/server/page/Page.java index fd22539..a2489d8 100644 --- a/src/io/github/norbipeti/chat/server/page/Page.java +++ b/src/io/github/norbipeti/chat/server/page/Page.java @@ -1,55 +1,55 @@ -package io.github.norbipeti.chat.server.page; - -import java.io.IOException; -import java.io.PrintStream; -import java.nio.charset.StandardCharsets; - -import org.apache.commons.io.output.ByteArrayOutputStream; - -import com.sun.net.httpserver.*; - -import io.github.norbipeti.chat.server.IOHelper; -import io.github.norbipeti.chat.server.Main; - -/** - * Add to {@link Main}.Pages - * - */ -public abstract class Page implements HttpHandler { - public abstract String GetName(); - - public final String GetHTMLPath() { - if (GetName().length() == 0) - return "pages/index.html"; - return new StringBuilder("pages/").append(GetName()).append(".html").toString(); - } - - @Override - public void handle(HttpExchange exchange) { - try { - if (!getDo404() || exchange.getRequestURI().getPath().equals("/" + GetName())) - handlePage(exchange); - else { - IOHelper.SendPage(404, NotFoundPage.Instance, exchange); - } - } catch (Exception e) { - e.printStackTrace(); - try { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - PrintStream str = new PrintStream(baos); - str.print("

500 Internal Server Error

");
-				e.printStackTrace(str);
-				str.print("
"); - IOHelper.SendResponse(500, baos.toString(StandardCharsets.ISO_8859_1), exchange); - } catch (Exception e1) { - e1.printStackTrace(); - } - } - } - - public abstract void handlePage(HttpExchange exchange) throws IOException; - - public boolean getDo404() { - return true; - } -} +package io.github.norbipeti.chat.server.page; + +import java.io.IOException; +import java.io.PrintStream; +import java.nio.charset.StandardCharsets; + +import org.apache.commons.io.output.ByteArrayOutputStream; + +import com.sun.net.httpserver.*; + +import io.github.norbipeti.chat.server.IOHelper; +import io.github.norbipeti.chat.server.Main; + +/** + * Add to {@link Main}.Pages + * + */ +public abstract class Page implements HttpHandler { + public abstract String GetName(); + + public final String GetHTMLPath() { + if (GetName().length() == 0) + return "pages/index.html"; + return new StringBuilder("pages/").append(GetName()).append(".html").toString(); + } + + @Override + public void handle(HttpExchange exchange) { + try { + if (!getDo404() || exchange.getRequestURI().getPath().equals("/" + GetName())) + handlePage(exchange); + else { + IOHelper.SendPage(404, NotFoundPage.Instance, exchange); + } + } catch (Exception e) { + e.printStackTrace(); + try { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream str = new PrintStream(baos); + str.print("

500 Internal Server Error

");
+				e.printStackTrace(str);
+				str.print("
"); + IOHelper.SendResponse(500, baos.toString(StandardCharsets.ISO_8859_1), exchange); + } catch (Exception e1) { + e1.printStackTrace(); //TODO: Message listener JS + } + } + } + + public abstract void handlePage(HttpExchange exchange) throws IOException; + + public boolean getDo404() { + return true; + } +}