aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2016-01-17 00:18:33 +0300
committerGravatar Vitaly Takmazov2016-01-17 00:18:33 +0300
commitd8f9f16d16f4e2f7133de464ba8d3f6cc704bd00 (patch)
tree3583ee665032f722a06d8d65a131b74447d9af94
parent261d89f398bde4aeb6d84c2ae789a90732793272 (diff)
almost working messages api
-rw-r--r--.gitignore26
-rw-r--r--build.gradle1
m---------deps/com.juick.server10
-rw-r--r--src/main/java/com/juick/api/Main.java31
-rw-r--r--src/main/java/com/juick/api/Messages.java97
-rw-r--r--src/main/java/com/juick/api/Utils.java5
-rw-r--r--src/main/webapp/WEB-INF/juick.conf.example6
-rw-r--r--src/test/java/com/juick/tests/ApiTests.java11
8 files changed, 122 insertions, 65 deletions
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
-Subproject 279765857724cf35addc0e3f5e7180a8724d23b
+Subproject 9232bdcb144bc3b94f4b825b3a4cb588078bd72
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<Integer> mids = MessagesQueries.getMyFeed(sql, vuid, before_mid);
+ void feedMessages(HttpServletRequest request, HttpServletResponse response, List<Integer> mids) throws IOException {
if (mids != null && !mids.isEmpty()) {
List<com.juick.Message> 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<com.juick.Message> 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());
}
}