diff options
author | Vitaly Takmazov | 2016-01-17 00:18:33 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2016-01-17 00:18:33 +0300 |
commit | d8f9f16d16f4e2f7133de464ba8d3f6cc704bd00 (patch) | |
tree | 3583ee665032f722a06d8d65a131b74447d9af94 /src/main/java/com/juick | |
parent | 261d89f398bde4aeb6d84c2ae789a90732793272 (diff) |
almost working messages api
Diffstat (limited to 'src/main/java/com/juick')
-rw-r--r-- | src/main/java/com/juick/api/Main.java | 31 | ||||
-rw-r--r-- | src/main/java/com/juick/api/Messages.java | 97 | ||||
-rw-r--r-- | src/main/java/com/juick/api/Utils.java | 5 |
3 files changed, 89 insertions, 44 deletions
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; |