Fixed sending messages and CSS improvements

This commit is contained in:
Norbi Peti 2016-08-01 10:17:21 +02:00
parent 8062fbee9e
commit d64b615020
7 changed files with 126 additions and 110 deletions

View file

@ -10,7 +10,7 @@
</File>
</Appenders>
<Loggers>
<Root level="debug">
<Root level="info">
<AppenderRef ref="Console" />
<AppenderRef ref="MyFile" />
</Root>

View file

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

View file

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

View file

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

View file

@ -36,9 +36,10 @@ public class IndexPage extends Page {
userbox.html(userbox.html().replace("<username />", 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");

View file

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

View file

@ -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("<h1>500 Internal Server Error</h1><pre>");
e.printStackTrace(str);
str.print("</pre>");
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("<h1>500 Internal Server Error</h1><pre>");
e.printStackTrace(str);
str.print("</pre>");
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;
}
}