From a4897522174fb48864a4ef7d6276167f9da61f3b Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sat, 31 Oct 2015 01:36:15 +0300 Subject: moved to Gradle --- src/main/java/com/juick/api/Messages.java | 89 +++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 src/main/java/com/juick/api/Messages.java (limited to 'src/main/java/com/juick/api/Messages.java') diff --git a/src/main/java/com/juick/api/Messages.java b/src/main/java/com/juick/api/Messages.java new file mode 100644 index 00000000..b5462258 --- /dev/null +++ b/src/main/java/com/juick/api/Messages.java @@ -0,0 +1,89 @@ +package com.juick.api; + +import com.juick.server.MessagesQueries; +import com.juick.xmpp.JID; +import com.juick.xmpp.Message; +import com.juick.xmpp.Stream; +import com.juick.xmpp.extensions.JuickMessage; +import java.io.IOException; +import java.sql.Connection; +import java.util.ArrayList; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * + * @author ugnich + */ +public class Messages { + + Connection sql; + + public Messages(Connection sql) { + this.sql = sql; + } + + public void doGetHome(HttpServletRequest request, HttpServletResponse response, int vuid) throws ServletException, IOException { + int before_mid = Utils.parseInt(request.getParameter("before_mid"), 0); + + ArrayList mids = MessagesQueries.getMyFeed(sql, vuid, before_mid); + if (mids != null && !mids.isEmpty()) { + ArrayList msgs = MessagesQueries.getMessages(sql, mids); + if (msgs != null && !msgs.isEmpty()) { + String json = com.juick.json.Messages.arrayToString(msgs); + Main.replyJSON(request, response, json); + } else { + response.sendError(404); + } + } else { + response.sendError(404); + } + } + + public void doSetPrivacy(HttpServletRequest request, HttpServletResponse response, Stream xmpp, int vuid) throws ServletException, IOException { + int mid = Utils.parseInt(request.getParameter("mid"), 0); + com.juick.User user = MessagesQueries.getMessageAuthor(sql, mid); + if (user != null && user.UID == vuid && MessagesQueries.setMessagePrivacy(sql, mid)) { + Main.replyJSON(request, response, "{\"status\":\"ok\"}"); + } else { + response.sendError(400); + } + } + + public void doSetPopular(HttpServletRequest request, HttpServletResponse response, Stream xmpp) throws ServletException, IOException { + int mid = Utils.parseInt(request.getParameter("mid"), 0); + int popular = Utils.parseInt(request.getParameter("popular"), 0); + + if (mid > 0) { + boolean ret = MessagesQueries.setMessagePopular(sql, mid, popular); + + if (ret && popular == 2) { + try { + com.juick.Message m = MessagesQueries.getMessage(sql, mid); + if (m != null) { + Message msg = new Message(); + msg.from = new JID("juick", "juick.com", null); + msg.to = new JID(null, "crosspost.juick.com", null); + JuickMessage jmsg = new JuickMessage(m); + jmsg.User.UID = 11574; + msg.childs.add(jmsg); + + msg.to.Username = "twitter"; + xmpp.send(msg); + msg.to.Username = "fb"; + xmpp.send(msg); + msg.to.Username = "vk"; + xmpp.send(msg); + } else { + throw new Exception("Message not found"); + } + } catch (Exception e) { + System.err.println("SETPOPULAR ERROR: " + e.toString()); + } + } + + Main.replyJSON(request, response, "{\"status\":\"ok\"}"); + } + } +} -- cgit v1.2.3 From 0acc3ca041434ca5486e7def8cb75729995106d5 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 3 Nov 2015 16:38:52 +0300 Subject: /messages/recommended --- src/main/java/com/juick/api/Main.java | 6 ++++++ src/main/java/com/juick/api/Messages.java | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) (limited to 'src/main/java/com/juick/api/Messages.java') diff --git a/src/main/java/com/juick/api/Main.java b/src/main/java/com/juick/api/Main.java index 5ee4027b..e10b9078 100644 --- a/src/main/java/com/juick/api/Main.java +++ b/src/main/java/com/juick/api/Main.java @@ -166,6 +166,12 @@ public class Main extends HttpServlet implements Stream.StreamListener { } else { response.sendError(401); } + } else if (uri.equals("/messages/recommended")) { + if (vuid > 0) { + messages.doGetRecommended(request, response, vuid); + } else { + response.sendError(401); + } } else if (uri.equals("/messages/set_popular") && vuid == 1) { messages.doSetPopular(request, response, xmpp); } else if (uri.equals("/messages/set_privacy") && vuid > 0) { diff --git a/src/main/java/com/juick/api/Messages.java b/src/main/java/com/juick/api/Messages.java index b5462258..76615e44 100644 --- a/src/main/java/com/juick/api/Messages.java +++ b/src/main/java/com/juick/api/Messages.java @@ -41,6 +41,23 @@ public class Messages { } } + public void doGetRecommended(HttpServletRequest request, HttpServletResponse response, int vuid) throws ServletException, IOException { + int before_mid = Utils.parseInt(request.getParameter("before_mid"), 0); + + ArrayList mids = MessagesQueries.getRecommended(sql, vuid, before_mid); + if (mids != null && !mids.isEmpty()) { + ArrayList msgs = MessagesQueries.getMessages(sql, mids); + if (msgs != null && !msgs.isEmpty()) { + String json = com.juick.json.Messages.arrayToString(msgs); + Main.replyJSON(request, response, json); + } else { + response.sendError(404); + } + } else { + response.sendError(404); + } + } + public void doSetPrivacy(HttpServletRequest request, HttpServletResponse response, Stream xmpp, int vuid) throws ServletException, IOException { int mid = Utils.parseInt(request.getParameter("mid"), 0); com.juick.User user = MessagesQueries.getMessageAuthor(sql, mid); -- cgit v1.2.3 From 9d7b2473308fe841d84ce8af9efef164c61518e8 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 3 Nov 2015 16:52:58 +0300 Subject: recommended -> UserRecommendations --- src/main/java/com/juick/api/Messages.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main/java/com/juick/api/Messages.java') diff --git a/src/main/java/com/juick/api/Messages.java b/src/main/java/com/juick/api/Messages.java index 76615e44..b7a55361 100644 --- a/src/main/java/com/juick/api/Messages.java +++ b/src/main/java/com/juick/api/Messages.java @@ -44,7 +44,7 @@ public class Messages { public void doGetRecommended(HttpServletRequest request, HttpServletResponse response, int vuid) throws ServletException, IOException { int before_mid = Utils.parseInt(request.getParameter("before_mid"), 0); - ArrayList mids = MessagesQueries.getRecommended(sql, vuid, before_mid); + ArrayList mids = MessagesQueries.getUserRecommendations(sql, vuid, before_mid); if (mids != null && !mids.isEmpty()) { ArrayList msgs = MessagesQueries.getMessages(sql, mids); if (msgs != null && !msgs.isEmpty()) { -- cgit v1.2.3 From 63b14e7818a20d598e4634a0ee686376d7f687b2 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 4 Nov 2015 15:18:41 +0300 Subject: drop gson, refactoring --- src/main/java/com/juick/api/Messages.java | 7 +++++-- src/main/java/com/juick/api/Others.java | 5 ++++- src/main/java/com/juick/api/PM.java | 7 +++++-- src/main/java/com/juick/api/Users.java | 9 ++++++--- src/test/java/com/juick/tests/JsonTests.java | 12 ------------ 5 files changed, 20 insertions(+), 20 deletions(-) (limited to 'src/main/java/com/juick/api/Messages.java') diff --git a/src/main/java/com/juick/api/Messages.java b/src/main/java/com/juick/api/Messages.java index b7a55361..548e816f 100644 --- a/src/main/java/com/juick/api/Messages.java +++ b/src/main/java/com/juick/api/Messages.java @@ -1,5 +1,6 @@ package com.juick.api; +import com.juick.json.MessageSerializer; import com.juick.server.MessagesQueries; import com.juick.xmpp.JID; import com.juick.xmpp.Message; @@ -19,6 +20,8 @@ import javax.servlet.http.HttpServletResponse; public class Messages { Connection sql; + + MessageSerializer messageSerializer = new MessageSerializer(); public Messages(Connection sql) { this.sql = sql; @@ -31,7 +34,7 @@ public class Messages { if (mids != null && !mids.isEmpty()) { ArrayList msgs = MessagesQueries.getMessages(sql, mids); if (msgs != null && !msgs.isEmpty()) { - String json = com.juick.json.Messages.arrayToString(msgs); + String json = messageSerializer.serializeList(msgs); Main.replyJSON(request, response, json); } else { response.sendError(404); @@ -48,7 +51,7 @@ public class Messages { if (mids != null && !mids.isEmpty()) { ArrayList msgs = MessagesQueries.getMessages(sql, mids); if (msgs != null && !msgs.isEmpty()) { - String json = com.juick.json.Messages.arrayToString(msgs); + String json = messageSerializer.serializeList(msgs); Main.replyJSON(request, response, json); } else { response.sendError(404); diff --git a/src/main/java/com/juick/api/Others.java b/src/main/java/com/juick/api/Others.java index 25df5bf4..5e5c7475 100644 --- a/src/main/java/com/juick/api/Others.java +++ b/src/main/java/com/juick/api/Others.java @@ -1,5 +1,6 @@ package com.juick.api; +import com.juick.json.UserSerializer; import com.juick.server.PMQueries; import java.io.IOException; import java.sql.Connection; @@ -15,6 +16,8 @@ import javax.servlet.http.HttpServletResponse; public class Others { Connection sql; + + UserSerializer userSerializer = new UserSerializer(); public Others(Connection sql) { this.sql = sql; @@ -36,7 +39,7 @@ public class Others { ArrayList lastconv = PMQueries.getPMLastConversationsUsers(sql, vuid, cnt); if (lastconv != null && !lastconv.isEmpty()) { - String json = "{\"pms\":" + com.juick.json.Users.arrayToString(lastconv) + "}"; + String json = "{\"pms\":" + userSerializer.serializeList(lastconv) + "}"; Main.replyJSON(request, response, json); } else { response.sendError(404); diff --git a/src/main/java/com/juick/api/PM.java b/src/main/java/com/juick/api/PM.java index 2722526d..ffcd155a 100644 --- a/src/main/java/com/juick/api/PM.java +++ b/src/main/java/com/juick/api/PM.java @@ -1,5 +1,6 @@ package com.juick.api; +import com.juick.json.MessageSerializer; import com.juick.server.PMQueries; import com.juick.server.UserQueries; import com.juick.xmpp.JID; @@ -20,6 +21,8 @@ import javax.servlet.http.HttpServletResponse; public class PM { Connection sql; + + MessageSerializer messageSerializer = new MessageSerializer(); public PM(Connection sql) { this.sql = sql; @@ -39,7 +42,7 @@ public class PM { ArrayList msgs = PMQueries.getPMMessages(sql, vuid, uid); if (msgs != null && !msgs.isEmpty()) { - String json = com.juick.json.Messages.arrayToString(msgs); + String json = messageSerializer.serializeList(msgs); Main.replyJSON(request, response, json); } else { response.sendError(404); @@ -77,7 +80,7 @@ public class PM { msg.to.Host = "ws.juick.com"; xmpp.send(msg); - Main.replyJSON(request, response, com.juick.json.Message.toJSON(jmsg).toString()); + Main.replyJSON(request, response, messageSerializer.serialize(jmsg).toString()); String jid = UserQueries.getJIDbyUID(sql, uid); if (jid != null) { diff --git a/src/main/java/com/juick/api/Users.java b/src/main/java/com/juick/api/Users.java index cdd48fd6..37c9ca49 100644 --- a/src/main/java/com/juick/api/Users.java +++ b/src/main/java/com/juick/api/Users.java @@ -1,6 +1,7 @@ package com.juick.api; import com.juick.User; +import com.juick.json.UserSerializer; import com.juick.server.UserQueries; import java.io.IOException; import java.sql.Connection; @@ -18,6 +19,8 @@ import javax.servlet.http.HttpServletResponse; public class Users { Connection sql; + + UserSerializer userSerializer = new UserSerializer(); public Users(Connection sql) { this.sql = sql; @@ -55,7 +58,7 @@ public class Users { } if (!users.isEmpty()) { - String json = com.juick.json.Users.arrayToString(users); + String json = userSerializer.serializeList(users); Main.replyJSON(request, response, json); } else { response.sendError(404); @@ -81,7 +84,7 @@ public class Users { if (uids.size() > 0) { ArrayList users = UserQueries.getUsersByID(sql, uids); if (users.size() > 0) { - String json = com.juick.json.Users.arrayToString(users); + String json = userSerializer.serializeList(users); Main.replyJSON(request, response, json); return; } @@ -109,7 +112,7 @@ public class Users { if (uids.size() > 0) { ArrayList users = UserQueries.getUsersByID(sql, uids); if (users.size() > 0) { - String json = com.juick.json.Users.arrayToString(users); + String json = userSerializer.serializeList(users); Main.replyJSON(request, response, json); return; } diff --git a/src/test/java/com/juick/tests/JsonTests.java b/src/test/java/com/juick/tests/JsonTests.java index 9a42dea5..f44fac20 100644 --- a/src/test/java/com/juick/tests/JsonTests.java +++ b/src/test/java/com/juick/tests/JsonTests.java @@ -1,23 +1,11 @@ package com.juick.tests; -import com.google.gson.Gson; import static org.junit.Assert.assertEquals; import org.junit.Test; -import com.juick.Message; - public class JsonTests { @Test public void CompareSerializers() { - Message msg = new Message(); - msg.ReplyTo = 1; - msg.MID = 234566; - Gson gson = new Gson(); - String gsonResult = gson.toJson(msg); - String ugnichResult = com.juick.json.Message.toJSON(msg).toString(); - System.out.println(gsonResult); - System.out.println(ugnichResult); - assertEquals("our result must equal to ugnich", ugnichResult, gsonResult); } } \ No newline at end of file -- cgit v1.2.3 From ee86970fcae9878af0adcad00cf5333647b8babf Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sun, 10 Jan 2016 18:54:05 +0300 Subject: refactoring --- src/main/java/com/juick/api/Main.java | 23 ++++++++++------------- src/main/java/com/juick/api/Messages.java | 15 ++++++++------- src/main/java/com/juick/api/Others.java | 10 ++++++---- src/main/java/com/juick/api/PM.java | 3 ++- src/main/java/com/juick/api/Users.java | 23 ++++++++++++----------- src/test/java/com/juick/tests/JsonTests.java | 5 +++-- 6 files changed, 41 insertions(+), 38 deletions(-) (limited to 'src/main/java/com/juick/api/Messages.java') diff --git a/src/main/java/com/juick/api/Main.java b/src/main/java/com/juick/api/Main.java index 07504179..09dd4cbd 100644 --- a/src/main/java/com/juick/api/Main.java +++ b/src/main/java/com/juick/api/Main.java @@ -28,6 +28,7 @@ import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; +import java.util.logging.Logger; import javax.servlet.ServletException; import javax.servlet.annotation.MultipartConfig; import javax.servlet.annotation.WebServlet; @@ -75,18 +76,14 @@ public class Main extends HttpServlet implements Stream.StreamListener { } public void setupXmppComponent(final String password) { - Thread thr = new Thread(new Runnable() { - - @Override - public void run() { - try { - Socket socket = new Socket("localhost", 5347); - xmpp = new StreamComponent(new JID("", "api.juick.com", ""), socket.getInputStream(), socket.getOutputStream(), password); - xmpp.addListener(Main.this); - xmpp.startParsing(); - } catch (IOException e) { - System.err.println(e); - } + Thread thr = new Thread(() -> { + try { + Socket socket = new Socket("localhost", 5347); + xmpp = new StreamComponent(new JID("", "api.juick.com", ""), socket.getInputStream(), socket.getOutputStream(), password); + xmpp.addListener(Main.this); + xmpp.startParsing(); + } catch (IOException e) { + log("XMPP exception", e); } }); thr.start(); @@ -94,7 +91,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { @Override public void onStreamFail(String msg) { - System.err.println("XMPP STREAM FAIL: " + msg); + log("XMPP failed: " + msg); } @Override diff --git a/src/main/java/com/juick/api/Messages.java b/src/main/java/com/juick/api/Messages.java index 548e816f..cde3987f 100644 --- a/src/main/java/com/juick/api/Messages.java +++ b/src/main/java/com/juick/api/Messages.java @@ -6,12 +6,13 @@ import com.juick.xmpp.JID; import com.juick.xmpp.Message; import com.juick.xmpp.Stream; import com.juick.xmpp.extensions.JuickMessage; -import java.io.IOException; -import java.sql.Connection; -import java.util.ArrayList; + import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.sql.Connection; +import java.util.List; /** * @@ -30,9 +31,9 @@ public class Messages { public void doGetHome(HttpServletRequest request, HttpServletResponse response, int vuid) throws ServletException, IOException { int before_mid = Utils.parseInt(request.getParameter("before_mid"), 0); - ArrayList mids = MessagesQueries.getMyFeed(sql, vuid, before_mid); + List mids = MessagesQueries.getMyFeed(sql, vuid, before_mid); if (mids != null && !mids.isEmpty()) { - ArrayList msgs = MessagesQueries.getMessages(sql, mids); + List msgs = MessagesQueries.getMessages(sql, mids); if (msgs != null && !msgs.isEmpty()) { String json = messageSerializer.serializeList(msgs); Main.replyJSON(request, response, json); @@ -47,9 +48,9 @@ public class Messages { public void doGetRecommended(HttpServletRequest request, HttpServletResponse response, int vuid) throws ServletException, IOException { int before_mid = Utils.parseInt(request.getParameter("before_mid"), 0); - ArrayList mids = MessagesQueries.getUserRecommendations(sql, vuid, before_mid); + List mids = MessagesQueries.getUserRecommendations(sql, vuid, before_mid); if (mids != null && !mids.isEmpty()) { - ArrayList msgs = MessagesQueries.getMessages(sql, mids); + List msgs = MessagesQueries.getMessages(sql, mids); if (msgs != null && !msgs.isEmpty()) { String json = messageSerializer.serializeList(msgs); Main.replyJSON(request, response, json); diff --git a/src/main/java/com/juick/api/Others.java b/src/main/java/com/juick/api/Others.java index 5e5c7475..323ed41a 100644 --- a/src/main/java/com/juick/api/Others.java +++ b/src/main/java/com/juick/api/Others.java @@ -1,13 +1,15 @@ package com.juick.api; +import com.juick.User; import com.juick.json.UserSerializer; import com.juick.server.PMQueries; -import java.io.IOException; -import java.sql.Connection; -import java.util.ArrayList; + import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.sql.Connection; +import java.util.List; /** * @@ -37,7 +39,7 @@ public class Others { } catch (Exception e) { } - ArrayList lastconv = PMQueries.getPMLastConversationsUsers(sql, vuid, cnt); + List lastconv = PMQueries.getPMLastConversationsUsers(sql, vuid, cnt); if (lastconv != null && !lastconv.isEmpty()) { String json = "{\"pms\":" + userSerializer.serializeList(lastconv) + "}"; Main.replyJSON(request, response, json); diff --git a/src/main/java/com/juick/api/PM.java b/src/main/java/com/juick/api/PM.java index ffcd155a..f22a4add 100644 --- a/src/main/java/com/juick/api/PM.java +++ b/src/main/java/com/juick/api/PM.java @@ -10,6 +10,7 @@ import com.juick.xmpp.extensions.JuickMessage; import java.io.IOException; import java.sql.Connection; import java.util.ArrayList; +import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -40,7 +41,7 @@ public class PM { return; } - ArrayList msgs = PMQueries.getPMMessages(sql, vuid, uid); + List msgs = PMQueries.getPMMessages(sql, vuid, uid); if (msgs != null && !msgs.isEmpty()) { String json = messageSerializer.serializeList(msgs); Main.replyJSON(request, response, json); diff --git a/src/main/java/com/juick/api/Users.java b/src/main/java/com/juick/api/Users.java index 37c9ca49..5bae4028 100644 --- a/src/main/java/com/juick/api/Users.java +++ b/src/main/java/com/juick/api/Users.java @@ -1,16 +1,17 @@ package com.juick.api; -import com.juick.User; import com.juick.json.UserSerializer; import com.juick.server.UserQueries; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.sql.Connection; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import java.util.List; /** * @@ -27,11 +28,11 @@ public class Users { } public void doGetUsers(HttpServletRequest request, HttpServletResponse response, int vuid) throws ServletException, IOException { - ArrayList users = new ArrayList(); + List users = new ArrayList<>(); String punames[] = request.getParameterValues("uname"); if (punames != null) { - ArrayList unames = new ArrayList(Arrays.asList(punames)); + ArrayList unames = new ArrayList<>(Arrays.asList(punames)); Iterator i = unames.iterator(); while (i.hasNext()) { if (!i.next().matches("^[a-zA-Z0-9\\-]{2,16}$")) { @@ -45,7 +46,7 @@ public class Users { String pjids[] = request.getParameterValues("jid"); if (pjids != null) { - ArrayList jids = new ArrayList(Arrays.asList(pjids)); + List jids = new ArrayList<>(Arrays.asList(pjids)); Iterator ii = jids.iterator(); while (ii.hasNext()) { if (!ii.next().matches("^[a-zA-Z0-9\\-\\_\\@\\.]{6,64}$")) { @@ -80,9 +81,9 @@ public class Users { } if (uid > 0) { - ArrayList uids = UserQueries.getUserRead(sql, uid); + List uids = UserQueries.getUserRead(sql, uid); if (uids.size() > 0) { - ArrayList users = UserQueries.getUsersByID(sql, uids); + List users = UserQueries.getUsersByID(sql, uids); if (users.size() > 0) { String json = userSerializer.serializeList(users); Main.replyJSON(request, response, json); @@ -108,9 +109,9 @@ public class Users { } if (uid > 0) { - ArrayList uids = UserQueries.getUserReaders(sql, uid); + List uids = UserQueries.getUserReaders(sql, uid); if (uids.size() > 0) { - ArrayList users = UserQueries.getUsersByID(sql, uids); + List users = UserQueries.getUsersByID(sql, uids); if (users.size() > 0) { String json = userSerializer.serializeList(users); Main.replyJSON(request, response, json); diff --git a/src/test/java/com/juick/tests/JsonTests.java b/src/test/java/com/juick/tests/JsonTests.java index c99432a6..35854171 100644 --- a/src/test/java/com/juick/tests/JsonTests.java +++ b/src/test/java/com/juick/tests/JsonTests.java @@ -14,6 +14,7 @@ import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.ArrayList; +import java.util.List; import java.util.Properties; public class JsonTests { @@ -40,10 +41,10 @@ public class JsonTests { @Test public void CompareSerializers() { - ArrayList mids = MessagesQueries.getMyFeed(sql, 3694, 2799786); + List mids = MessagesQueries.getMyFeed(sql, 3694, 2799786); MessageSerializer messageSerializer = new MessageSerializer(); if (mids != null && !mids.isEmpty()) { - ArrayList msgs = MessagesQueries.getMessages(sql, mids); + List msgs = MessagesQueries.getMessages(sql, mids); if (msgs != null && !msgs.isEmpty()) { String json = messageSerializer.serializeList(msgs); try { -- cgit v1.2.3 From 690b3ea447a7a2434ecf1d210e5f5ef89ce27f04 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 15 Jan 2016 12:51:21 +0300 Subject: spring-jdbc WIP --- build.gradle | 2 ++ deps/com.juick.server | 2 +- src/main/java/com/juick/api/Main.java | 47 ++++++++++++++----------- src/main/java/com/juick/api/Messages.java | 54 ++++++++++++++++++++++++----- src/main/java/com/juick/api/Others.java | 5 +-- src/main/java/com/juick/api/PM.java | 12 +++---- src/main/java/com/juick/api/Users.java | 6 ++-- src/main/java/com/juick/api/Utils.java | 7 ++-- src/test/java/com/juick/tests/ApiTests.java | 40 +++++++++++++++++++++ 9 files changed, 131 insertions(+), 44 deletions(-) create mode 100644 src/test/java/com/juick/tests/ApiTests.java (limited to 'src/main/java/com/juick/api/Messages.java') diff --git a/build.gradle b/build.gradle index 14e74f02..c2ca747e 100644 --- a/build.gradle +++ b/build.gradle @@ -53,6 +53,8 @@ dependencies { compile server compile xmpp compile json + compile "org.springframework:spring-jdbc:4.2.4.RELEASE" + compile "org.apache.commons:commons-dbcp2:2.1.1" providedCompile 'javax.servlet:javax.servlet-api:3.1.0' def tomcatVersion = '7.0.+' tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}", diff --git a/deps/com.juick.server b/deps/com.juick.server index c37ed56f..9bd8a41c 160000 --- a/deps/com.juick.server +++ b/deps/com.juick.server @@ -1 +1 @@ -Subproject commit c37ed56f884661666b964e47efd23a36be9b24d4 +Subproject commit 9bd8a41c0db5a1027a184facfca8d0152945c078 diff --git a/src/main/java/com/juick/api/Main.java b/src/main/java/com/juick/api/Main.java index 09dd4cbd..71f7f2d7 100644 --- a/src/main/java/com/juick/api/Main.java +++ b/src/main/java/com/juick/api/Main.java @@ -20,6 +20,9 @@ package com.juick.api; import com.juick.xmpp.JID; import com.juick.xmpp.Stream; import com.juick.xmpp.StreamComponent; +import org.apache.commons.dbcp2.BasicDataSource; +import org.springframework.jdbc.core.JdbcTemplate; + import java.io.FileInputStream; import java.io.IOException; import java.io.PrintWriter; @@ -28,7 +31,6 @@ import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; -import java.util.logging.Logger; import javax.servlet.ServletException; import javax.servlet.annotation.MultipartConfig; import javax.servlet.annotation.WebServlet; @@ -46,6 +48,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { Connection sql; Connection sqlSearch; + JdbcTemplate jdbc; Stream xmpp; Messages messages; Users users; @@ -58,19 +61,22 @@ public class Main extends HttpServlet implements Stream.StreamListener { try { Properties conf = new Properties(); conf.load(new FileInputStream("/etc/juick/api.conf")); - - Class.forName("com.mysql.jdbc.Driver"); + final String driverClassName = "com.mysql.jdbc.Driver"; + Class.forName(driverClassName); sql = DriverManager.getConnection("jdbc:mysql://localhost/juick?autoReconnect=true&user=" + conf.getProperty("mysql_username", "") + "&password=" + conf.getProperty("mysql_password", "")); sqlSearch = DriverManager.getConnection("jdbc:mysql://127.0.0.1:9306/juick?autoReconnect=true&characterEncoding=utf8&maxAllowedPacket=512000&relaxAutoCommit=true&user=root&password="); - - messages = new Messages(sql); - users = new Users(sql); - pm = new PM(sql); - others = new Others(sql); + BasicDataSource dataSource = new BasicDataSource(); + dataSource.setUrl(sql.getMetaData().getURL()); + dataSource.setDriverClassName(driverClassName); + jdbc = new JdbcTemplate(dataSource); + messages = new Messages(jdbc); + users = new Users(jdbc); + pm = new PM(jdbc); + others = new Others(jdbc); setupXmppComponent(conf.getProperty("xmpp_password")); - } catch (Exception e) { + } catch (IOException | ClassNotFoundException | SQLException e) { log(null, e); } } @@ -135,7 +141,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { int vuid = Utils.getHttpAuthUID(sql, request); if (vuid == 0) { - vuid = Utils.getVisitorQueryStringUID(sql, request); + vuid = Utils.getVisitorQueryStringUID(jdbc, request); } String uri = request.getRequestURI(); @@ -193,7 +199,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { int vuid = Utils.getHttpAuthUID(sql, request); if (vuid == 0) { - vuid = Utils.getVisitorQueryStringUID(sql, request); + vuid = Utils.getVisitorQueryStringUID(jdbc, request); } if (vuid == 0) { response.sendError(401); @@ -201,11 +207,15 @@ public class Main extends HttpServlet implements Stream.StreamListener { } String uri = request.getRequestURI(); - if (uri.equals("/post")) { - } else if (uri.equals("/pm")) { - pm.doPostPM(request, response, xmpp, vuid); - } else { - response.sendError(405); + switch (uri) { + case "/post": + break; + case "/pm": + pm.doPostPM(request, response, xmpp, vuid); + break; + default: + response.sendError(405); + break; } } @@ -218,8 +228,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { callback = null; } - PrintWriter out = response.getWriter(); - try { + try (PrintWriter out = response.getWriter()) { if (callback != null) { out.print(callback + "("); out.print(json); @@ -227,8 +236,6 @@ public class Main extends HttpServlet implements Stream.StreamListener { } else { out.print(json); } - } finally { - out.close(); } } } diff --git a/src/main/java/com/juick/api/Messages.java b/src/main/java/com/juick/api/Messages.java index cde3987f..e3dcabfd 100644 --- a/src/main/java/com/juick/api/Messages.java +++ b/src/main/java/com/juick/api/Messages.java @@ -1,17 +1,21 @@ package com.juick.api; +import com.juick.Tag; +import com.juick.User; import com.juick.json.MessageSerializer; import com.juick.server.MessagesQueries; +import com.juick.server.TagQueries; +import com.juick.server.UserQueries; import com.juick.xmpp.JID; import com.juick.xmpp.Message; import com.juick.xmpp.Stream; import com.juick.xmpp.extensions.JuickMessage; +import org.springframework.jdbc.core.JdbcTemplate; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; -import java.sql.Connection; import java.util.List; /** @@ -20,11 +24,32 @@ import java.util.List; */ public class Messages { - Connection sql; + JdbcTemplate sql; MessageSerializer messageSerializer = new MessageSerializer(); - public Messages(Connection sql) { + enum MediaType { + ALL("all"), + PHOTO("photo"), + VIDEO("video"), + NONE(""); + + private final String value; + + MediaType(final String value) { + this.value = value; + } + public String getValue() { + return value; + } + + @Override + public String toString() { + return this.getValue(); + } + } + + public Messages(JdbcTemplate sql) { this.sql = sql; } @@ -45,8 +70,19 @@ public class Messages { } } - public void doGetRecommended(HttpServletRequest request, HttpServletResponse response, int vuid) throws ServletException, IOException { + public void doGet(HttpServletRequest request, HttpServletResponse response, int vuid) + throws ServletException, IOException { + User user = UserQueries.getUserByName(sql, request.getParameter("uname")); int before_mid = Utils.parseInt(request.getParameter("before_mid"), 0); + int page = Utils.parseInt(request.getParameter("page"), 0); + MediaType media = MediaType.valueOf(request.getParameter("media")); + boolean popular = Boolean.valueOf(request.getParameter("popular")); + Tag tag = TagQueries.getTag(sql, request.getParameter("tag"), false); + + } + + public void doGetRecommended(HttpServletRequest request, HttpServletResponse response, int vuid) throws ServletException, IOException { + /*int before_mid = Utils.parseInt(request.getParameter("before_mid"), 0); List mids = MessagesQueries.getUserRecommendations(sql, vuid, before_mid); if (mids != null && !mids.isEmpty()) { @@ -59,21 +95,21 @@ public class Messages { } } else { response.sendError(404); - } + }*/ } public void doSetPrivacy(HttpServletRequest request, HttpServletResponse response, Stream xmpp, int vuid) throws ServletException, IOException { - int mid = Utils.parseInt(request.getParameter("mid"), 0); + /*int mid = Utils.parseInt(request.getParameter("mid"), 0); com.juick.User user = MessagesQueries.getMessageAuthor(sql, mid); if (user != null && user.UID == vuid && MessagesQueries.setMessagePrivacy(sql, mid)) { Main.replyJSON(request, response, "{\"status\":\"ok\"}"); } else { response.sendError(400); - } + }*/ } public void doSetPopular(HttpServletRequest request, HttpServletResponse response, Stream xmpp) throws ServletException, IOException { - int mid = Utils.parseInt(request.getParameter("mid"), 0); + /*int mid = Utils.parseInt(request.getParameter("mid"), 0); int popular = Utils.parseInt(request.getParameter("popular"), 0); if (mid > 0) { @@ -105,6 +141,6 @@ public class Messages { } Main.replyJSON(request, response, "{\"status\":\"ok\"}"); - } + }*/ } } diff --git a/src/main/java/com/juick/api/Others.java b/src/main/java/com/juick/api/Others.java index 323ed41a..3c8d4a96 100644 --- a/src/main/java/com/juick/api/Others.java +++ b/src/main/java/com/juick/api/Others.java @@ -3,6 +3,7 @@ package com.juick.api; import com.juick.User; import com.juick.json.UserSerializer; import com.juick.server.PMQueries; +import org.springframework.jdbc.core.JdbcTemplate; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; @@ -17,11 +18,11 @@ import java.util.List; */ public class Others { - Connection sql; + JdbcTemplate sql; UserSerializer userSerializer = new UserSerializer(); - public Others(Connection sql) { + public Others(JdbcTemplate sql) { this.sql = sql; } diff --git a/src/main/java/com/juick/api/PM.java b/src/main/java/com/juick/api/PM.java index f22a4add..01a5669a 100644 --- a/src/main/java/com/juick/api/PM.java +++ b/src/main/java/com/juick/api/PM.java @@ -7,9 +7,9 @@ import com.juick.xmpp.JID; import com.juick.xmpp.Message; import com.juick.xmpp.Stream; import com.juick.xmpp.extensions.JuickMessage; +import org.springframework.jdbc.core.JdbcTemplate; + import java.io.IOException; -import java.sql.Connection; -import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; @@ -21,11 +21,11 @@ import javax.servlet.http.HttpServletResponse; */ public class PM { - Connection sql; - - MessageSerializer messageSerializer = new MessageSerializer(); + JdbcTemplate sql; + + MessageSerializer messageSerializer = new MessageSerializer(); - public PM(Connection sql) { + public PM(JdbcTemplate sql) { this.sql = sql; } diff --git a/src/main/java/com/juick/api/Users.java b/src/main/java/com/juick/api/Users.java index 5bae4028..d3818a5a 100644 --- a/src/main/java/com/juick/api/Users.java +++ b/src/main/java/com/juick/api/Users.java @@ -2,12 +2,12 @@ package com.juick.api; import com.juick.json.UserSerializer; import com.juick.server.UserQueries; +import org.springframework.jdbc.core.JdbcTemplate; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; -import java.sql.Connection; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; @@ -19,11 +19,11 @@ import java.util.List; */ public class Users { - Connection sql; + JdbcTemplate sql; UserSerializer userSerializer = new UserSerializer(); - public Users(Connection sql) { + public Users(JdbcTemplate sql) { this.sql = sql; } diff --git a/src/main/java/com/juick/api/Utils.java b/src/main/java/com/juick/api/Utils.java index 10e0cccb..db73fdaa 100644 --- a/src/main/java/com/juick/api/Utils.java +++ b/src/main/java/com/juick/api/Utils.java @@ -27,6 +27,7 @@ import java.util.ArrayList; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.springframework.jdbc.core.JdbcTemplate; import sun.misc.BASE64Decoder; /** @@ -47,7 +48,7 @@ public class Utils { return null; } - public static com.juick.User getVisitorUser(Connection sql, HttpServletRequest request) { + public static com.juick.User getVisitorUser(JdbcTemplate sql, HttpServletRequest request) { String hash = getCookie(request, "hash"); if (hash != null) { return com.juick.server.UserQueries.getUserByHash(sql, hash); @@ -56,7 +57,7 @@ public class Utils { } } - public static int getVisitorUID(Connection sql, HttpServletRequest request) { + public static int getVisitorUID(JdbcTemplate sql, HttpServletRequest request) { Cookie cookies[] = request.getCookies(); if (cookies != null) { for (int i = 0; i < cookies.length; i++) { @@ -84,7 +85,7 @@ public class Utils { return 0; } - public static int getVisitorQueryStringUID(Connection sql, HttpServletRequest request) { + public static int getVisitorQueryStringUID(JdbcTemplate sql, HttpServletRequest request) { String hash = request.getParameter("hash"); if (hash != null && hash.length() == 16) { return com.juick.server.UserQueries.getUIDbyHash(sql, hash); diff --git a/src/test/java/com/juick/tests/ApiTests.java b/src/test/java/com/juick/tests/ApiTests.java new file mode 100644 index 00000000..01cdd4c5 --- /dev/null +++ b/src/test/java/com/juick/tests/ApiTests.java @@ -0,0 +1,40 @@ +package com.juick.tests; + +import com.juick.User; +import com.juick.server.UserQueries; +import junit.framework.Assert; +import org.apache.commons.dbcp2.BasicDataSource; +import org.junit.Before; +import org.junit.Test; +import org.springframework.jdbc.core.JdbcTemplate; + +import java.io.FileInputStream; +import java.io.IOException; +import java.util.Properties; + +/** + * Created by vt on 14.01.2016. + */ +public class ApiTests { + JdbcTemplate jdbc; + @Before + public void setupConnection() { + Properties conf = new Properties(); + try { + conf.load(new FileInputStream("/etc/juick/api.conf")); + BasicDataSource dataSource = new BasicDataSource(); + dataSource.setDriverClassName("com.mysql.jdbc.Driver"); + dataSource.setUrl("jdbc:mysql://localhost/juick?autoReconnect=true&user=" + + conf.getProperty("mysql_username", "") + + "&password=" + conf.getProperty("mysql_password", "")); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void userTests() { + User user = UserQueries.getUserByUID(jdbc, 3694); + Assert.assertEquals("it should be me", "vt", user.UName); + } +} -- cgit v1.2.3 From f9d5099bf4be816aa267e02b5713a672fd4da562 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 15 Jan 2016 14:49:51 +0300 Subject: spring-jdbc WIP --- deps/com.juick.server | 2 +- src/main/java/com/juick/api/Messages.java | 12 ++++++------ src/test/java/com/juick/tests/ApiTests.java | 9 +++++++++ 3 files changed, 16 insertions(+), 7 deletions(-) (limited to 'src/main/java/com/juick/api/Messages.java') diff --git a/deps/com.juick.server b/deps/com.juick.server index 1ef890d2..2635d618 160000 --- a/deps/com.juick.server +++ b/deps/com.juick.server @@ -1 +1 @@ -Subproject commit 1ef890d21db2ed9aeefc37b71ff82d0f59df8516 +Subproject commit 2635d6187ecd48c732d21e4f510f1df8451ce646 diff --git a/src/main/java/com/juick/api/Messages.java b/src/main/java/com/juick/api/Messages.java index e3dcabfd..fb9be051 100644 --- a/src/main/java/com/juick/api/Messages.java +++ b/src/main/java/com/juick/api/Messages.java @@ -82,7 +82,7 @@ public class Messages { } public void doGetRecommended(HttpServletRequest request, HttpServletResponse response, int vuid) throws ServletException, IOException { - /*int before_mid = Utils.parseInt(request.getParameter("before_mid"), 0); + int before_mid = Utils.parseInt(request.getParameter("before_mid"), 0); List mids = MessagesQueries.getUserRecommendations(sql, vuid, before_mid); if (mids != null && !mids.isEmpty()) { @@ -95,21 +95,21 @@ public class Messages { } } else { response.sendError(404); - }*/ + } } public void doSetPrivacy(HttpServletRequest request, HttpServletResponse response, Stream xmpp, int vuid) throws ServletException, IOException { - /*int mid = Utils.parseInt(request.getParameter("mid"), 0); + int mid = Utils.parseInt(request.getParameter("mid"), 0); com.juick.User user = MessagesQueries.getMessageAuthor(sql, mid); if (user != null && user.UID == vuid && MessagesQueries.setMessagePrivacy(sql, mid)) { Main.replyJSON(request, response, "{\"status\":\"ok\"}"); } else { response.sendError(400); - }*/ + } } public void doSetPopular(HttpServletRequest request, HttpServletResponse response, Stream xmpp) throws ServletException, IOException { - /*int mid = Utils.parseInt(request.getParameter("mid"), 0); + int mid = Utils.parseInt(request.getParameter("mid"), 0); int popular = Utils.parseInt(request.getParameter("popular"), 0); if (mid > 0) { @@ -141,6 +141,6 @@ public class Messages { } Main.replyJSON(request, response, "{\"status\":\"ok\"}"); - }*/ + } } } diff --git a/src/test/java/com/juick/tests/ApiTests.java b/src/test/java/com/juick/tests/ApiTests.java index f111ffc7..e67c23f2 100644 --- a/src/test/java/com/juick/tests/ApiTests.java +++ b/src/test/java/com/juick/tests/ApiTests.java @@ -1,7 +1,9 @@ package com.juick.tests; +import com.juick.Message; import com.juick.User; import com.juick.server.UserQueries; +import com.juick.server.MessagesQueries; import junit.framework.Assert; import org.apache.commons.dbcp2.BasicDataSource; import org.junit.Before; @@ -38,4 +40,11 @@ public class ApiTests { User user = UserQueries.getUserByUID(jdbc, 3694); Assert.assertEquals("it should be me", "vt", user.UName); } + + @Test void messageTests() { + Message msg = MessagesQueries.getMessage(jdbc, 2817722); + User ugnich = msg.User; + Assert.assertEquals("ugnich", ugnich.UName); + Assert.assertEquals("Microsoft", msg.Tags.get(0)); + } } -- cgit v1.2.3 From 30068e18a3d8ae37b509f5cd766515735a818692 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 15 Jan 2016 15:54:04 +0300 Subject: update core --- deps/com.juick | 2 +- deps/com.juick.server | 2 +- src/main/java/com/juick/api/Messages.java | 23 ++++++++++++++++------- src/main/java/com/juick/api/Others.java | 4 +++- src/main/java/com/juick/api/PM.java | 16 ++++++++++------ src/main/java/com/juick/api/Users.java | 20 +++++++++++++------- src/test/java/com/juick/tests/ApiTests.java | 6 +++--- 7 files changed, 47 insertions(+), 26 deletions(-) (limited to 'src/main/java/com/juick/api/Messages.java') diff --git a/deps/com.juick b/deps/com.juick index 5f08b8b1..1df19b47 160000 --- a/deps/com.juick +++ b/deps/com.juick @@ -1 +1 @@ -Subproject commit 5f08b8b1cfaacf1e78c56e7752277533eb84b02e +Subproject commit 1df19b47f7f463e6cafa7dcf877cb42967fff166 diff --git a/deps/com.juick.server b/deps/com.juick.server index 0ac9d76d..f14eea2c 160000 --- a/deps/com.juick.server +++ b/deps/com.juick.server @@ -1 +1 @@ -Subproject commit 0ac9d76d6348f487ada697678395d15abee244c9 +Subproject commit f14eea2cfc80dac5e9b304a57457e7f82842a8ed diff --git a/src/main/java/com/juick/api/Messages.java b/src/main/java/com/juick/api/Messages.java index fb9be051..e242d05b 100644 --- a/src/main/java/com/juick/api/Messages.java +++ b/src/main/java/com/juick/api/Messages.java @@ -53,7 +53,9 @@ public class Messages { this.sql = sql; } - public void doGetHome(HttpServletRequest request, HttpServletResponse response, int vuid) throws ServletException, IOException { + public void doGetHome(HttpServletRequest request, + HttpServletResponse response, int vuid) + throws ServletException, IOException { int before_mid = Utils.parseInt(request.getParameter("before_mid"), 0); List mids = MessagesQueries.getMyFeed(sql, vuid, before_mid); @@ -70,7 +72,8 @@ public class Messages { } } - public void doGet(HttpServletRequest request, HttpServletResponse response, int vuid) + public void doGet(HttpServletRequest request, + HttpServletResponse response, int vuid) throws ServletException, IOException { User user = UserQueries.getUserByName(sql, request.getParameter("uname")); int before_mid = Utils.parseInt(request.getParameter("before_mid"), 0); @@ -81,7 +84,9 @@ public class Messages { } - public void doGetRecommended(HttpServletRequest request, HttpServletResponse response, int vuid) throws ServletException, IOException { + public void doGetRecommended(HttpServletRequest request, + HttpServletResponse response, int vuid) + throws ServletException, IOException { int before_mid = Utils.parseInt(request.getParameter("before_mid"), 0); List mids = MessagesQueries.getUserRecommendations(sql, vuid, before_mid); @@ -98,17 +103,21 @@ public class Messages { } } - public void doSetPrivacy(HttpServletRequest request, HttpServletResponse response, Stream xmpp, int vuid) throws ServletException, IOException { + public void doSetPrivacy(HttpServletRequest request, + HttpServletResponse response, Stream xmpp, int vuid) + throws ServletException, IOException { int mid = Utils.parseInt(request.getParameter("mid"), 0); com.juick.User user = MessagesQueries.getMessageAuthor(sql, mid); - if (user != null && user.UID == vuid && MessagesQueries.setMessagePrivacy(sql, mid)) { + if (user != null && user.getUID() == vuid && MessagesQueries.setMessagePrivacy(sql, mid)) { Main.replyJSON(request, response, "{\"status\":\"ok\"}"); } else { response.sendError(400); } } - public void doSetPopular(HttpServletRequest request, HttpServletResponse response, Stream xmpp) throws ServletException, IOException { + public void doSetPopular(HttpServletRequest request, + HttpServletResponse response, Stream xmpp) + throws ServletException, IOException { int mid = Utils.parseInt(request.getParameter("mid"), 0); int popular = Utils.parseInt(request.getParameter("popular"), 0); @@ -123,7 +132,7 @@ public class Messages { msg.from = new JID("juick", "juick.com", null); msg.to = new JID(null, "crosspost.juick.com", null); JuickMessage jmsg = new JuickMessage(m); - jmsg.User.UID = 11574; + jmsg.setUser(UserQueries.getUserByUID(sql, 11574)); msg.childs.add(jmsg); msg.to.Username = "twitter"; diff --git a/src/main/java/com/juick/api/Others.java b/src/main/java/com/juick/api/Others.java index 3c8d4a96..12849f90 100644 --- a/src/main/java/com/juick/api/Others.java +++ b/src/main/java/com/juick/api/Others.java @@ -26,7 +26,9 @@ public class Others { this.sql = sql; } - public void doGetGroupsPMs(HttpServletRequest request, HttpServletResponse response, int vuid) throws ServletException, IOException { + public void doGetGroupsPMs(HttpServletRequest request, + HttpServletResponse response, int vuid) + throws ServletException, IOException { int cnt = 5; try { String cntStr = request.getParameter("cnt"); diff --git a/src/main/java/com/juick/api/PM.java b/src/main/java/com/juick/api/PM.java index 01a5669a..3ca9eada 100644 --- a/src/main/java/com/juick/api/PM.java +++ b/src/main/java/com/juick/api/PM.java @@ -29,7 +29,9 @@ public class PM { this.sql = sql; } - public void doGetPM(HttpServletRequest request, HttpServletResponse response, int vuid) throws ServletException, IOException { + public void doGetPM(HttpServletRequest request, + HttpServletResponse response, int vuid) + throws ServletException, IOException { String uname = request.getParameter("uname"); int uid = 0; if (uname != null && uname.matches("^[a-zA-Z0-9\\-]{2,16}$")) { @@ -50,7 +52,9 @@ public class PM { } } - public void doPostPM(HttpServletRequest request, HttpServletResponse response, Stream xmpp, int vuid) throws ServletException, IOException { + public void doPostPM(HttpServletRequest request, + HttpServletResponse response, Stream xmpp, int vuid) + throws ServletException, IOException { String uname = request.getParameter("uname"); int uid = 0; if (UserQueries.checkUserNameValid(uname)) { @@ -73,8 +77,8 @@ public class PM { msg.from = new JID("juick", "juick.com", null); msg.to = new JID(Integer.toString(uid), "push.juick.com", null); JuickMessage jmsg = new JuickMessage(); - jmsg.User = UserQueries.getUserByUID(sql, vuid); - jmsg.Text = body; + jmsg.setUser(UserQueries.getUserByUID(sql, vuid)); + jmsg.setText(body); msg.childs.add(jmsg); xmpp.send(msg); @@ -89,11 +93,11 @@ public class PM { mm.to = new JID(jid); mm.type = Message.Type.chat; if (PMQueries.havePMinRoster(sql, vuid, jid)) { - mm.from = new JID(jmsg.User.UName, "juick.com", "Juick"); + mm.from = new JID(jmsg.getUser().getUName(), "juick.com", "Juick"); mm.body = body; } else { mm.from = new JID("juick", "juick.com", "Juick"); - mm.body = "Private message from @" + jmsg.User.UName + ":\n" + body; + mm.body = "Private message from @" + jmsg.getUser().getUName() + ":\n" + body; } xmpp.send(mm); } diff --git a/src/main/java/com/juick/api/Users.java b/src/main/java/com/juick/api/Users.java index d3818a5a..57f8c4b9 100644 --- a/src/main/java/com/juick/api/Users.java +++ b/src/main/java/com/juick/api/Users.java @@ -27,7 +27,9 @@ public class Users { this.sql = sql; } - public void doGetUsers(HttpServletRequest request, HttpServletResponse response, int vuid) throws ServletException, IOException { + public void doGetUsers(HttpServletRequest request, + HttpServletResponse response, int vuid) + throws ServletException, IOException { List users = new ArrayList<>(); String punames[] = request.getParameterValues("uname"); @@ -66,7 +68,9 @@ public class Users { } } - public void doGetUserRead(HttpServletRequest request, HttpServletResponse response, int vuid) throws ServletException, IOException { + public void doGetUserRead(HttpServletRequest request, + HttpServletResponse response, int vuid) + throws ServletException, IOException { int uid = 0; String uname = request.getParameter("uname"); if (uname == null) { @@ -74,8 +78,8 @@ public class Users { } else { if (UserQueries.checkUserNameValid(uname)) { com.juick.User u = UserQueries.getUserByName(sql, uname); - if (u != null && u.UID > 0) { - uid = u.UID; + if (u != null && u.getUID() > 0) { + uid = u.getUID(); } } } @@ -94,7 +98,9 @@ public class Users { response.sendError(404); } - public void doGetUserReaders(HttpServletRequest request, HttpServletResponse response, int vuid) throws ServletException, IOException { + public void doGetUserReaders(HttpServletRequest request, + HttpServletResponse response, int vuid) + throws ServletException, IOException { int uid = 0; String uname = request.getParameter("uname"); if (uname == null) { @@ -102,8 +108,8 @@ public class Users { } else { if (UserQueries.checkUserNameValid(uname)) { com.juick.User u = UserQueries.getUserByName(sql, uname); - if (u != null && u.UID > 0) { - uid = u.UID; + if (u != null && u.getUID() > 0) { + uid = u.getUID(); } } } diff --git a/src/test/java/com/juick/tests/ApiTests.java b/src/test/java/com/juick/tests/ApiTests.java index b679f528..046077ef 100644 --- a/src/test/java/com/juick/tests/ApiTests.java +++ b/src/test/java/com/juick/tests/ApiTests.java @@ -38,14 +38,14 @@ public class ApiTests { @Test public void userTests() { User user = UserQueries.getUserByUID(jdbc, 3694); - Assert.assertEquals("it should be me", "vt", user.UName); + Assert.assertEquals("it should be me", "vt", user.getUName()); } @Test public void messageTests() { Message msg = MessagesQueries.getMessage(jdbc, 2817722); - User ugnich = msg.User; - Assert.assertEquals("ugnich", ugnich.UName); + User ugnich = msg.getUser(); + Assert.assertEquals("ugnich", ugnich.getUName()); Assert.assertEquals("Microsoft", msg.Tags.get(0)); } } -- cgit v1.2.3 From d8f9f16d16f4e2f7133de464ba8d3f6cc704bd00 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sun, 17 Jan 2016 00:18:33 +0300 Subject: almost working messages api --- .gitignore | 26 ++++---- build.gradle | 1 + deps/com.juick.server | 2 +- src/main/java/com/juick/api/Main.java | 31 +++++---- src/main/java/com/juick/api/Messages.java | 97 +++++++++++++++++++---------- src/main/java/com/juick/api/Utils.java | 5 ++ src/main/webapp/WEB-INF/juick.conf.example | 6 ++ src/test/java/com/juick/tests/ApiTests.java | 11 ++-- 8 files changed, 118 insertions(+), 61 deletions(-) create mode 100644 src/main/webapp/WEB-INF/juick.conf.example (limited to 'src/main/java/com/juick/api/Messages.java') diff --git a/.gitignore b/.gitignore index b25773e5..56340f6c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1,14 @@ -build/* -.gradle/* -deps/* -.idea/* -**/*.iml -**/juick.conf -/build/ -/bin/ -/.nb-gradle/ -.settings/* -.classpath -.project +build/* +.gradle/* +deps/* +.idea/* +**/*.iml +**/juick.conf +/build/ +/bin/ +/.nb-gradle/ +.settings/* +.classpath +.project +**/*.properties +**/*.conf diff --git a/build.gradle b/build.gradle index ea705d8c..b76df16a 100644 --- a/build.gradle +++ b/build.gradle @@ -53,6 +53,7 @@ dependencies { compile server compile xmpp compile json + compile "org.apache.commons:commons-lang3:3.4" compile "org.springframework:spring-jdbc:4.2.4.RELEASE" providedCompile 'javax.servlet:javax.servlet-api:3.1.0' def tomcatVersion = '7.0.+' diff --git a/deps/com.juick.server b/deps/com.juick.server index 27976585..9232bdcb 160000 --- a/deps/com.juick.server +++ b/deps/com.juick.server @@ -1 +1 @@ -Subproject commit 279765857724cf35addc0e3f5e7180a8724d23b9 +Subproject commit 9232bdcb144bc3b94f4b825b3a4cb588078bd72d diff --git a/src/main/java/com/juick/api/Main.java b/src/main/java/com/juick/api/Main.java index cdced593..cc068ccd 100644 --- a/src/main/java/com/juick/api/Main.java +++ b/src/main/java/com/juick/api/Main.java @@ -29,11 +29,13 @@ import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import java.io.FileInputStream; import java.io.IOException; import java.io.PrintWriter; import java.net.Socket; import java.util.Properties; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.logging.LogManager; /** * @@ -54,37 +56,38 @@ public class Main extends HttpServlet implements Stream.StreamListener { public void init() throws ServletException { super.init(); try { + LogManager.getLogManager().readConfiguration(getServletContext().getResourceAsStream("WEB-INF/logging.properties")); Properties conf = new Properties(); - conf.load(new FileInputStream("/etc/juick/api.conf")); - final String driverClassName = "com.mysql.jdbc.Driver"; + conf.load(getServletContext().getResourceAsStream("WEB-INF/juick.conf")); DriverManagerDataSource dataSource = new DriverManagerDataSource(); - dataSource.setUrl("jdbc:mysql://localhost/juick?autoReconnect=true&user=" + conf.getProperty("mysql_username", "") + "&password=" + conf.getProperty("mysql_password", "")); - dataSource.setDriverClassName(driverClassName); + dataSource.setDriverClassName(conf.getProperty("datasource_driver")); + dataSource.setUrl(conf.getProperty("datasource_url")); jdbc = new JdbcTemplate(dataSource); messages = new Messages(jdbc); users = new Users(jdbc); pm = new PM(jdbc); others = new Others(jdbc); - setupXmppComponent(conf.getProperty("xmpp_password")); + setupXmppComponent(conf.getProperty("xmpp_host", "localhost"), Integer.parseInt(conf.getProperty("xmpp_port", "5347")), + conf.getProperty("xmpp_jid", "api.localhost"), conf.getProperty("xmpp_password")); } catch (IOException e) { log("API initialization error", e); } } - public void setupXmppComponent(final String password) { - Thread thr = new Thread(() -> { + public void setupXmppComponent(final String host, final int port, final String jid, final String password) { + ExecutorService executorService = Executors.newSingleThreadExecutor(); + executorService.submit(() -> { try { - Socket socket = new Socket("localhost", 5347); - xmpp = new StreamComponent(new JID("", "api.juick.com", ""), socket.getInputStream(), socket.getOutputStream(), password); + Socket socket = new Socket(host, port); + xmpp = new StreamComponent(new JID(jid), socket.getInputStream(), socket.getOutputStream(), password); xmpp.addListener(Main.this); xmpp.startParsing(); } catch (IOException e) { log("XMPP exception", e); } }); - thr.start(); } @Override @@ -94,7 +97,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { @Override public void onStreamReady() { - System.err.println("XMPP STREAM READY"); + log("XMPP STREAM READY"); } /** @@ -122,6 +125,10 @@ public class Main extends HttpServlet implements Stream.StreamListener { } else { response.sendError(401); } + } else if (uri.equals("/messages")) { + messages.doGet(request, response, vuid); + } else if (uri.equals("/thread")) { + messages.doThreadGet(request, response, vuid); } else if (uri.equals("/users")) { users.doGetUsers(request, response, vuid); } else if (uri.equals("/users/read")) { diff --git a/src/main/java/com/juick/api/Messages.java b/src/main/java/com/juick/api/Messages.java index e242d05b..d86193f1 100644 --- a/src/main/java/com/juick/api/Messages.java +++ b/src/main/java/com/juick/api/Messages.java @@ -11,6 +11,7 @@ import com.juick.xmpp.Message; import com.juick.xmpp.Stream; import com.juick.xmpp.extensions.JuickMessage; import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.util.StringUtils; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; @@ -28,37 +29,11 @@ public class Messages { MessageSerializer messageSerializer = new MessageSerializer(); - enum MediaType { - ALL("all"), - PHOTO("photo"), - VIDEO("video"), - NONE(""); - - private final String value; - - MediaType(final String value) { - this.value = value; - } - public String getValue() { - return value; - } - - @Override - public String toString() { - return this.getValue(); - } - } - public Messages(JdbcTemplate sql) { this.sql = sql; } - public void doGetHome(HttpServletRequest request, - HttpServletResponse response, int vuid) - throws ServletException, IOException { - int before_mid = Utils.parseInt(request.getParameter("before_mid"), 0); - - List mids = MessagesQueries.getMyFeed(sql, vuid, before_mid); + void feedMessages(HttpServletRequest request, HttpServletResponse response, List mids) throws IOException { if (mids != null && !mids.isEmpty()) { List msgs = MessagesQueries.getMessages(sql, mids); if (msgs != null && !msgs.isEmpty()) { @@ -72,16 +47,74 @@ public class Messages { } } + public void doGetHome(HttpServletRequest request, + HttpServletResponse response, int vuid) + throws ServletException, IOException { + int before_mid = Utils.parseInt(request.getParameter("before_mid"), 0); + + feedMessages(request, response, MessagesQueries.getMyFeed(sql, vuid, before_mid)); + + } + public void doGet(HttpServletRequest request, HttpServletResponse response, int vuid) throws ServletException, IOException { - User user = UserQueries.getUserByName(sql, request.getParameter("uname")); int before_mid = Utils.parseInt(request.getParameter("before_mid"), 0); - int page = Utils.parseInt(request.getParameter("page"), 0); - MediaType media = MediaType.valueOf(request.getParameter("media")); - boolean popular = Boolean.valueOf(request.getParameter("popular")); - Tag tag = TagQueries.getTag(sql, request.getParameter("tag"), false); + String uname = request.getParameter("uname"); + String popular = request.getParameter("popular"); + String media = request.getParameter("media"); + String tag = request.getParameter("tag"); + if (!StringUtils.isEmpty(uname)) { + User user = UserQueries.getUserByName(sql, uname); + if (user != null) { + if (!StringUtils.isEmpty(media)) { + feedMessages(request, response, MessagesQueries.getUserPhotos(sql, user.getUID(), 0, before_mid)); + } else if (!StringUtils.isEmpty(tag)) { + Tag tagObject = TagQueries.getTag(sql, tag, false); + if (tagObject != null) { + feedMessages(request, response, MessagesQueries.getUserTag(sql, user.getUID(), tagObject.TID, 0, before_mid)); + } else { + response.sendError(404); + } + } else { + feedMessages(request, response, MessagesQueries.getUserBlog(sql, user.getUID(), 0, before_mid)); + } + } else { + response.sendError(404); + } + } else { + if (!StringUtils.isEmpty(popular)) { + feedMessages(request, response, MessagesQueries.getPopular(sql, before_mid)); + } else if (!StringUtils.isEmpty(media)) { + feedMessages(request, response, MessagesQueries.getPhotos(sql, vuid, before_mid)); + } else if (!StringUtils.isEmpty(tag)) { + Tag tagObject = TagQueries.getTag(sql, tag, false); + if (tagObject != null) { + feedMessages(request, response, MessagesQueries.getTag(sql, tagObject.TID, vuid, before_mid, 20)); + } else { + response.sendError(404); + } + } else { + feedMessages(request, response, MessagesQueries.getAll(sql, vuid, before_mid)); + } + } + } + public void doThreadGet(HttpServletRequest request, HttpServletResponse response, int vuid) throws IOException { + int mid = Utils.parseInt(request.getParameter("mid"), 0); + com.juick.Message msg = MessagesQueries.getMessage(sql, mid); + if (msg != null) { + if (!MessagesQueries.canViewThread(sql, mid, vuid)) { + response.sendError(403); + } else { + List replies = MessagesQueries.getReplies(sql, mid); + replies.add(0, msg); + String json = messageSerializer.serializeList(replies); + Main.replyJSON(request, response, json); + } + } else { + response.sendError(404); + } } public void doGetRecommended(HttpServletRequest request, diff --git a/src/main/java/com/juick/api/Utils.java b/src/main/java/com/juick/api/Utils.java index ccb81f8e..6383fec0 100644 --- a/src/main/java/com/juick/api/Utils.java +++ b/src/main/java/com/juick/api/Utils.java @@ -24,6 +24,8 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; +import java.util.logging.Level; +import java.util.logging.Logger; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -36,6 +38,8 @@ import sun.misc.BASE64Decoder; */ public class Utils { + private static final Logger LOGGER = Logger.getLogger(Utils.class.getName()); + public static String getCookie(HttpServletRequest request, String name) { Cookie cookies[] = request.getCookies(); if (cookies != null) { @@ -80,6 +84,7 @@ public class Utils { return UserQueries.checkPassword(sql, loginpassw[0], loginpassw[1]); } } catch (IOException e) { + LOGGER.log(Level.WARNING, "Auth", e); } } return 0; diff --git a/src/main/webapp/WEB-INF/juick.conf.example b/src/main/webapp/WEB-INF/juick.conf.example new file mode 100644 index 00000000..61ebbc29 --- /dev/null +++ b/src/main/webapp/WEB-INF/juick.conf.example @@ -0,0 +1,6 @@ +datasource_driver=org.h2.Driver +datasource_url=jdbc:h2:~/test +xmpp_host=localhost +xmpp_port=5347 +xmpp_jid=api.localhost +xmpp_password=secret \ No newline at end of file diff --git a/src/test/java/com/juick/tests/ApiTests.java b/src/test/java/com/juick/tests/ApiTests.java index 83afb62e..7b0264c0 100644 --- a/src/test/java/com/juick/tests/ApiTests.java +++ b/src/test/java/com/juick/tests/ApiTests.java @@ -4,6 +4,7 @@ import com.juick.Message; import com.juick.Tag; import com.juick.User; import com.juick.server.MessagesQueries; +import com.juick.server.SubscriptionsQueries; import com.juick.server.TagQueries; import com.juick.server.UserQueries; import junit.framework.Assert; @@ -28,6 +29,7 @@ public class ApiTests { dataSource.setDriverClassName("org.h2.Driver"); dataSource.setUrl("jdbc:h2:~/test"); jdbc = new JdbcTemplate(dataSource); + jdbc.execute("DROP ALL OBJECTS DELETE FILES"); jdbc.execute("CREATE TABLE bl_users (user_id int(10) unsigned NOT NULL, " + "bl_user_id int(10) unsigned NOT NULL, ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP )"); jdbc.execute("CREATE TABLE messages (" + @@ -64,6 +66,9 @@ public class ApiTests { "jid char(64) DEFAULT NULL," + "active tinyint(1) NOT NULL DEFAULT 1," + "ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP)"); + jdbc.execute("CREATE TABLE subscr_messages (" + + "message_id int(10) unsigned NOT NULL," + + "suser_id int(10) unsigned NOT NULL)"); jdbc.execute("CREATE TABLE messages_txt (" + "message_id int(10) unsigned NOT NULL," + "tags varchar(255)," + @@ -85,10 +90,6 @@ public class ApiTests { "attach nchar(3) check (attach in ('jpg', 'mp4', 'png'))," + "txt text)"); } - @After - public void tearDown() { - jdbc.execute("DROP ALL OBJECTS DELETE FILES"); - } @Test @@ -121,6 +122,8 @@ public class ApiTests { org.junit.Assert.assertEquals("weather", msg3.Tags.get(0)); org.junit.Assert.assertEquals(ugnich.getUID(), UserQueries.checkPassword(jdbc, ugnich.getUName(), "x")); org.junit.Assert.assertEquals(-1, UserQueries.checkPassword(jdbc, ugnich.getUName(), "xy")); + SubscriptionsQueries.subscribeMessage(jdbc, msg.getMID(), ugnich.getUID()); + Assert.assertEquals(1, SubscriptionsQueries.getUsersSubscribedToComments(jdbc, msg.getMID(), user.getUID()).size()); } } -- cgit v1.2.3 From 7fb8cdfb02c24c265a081499c858f3aca1efbb2b Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 19 May 2016 23:24:22 +0300 Subject: update server api and try to fix anonymous chats --- deps/com.juick.server | 2 +- src/main/java/com/juick/api/Messages.java | 2 +- src/main/java/com/juick/api/PM.java | 2 +- src/main/java/com/juick/api/TelegramBotHook.java | 12 ++++++------ src/test/java/com/juick/tests/ApiTests.java | 4 ++-- 5 files changed, 11 insertions(+), 11 deletions(-) (limited to 'src/main/java/com/juick/api/Messages.java') diff --git a/deps/com.juick.server b/deps/com.juick.server index a4110979..b633724c 160000 --- a/deps/com.juick.server +++ b/deps/com.juick.server @@ -1 +1 @@ -Subproject commit a41109795f6f6a3803d52606df9bd1a0da641f43 +Subproject commit b633724c75081eb05b70ceee74636f19576a4483 diff --git a/src/main/java/com/juick/api/Messages.java b/src/main/java/com/juick/api/Messages.java index d86193f1..18c868f4 100644 --- a/src/main/java/com/juick/api/Messages.java +++ b/src/main/java/com/juick/api/Messages.java @@ -165,7 +165,7 @@ public class Messages { msg.from = new JID("juick", "juick.com", null); msg.to = new JID(null, "crosspost.juick.com", null); JuickMessage jmsg = new JuickMessage(m); - jmsg.setUser(UserQueries.getUserByUID(sql, 11574)); + jmsg.setUser(UserQueries.getUserByUID(sql, 11574).get()); msg.childs.add(jmsg); msg.to.Username = "twitter"; diff --git a/src/main/java/com/juick/api/PM.java b/src/main/java/com/juick/api/PM.java index 3ca9eada..c7123d0a 100644 --- a/src/main/java/com/juick/api/PM.java +++ b/src/main/java/com/juick/api/PM.java @@ -77,7 +77,7 @@ public class PM { msg.from = new JID("juick", "juick.com", null); msg.to = new JID(Integer.toString(uid), "push.juick.com", null); JuickMessage jmsg = new JuickMessage(); - jmsg.setUser(UserQueries.getUserByUID(sql, vuid)); + jmsg.setUser(UserQueries.getUserByUID(sql, vuid).get()); jmsg.setText(body); msg.childs.add(jmsg); xmpp.send(msg); diff --git a/src/main/java/com/juick/api/TelegramBotHook.java b/src/main/java/com/juick/api/TelegramBotHook.java index 6b1f946c..8bbf0e33 100644 --- a/src/main/java/com/juick/api/TelegramBotHook.java +++ b/src/main/java/com/juick/api/TelegramBotHook.java @@ -76,7 +76,7 @@ public class TelegramBotHook { String msgUrl = "https://juick.com/" + jmsg.getMID(); users.stream().forEach(c -> telegramNotify(c, msg, msgUrl)); // anonymous - chats.stream().filter(u -> !getUser(u).isPresent()).forEach(c -> telegramNotify(c, msg, msgUrl)); + chats.stream().filter(u -> getUser(u) == 0).forEach(c -> telegramNotify(c, msg, msgUrl)); } }); ws.connect(); @@ -146,7 +146,7 @@ public class TelegramBotHook { public void doPost(HttpServletRequest request) throws IOException { try (BufferedReader reader = request.getReader()) { Message message = BotUtils.parseUpdate(reader).message(); - User user_from = getUser(message.chat().id()).orElse(new User()); + User user_from = UserQueries.getUserByUID(jdbc, getUser(message.chat().id())).orElse(new User()); logger.info(String.format("got telegram msg %s from juick user %d", message.toString(), user_from.getUID())); List chats = getChats(); if (!chats.contains(message.chat().id())) { @@ -192,12 +192,12 @@ public class TelegramBotHook { return jdbc.update("INSERT INTO telegram(tg_id, tg_name, loginhash) VALUES(?,?,?)", tgID, tgName, UUID.randomUUID().toString()) > 0; } - private Optional getUser(long tgId) { + private int getUser(long tgId) { try { - return Optional.of(jdbc.queryForObject("SELECT id, nick, banned FROM users INNER JOIN telegram " + - "ON telegram.user_id = users.id WHERE telegram.tg_id=?", new UserQueries.UserMapper(), tgId)); + return jdbc.queryForObject("SELECT id, nick, banned FROM users INNER JOIN telegram " + + "ON telegram.user_id = users.id WHERE telegram.tg_id=?", Integer.class, tgId); } catch (EmptyResultDataAccessException e) { - return Optional.empty(); + return 0; } } private List getSubscribers(int uid) { diff --git a/src/test/java/com/juick/tests/ApiTests.java b/src/test/java/com/juick/tests/ApiTests.java index 5bd90509..2bae183d 100644 --- a/src/test/java/com/juick/tests/ApiTests.java +++ b/src/test/java/com/juick/tests/ApiTests.java @@ -94,7 +94,7 @@ public class ApiTests { @Test public void messageTests() { int user_id = UserQueries.createUser(jdbc, "me", "secret"); - User user = UserQueries.getUserByUID(jdbc, user_id); + User user = UserQueries.getUserByUID(jdbc, user_id).orElse(new User()); Assert.assertEquals("it should be me", "me", user.getUName()); int mid = MessagesQueries.createMessage(jdbc, user_id, "yo", null, new ArrayList<>()); Message msg = MessagesQueries.getMessage(jdbc, mid); @@ -113,7 +113,7 @@ public class ApiTests { Message msg2 = MessagesQueries.getMessage(jdbc, mid2); org.junit.Assert.assertEquals(1, msg2.Tags.size()); int ugnich_id = UserQueries.createUser(jdbc, "ugnich", "x"); - User ugnich = UserQueries.getUserByUID(jdbc, ugnich_id); + User ugnich = UserQueries.getUserByUID(jdbc, ugnich_id).orElse(new User()); int rid = MessagesQueries.createReply(jdbc, msg2.getMID(), 0, ugnich.getUID(), "bla-bla", null); org.junit.Assert.assertEquals(1, rid); Message msg3 = MessagesQueries.getMessage(jdbc, mid2); -- cgit v1.2.3