Fixed date format and introduced an encoding error

This commit is contained in:
Norbi Peti 2016-08-10 14:05:30 +02:00
parent f5deb2243c
commit cc6fdf38ce
14 changed files with 11991 additions and 27 deletions

View file

@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding//pages/js/moment-with-locales.js=UTF-8

View file

@ -1,12 +1,13 @@
<!DOCTYPE html> <!DOCTYPE html>
<head> <head>
<title>ChatServer</title> <title>ChatServer</title>
<meta charset="UTF-8"/>
<script src="js/jquery-3.1.0.js"></script> <script src="js/jquery-3.1.0.js"></script>
<script src="js/moment-with-locales.js" charset="UTF-8"></script>
<script src="js/utils.js"></script> <script src="js/utils.js"></script>
<script src="js/message.js"></script> <script src="js/message.js"></script>
<script src="js/login.js"></script> <script src="js/login.js"></script>
<script src="js/register.js"></script> <script src="js/register.js"></script>
<meta charset="UTF-8"/>
<link rel="stylesheet" href="css/style.css"/> <link rel="stylesheet" href="css/style.css"/>
<script src="js/index.js"></script> <script src="js/index.js"></script>
</head> </head>

View file

@ -5,10 +5,19 @@
$(document).ready(function () { $(document).ready(function () {
var cmsgs = document.getElementById("channelmessages"); var cmsgs = document.getElementById("channelmessages");
if (cmsgs != null && cmsgs.childElementCount > 0) { if (cmsgs != null && cmsgs.childElementCount > 0) {
cmsgs.forEach(function (item) { var nodes = cmsgs.children;
var ctime = item.getElementById("converttime"); for (var x = 0; x < nodes.length; x++) {
ctime.innerText = new Date(ctime.innerText * 1).toDateString(); var item = nodes[x];
}); console.log(item);
var spans = item.getElementsByTagName("span");
var ctime = null;
for (var i = 0; i < spans.length; i++)
if (spans[i].className.split(' ').indexOf("converttime") > -1)
ctime = spans[i];
if (ctime != null)
ctime.innerText = moment(ctime.innerText).format("lll");
//ctime.innerText = new Date(ctime.innerText * 1).toDateString();
}
cmsgs.lastElementChild.scrollIntoView(false); cmsgs.lastElementChild.scrollIntoView(false);
} }
}); });

View file

