void save(T object) {
try {
File file = new File(object.getClass().getName() + "-" + object.getId());
- while (file.exists())
+ while (file.exists()) {
object.setId(object.getId() + 1);
+ file = new File(object.getClass().getName() + "-" + object.getId());
+ }
Files.write(Main.gson.toJson(object), file, StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
diff --git a/src/io/github/norbipeti/chat/server/page/ReceiveMessageAjaxPage.java b/src/io/github/norbipeti/chat/server/page/ReceiveMessageAjaxPage.java
new file mode 100644
index 0000000..4d2fa71
--- /dev/null
+++ b/src/io/github/norbipeti/chat/server/page/ReceiveMessageAjaxPage.java
@@ -0,0 +1,88 @@
+package io.github.norbipeti.chat.server.page;
+
+import java.io.IOException;
+import java.util.Date;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
+import com.google.gson.JsonObject;
+import com.sun.net.httpserver.HttpExchange;
+
+import io.github.norbipeti.chat.server.data.DataManager;
+import io.github.norbipeti.chat.server.data.LoaderCollection;
+import io.github.norbipeti.chat.server.db.domain.Conversation;
+import io.github.norbipeti.chat.server.db.domain.Message;
+import io.github.norbipeti.chat.server.db.domain.MessageChunk;
+import io.github.norbipeti.chat.server.db.domain.User;
+import io.github.norbipeti.chat.server.io.IOHelper;
+
+public class ReceiveMessageAjaxPage extends Page {
+
+ @Override
+ public String GetName() { //TODO
+ return "message"; //TODO: Update cookie every once in a while
+ }
+
+ @Override
+ public void handlePage(HttpExchange exchange) throws IOException {
+ User user = IOHelper.GetLoggedInUser(exchange);
+ if (user == null) {
+ IOHelper.SendResponse(403, "Please log in to send messages
", exchange);
+ return; // TODO: Fix sending messages
+ }
+ JsonObject obj = IOHelper.GetPOSTJSON(exchange);
+ if (obj == null) {
+ /*
+ * IOHelper.SendResponse(400,
+ * "400 Bad request
Not a JSON string!
" +
+ * IOHelper.GetPOST(exchange) + "
", exchange);
+ */
+ IOHelper.SendResponse(400, "JSONERROR", exchange);
+ return;
+ }
+ if (!obj.has("message") || !obj.has("conversation")) {
+ IOHelper.SendResponse(400,
+ "400 Bad request
Message or conversation not found in JSON response.
"
+ + IOHelper.GetPOST(exchange) + "
",
+ exchange);
+ return;
+ }
+ String message = obj.get("message").getAsString().trim();
+ int conversation = obj.get("conversation").getAsInt();
+ if (message.trim().length() == 0) {
+ IOHelper.SendResponse(400, "400 Bad request
The message cannot be empty.
", exchange);
+ return;
+ }
+ LoaderCollection convos = user.getConversations();
+ Conversation conv = null;
+ LogManager.getLogger().log(Level.DEBUG, "Len: " + convos.size());
+ for (Conversation con : convos) {
+ LogManager.getLogger().log(Level.DEBUG, con.getId());
+ if (con.getId() == conversation) {
+ conv = con;
+ break;
+ }
+ }
+ if (conv == null) {
+ IOHelper.SendResponse(400, "400 Conversation not found
The conversation with the id "
+ + conversation + " is not found.
", exchange);
+ return;
+ }
+ MessageChunk chunk = new MessageChunk(); // TODO: Automatize
+ chunk.setConversation(conv);
+ Message msg = new Message();
+ msg.setSender(user);
+ msg.setMessage(message);
+ msg.setTime(new Date());
+ msg.setMessageChunk(chunk); // TODO: Store relations at one side or
+ // both);
+ chunk.getMessages().add(msg);
+ //DataManager.save(chunk); - TODO
+ conv.getMesssageChunks().add(chunk);
+ DataManager.save(conv);
+ LogManager.getLogger().log(Level.DEBUG,
+ "Added conversation's message count: " + conv.getMesssageChunks().size());
+
+ IOHelper.SendResponse(200, "Success", exchange);
+ }
+
+}