@ -9,8 +9,9 @@ var sendmsg = function sendmsg(msginputta) {
var respfunc = function respfunc(result) { var respfunc = function respfunc(result) {
if (result != "Success") { //on success result is string if (result != "Success") { //on success result is string
var msginput = document.getElementById("msginput"); var msginput = document.getElementById("msginput");
if (result.responseText == "JSONERROR") { if (result.responseText.indexOf("JSONERROR") != -1) {
console.log("Got JSON error. Retrying..."); console.log("Got JSON error. Retrying...");
console.log(result.responseText);
sendmsg(msginput); sendmsg(msginput);
} }
else { else {

File diff suppressed because it is too large Load diff

View file

@ -14,7 +14,6 @@ import org.reflections.util.ConfigurationBuilder;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.sun.net.httpserver.HttpServer; import com.sun.net.httpserver.HttpServer;
import io.github.norbipeti.chat.server.data.*; import io.github.norbipeti.chat.server.data.*;
import io.github.norbipeti.chat.server.db.domain.*; import io.github.norbipeti.chat.server.db.domain.*;

View file

@ -1,6 +1,5 @@
package io.github.norbipeti.chat.server.data; package io.github.norbipeti.chat.server.data;
import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;

View file

@ -1,9 +1,6 @@
package io.github.norbipeti.chat.server.data; package io.github.norbipeti.chat.server.data;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.List; import java.util.List;
import com.google.gson.Gson; import com.google.gson.Gson;

View file

@ -36,7 +36,7 @@ public class LoaderRefSerializer extends TypeAdapter<LoaderRef<?>> {
in.nextName(); in.nextName();
long id = in.nextLong(); long id = in.nextLong();
if (!in.nextName().equals("class")) { if (!in.nextName().equals("class")) {
new Exception("Error: Next isn't \"class\"").printStackTrace(); // TODO: Same as at LoaderCollectionSerializer new Exception("Error: Next isn't \"class\"").printStackTrace();
return null; return null;
} }
Class<? extends SavedData> cl; Class<? extends SavedData> cl;
@ -48,12 +48,11 @@ public class LoaderRefSerializer extends TypeAdapter<LoaderRef<?>> {
} }
LoaderRef<? extends SavedData> ref; LoaderRef<? extends SavedData> ref;
try { try {
ref = LoaderRef.class.getDeclaredConstructor(Class.class).newInstance(cl); ref = LoaderRef.class.getDeclaredConstructor(Class.class, Long.class).newInstance(cl, id);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return null; return null;
} }
ref.id = id;
in.endObject(); in.endObject();
return ref; return ref;
} }

View file

@ -3,7 +3,6 @@ package io.github.norbipeti.chat.server.io;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -15,6 +14,7 @@ import java.util.Map.Entry;
import java.util.UUID; import java.util.UUID;
import java.util.function.Function; import java.util.function.Function;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
@ -62,8 +62,8 @@ public class IOHelper {
} }
public static String ReadFile(File file) throws FileNotFoundException, IOException { public static String ReadFile(File file) throws FileNotFoundException, IOException {
FileInputStream inputStream = new FileInputStream(file); String content = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
String content = IOUtils.toString(inputStream, StandardCharsets.UTF_8); LogManager.getLogger().debug(content); // TODO: FIx UTF-8 file reading
return content; return content;
} }

View file

@ -1,6 +1,8 @@
package io.github.norbipeti.chat.server.page; package io.github.norbipeti.chat.server.page;
import java.io.IOException; import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
@ -61,8 +63,10 @@ public class IndexPage extends Page {
for (Message message : chunk.getMessages()) { for (Message message : chunk.getMessages()) {
Element msgelement = channelmessages.appendElement("div"); Element msgelement = channelmessages.appendElement("div");
Element header = msgelement.appendElement("p"); Element header = msgelement.appendElement("p");
header.text(message.getSender().get().getName() + " - <span id=\"converttime\">" header.text(message.getSender().get().getName() + " - ");
+ message.getTime().getTime() + "</span>"); SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
isoFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
header.appendElement("span").addClass("converttime").text(isoFormat.format(message.getTime()));
Element body = msgelement.appendElement("p"); Element body = msgelement.appendElement("p");
body.text(message.getMessage()); // TODO: Use JavaScript to convert time body.text(message.getMessage()); // TODO: Use JavaScript to convert time
} }

View file

@ -13,7 +13,6 @@ import io.github.norbipeti.chat.server.io.IOHelper;
/** /**
* Add to {@link Main}.Pages * Add to {@link Main}.Pages
*
*/ */
public abstract class Page implements HttpHandler { public abstract class Page implements HttpHandler {
public abstract String GetName(); public abstract String GetName();
@ -42,7 +41,7 @@ public abstract class Page implements HttpHandler {
str.print("</pre>"); str.print("</pre>");
IOHelper.SendResponse(500, baos.toString(StandardCharsets.ISO_8859_1), exchange); IOHelper.SendResponse(500, baos.toString(StandardCharsets.ISO_8859_1), exchange);
} catch (Exception e1) { } catch (Exception e1) {
e1.printStackTrace(); //TODO: Message listener JS e1.printStackTrace(); // TODO: Message listener JS
} }
} }
} }

View file

@ -17,10 +17,10 @@ import io.github.norbipeti.chat.server.db.domain.User;
import io.github.norbipeti.chat.server.io.IOHelper; import io.github.norbipeti.chat.server.io.IOHelper;
public class ReceiveMessageAjaxPage extends Page { public class ReceiveMessageAjaxPage extends Page {
// http://stackoverflow.com/questions/9242404/javascript-listen-to-server
@Override @Override
public String GetName() { public String GetName() {
return "receivemessage"; // TODO: Update cookie every once in a while return "receivemessage";
} }
@Override @Override
@ -32,10 +32,7 @@ public class ReceiveMessageAjaxPage extends Page {
} }
JsonObject obj = IOHelper.GetPOSTJSON(exchange); JsonObject obj = IOHelper.GetPOSTJSON(exchange);
if (obj == null) { if (obj == null) {
/* IOHelper.SendResponse(400, "JSONERROR: " + IOHelper.GetPOST(exchange), exchange);
* IOHelper.SendResponse(400, "<h1>400 Bad request</h1><p>Not a JSON string!</p><p>" + IOHelper.GetPOST(exchange) + "</p>", exchange);
*/
IOHelper.SendResponse(400, "JSONERROR", exchange);
return; return;
} }
if (!obj.has("message") || !obj.has("conversation")) { if (!obj.has("message") || !obj.has("conversation")) {

View file

@ -29,7 +29,10 @@ public class ScriptsPage extends Page {
if (!jsfile.exists()) if (!jsfile.exists())
IOHelper.SendResponse(404, "<h1>JavaScript file not found</h1>", exchange); IOHelper.SendResponse(404, "<h1>JavaScript file not found</h1>", exchange);
else else
{
exchange.getResponseHeaders().add("Content-Type", "application/javascript; charset=UTF-8");
IOHelper.SendResponse(200, IOHelper.ReadFile(jsfile), exchange); IOHelper.SendResponse(200, IOHelper.ReadFile(jsfile), exchange);
}
} else } else
LogManager.getLogger().log(Level.DEBUG, exchange.getRequestURI().getPath()); LogManager.getLogger().log(Level.DEBUG, exchange.getRequestURI().getPath());
} }