From 53fb0b3e0e790df007a198deef21c115ed236b3e Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 27 Jun 2016 10:33:52 +0300 Subject: merge with spring-jdbc --- src/main/java/com/juick/PushComponent.java | 2 +- src/main/java/com/juick/http/www/Discover.java | 4 +- src/main/java/com/juick/http/www/Errors.java | 14 +- .../java/com/juick/http/www/FacebookLogin.java | 80 +++------ src/main/java/com/juick/http/www/Help.java | 18 +- src/main/java/com/juick/http/www/Home.java | 4 +- src/main/java/com/juick/http/www/Login.java | 29 +-- src/main/java/com/juick/http/www/Main.java | 73 ++------ src/main/java/com/juick/http/www/NewMessage.java | 34 ++-- src/main/java/com/juick/http/www/PM.java | 20 +-- .../java/com/juick/http/www/PageTemplates.java | 75 ++------ src/main/java/com/juick/http/www/RSS.java | 24 +-- src/main/java/com/juick/http/www/Settings.java | 22 +-- src/main/java/com/juick/http/www/SignUp.java | 197 +++++---------------- src/main/java/com/juick/http/www/TwitterAuth.java | 3 +- src/main/java/com/juick/http/www/User.java | 27 ++- src/main/java/com/juick/http/www/Utils.java | 4 +- .../java/com/juick/http/www/VKontakteLogin.java | 59 ++---- 18 files changed, 188 insertions(+), 501 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/PushComponent.java b/src/main/java/com/juick/PushComponent.java index 386448f4..0cb8e626 100644 --- a/src/main/java/com/juick/PushComponent.java +++ b/src/main/java/com/juick/PushComponent.java @@ -157,7 +157,7 @@ public class PushComponent implements ServletContextListener, Stream.StreamListe } @Override - public void onStreamFail(Exception e) {logger.log(Level.SEVERE, "XMPP STREAM FAIL", e);} + public void onStreamFail(String e) {logger.log(Level.SEVERE, "XMPP STREAM FAIL", e);} @Override public void onMessage(com.juick.xmpp.Message msg) { diff --git a/src/main/java/com/juick/http/www/Discover.java b/src/main/java/com/juick/http/www/Discover.java index e7d85d8a..bdb86380 100644 --- a/src/main/java/com/juick/http/www/Discover.java +++ b/src/main/java/com/juick/http/www/Discover.java @@ -20,6 +20,8 @@ package com.juick.http.www; import com.juick.server.AdsQueries; import com.juick.server.MessagesQueries; import com.juick.server.TagQueries; +import org.springframework.jdbc.core.JdbcTemplate; + import java.io.IOException; import java.io.PrintWriter; import java.net.URLDecoder; @@ -37,7 +39,7 @@ import javax.servlet.http.HttpServletResponse; */ public class Discover { - protected void doGet(Connection sql, Connection sqlSearch, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + protected void doGet(JdbcTemplate sql, JdbcTemplate sqlSearch, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); String paramTagStr = URLDecoder.decode(request.getRequestURI().substring(5), "UTF-8"); diff --git a/src/main/java/com/juick/http/www/Errors.java b/src/main/java/com/juick/http/www/Errors.java index f65b6201..2161daf1 100644 --- a/src/main/java/com/juick/http/www/Errors.java +++ b/src/main/java/com/juick/http/www/Errors.java @@ -1,11 +1,12 @@ package com.juick.http.www; -import java.io.IOException; -import java.io.PrintWriter; -import java.sql.Connection; +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.io.PrintWriter; /** * @@ -15,7 +16,7 @@ public class Errors { public static String tagsHTML = null; - public static void doGet404(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + public static void doGet404(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); if (tagsHTML == null) { @@ -24,8 +25,7 @@ public class Errors { response.setStatus(404); response.setContentType("text/html; charset=UTF-8"); - PrintWriter out = response.getWriter(); - try { + try (PrintWriter out = response.getWriter()) { PageTemplates.pageHead(out, "404 Страница не найдена", null); PageTemplates.pageNavigation(out, visitor, null); PageTemplates.pageHomeColumn(out, sql, visitor); @@ -37,8 +37,6 @@ public class Errors { PageTemplates.pageFooter(request, out, visitor, false); PageTemplates.pageEnd(out); - } finally { - out.close(); } } } diff --git a/src/main/java/com/juick/http/www/FacebookLogin.java b/src/main/java/com/juick/http/www/FacebookLogin.java index fcb08d6c..cf444abc 100644 --- a/src/main/java/com/juick/http/www/FacebookLogin.java +++ b/src/main/java/com/juick/http/www/FacebookLogin.java @@ -18,20 +18,19 @@ package com.juick.http.www; import com.juick.server.UserQueries; +import org.json.JSONObject; +import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; + +import javax.servlet.ServletException; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.net.URLEncoder; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; import java.util.UUID; import java.util.logging.Level; import java.util.logging.Logger; -import javax.servlet.ServletException; -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import org.json.JSONObject; /** * @@ -45,7 +44,7 @@ public class FacebookLogin { private static final String FACEBOOK_SECRET = "95813bfb6ab8f473410c50d4f971649e"; private static final String FACEBOOK_REDIRECT = "http://juick.com/_fblogin"; - protected void doGet(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + protected void doGet(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String fbstate; String code = request.getParameter("code"); @@ -132,61 +131,22 @@ public class FacebookLogin { } } - private int getUIDbyFBID(Connection sql, long fbID) { - int uid = 0; - PreparedStatement stmt = null; - ResultSet rs = null; + private int getUIDbyFBID(JdbcTemplate sql, long fbID) { try { - stmt = sql.prepareStatement("SELECT user_id FROM facebook WHERE fb_id=? AND user_id IS NOT NULL"); - stmt.setLong(1, fbID); - rs = stmt.executeQuery(); - if (rs.first()) { - uid = rs.getInt(1); - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); + return sql.queryForObject("SELECT user_id FROM facebook WHERE fb_id=? AND user_id IS NOT NULL", + Integer.class, fbID); + } catch (EmptyResultDataAccessException e) { + return 0; } - return uid; } - private boolean insertDB(Connection sql, long fbID, String loginhash, String token, String fbName, String fbLink) { - boolean ret = false; - PreparedStatement stmt = null; - try { - stmt = sql.prepareStatement("INSERT INTO facebook(fb_id,loginhash,access_token,fb_name,fb_link) VALUES (?,?,?,?,?)"); - stmt.setLong(1, fbID); - stmt.setString(2, loginhash); - stmt.setString(3, token); - stmt.setString(4, fbName); - stmt.setString(5, fbLink); - stmt.executeUpdate(); - ret = true; - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(null, stmt); - } - return ret; + private boolean insertDB(JdbcTemplate sql, long fbID, String loginhash, String token, String fbName, String fbLink) { + return sql.update("INSERT INTO facebook(fb_id,loginhash,access_token,fb_name,fb_link) VALUES (?,?,?,?,?)", + fbID, loginhash, token, fbName, fbLink) > 0; } - private boolean updateDB(Connection sql, long fbID, String token, String fbName, String fbLink) { - boolean ret = false; - PreparedStatement stmt = null; - try { - stmt = sql.prepareStatement("UPDATE facebook SET access_token=?,fb_name=?,fb_link=? WHERE fb_id=?"); - stmt.setString(1, token); - stmt.setString(2, fbName); - stmt.setString(3, fbLink); - stmt.setLong(4, fbID); - stmt.executeUpdate(); - ret = true; - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(null, stmt); - } - return ret; + private boolean updateDB(JdbcTemplate sql, long fbID, String token, String fbName, String fbLink) { + return sql.update("UPDATE facebook SET access_token=?,fb_name=?,fb_link=? WHERE fb_id=?", + token, fbName, fbLink, fbID) > 0; } } diff --git a/src/main/java/com/juick/http/www/Help.java b/src/main/java/com/juick/http/www/Help.java index 438fc3a9..503044e6 100644 --- a/src/main/java/com/juick/http/www/Help.java +++ b/src/main/java/com/juick/http/www/Help.java @@ -17,15 +17,12 @@ */ package com.juick.http.www; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.io.PrintWriter; -import java.sql.Connection; +import org.springframework.jdbc.core.JdbcTemplate; + import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.io.*; /** * @@ -33,11 +30,11 @@ import javax.servlet.http.HttpServletResponse; */ public class Help { - protected void doRedirectToHelpIndex(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + protected void doRedirectToHelpIndex(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Utils.sendTemporaryRedirect(response, "/help/ru/"); } - protected void doGetHelp(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + protected void doGetHelp(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); String path[] = request.getRequestURI().split("/"); @@ -64,8 +61,7 @@ public class Help { } response.setContentType("text/html; charset=UTF-8"); - PrintWriter out = response.getWriter(); - try { + try (PrintWriter out = response.getWriter()) { PageTemplates.pageHead(out, "Помощь", null); PageTemplates.pageNavigation(out, visitor, null); @@ -81,8 +77,6 @@ public class Help { PageTemplates.pageFooter(request, out, visitor, false); PageTemplates.pageEnd(out); - } finally { - out.close(); } } diff --git a/src/main/java/com/juick/http/www/Home.java b/src/main/java/com/juick/http/www/Home.java index 8dc3e70c..89d4005f 100644 --- a/src/main/java/com/juick/http/www/Home.java +++ b/src/main/java/com/juick/http/www/Home.java @@ -19,6 +19,8 @@ package com.juick.http.www; import com.juick.server.AdsQueries; import com.juick.server.MessagesQueries; +import org.springframework.jdbc.core.JdbcTemplate; + import java.io.IOException; import java.io.PrintWriter; import java.net.URLEncoder; @@ -35,7 +37,7 @@ import javax.servlet.http.HttpServletResponse; */ public class Home { - protected void doGet(Connection sql, Connection sqlSearch, HttpServletRequest request, HttpServletResponse response, com.juick.User visitor) throws ServletException, IOException { + protected void doGet(JdbcTemplate sql, JdbcTemplate sqlSearch, HttpServletRequest request, HttpServletResponse response, com.juick.User visitor) throws ServletException, IOException { int paramBefore = 0; String paramBeforeStr = request.getParameter("before"); if (paramBeforeStr != null) { diff --git a/src/main/java/com/juick/http/www/Login.java b/src/main/java/com/juick/http/www/Login.java index 3e9c5e09..f12c7096 100644 --- a/src/main/java/com/juick/http/www/Login.java +++ b/src/main/java/com/juick/http/www/Login.java @@ -17,11 +17,10 @@ */ package com.juick.http.www; +import org.springframework.jdbc.core.JdbcTemplate; + import java.io.IOException; import java.io.PrintWriter; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; @@ -33,7 +32,7 @@ import javax.servlet.http.HttpServletResponse; */ public class Login { - protected void doGetLoginForm(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + protected void doGetLoginForm(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); if (visitor != null) { Utils.sendTemporaryRedirect(response, "/"); @@ -41,8 +40,7 @@ public class Login { } response.setContentType("text/html; charset=UTF-8"); - PrintWriter out = response.getWriter(); - try { + try (PrintWriter out = response.getWriter()) { out.println(""); out.println(""); out.println(""); @@ -182,12 +180,10 @@ public class Login { out.println(""); out.println(""); - } finally { - out.close(); } } - protected void doGetLogin(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + protected void doGetLogin(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String hash = request.getQueryString(); if (hash.length() > 32) { response.sendError(400); @@ -204,7 +200,7 @@ public class Login { } } - protected void doPostLogin(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + protected void doPostLogin(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); if (username == null || password == null || username.length() > 32 || password.isEmpty()) { @@ -230,19 +226,10 @@ public class Login { } } - protected void doGetLogout(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + protected void doGetLogout(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); if (visitor != null) { - PreparedStatement stmt = null; - try { - stmt = sql.prepareStatement("DELETE FROM logins WHERE user_id=?"); - stmt.setInt(1, visitor.getUID()); - stmt.executeUpdate(); - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(null, stmt); - } + sql.update("DELETE FROM logins WHERE user_id=?", visitor.getUID()); } Cookie c = new Cookie("hash", "-"); diff --git a/src/main/java/com/juick/http/www/Main.java b/src/main/java/com/juick/http/www/Main.java index 7896c207..604ccf9a 100644 --- a/src/main/java/com/juick/http/www/Main.java +++ b/src/main/java/com/juick/http/www/Main.java @@ -21,6 +21,8 @@ import com.juick.server.UserQueries; import com.juick.xmpp.JID; import com.juick.xmpp.Stream; import com.juick.xmpp.StreamComponent; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.datasource.DriverManagerDataSource; import ru.sape.Sape; import javax.servlet.ServletException; @@ -32,9 +34,6 @@ import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.net.Socket; import java.net.URLEncoder; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; import java.util.Properties; /** @@ -45,8 +44,8 @@ import java.util.Properties; @MultipartConfig(fileSizeThreshold = 1024 * 1024, maxRequestSize = 1024 * 1024 * 10) public class Main extends HttpServlet implements Stream.StreamListener { - Connection sql; - Connection sqlSearch; + JdbcTemplate sql; + JdbcTemplate sqlSearch; String sqlSearchConnStr = "jdbc:mysql://127.0.0.1:9306?autoReconnect=true&useUnicode=yes&characterEncoding=utf8&maxAllowedPacket=512000"; Stream xmpp; Home home = new Home(); @@ -72,9 +71,14 @@ public class Main extends HttpServlet implements Stream.StreamListener { Properties conf = new Properties(); conf.load(getServletContext().getResourceAsStream("/WEB-INF/juick.conf")); - Class.forName("com.mysql.jdbc.Driver"); - sql = DriverManager.getConnection("jdbc:mysql://localhost/juick?autoReconnect=true&user=" + conf.getProperty("mysql_username", "") + "&password=" + conf.getProperty("mysql_password", "")); - sqlSearch = null; // init this on search, timeout is too low + DriverManagerDataSource dataSource = new DriverManagerDataSource(); + dataSource.setDriverClassName(conf.getProperty("datasource_driver", "com.mysql.jdbc.Driver")); + dataSource.setUrl(conf.getProperty("datasource_url")); + DriverManagerDataSource dataSourceSearch = new DriverManagerDataSource(); + dataSourceSearch.setDriverClassName(conf.getProperty("datasource_driver", "com.mysql.jdbc.Driver")); + dataSourceSearch.setUrl(sqlSearchConnStr); + sql = new JdbcTemplate(dataSource); + sqlSearch = new JdbcTemplate(dataSourceSearch); setupXmppComponent(conf.getProperty("xmpp_password")); twitterAuth = new TwitterAuth(conf.getProperty("twitter_consumer_key"), @@ -84,27 +88,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { log(null, e); } } - public void closeSqlSearch() { - if (sqlSearch != null) { - try { - sqlSearch.close(); - sqlSearch = null; - } catch (SQLException e) { - log("An error on closing sql search connection", e); - } - } - }; - public Connection getSqlSearch() { - if (sqlSearch == null) { - try { - sqlSearch = DriverManager.getConnection(sqlSearchConnStr, "", ""); - } - catch (Exception e) { - log("Couldn't open sqlSearch connection",e); - } - } - return sqlSearch; - } + public void setupXmppComponent(final String password) { Thread thr = new Thread(() -> { try { @@ -120,33 +104,13 @@ public class Main extends HttpServlet implements Stream.StreamListener { } @Override - public void onStreamFail(Exception e) {log("XMPP STREAM FAIL", e);} + public void onStreamFail(String e) {log("XMPP STREAM FAIL:" + e);} @Override public void onStreamReady() { log("XMPP STREAM READY"); } - @Override - public void destroy() { - super.destroy(); - if (sql != null) { - try { - sql.close(); - sql = null; - } catch (SQLException e) { - log(null, e); - } - } - if (sqlSearch != null) { - try { - sqlSearch.close(); - sqlSearch = null; - } catch (SQLException e) { - log(null, e); - } - } - } /** * Handles the HTTP GET method. @@ -168,8 +132,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { Utils.sendPermanentRedirect(response, "/tag/" + URLEncoder.encode(tag, "UTF-8")); } else { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); - home.doGet(sql, getSqlSearch(), request, response, visitor); - closeSqlSearch(); + home.doGet(sql, sqlSearch, request, response, visitor); } } else if (uri.equals("/post")) { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); @@ -226,8 +189,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { } else if (uri.startsWith("/help/")) { help.doGetHelp(sql, request, response); } else if (uri.startsWith("/tag/")) { - discover.doGet(sql, getSqlSearch(), request, response); - closeSqlSearch(); + discover.doGet(sql, sqlSearch, request, response); } else if (uri.matches("^/\\d+$")) { String strID = request.getRequestURI().substring(1); int mid = 0; @@ -255,8 +217,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { com.juick.User user = com.juick.server.UserQueries.getUserByName(sql, uriparts[1]); if (user != null && user.getUName().equals(uriparts[1]) && !user.Banned) { if (uriparts.length == 2) { // http://juick.com/username/ - pagesUser.doGetBlog(sql, getSqlSearch(), request, response, user); - closeSqlSearch(); + pagesUser.doGetBlog(sql, sqlSearch, request, response, user); } else if (uriparts[2].equals("tags")) { pagesUser.doGetTags(sql, request, response, user); } else if (uriparts[2].equals("friends")) { diff --git a/src/main/java/com/juick/http/www/NewMessage.java b/src/main/java/com/juick/http/www/NewMessage.java index 86725db7..642bd794 100644 --- a/src/main/java/com/juick/http/www/NewMessage.java +++ b/src/main/java/com/juick/http/www/NewMessage.java @@ -18,11 +18,7 @@ package com.juick.http.www; import com.juick.Tag; -import com.juick.server.CrosspostQueries; -import com.juick.server.MessagesQueries; -import com.juick.server.SubscriptionsQueries; -import com.juick.server.TagQueries; -import com.juick.server.UserQueries; +import com.juick.server.*; import com.juick.xmpp.JID; import com.juick.xmpp.Message; import com.juick.xmpp.Stream; @@ -30,17 +26,18 @@ import com.juick.xmpp.extensions.JuickMessage; import com.juick.xmpp.extensions.JuickUser; import com.juick.xmpp.extensions.Nickname; import com.juick.xmpp.extensions.XOOB; +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.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.net.URL; import java.net.URLEncoder; -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; /** * @@ -48,10 +45,9 @@ import javax.servlet.http.HttpServletResponse; */ public class NewMessage { - protected void doGetNewMessage(Connection sql, HttpServletRequest request, HttpServletResponse response, com.juick.User visitor) throws ServletException, IOException { + protected void doGetNewMessage(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, com.juick.User visitor) throws ServletException, IOException { response.setContentType("text/html; charset=UTF-8"); - PrintWriter out = response.getWriter(); - try { + try (PrintWriter out = response.getWriter()) { PageTemplates.pageHead(out, "Написать", "" + "" + "" @@ -83,12 +79,10 @@ public class NewMessage { PageTemplates.pageFooter(request, out, visitor, false); PageTemplates.pageEnd(out); - } finally { - out.close(); } } - void printUserTags(Connection sql, PrintWriter out, com.juick.User visitor) { + void printUserTags(JdbcTemplate sql, PrintWriter out, com.juick.User visitor) { List tags = TagQueries.getUserTagsAll(sql, visitor.getUID()); if (tags.isEmpty()) { @@ -134,7 +128,7 @@ public class NewMessage { out.println("

"); } - public void doPostMessage(Connection sql, HttpServletRequest request, HttpServletResponse response, Stream xmpp, com.juick.User visitor) throws ServletException, IOException { + public void doPostMessage(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, Stream xmpp, com.juick.User visitor) throws ServletException, IOException { String body = request.getParameter("body"); if (body == null || body.length() < 1 || body.length() > 4096) { response.sendError(400); @@ -265,12 +259,12 @@ public class NewMessage { out.println("
"); out.println("

Сообщение опубликовано

"); out.println("

Поделитесь своим новым постом в социальных сетях:

"); - if (CrosspostQueries.getTwitterTokens(sql, visitor.getUID()) == null) { + if (CrosspostQueries.getTwitterTokens(sql, visitor.getUID()).isPresent()) { out.println("

Отправить в Twitter

"); } out.println("

Отправить в LiveJournal

"); out.println("

Отправить в ВКонтакте

"); - if (CrosspostQueries.getFacebookToken(sql, visitor.getUID()) == null) { + if (CrosspostQueries.getFacebookToken(sql, visitor.getUID()).isPresent()) { out.println("

Отправить в Facebook

"); } out.println("

Отправить в Google+

"); @@ -282,7 +276,7 @@ public class NewMessage { } } - public void doPostComment(Connection sql, HttpServletRequest request, HttpServletResponse response, Stream xmpp, com.juick.User visitor) throws ServletException, IOException { + public void doPostComment(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, Stream xmpp, com.juick.User visitor) throws ServletException, IOException { int mid = Utils.parseInt(request.getParameter("mid"), 0); if (mid == 0) { response.sendError(400); @@ -383,7 +377,7 @@ public class NewMessage { Utils.sendTemporaryRedirect(response, "/" + msg.getUser().getUName() + "/" + mid + "#" + ridnew); } - public void doPostRecomm(Connection sql, HttpServletRequest request, HttpServletResponse response, Stream xmpp, com.juick.User visitor) throws ServletException, IOException { + public void doPostRecomm(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, Stream xmpp, com.juick.User visitor) throws ServletException, IOException { int mid = Utils.parseInt(request.getParameter("mid"), 0); if (mid == 0) { response.sendError(400); diff --git a/src/main/java/com/juick/http/www/PM.java b/src/main/java/com/juick/http/www/PM.java index a9505bdb..b64e6c43 100644 --- a/src/main/java/com/juick/http/www/PM.java +++ b/src/main/java/com/juick/http/www/PM.java @@ -23,6 +23,8 @@ 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.io.PrintWriter; import java.sql.Connection; @@ -38,7 +40,7 @@ import javax.servlet.http.HttpServletResponse; */ public class PM { - protected void doGetInbox(Connection sql, HttpServletRequest request, HttpServletResponse response, com.juick.User visitor) throws ServletException, IOException { + protected void doGetInbox(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, com.juick.User visitor) throws ServletException, IOException { /* int paramBefore = 0; String paramBeforeStr = request.getParameter("before"); @@ -54,8 +56,7 @@ public class PM { List msgs = PMQueries.getLastPMInbox(sql, visitor.getUID()); response.setContentType("text/html; charset=UTF-8"); - PrintWriter out = response.getWriter(); - try { + try (PrintWriter out = response.getWriter()) { PageTemplates.pageHead(out, title, null); PageTemplates.pageNavigation(out, visitor, null); PageTemplates.pageHomeColumn(out, sql, visitor); @@ -97,12 +98,10 @@ public class PM { PageTemplates.pageFooter(request, out, visitor, false); PageTemplates.pageEnd(out); - } finally { - out.close(); } } - protected void doGetSent(Connection sql, HttpServletRequest request, HttpServletResponse response, com.juick.User visitor) throws ServletException, IOException { + protected void doGetSent(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, com.juick.User visitor) throws ServletException, IOException { /* int paramBefore = 0; String paramBeforeStr = request.getParameter("before"); @@ -123,8 +122,7 @@ public class PM { } response.setContentType("text/html; charset=UTF-8"); - PrintWriter out = response.getWriter(); - try { + try (PrintWriter out = response.getWriter()) { PageTemplates.pageHead(out, title, null); PageTemplates.pageNavigation(out, visitor, null); PageTemplates.pageHomeColumn(out, sql, visitor); @@ -169,12 +167,10 @@ public class PM { PageTemplates.pageFooter(request, out, visitor, false); PageTemplates.pageEnd(out); - } finally { - out.close(); } } - public void doPostPM(Connection sql, HttpServletRequest request, HttpServletResponse response, Stream xmpp, com.juick.User visitor) throws ServletException, IOException { + public void doPostPM(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, Stream xmpp, com.juick.User visitor) throws ServletException, IOException { String uname = request.getParameter("uname"); if (uname.startsWith("@")) { uname = uname.substring(1); @@ -200,7 +196,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, visitor.getUID())); + jmsg.setUser(visitor); jmsg.setText(body); msg.childs.add(jmsg); xmpp.send(msg); diff --git a/src/main/java/com/juick/http/www/PageTemplates.java b/src/main/java/com/juick/http/www/PageTemplates.java index fedb11bf..89b0c737 100644 --- a/src/main/java/com/juick/http/www/PageTemplates.java +++ b/src/main/java/com/juick/http/www/PageTemplates.java @@ -20,6 +20,7 @@ package com.juick.http.www; import com.juick.Message; import com.juick.Tag; import com.juick.server.MessagesQueries; +import com.juick.server.TagQueries; import com.juick.server.UserQueries; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; @@ -35,7 +36,11 @@ import java.util.Date; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; import javax.servlet.http.HttpServletRequest; + +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.util.StringUtils; import ru.sape.Sape; /** @@ -105,13 +110,13 @@ public class PageTemplates { out.println(""); } - public static void pageHomeColumn(PrintWriter out, Connection sql, com.juick.User visitor) { + public static void pageHomeColumn(PrintWriter out, JdbcTemplate sql, com.juick.User visitor) { pageHomeColumn(out, sql, visitor, false); } - public static void pageHomeColumn(PrintWriter out, Connection sql, com.juick.User visitor, boolean showAdv) { + public static void pageHomeColumn(PrintWriter out, JdbcTemplate sql, com.juick.User visitor, boolean showAdv) { if (tagsHTML == null) { - tagsHTML = PageTemplates.getPopularTags(sql, 80); + tagsHTML = PageTemplates.formatPopularTags(sql, 80); } out.println(""); } - public static String getPopularTags(Connection sql, int cnt) { - String ret = ""; - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT name FROM tags WHERE top=1 ORDER BY name ASC"); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - if (!ret.isEmpty()) { - ret += " "; - } - try { - ret += "" + Utils.encodeHTML(rs.getString(1)) + ""; - } catch (UnsupportedEncodingException e) { - } - - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); - } - - return ret; - } - - public static void printContestRating(PrintWriter out, Connection sql) { - out.println("
"); - out.println(""); - out.println("

Кто выиграет iPod?

"); - out.println(""); - - int i = 0; - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT users.id,users.nick,COUNT(users_refs.user_id) AS cnt FROM users INNER JOIN users_refs ON users.id=users_refs.ref WHERE users.id>2 GROUP BY users_refs.ref ORDER BY cnt DESC LIMIT 10"); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - String uname = rs.getString(2); - if (i == 0) { - out.println(" "); - } else { - out.println(" "); - } - i++; - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); - } - - out.println("
" + uname + "" + rs.getInt(3) + "
" + uname + "" + rs.getInt(3) + "
"); - out.println(""); + public static String formatPopularTags(JdbcTemplate sql, int cnt) { + List popularTags = TagQueries.getPopularTags(sql).stream() + .map(t -> "" + Utils.encodeHTML(t) + "").collect(Collectors.toList()); + return StringUtils.collectionToDelimitedString(popularTags, " "); } public static void pageFooter(HttpServletRequest request, PrintWriter out, com.juick.User visitor, boolean sapeon) { @@ -455,7 +406,7 @@ public class PageTemplates { return msg; } - public static void printMessages(PrintWriter out, Connection sql, com.juick.User user, List mids, com.juick.User visitor, int YandexID, int ad_mid) { + public static void printMessages(PrintWriter out, JdbcTemplate sql, com.juick.User user, List mids, com.juick.User visitor, int YandexID, int ad_mid) { List msgs = MessagesQueries.getMessages(sql, mids); for (int i = 0; i < msgs.size(); i++) { diff --git a/src/main/java/com/juick/http/www/RSS.java b/src/main/java/com/juick/http/www/RSS.java index 72893915..349743b5 100644 --- a/src/main/java/com/juick/http/www/RSS.java +++ b/src/main/java/com/juick/http/www/RSS.java @@ -19,16 +19,16 @@ package com.juick.http.www; import com.juick.Message; import com.juick.server.MessagesQueries; +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.io.PrintWriter; -import java.sql.Connection; import java.text.SimpleDateFormat; import java.util.Date; -import java.util.Iterator; import java.util.List; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; /** * @@ -36,10 +36,9 @@ import javax.servlet.http.HttpServletResponse; */ public class RSS { - private static final SimpleDateFormat sdfSQL = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private static final SimpleDateFormat sdfRSS = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z"); - protected void doGet(Connection sql, HttpServletRequest request, HttpServletResponse response, int uid, String uname) throws ServletException, IOException { + protected void doGet(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, int uid, String uname) throws ServletException, IOException { List mids = MessagesQueries.getUserBlog(sql, uid, 0, 0); if (mids.isEmpty()) { response.sendError(404); @@ -73,14 +72,9 @@ public class RSS { out.println("]]>"); out.println(""); - synchronized (sdfSQL) { - try { - Date date = sdfSQL.parse(msg.TimestampString); - out.println("" + sdfRSS.format(date) + ""); - } catch (Exception e) { - System.err.println("PARSE EXCEPTION: " + msg.TimestampString); - } - } + Date date = msg.getDate(); + out.println("" + sdfRSS.format(date) + ""); + out.println("http://juick.com/" + msg.getUser().getUName() + "/" + msg.getMID() + ""); if (!msg.Tags.isEmpty()) { diff --git a/src/main/java/com/juick/http/www/Settings.java b/src/main/java/com/juick/http/www/Settings.java index de37bdd0..f197ff12 100644 --- a/src/main/java/com/juick/http/www/Settings.java +++ b/src/main/java/com/juick/http/www/Settings.java @@ -17,6 +17,8 @@ */ package com.juick.http.www; +import org.springframework.jdbc.core.JdbcTemplate; + import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; @@ -33,12 +35,11 @@ import javax.servlet.http.HttpServletResponse; */ public class Settings { - protected void doGet(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + protected void doGet(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); response.setContentType("text/html; charset=UTF-8"); - PrintWriter out = response.getWriter(); - try { + try (PrintWriter out = response.getWriter()) { PageTemplates.pageHead(out, "Логин", ""); PageTemplates.pageNavigation(out, visitor, null); @@ -56,12 +57,10 @@ public class Settings { PageTemplates.pageFooter(request, out, visitor, false); PageTemplates.pageEnd(out); - } finally { - out.close(); } } - protected void doPost(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + protected void doPost(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); if (username == null || password == null || username.length() > 32 || password.isEmpty()) { @@ -79,16 +78,7 @@ public class Settings { if (uid > 0) { - PreparedStatement stmt = null; - try { - stmt = sql.prepareStatement("DELETE FROM logins WHERE user_id=?"); - stmt.setInt(1, uid); - stmt.executeUpdate(); - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(null, stmt); - } + throw new IOException("Settings"); } String referer = request.getHeader("Referer"); diff --git a/src/main/java/com/juick/http/www/SignUp.java b/src/main/java/com/juick/http/www/SignUp.java index 64b62e48..1ee23386 100644 --- a/src/main/java/com/juick/http/www/SignUp.java +++ b/src/main/java/com/juick/http/www/SignUp.java @@ -18,16 +18,17 @@ package com.juick.http.www; import com.juick.server.UserQueries; -import java.io.IOException; -import java.io.PrintWriter; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; +import org.apache.commons.lang3.tuple.Pair; +import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; + import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.List; /** * @@ -35,7 +36,7 @@ import javax.servlet.http.HttpServletResponse; */ public class SignUp { - protected void doGet(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + protected void doGet(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); String type = request.getParameter("type"); @@ -61,8 +62,7 @@ public class SignUp { } response.setContentType("text/html; charset=UTF-8"); - PrintWriter out = response.getWriter(); - try { + try (PrintWriter out = response.getWriter()) { PageTemplates.pageHead(out, "Новый пользователь", null); PageTemplates.pageNavigation(out, visitor, null); @@ -110,12 +110,10 @@ public class SignUp { PageTemplates.pageFooter(request, out, visitor, false); PageTemplates.pageEnd(out); - } finally { - out.close(); } } - protected void doPost(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + protected void doPost(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); int uid = 0; @@ -204,170 +202,57 @@ public class SignUp { response.sendRedirect("/"); } - private boolean setUserRef(Connection sql, int uid, int ref) { - boolean ret = false; - PreparedStatement stmt = null; - try { - stmt = sql.prepareStatement("INSERT INTO users_refs(user_id,ref) VALUES (?,?)"); - stmt.setInt(1, uid); - stmt.setInt(2, ref); - stmt.executeUpdate(); - ret = true; - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(null, stmt); - } - return ret; + private boolean setUserRef(JdbcTemplate sql, int uid, int ref) { + return sql.update("INSERT INTO users_refs(user_id,ref) VALUES (?,?)", uid, ref) > 0; } - private String getFacebookNameByHash(Connection sql, String hash) { - String ret = null; - - PreparedStatement stmt = null; - ResultSet rs = null; + private String getFacebookNameByHash(JdbcTemplate sql, String hash) { try { - stmt = sql.prepareStatement("SELECT fb_name,fb_link FROM facebook WHERE loginhash=?"); - stmt.setString(1, hash); - rs = stmt.executeQuery(); - if (rs.first()) { - ret = "" + rs.getString(1) + ""; - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); + return sql.queryForObject("SELECT fb_name,fb_link FROM facebook WHERE loginhash=?", String.class, hash); + } catch (EmptyResultDataAccessException e) { + return null; } - - return ret; } - private String getTelegramNameByHash(Connection sql, String hash) { - String ret = null; - - PreparedStatement stmt = null; - ResultSet rs = null; + private String getTelegramNameByHash(JdbcTemplate sql, String hash) { try { - stmt = sql.prepareStatement("SELECT tg_name FROM telegram WHERE loginhash=?"); - stmt.setString(1, hash); - rs = stmt.executeQuery(); - if (rs.first()) { - ret = "" + rs.getString(1) + ""; - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); + String name = sql.queryForObject("SELECT tg_name FROM telegram WHERE loginhash=?", String.class, hash); + return "" + name + ""; + } catch (EmptyResultDataAccessException e) { + return null; } - - return ret; } - private boolean setFacebookUser(Connection sql, String hash, int uid) { - boolean ret = false; - PreparedStatement stmt = null; - try { - stmt = sql.prepareStatement("UPDATE facebook SET user_id=?,loginhash=NULL WHERE loginhash=?"); - stmt.setInt(1, uid); - stmt.setString(2, hash); - stmt.executeUpdate(); - ret = true; - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(null, stmt); - } - return ret; + private boolean setFacebookUser(JdbcTemplate sql, String hash, int uid) { + return sql.update("UPDATE facebook SET user_id=?,loginhash=NULL WHERE loginhash=?", uid, hash) > 0; } - private String getVKNameByHash(Connection sql, String hash) { - String ret = null; - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT vk_name,vk_link FROM vk WHERE loginhash=?"); - stmt.setString(1, hash); - rs = stmt.executeQuery(); - if (rs.first()) { - ret = "" + rs.getString(1) + ""; - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); + private String getVKNameByHash(JdbcTemplate sql, String hash) { + List> logins = sql.query("SELECT vk_name,vk_link FROM vk WHERE loginhash=?", + (rs, num) -> { + return Pair.of(rs.getString(1), rs.getString(2)); + }, hash); + if (logins.size() > 0) { + return "" + logins.get(0).getLeft() + ""; } - - return ret; + return null; } - private boolean setVKUser(Connection sql, String hash, int uid) { - boolean ret = false; - PreparedStatement stmt = null; - try { - stmt = sql.prepareStatement("UPDATE vk SET user_id=?,loginhash=NULL WHERE loginhash=?"); - stmt.setInt(1, uid); - stmt.setString(2, hash); - stmt.executeUpdate(); - ret = true; - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(null, stmt); - } - return ret; + private boolean setVKUser(JdbcTemplate sql, String hash, int uid) { + return sql.update("UPDATE vk SET user_id=?,loginhash=NULL WHERE loginhash=?", uid, hash) > 0; } - private boolean setTelegramUser(Connection sql, String hash, int uid) { - boolean ret = false; - PreparedStatement stmt = null; - try { - stmt = sql.prepareStatement("UPDATE telegram SET user_id=?,loginhash=NULL WHERE loginhash=?"); - stmt.setInt(1, uid); - stmt.setString(2, hash); - stmt.executeUpdate(); - ret = true; - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(null, stmt); - } - return ret; + private boolean setTelegramUser(JdbcTemplate sql, String hash, int uid) { + return sql.update("UPDATE telegram SET user_id=?,loginhash=NULL WHERE loginhash=?", uid, hash) > 0; } - private String getJIDByHash(Connection sql, String hash) { - String ret = null; - - PreparedStatement stmt = null; - ResultSet rs = null; + private String getJIDByHash(JdbcTemplate sql, String hash) { try { - stmt = sql.prepareStatement("SELECT jid FROM jids WHERE loginhash=?"); - stmt.setString(1, hash); - rs = stmt.executeQuery(); - if (rs.first()) { - ret = rs.getString(1); - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); + return sql.queryForObject("SELECT jid FROM jids WHERE loginhash=?", String.class, hash); + } catch (EmptyResultDataAccessException e) { + return null; } - - return ret; } - private boolean setJIDUser(Connection sql, String hash, int uid) { - boolean ret = false; - PreparedStatement stmt = null; - try { - stmt = sql.prepareStatement("UPDATE jids SET user_id=?,loginhash=NULL WHERE loginhash=?"); - stmt.setInt(1, uid); - stmt.setString(2, hash); - stmt.executeUpdate(); - ret = true; - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(null, stmt); - } - return ret; + private boolean setJIDUser(JdbcTemplate sql, String hash, int uid) { + return sql.update("UPDATE jids SET user_id=?,loginhash=NULL WHERE loginhash=?", uid, hash) > 0; } } diff --git a/src/main/java/com/juick/http/www/TwitterAuth.java b/src/main/java/com/juick/http/www/TwitterAuth.java index ff02c28b..bbad4d83 100644 --- a/src/main/java/com/juick/http/www/TwitterAuth.java +++ b/src/main/java/com/juick/http/www/TwitterAuth.java @@ -7,6 +7,7 @@ import com.github.scribejava.core.oauth.OAuth10aService; import com.github.scribejava.core.oauth.OAuthService; import com.juick.server.UserQueries; import org.json.JSONObject; +import org.springframework.jdbc.core.JdbcTemplate; import javax.servlet.ServletException; import javax.servlet.http.Cookie; @@ -29,7 +30,7 @@ public class TwitterAuth { this.consumerSecret = consumerSecret; } - protected void doGet(Connection sql, HttpServletRequest request, HttpServletResponse response) + protected void doGet(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String hash = "", request_token = "", request_token_secret = ""; String verifier = request.getParameter("oauth_verifier"); diff --git a/src/main/java/com/juick/http/www/User.java b/src/main/java/com/juick/http/www/User.java index 84d1ad9a..a4fdcf21 100644 --- a/src/main/java/com/juick/http/www/User.java +++ b/src/main/java/com/juick/http/www/User.java @@ -20,21 +20,18 @@ package com.juick.http.www; import com.juick.server.MessagesQueries; import com.juick.server.TagQueries; import com.juick.server.UserQueries; +import org.springframework.jdbc.core.JdbcTemplate; + +import javax.servlet.ServletException; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import javax.servlet.ServletException; -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; /** * @@ -42,7 +39,7 @@ import javax.servlet.http.HttpServletResponse; */ public class User { - protected void doGetBlog(Connection sql, Connection sqlSearch, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException { + protected void doGetBlog(JdbcTemplate sql, JdbcTemplate sqlSearch, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); List mids; @@ -158,7 +155,7 @@ public class User { } } - protected void doGetTags(Connection sql, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException { + protected void doGetTags(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); if (visitor == null) { @@ -181,7 +178,7 @@ public class User { } } - protected void doGetFriends(Connection sql, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException { + protected void doGetFriends(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); if (visitor == null) { @@ -227,7 +224,7 @@ public class User { } } - protected void doGetReaders(Connection sql, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException { + protected void doGetReaders(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); if (visitor == null) { @@ -285,7 +282,7 @@ public class User { } } - public static void pageUserColumn(PrintWriter out, Connection sql, com.juick.User user, com.juick.User visitor) { + public static void pageUserColumn(PrintWriter out, JdbcTemplate sql, com.juick.User user, com.juick.User visitor) { out.println(""); } - public static String pageUserTags(Connection sql, com.juick.User user, com.juick.User visitor, int cnt) { + public static String pageUserTags(JdbcTemplate sql, com.juick.User user, com.juick.User visitor, int cnt) { com.juick.Tag tags[] = null; int maxUsageCnt = 0; diff --git a/src/main/java/com/juick/http/www/Utils.java b/src/main/java/com/juick/http/www/Utils.java index 931e2c21..ab721020 100644 --- a/src/main/java/com/juick/http/www/Utils.java +++ b/src/main/java/com/juick/http/www/Utils.java @@ -17,6 +17,8 @@ */ package com.juick.http.www; +import org.springframework.jdbc.core.JdbcTemplate; + import java.io.BufferedReader; import java.io.FileOutputStream; import java.io.IOException; @@ -77,7 +79,7 @@ public class Utils { return attachmentFName; } - public static com.juick.User getVisitorUser(Connection sql, HttpServletRequest request, HttpServletResponse response) { + public static com.juick.User getVisitorUser(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) { String hash = getCookie(request, "hash"); if (hash != null) { com.juick.User visitor = com.juick.server.UserQueries.getUserByHash(sql, hash); diff --git a/src/main/java/com/juick/http/www/VKontakteLogin.java b/src/main/java/com/juick/http/www/VKontakteLogin.java index 5f26fef1..8fad3b7a 100644 --- a/src/main/java/com/juick/http/www/VKontakteLogin.java +++ b/src/main/java/com/juick/http/www/VKontakteLogin.java @@ -18,19 +18,18 @@ package com.juick.http.www; import com.juick.server.UserQueries; -import java.io.IOException; -import java.net.URLEncoder; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.UUID; +import org.json.JSONException; +import org.json.JSONObject; +import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; + import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.json.JSONException; -import org.json.JSONObject; +import java.io.IOException; +import java.net.URLEncoder; +import java.util.UUID; /** * @@ -42,7 +41,7 @@ public class VKontakteLogin { private static final String VK_SECRET = "z2afNI8jA5lIpZ2jsTm1"; private static final String VK_REDIRECT = "http://juick.com/_vklogin"; - protected void doGet(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + protected void doGet(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String code = request.getParameter("code"); if (code == null || code.equals("")) { response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); @@ -114,42 +113,16 @@ public class VKontakteLogin { } } - private int getUIDbyVKID(Connection sql, long vkID) { - int uid = 0; - PreparedStatement stmt = null; - ResultSet rs = null; + private int getUIDbyVKID(JdbcTemplate sql, long vkID) { try { - stmt = sql.prepareStatement("SELECT user_id FROM vk WHERE vk_id=? AND user_id IS NOT NULL"); - stmt.setLong(1, vkID); - rs = stmt.executeQuery(); - if (rs.first()) { - uid = rs.getInt(1); - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); + return sql.queryForObject("SELECT user_id FROM vk WHERE vk_id=? AND user_id IS NOT NULL", Integer.class, vkID); + } catch (EmptyResultDataAccessException e) { + return 0; } - return uid; } - private boolean insertDB(Connection sql, long vkID, String loginhash, String token, String vkName, String vkLink) { - boolean ret = false; - PreparedStatement stmt = null; - try { - stmt = sql.prepareStatement("INSERT INTO vk(vk_id,loginhash,access_token,vk_name,vk_link) VALUES (?,?,?,?,?)"); - stmt.setLong(1, vkID); - stmt.setString(2, loginhash); - stmt.setString(3, token); - stmt.setString(4, vkName); - stmt.setString(5, vkLink); - stmt.executeUpdate(); - ret = true; - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(null, stmt); - } - return ret; + private boolean insertDB(JdbcTemplate sql, long vkID, String loginhash, String token, String vkName, String vkLink) { + return sql.update("INSERT INTO vk(vk_id,loginhash,access_token,vk_name,vk_link) VALUES (?,?,?,?,?)", + vkID, loginhash, token, vkName, vkLink) > 0; } } -- cgit v1.2.3 From 606f43f50904f7d811cf9b438e0e820e586be900 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 27 Jun 2016 13:16:43 +0300 Subject: spring-jdbc near complete --- deps/com.juick.server | 2 +- src/main/java/com/juick/CrosspostComponent.java | 41 ++++--- src/main/java/com/juick/PushComponent.java | 92 ++++++++-------- src/main/java/com/juick/http/www/Errors.java | 2 +- src/main/java/com/juick/http/www/Main.java | 2 +- src/main/java/com/juick/http/www/PM.java | 16 ++- .../java/com/juick/http/www/PageTemplates.java | 30 ++---- src/main/java/com/juick/http/www/Settings.java | 7 +- src/main/java/com/juick/http/www/User.java | 100 ++++------------- src/main/java/com/juick/http/www/UserThread.java | 15 +-- .../com/juick/xmpp/extensions/JuickMessage.java | 17 ++- .../java/com/juick/xmpp/s2s/ConnectionRouter.java | 45 +++++--- src/main/java/com/juick/xmpp/s2s/JuickBot.java | 118 ++++++++++----------- .../java/com/juick/xmpp/s2s/XMPPComponent.java | 24 ++--- 14 files changed, 222 insertions(+), 289 deletions(-) (limited to 'src/main') diff --git a/deps/com.juick.server b/deps/com.juick.server index 9e740f48..6362eab3 160000 --- a/deps/com.juick.server +++ b/deps/com.juick.server @@ -1 +1 @@ -Subproject commit 9e740f48cca7332c67a165c6cf60e09bc549e686 +Subproject commit 6362eab34390b187944c7e3c12eb444e2fb2c455 diff --git a/src/main/java/com/juick/CrosspostComponent.java b/src/main/java/com/juick/CrosspostComponent.java index d5f13ab2..d7ee8724 100644 --- a/src/main/java/com/juick/CrosspostComponent.java +++ b/src/main/java/com/juick/CrosspostComponent.java @@ -24,6 +24,9 @@ import com.juick.xmpp.Stream; import com.juick.xmpp.StreamComponent; import com.juick.xmpp.extensions.JuickMessage; import org.apache.commons.codec.binary.Base64; +import org.apache.commons.lang3.tuple.Pair; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.datasource.DriverManagerDataSource; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; @@ -35,7 +38,6 @@ import java.net.Socket; import java.net.URL; import java.net.URLEncoder; import java.security.Key; -import java.sql.Connection; import java.sql.Driver; import java.sql.DriverManager; import java.sql.SQLException; @@ -61,7 +63,7 @@ public class CrosspostComponent implements ServletContextListener, Stream.Strea public final static String TWITTERURL = "https://api.twitter.com/1.1/statuses/update.json"; public final static String FBURL = "https://graph.facebook.com/me/feed"; public final static String VKURL = "https://api.vk.com/method/wall.post"; - Connection sql; + JdbcTemplate sql; Stream xmpp; String twitter_consumer_key; String twitter_consumer_secret; @@ -80,8 +82,7 @@ public class CrosspostComponent implements ServletContextListener, Stream.Strea twitter_consumer_key = conf.getProperty("twitter_consumer_key", ""); twitter_consumer_secret = conf.getProperty("twitter_consumer_secret", ""); - setupSql(conf.getProperty("mysql_host"), conf.getProperty("mysql_username"), - conf.getProperty("mysql_password", ""), conf.getProperty("mysql_database", "")); + setupSql(conf.getProperty("datasource_driver", "com.mysql.jdbc.Driver"), conf.getProperty("datasource_url", "")); setupXmppComponent(conf.getProperty("xmpp_password", "")); } catch (Exception e) { logger.log(Level.SEVERE, e.getMessage(), e); @@ -115,15 +116,11 @@ public class CrosspostComponent implements ServletContextListener, Stream.Strea logger.info("component destroyed"); } - public void setupSql(String host, String username, String password, String database) { - try { - Class.forName("com.mysql.jdbc.Driver"); - sql = DriverManager.getConnection( - String.format("jdbc:mysql://%s/%s?autoReconnect=true&user=%s&password=%s", - host, database, username, password)); - } catch (SQLException | ClassNotFoundException e) { - logger.log(Level.SEVERE, e.getMessage(), e); - } + public void setupSql(String driver, String url) { + DriverManagerDataSource dataSource = new DriverManagerDataSource(); + dataSource.setDriverClassName(driver); + dataSource.setUrl(url); + sql = new JdbcTemplate(dataSource); } public void setupXmppComponent(String password) { @@ -161,8 +158,8 @@ public class CrosspostComponent implements ServletContextListener, Stream.Strea } public boolean facebookPost(com.juick.Message jmsg) { - String token = CrosspostQueries.getFacebookToken(sql, jmsg.getUser().getUID()); - if (token == null) { + String token = CrosspostQueries.getFacebookToken(sql, jmsg.getUser().getUID()).orElse(""); + if (token.isEmpty()) { return false; } @@ -198,8 +195,8 @@ public class CrosspostComponent implements ServletContextListener, Stream.Strea } public boolean vkontaktePost(com.juick.Message jmsg) { - String tokens[] = CrosspostQueries.getVKTokens(sql, jmsg.getUser().getUID()); - if (tokens == null || tokens.length != 2) { + Pair tokens = CrosspostQueries.getVKTokens(sql, jmsg.getUser().getUID()).orElse(Pair.of("", "")); + if (tokens.getLeft().isEmpty() || tokens.getRight().isEmpty()) { return false; } @@ -209,7 +206,7 @@ public class CrosspostComponent implements ServletContextListener, Stream.Strea boolean ret = false; try { - String body = "owner_id=" + tokens[0] + "&access_token=" + URLEncoder.encode(tokens[1], "UTF-8") + "&from_group=1&message=" + URLEncoder.encode(status, "UTF-8"); + String body = "owner_id=" + tokens.getLeft() + "&access_token=" + URLEncoder.encode(tokens.getRight(), "UTF-8") + "&from_group=1&message=" + URLEncoder.encode(status, "UTF-8"); HttpsURLConnection conn = (HttpsURLConnection) new URL(VKURL).openConnection(); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); @@ -235,12 +232,12 @@ public class CrosspostComponent implements ServletContextListener, Stream.Strea } public boolean twitterPost(com.juick.Message jmsg) { - String tokens[] = CrosspostQueries.getTwitterTokens(sql, jmsg.getUser().getUID()); - if (tokens == null || tokens.length != 2) { + Pair tokens = CrosspostQueries.getTwitterTokens(sql, jmsg.getUser().getUID()).orElse(Pair.of("", "")); + if (tokens.getLeft().isEmpty() || tokens.getRight().isEmpty()) { return false; } - String token = percentEncode(tokens[0]); - String token_secret = percentEncode(tokens[1]); + String token = percentEncode(tokens.getLeft()); + String token_secret = percentEncode(tokens.getRight()); logger.info("TWITTER: #" + jmsg.getMID()); diff --git a/src/main/java/com/juick/PushComponent.java b/src/main/java/com/juick/PushComponent.java index 0cb8e626..e33d5fc3 100644 --- a/src/main/java/com/juick/PushComponent.java +++ b/src/main/java/com/juick/PushComponent.java @@ -24,6 +24,8 @@ import com.google.android.gcm.server.Sender; import com.juick.json.MessageSerializer; import com.juick.server.MessagesQueries; import com.juick.server.PushQueries; +import com.juick.server.SubscriptionsQueries; +import com.juick.server.UserQueries; import com.juick.xmpp.JID; import com.juick.xmpp.Message.MessageListener; import com.juick.xmpp.Stream; @@ -45,6 +47,8 @@ import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.apache.http.util.TextUtils; import org.json.JSONObject; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.datasource.DriverManagerDataSource; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; @@ -60,6 +64,7 @@ import java.util.concurrent.Executors; import java.util.logging.Level; import java.util.logging.LogManager; import java.util.logging.Logger; +import java.util.stream.Collectors; /** * @@ -72,7 +77,7 @@ public class PushComponent implements ServletContextListener, Stream.StreamListe private ExecutorService executorService; String wns_application_sip; String wns_client_secret; - Connection sql; + JdbcTemplate sql; Socket socket; Stream xmpp; Sender GCMSender; @@ -91,8 +96,7 @@ public class PushComponent implements ServletContextListener, Stream.StreamListe wns_client_secret = conf.getProperty("wns_client_secret", ""); GCMSender = new Sender(conf.getProperty("gcm_key")); - setupSql(conf.getProperty("mysql_host"), conf.getProperty("mysql_username"), - conf.getProperty("mysql_password", ""), conf.getProperty("mysql_database", "")); + setupSql(conf.getProperty("datasource_driver", "com.mysql.jdbc.Driver"), conf.getProperty("datasource_url", "")); setupXmppComponent(new JID("", conf.getProperty("push_jid"), ""), conf.getProperty("xmpp_host", "localhost"), Integer.parseInt(conf.getProperty("xmpp_port", "5347")), conf.getProperty("push_xmpp_password", "")); } catch (IOException e) { @@ -127,15 +131,11 @@ public class PushComponent implements ServletContextListener, Stream.StreamListe logger.info("component destroyed"); } - public void setupSql(String host, String username, String password, String database) { - try { - Class.forName("com.mysql.jdbc.Driver"); - sql = DriverManager.getConnection( - String.format("jdbc:mysql://%s/%s?autoReconnect=true&user=%s&password=%s", - host, database, username, password)); - } catch (SQLException | ClassNotFoundException e) { - logger.log(Level.SEVERE, e.getMessage(), e); - } + public void setupSql(String driver, String url) { + DriverManagerDataSource dataSource = new DriverManagerDataSource(); + dataSource.setDriverClassName(driver); + dataSource.setUrl(url); + sql = new JdbcTemplate(dataSource); } public void setupXmppComponent(JID jid, String host, int port, String password) { @@ -157,42 +157,36 @@ public class PushComponent implements ServletContextListener, Stream.StreamListe } @Override - public void onStreamFail(String e) {logger.log(Level.SEVERE, "XMPP STREAM FAIL", e);} + public void onStreamFail(Exception e) {logger.log(Level.SEVERE, "XMPP STREAM FAIL", e);} @Override public void onMessage(com.juick.xmpp.Message msg) { - JuickMessage jmsg = (JuickMessage) msg.getChild(JuickMessage.XMLNS); - if (jmsg == null) { - return; - } - logger.info("Message to push: " + msg.toString()); - + JuickMessage jmsg = (JuickMessage)msg.getChild(JuickMessage.XMLNS); + List subscribedUsers = new ArrayList<>(); boolean isPM = jmsg.getMID() == 0; boolean isReply = jmsg.getRID() > 0; - int senderID = 0, recipientID = 0; - if (isReply) { - senderID = jmsg.getUser().getUID(); - } + int pmTo = 0; if (isPM) { - // PM - try { - recipientID = Integer.parseInt(msg.to.Username); - } catch (NumberFormatException e) { - logger.info("Wrong PM recipient: " + msg.to.Username); - return; + pmTo = Integer.parseInt(msg.to.Username); + } else { + if (isReply) { + subscribedUsers = + SubscriptionsQueries.getUsersSubscribedToComments(sql, jmsg.getMID(), jmsg.getUser().getUID()); + } else { + // new message + subscribedUsers = SubscriptionsQueries.getSubscribedUsers(sql, jmsg.getUser().getUID(), jmsg.getMID()); } } /*** ANDROID ***/ - List regids; + final List regids = new ArrayList<>(); if (isPM) { - regids = new ArrayList<>(); - String targetId = PushQueries.getAndroidRegID(sql, recipientID); - if (targetId != null && !targetId.isEmpty()) { - regids.add(targetId); - } + PushQueries.getAndroidRegID(sql, pmTo).ifPresent(regids::add); } else { - regids = isReply ? PushQueries.getAndroidSubscribersToComments(sql, jmsg.getMID(), senderID) : PushQueries.getAndroidSubscribers(sql, senderID); + List uids = subscribedUsers.stream().map(User::getUID).collect(Collectors.toList()); + if (uids.size() > 0) { + regids.addAll(PushQueries.getAndroidTokens(sql, uids)); + } } if (!regids.isEmpty()) { @@ -216,15 +210,14 @@ public class PushComponent implements ServletContextListener, Stream.StreamListe } /*** WinPhone ***/ - List urls; + final List urls = new ArrayList<>(); if (isPM) { - urls = new ArrayList<>(); - String targetURL = PushQueries.getWinPhoneURL(sql, recipientID); - if (!TextUtils.isEmpty(targetURL)) { - urls.add(targetURL); - } + PushQueries.getWinPhoneURL(sql, pmTo).ifPresent(urls::add); } else { - urls = isReply ? PushQueries.getWindowsSubscribersToComments(sql, jmsg.getMID(), senderID) :PushQueries.getWinPhoneSubscribers(sql, senderID); + List uids = subscribedUsers.stream().map(User::getUID).collect(Collectors.toList()); + if (uids.size() > 0) { + urls.addAll(PushQueries.getWindowsTokens(sql, uids)); + } } @@ -262,15 +255,14 @@ public class PushComponent implements ServletContextListener, Stream.StreamListe } /*** iOS ***/ - List tokens; + final List tokens = new ArrayList<>(); if (isPM) { - tokens = new ArrayList<>(); - String targetToken = PushQueries.getAPNSToken(sql, recipientID); - if (targetToken != null && !targetToken.isEmpty()) { - tokens.add(targetToken); - } + PushQueries.getAPNSToken(sql, pmTo).ifPresent(tokens::add); } else { - tokens = isReply ? PushQueries.getAppleSubscribersToComments(sql, jmsg.getMID(), senderID) : PushQueries.getAPNSSubscribers(sql, senderID); + List uids = subscribedUsers.stream().map(User::getUID).collect(Collectors.toList()); + if (uids.size() > 0) { + tokens.addAll(PushQueries.getAPNSTokens(sql, uids)); + } } if (!tokens.isEmpty()) { ApnsService service = APNS.newService().withCert("/etc/juick/ios.p12", "juick") diff --git a/src/main/java/com/juick/http/www/Errors.java b/src/main/java/com/juick/http/www/Errors.java index 2161daf1..0044c209 100644 --- a/src/main/java/com/juick/http/www/Errors.java +++ b/src/main/java/com/juick/http/www/Errors.java @@ -20,7 +20,7 @@ public class Errors { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); if (tagsHTML == null) { - tagsHTML = PageTemplates.getPopularTags(sql, 80); + tagsHTML = PageTemplates.formatPopularTags(sql, 80); } response.setStatus(404); diff --git a/src/main/java/com/juick/http/www/Main.java b/src/main/java/com/juick/http/www/Main.java index 604ccf9a..63bf1c1f 100644 --- a/src/main/java/com/juick/http/www/Main.java +++ b/src/main/java/com/juick/http/www/Main.java @@ -104,7 +104,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { } @Override - public void onStreamFail(String e) {log("XMPP STREAM FAIL:" + e);} + public void onStreamFail(Exception e) {log("XMPP STREAM FAIL:" + e);} @Override public void onStreamReady() { diff --git a/src/main/java/com/juick/http/www/PM.java b/src/main/java/com/juick/http/www/PM.java index b64e6c43..b351eb0e 100644 --- a/src/main/java/com/juick/http/www/PM.java +++ b/src/main/java/com/juick/http/www/PM.java @@ -25,14 +25,12 @@ import com.juick.xmpp.Stream; import com.juick.xmpp.extensions.JuickMessage; import org.springframework.jdbc.core.JdbcTemplate; -import java.io.IOException; -import java.io.PrintWriter; -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; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.List; /** * @@ -74,7 +72,7 @@ public class PM { out.println("
\""
"); out.println("
"); out.println(" "); - out.println(" "); + out.println(" "); out.println("
" + txt + "
"); out.println("
"); @@ -148,7 +146,7 @@ public class PM { out.println("
"); out.println("
"); out.println(" "); - out.println(" "); + out.println(" "); out.println("
" + txt + "
"); out.println("
"); out.println(" "); @@ -204,8 +202,8 @@ public class PM { msg.to.Host = "ws.juick.com"; xmpp.send(msg); - String jid = UserQueries.getJIDbyUID(sql, uid); - if (jid != null) { + List jids = UserQueries.getJIDsbyUID(sql, uid); + for (String jid : jids) { Message mm = new Message(); mm.to = new JID(jid); mm.type = Message.Type.chat; diff --git a/src/main/java/com/juick/http/www/PageTemplates.java b/src/main/java/com/juick/http/www/PageTemplates.java index 89b0c737..7021ccba 100644 --- a/src/main/java/com/juick/http/www/PageTemplates.java +++ b/src/main/java/com/juick/http/www/PageTemplates.java @@ -50,7 +50,7 @@ import ru.sape.Sape; public class PageTemplates { public static Sape sape = null; - private static final SimpleDateFormat sdfSQL = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + protected static final SimpleDateFormat sdfSQL = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private static SimpleDateFormat sdfSimple = new SimpleDateFormat("d MMM"); private static SimpleDateFormat sdfFull = new SimpleDateFormat("d MMM yyyy"); private static String tagsHTML = null; @@ -226,7 +226,7 @@ public class PageTemplates { return ret; } - public static String formatDate(int minutes, String fulldate) { + public static String formatDate(int minutes, Date fulldate) { if (minutes < 1) { return "сейчас"; } else if (minutes < 60) { @@ -265,17 +265,16 @@ public class PageTemplates { } return days + " " + unit + " назад"; } else { - String ret = fulldate; + String ret = sdfFull.format(fulldate); synchronized (sdfSQL) { try { - Date pDate = sdfSQL.parse(fulldate); Calendar c = Calendar.getInstance(); int curyear = c.get(Calendar.YEAR); - c.setTime(pDate); + c.setTime(fulldate); if (c.get(Calendar.YEAR) == curyear) { - ret = sdfSimple.format(pDate); + ret = sdfSimple.format(fulldate); } else { - ret = sdfFull.format(pDate); + ret = sdfFull.format(fulldate); } } catch (Exception e) { System.err.println("PARSE EXCEPTION: " + fulldate); @@ -285,20 +284,11 @@ public class PageTemplates { } } - public static String formatJSLocalTime(String ts) { - String ret = ""; - synchronized (sdfSQL) { - try { - Date date = sdfSQL.parse(ts); - ret = ""; - } catch (Exception e) { - System.err.println("PARSE EXCEPTION: " + ts); - } - } - return ret; } public static String formatReplies(int replies) { @@ -452,7 +442,7 @@ public class PageTemplates { out.println("
"); out.println(" "); out.println("
@" + msg.getUser().getUName() + ":" + tagsStr + "
"); - out.println("
"); + out.println("
"); if (msg.AttachmentType != null) { String fname = msg.getMID() + "." + msg.AttachmentType; out.println("

\"\"/

"); diff --git a/src/main/java/com/juick/http/www/Settings.java b/src/main/java/com/juick/http/www/Settings.java index f197ff12..54ee0ee9 100644 --- a/src/main/java/com/juick/http/www/Settings.java +++ b/src/main/java/com/juick/http/www/Settings.java @@ -19,15 +19,12 @@ package com.juick.http.www; import org.springframework.jdbc.core.JdbcTemplate; -import java.io.IOException; -import java.io.PrintWriter; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; /** * diff --git a/src/main/java/com/juick/http/www/User.java b/src/main/java/com/juick/http/www/User.java index a4fdcf21..d0c1aaee 100644 --- a/src/main/java/com/juick/http/www/User.java +++ b/src/main/java/com/juick/http/www/User.java @@ -17,6 +17,7 @@ */ package com.juick.http.www; +import com.juick.Tag; import com.juick.server.MessagesQueries; import com.juick.server.TagQueries; import com.juick.server.UserQueries; @@ -195,25 +196,14 @@ public class User { out.println("
"); out.println(""); - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT users.id,users.nick FROM subscr_users INNER JOIN users ON subscr_users.user_id=users.id WHERE subscr_users.suser_id=? ORDER BY users.nick"); - stmt.setInt(1, user.getUID()); - rs = stmt.executeQuery(); - rs.beforeFirst(); - int cnt = 0; - while (rs.next()) { - if (cnt % 3 == 0 && cnt > 0) { - out.print(""); - } - out.print(""); - cnt++; + List friends = UserQueries.getUserFriends(sql, user.getUID()); + for (int i = 0; i < friends.size(); i++) { + if (i % 3 == 0 && i > 0) { + out.print(""); } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); + out.print(""); } out.println("
" + rs.getString(2) + "
" + + friends.get(i).getUName() + "
"); @@ -241,25 +231,14 @@ public class User { out.println("
"); out.println(""); - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT users.id,users.nick FROM subscr_users INNER JOIN users ON subscr_users.suser_id=users.id WHERE subscr_users.user_id=? ORDER BY users.nick"); - stmt.setInt(1, user.getUID()); - rs = stmt.executeQuery(); - rs.beforeFirst(); - int cnt = 0; - while (rs.next()) { - if (cnt % 3 == 0 && cnt > 0) { - out.print(""); - } - out.print(""); - cnt++; + List readers = UserQueries.getUserReaders(sql, user.getUID()); + for (int i = 0; i < readers.size(); i++) { + if (i % 3 == 0 && i > 0) { + out.print(""); } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); + out.print(""); } out.println("
" + rs.getString(2) + "
" + + readers.get(i).getUName() + "
"); @@ -340,55 +319,20 @@ public class User { } public static String pageUserTags(JdbcTemplate sql, com.juick.User user, com.juick.User visitor, int cnt) { - com.juick.Tag tags[] = null; - - int maxUsageCnt = 0; - PreparedStatement stmt = null; - ResultSet rs = null; - try { - if (cnt > 0) { - stmt = sql.prepareStatement("SELECT tags.name AS name,COUNT(DISTINCT messages_tags.message_id) AS cnt FROM (messages INNER JOIN messages_tags ON (messages.message_id=messages_tags.message_id)) INNER JOIN tags ON messages_tags.tag_id=tags.tag_id WHERE messages.user_id=? GROUP BY messages_tags.tag_id ORDER BY cnt DESC LIMIT ?", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); - stmt.setInt(1, user.getUID()); - stmt.setInt(2, cnt); - } else { - stmt = sql.prepareStatement("SELECT tags.name AS name,COUNT(DISTINCT messages_tags.message_id) AS cnt FROM (messages INNER JOIN messages_tags ON (messages.message_id=messages_tags.message_id)) INNER JOIN tags ON messages_tags.tag_id=tags.tag_id WHERE messages.user_id=? GROUP BY messages_tags.tag_id ORDER BY cnt DESC", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); - stmt.setInt(1, user.getUID()); - } - rs = stmt.executeQuery(); - rs.last(); - tags = new com.juick.Tag[rs.getRow()]; - rs.beforeFirst(); - cnt = 0; - while (rs.next()) { - tags[cnt] = new com.juick.Tag(); - tags[cnt].Name = rs.getString(1); - tags[cnt].UsageCnt = rs.getInt(2); - if (tags[cnt].UsageCnt > maxUsageCnt) { - maxUsageCnt = tags[cnt].UsageCnt; - } - cnt++; - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); - } - - if (tags != null && cnt > 0) { - Arrays.sort(tags, 0, cnt); - } - + List tags = TagQueries.getUserTagsAll(sql, user.getUID()); + int maxUsageCnt = tags.stream().map(t -> t.UsageCnt).max(Integer::max).get(); String ret = ""; for (int i = 0; i < cnt; i++) { - String tag = Utils.encodeHTML(tags[i].Name); + String tag = Utils.encodeHTML(tags.get(i).Name); try { - tag = "" + tag + ""; + tag = "" + tag + ""; } catch (UnsupportedEncodingException e) { } - if (tags[i].UsageCnt > maxUsageCnt / 3 * 2) { + if (tags.get(i).UsageCnt > maxUsageCnt / 3 * 2) { ret += "" + tag + " "; - } else if (tags[i].UsageCnt > maxUsageCnt / 3) { + } else if (tags.get(i).UsageCnt > maxUsageCnt / 3) { ret += "" + tag + " "; } else { ret += tag + " "; diff --git a/src/main/java/com/juick/http/www/UserThread.java b/src/main/java/com/juick/http/www/UserThread.java index 4ee290ab..638e3a3b 100644 --- a/src/main/java/com/juick/http/www/UserThread.java +++ b/src/main/java/com/juick/http/www/UserThread.java @@ -21,9 +21,10 @@ import com.juick.Message; import com.juick.Tag; import com.juick.server.MessagesQueries; import com.juick.server.UserQueries; +import org.springframework.jdbc.core.JdbcTemplate; + import java.io.IOException; import java.io.PrintWriter; -import java.sql.Connection; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; @@ -36,7 +37,7 @@ import javax.servlet.http.HttpServletResponse; */ public class UserThread { - protected void doGetThread(Connection sql, HttpServletRequest request, HttpServletResponse response, int MID) throws ServletException, IOException { + protected void doGetThread(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, int MID) throws ServletException, IOException { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); if (!MessagesQueries.canViewThread(sql, MID, visitor != null ? visitor.getUID() : 0)) { @@ -98,7 +99,7 @@ public class UserThread { } } - public static com.juick.Message printMessage(PrintWriter out, Connection sql, com.juick.Message msg, com.juick.User visitor) { + public static com.juick.Message printMessage(PrintWriter out, JdbcTemplate sql, com.juick.Message msg, com.juick.User visitor) { msg.VisitorCanComment = visitor != null; List tags = MessagesQueries.getMessageTags(sql, msg.getMID()); @@ -128,7 +129,7 @@ public class UserThread { out.println("
"); out.println("
"); out.println(" "); - out.println("
" + PageTemplates.formatJSLocalTime(msg.TimestampString) + "
"); + out.println("
" + PageTemplates.formatJSLocalTime(msg.getDate()) + "
"); out.println("
" + txt + "
"); if (msg.AttachmentType != null) { @@ -189,7 +190,7 @@ public class UserThread { return msg; } - public static void printReplies(PrintWriter out, Connection sql, com.juick.Message msg, com.juick.User visitor, boolean listview) { + public static void printReplies(PrintWriter out, JdbcTemplate sql, com.juick.Message msg, com.juick.User visitor, boolean listview) { List replies = MessagesQueries.getReplies(sql, msg.getMID()); List blUIDs = new ArrayList(); @@ -297,7 +298,7 @@ public class UserThread { } else { out.println("
[удалено]:
"); } - out.println(" "); + out.println(" "); out.println("
" + PageTemplates.formatMessage(msg.getText()) + "
"); if (msg.AttachmentType != null) { out.println("
\"\"/
"); @@ -341,7 +342,7 @@ public class UserThread { } else { out.println("
[удалено]:
"); } - out.println(" "); + out.println(" "); out.println("
" + PageTemplates.formatMessage(msg.getText()) + "
"); if (msg.AttachmentType != null) { out.println("
\"\"/
"); diff --git a/src/main/java/com/juick/xmpp/extensions/JuickMessage.java b/src/main/java/com/juick/xmpp/extensions/JuickMessage.java index 53dd6deb..220caba7 100644 --- a/src/main/java/com/juick/xmpp/extensions/JuickMessage.java +++ b/src/main/java/com/juick/xmpp/extensions/JuickMessage.java @@ -20,6 +20,10 @@ package com.juick.xmpp.extensions; import com.juick.xmpp.utils.XmlUtils; import com.juick.xmpp.*; import java.io.IOException; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.TimeZone; + import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; @@ -31,12 +35,17 @@ public class JuickMessage extends com.juick.Message implements StanzaChild { public final static String XMLNS = "http://juick.com/message"; public final static String TagName = "juick"; + private SimpleDateFormat df; public JuickMessage() { + df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + df.setTimeZone(TimeZone.getTimeZone("UTC")); } public JuickMessage(com.juick.Message msg) { super(msg); + df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + df.setTimeZone(TimeZone.getTimeZone("UTC")); } @Override @@ -45,7 +54,7 @@ public class JuickMessage extends com.juick.Message implements StanzaChild { } @Override - public JuickMessage parse(XmlPullParser parser) throws XmlPullParserException, IOException { + public JuickMessage parse(XmlPullParser parser) throws XmlPullParserException, IOException, ParseException { JuickMessage jmsg = new JuickMessage(); final String sMID = parser.getAttributeValue(null, "mid"); @@ -72,7 +81,7 @@ public class JuickMessage extends com.juick.Message implements StanzaChild { if (sReadOnly != null) { jmsg.ReadOnly = true; } - jmsg.TimestampString = parser.getAttributeValue(null, "ts"); + jmsg.setDate(df.parse(parser.getAttributeValue(null, "ts"))); jmsg.AttachmentType = parser.getAttributeValue(null, "attach"); while (parser.next() == XmlPullParser.START_TAG) { @@ -112,8 +121,8 @@ public class JuickMessage extends com.juick.Message implements StanzaChild { if (ReadOnly) { ret += " readonly=\"1\""; } - if (TimestampString != null) { - ret += " ts=\"" + TimestampString + "\""; + if (getDate() != null) { + ret += " ts=\"" + df.format(getDate()) + "\""; } if (AttachmentType != null) { ret += " attach=\"" + AttachmentType + "\""; diff --git a/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java b/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java index 443bfa82..d0b3a93b 100644 --- a/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java +++ b/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java @@ -1,7 +1,9 @@ package com.juick.xmpp.s2s; +import com.juick.User; import com.juick.server.MessagesQueries; import com.juick.server.SubscriptionsQueries; +import com.juick.server.UserQueries; import com.juick.xmpp.*; import com.juick.xmpp.extensions.JuickMessage; import com.juick.xmpp.extensions.Nickname; @@ -9,6 +11,8 @@ import com.juick.xmpp.extensions.XOOB; import java.io.IOException; import java.net.Socket; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; @@ -45,13 +49,18 @@ public class ConnectionRouter implements Stream.StreamListener, } public void sendJuickMessage(JuickMessage jmsg) { - List jids; + List jids = new ArrayList<>(); synchronized (XMPPComponent.sqlSync) { if (jmsg.FriendsOnly) { jids = SubscriptionsQueries.getJIDSubscribedToUser(XMPPComponent.sql, jmsg.getUser().getUID(), jmsg.FriendsOnly); } else { - jids = SubscriptionsQueries.getJIDSubscribedToUserAndTags(XMPPComponent.sql, jmsg.getUser().getUID(), jmsg.getMID()); + List users = SubscriptionsQueries.getSubscribedUsers(XMPPComponent.sql, jmsg.getUser().getUID(), jmsg.getMID()); + for (User user : users) { + for (String jid : UserQueries.getJIDsbyUID(XMPPComponent.sql, user.getUID())) { + jids.add(jid); + } + } } } @@ -86,14 +95,14 @@ public class ConnectionRouter implements Stream.StreamListener, } public void sendJuickComment(JuickMessage jmsg) { - List jids; + List users; String replyQuote; String replyTo; synchronized (XMPPComponent.sqlSync) { - jids = SubscriptionsQueries.getJIDSubscribedToComments(XMPPComponent.sql, jmsg.getMID(), jmsg.getUser().getUID()); - com.juick.Message replyMessage = jmsg.ReplyTo > 0 ? MessagesQueries.getReply(XMPPComponent.sql, jmsg.getMID(), jmsg.ReplyTo) - : MessagesQueries.getMessage(XMPPComponent.sql, jmsg.getMID()); + users = SubscriptionsQueries.getUsersSubscribedToComments(XMPPComponent.sql, jmsg.getMID(), jmsg.getUser().getUID()); + com.juick.Message replyMessage = jmsg.ReplyTo > 0 ? MessagesQueries.getReply(XMPPComponent.sql, jmsg.getMID(), jmsg.ReplyTo) + : MessagesQueries.getMessage(XMPPComponent.sql, jmsg.getMID()); replyTo = replyMessage.getUser().getUName(); replyQuote = getReplyQuote(replyMessage); } @@ -110,9 +119,13 @@ public class ConnectionRouter implements Stream.StreamListener, msg.body = txt; msg.type = Message.Type.chat; msg.addChild(jmsg); - for (String jid : jids) { - msg.to = new JID(jid); - XMPPComponent.sendOut(msg); + for (User user : users) { + synchronized (XMPPComponent.sqlSync) { + for (String jid : UserQueries.getJIDsbyUID(XMPPComponent.sql, user.getUID())) { + msg.to = new JID(jid); + XMPPComponent.sendOut(msg); + } + } } } @@ -127,11 +140,11 @@ public class ConnectionRouter implements Stream.StreamListener, } public void sendJuickRecommendation(JuickMessage recomm) { - List jids; + List users; JuickMessage jmsg; synchronized (XMPPComponent.sqlSync) { jmsg = new JuickMessage(MessagesQueries.getMessage(XMPPComponent.sql, recomm.getMID())); - jids = SubscriptionsQueries.getJIDSubscribedToUserRecommendations(XMPPComponent.sql, + users = SubscriptionsQueries.getUsersSubscribedToUserRecommendations(XMPPComponent.sql, recomm.getUser().getUID(), recomm.getMID(), jmsg.getUser().getUID()); } @@ -168,9 +181,13 @@ public class ConnectionRouter implements Stream.StreamListener, msg.addChild(oob); } - for (String jid : jids) { - msg.to = new JID(jid); - XMPPComponent.sendOut(msg); + for (User user : users) { + synchronized (XMPPComponent.sqlSync) { + for (String jid : UserQueries.getJIDsbyUID(XMPPComponent.sql, user.getUID())) { + msg.to = new JID(jid); + XMPPComponent.sendOut(msg); + } + } } } diff --git a/src/main/java/com/juick/xmpp/s2s/JuickBot.java b/src/main/java/com/juick/xmpp/s2s/JuickBot.java index 25c75dfe..1172d75f 100644 --- a/src/main/java/com/juick/xmpp/s2s/JuickBot.java +++ b/src/main/java/com/juick/xmpp/s2s/JuickBot.java @@ -194,7 +194,7 @@ public class JuickBot { m.to = new JID(Integer.toString(uid_to), "push.juick.com", null); JuickMessage jmsg = new JuickMessage(); synchronized (XMPPComponent.sqlSync) { - jmsg.setUser(UserQueries.getUserByUID(XMPPComponent.sql, user_from.getUID())); + jmsg.setUser(user_from); } jmsg.setText(msg.body); m.childs.add(jmsg); @@ -203,29 +203,25 @@ public class JuickBot { m.to.Host = "ws.juick.com"; XMPPComponent.connRouter.router.send(m.toString()); - String jid; + List jids; boolean inroster = false; synchronized (XMPPComponent.sqlSync) { - jid = UserQueries.getJIDbyUID(XMPPComponent.sql, uid_to); - if (jid != null) { + jids = UserQueries.getJIDsbyUID(XMPPComponent.sql, uid_to); + for (String jid : jids) { + Message mm = new Message(); + mm.to = new JID(jid); + mm.type = Message.Type.chat; inroster = PMQueries.havePMinRoster(XMPPComponent.sql, user_from.getUID(), jid); + if (inroster) { + mm.from = new JID(jmsg.getUser().getUName(), "juick.com", "Juick"); + mm.body = msg.body; + } else { + mm.from = new JID("juick", "juick.com", "Juick"); + mm.body = "Private message from @" + jmsg.getUser().getUName() + ":\n" + msg.body; + } + XMPPComponent.sendOut(mm); } } - - if (jid != null) { - Message mm = new Message(); - mm.to = new JID(jid); - mm.type = Message.Type.chat; - if (inroster) { - mm.from = new JID(jmsg.getUser().getUName(), "juick.com", "Juick"); - mm.body = msg.body; - } else { - mm.from = new JID("juick", "juick.com", "Juick"); - mm.body = "Private message from @" + jmsg.getUser().getUName() + ":\n" + msg.body; - } - XMPPComponent.sendOut(mm); - } - } else { Message reply = new Message(msg.to, msg.from, Message.Type.error); reply.id = msg.id; @@ -300,7 +296,7 @@ public class JuickBot { int ret = 0; int uid_to = 0; - String jid_to = null; + List jids_to = null; boolean haveInRoster = false; synchronized (XMPPComponent.sqlSync) { @@ -313,10 +309,7 @@ public class JuickBot { if (uid_to > 0) { if (!UserQueries.isInBLAny(XMPPComponent.sql, uid_to, user_from.getUID())) { if (PMQueries.createPM(XMPPComponent.sql, user_from.getUID(), uid_to, body)) { - jid_to = UserQueries.getJIDbyUID(XMPPComponent.sql, uid_to); - if (jid_to != null) { - haveInRoster = PMQueries.havePMinRoster(XMPPComponent.sql, user_from.getUID(), jid_to); - } + jids_to = UserQueries.getJIDsbyUID(XMPPComponent.sql, uid_to); ret = 200; } else { ret = 500; @@ -327,45 +320,46 @@ public class JuickBot { } else { ret = 404; } - } - if (ret == 200) { - Message msg = new Message(); - msg.from = new JID("juick", "juick.com", null); - msg.to = new JID(Integer.toString(uid_to), "push.juick.com", null); - JuickMessage jmsg = new JuickMessage(); - jmsg.setUser(user_from); - jmsg.setText(body); - msg.childs.add(jmsg); - XMPPComponent.connRouter.router.send(msg.toString()); - - msg.to.Host = "ws.juick.com"; - XMPPComponent.connRouter.router.send(msg.toString()); - - if (jid_to != null) { - Message mm = new Message(); - mm.to = new JID(jid_to); - mm.type = Message.Type.chat; - if (haveInRoster) { - mm.from = new JID(user_from.getUName(), "juick.com", "Juick"); - mm.body = body; - } else { - mm.from = new JID("juick", "juick.com", "Juick"); - mm.body = "Private message from @" + user_from.getUName() + ":\n" + body; + if (ret == 200) { + Message msg = new Message(); + msg.from = new JID("juick", "juick.com", null); + msg.to = new JID(Integer.toString(uid_to), "push.juick.com", null); + JuickMessage jmsg = new JuickMessage(); + jmsg.setUser(user_from); + jmsg.setText(body); + msg.childs.add(jmsg); + XMPPComponent.connRouter.router.send(msg.toString()); + + msg.to.Host = "ws.juick.com"; + XMPPComponent.connRouter.router.send(msg.toString()); + + for (String jid : jids_to) { + Message mm = new Message(); + mm.to = new JID(jid); + mm.type = Message.Type.chat; + haveInRoster = PMQueries.havePMinRoster(XMPPComponent.sql, user_from.getUID(), jid); + if (haveInRoster) { + mm.from = new JID(user_from.getUName(), "juick.com", "Juick"); + mm.body = body; + } else { + mm.from = new JID("juick", "juick.com", "Juick"); + mm.body = "Private message from @" + user_from.getUName() + ":\n" + body; + } + XMPPComponent.sendOut(mm); } - XMPPComponent.sendOut(mm); } - } - Message reply = new Message(m.to, m.from); - if (ret == 200) { - reply.type = m.type; - reply.body = "Private message sent"; - } else { - reply.type = Message.Type.error; - reply.body = "Error " + ret; + Message reply = new Message(m.to, m.from); + if (ret == 200) { + reply.type = m.type; + reply.body = "Private message sent"; + } else { + reply.type = Message.Type.error; + reply.body = "Error " + ret; + } + XMPPComponent.sendOut(reply); } - XMPPComponent.sendOut(reply); } private static void commandBLShow(Message m, User user_from) throws Exception { @@ -379,8 +373,8 @@ public class JuickBot { String txt = ""; if (bltags.size() > 0) { - for (int i = 0; i < bltags.size(); i++) { - txt += "*" + bltags.get(i) + "\n"; + for (String bltag : bltags) { + txt += "*" + bltag + "\n"; } if (blusers.size() > 0) { @@ -388,8 +382,8 @@ public class JuickBot { } } if (blusers.size() > 0) { - for (int i = 0; i < blusers.size(); i++) { - txt += "@" + blusers.get(i).getUName() + "\n"; + for (User bluser : blusers) { + txt += "@" + bluser.getUName() + "\n"; } } if (txt.isEmpty()) { diff --git a/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java b/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java index 13df5fd0..70f01553 100644 --- a/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java +++ b/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java @@ -3,6 +3,8 @@ package com.juick.xmpp.s2s; import com.juick.xmpp.Stanza; import com.juick.xmpp.StanzaChild; import com.juick.xmpp.extensions.JuickMessage; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.xmlpull.v1.XmlPullParserException; import javax.servlet.ServletContextEvent; @@ -42,7 +44,7 @@ public class XMPPComponent implements ServletContextListener { static final List outConnections = Collections.synchronizedList(new ArrayList<>()); static final List outCache = Collections.synchronizedList(new ArrayList<>()); static final Integer sqlSync = 0; - static java.sql.Connection sql; + static JdbcTemplate sql; final public static HashMap childParsers = new HashMap<>(); public static void addConnectionIn(ConnectionIn c) { @@ -176,9 +178,12 @@ public class XMPPComponent implements ServletContextListener { keystore = conf.getProperty("keystore"); keystorePassword = conf.getProperty("keystore_password"); brokenSSLhosts = Arrays.asList(conf.getProperty("broken_ssl_hosts", "").split(",")); - Class.forName("com.mysql.jdbc.Driver"); - sql = DriverManager.getConnection("jdbc:mysql://localhost/juick?autoReconnect=true&user=" + - conf.getProperty("mysql_username", "") + "&password=" + conf.getProperty("mysql_password", "")); + DriverManagerDataSource dataSource = new DriverManagerDataSource(); + dataSource.setDriverClassName(conf.getProperty("datasource_driver", "com.mysql.jdbc.Driver")); + dataSource.setUrl(conf.getProperty("datasource_url")); + DriverManagerDataSource dataSourceSearch = new DriverManagerDataSource(); + dataSourceSearch.setDriverClassName(conf.getProperty("datasource_driver", "com.mysql.jdbc.Driver")); + sql = new JdbcTemplate(dataSource); childParsers.put(JuickMessage.XMLNS, new JuickMessage()); executorService.submit(() -> connRouter = new ConnectionRouter(componentName, conf.getProperty("xmpp_password"))); @@ -215,17 +220,6 @@ public class XMPPComponent implements ServletContextListener { } catch (IOException e) { LOGGER.log(Level.WARNING, "router warning", e); } - - synchronized (XMPPComponent.sqlSync) { - if (XMPPComponent.sql != null) { - try { - XMPPComponent.sql.close(); - XMPPComponent.sql = null; - } catch (SQLException e) { - LOGGER.warning("SQL ERROR: " + e); - } - } - } // Now deregister JDBC drivers in this context's ClassLoader: // Get the webapp's ClassLoader ClassLoader cl = Thread.currentThread().getContextClassLoader(); -- cgit v1.2.3 From 8ecdfbd5a36c7cc4d48bb563c9b561a904033514 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 27 Jun 2016 14:03:28 +0300 Subject: fixes --- src/main/java/com/juick/http/www/User.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/http/www/User.java b/src/main/java/com/juick/http/www/User.java index d0c1aaee..51b3965f 100644 --- a/src/main/java/com/juick/http/www/User.java +++ b/src/main/java/com/juick/http/www/User.java @@ -322,7 +322,8 @@ public class User { List tags = TagQueries.getUserTagsAll(sql, user.getUID()); int maxUsageCnt = tags.stream().map(t -> t.UsageCnt).max(Integer::max).get(); String ret = ""; - for (int i = 0; i < cnt; i++) { + int count = Math.min(tags.size(), cnt); + for (int i = 0; i < count; i++) { String tag = Utils.encodeHTML(tags.get(i).Name); try { tag = " tags = TagQueries.getUserTagsAll(sql, user.getUID()); - int maxUsageCnt = tags.stream().map(t -> t.UsageCnt).max(Integer::max).get(); + int maxUsageCnt = tags.stream().map(t -> t.UsageCnt).max(Integer::max).orElse(0); String ret = ""; int count = Math.min(tags.size(), cnt); for (int i = 0; i < count; i++) { -- cgit v1.2.3 From 3983a459e1756d21cdb2ca3811a99800ae1bf3b1 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 27 Jun 2016 14:33:32 +0300 Subject: quick s2s restore --- src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java b/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java index d0b3a93b..e68e030f 100644 --- a/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java +++ b/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java @@ -33,7 +33,7 @@ public class ConnectionRouter implements Stream.StreamListener, logger.info("STREAM ROUTER START"); try { socket = new Socket("localhost", 5347); - router = new StreamComponent(new JID(componentName), socket.getInputStream(), socket.getOutputStream(), password); + router = new StreamComponent(new JID("s2s"), socket.getInputStream(), socket.getOutputStream(), password); router.addChildParser(new JuickMessage()); router.addListener((Stream.StreamListener) this); router.addListener((Message.MessageListener) this); -- cgit v1.2.3 From 78caa7f9617ce2eb07cabd751a9d1594fe41976b Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 27 Jun 2016 14:46:09 +0300 Subject: reconnect router --- src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java b/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java index e68e030f..95878edd 100644 --- a/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java +++ b/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java @@ -12,7 +12,6 @@ import com.juick.xmpp.extensions.XOOB; import java.io.IOException; import java.net.Socket; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; @@ -25,12 +24,16 @@ public class ConnectionRouter implements Stream.StreamListener, private static final Logger logger = Logger.getLogger(ConnectionRouter.class.getName()); private String componentName; + private String password; Stream router; Socket socket; ConnectionRouter(String componentName, String password) { this.componentName = componentName; - logger.info("STREAM ROUTER START"); + this.password = password; + reconnect(); + } + public void reconnect() { try { socket = new Socket("localhost", 5347); router = new StreamComponent(new JID("s2s"), socket.getInputStream(), socket.getOutputStream(), password); @@ -39,6 +42,7 @@ public class ConnectionRouter implements Stream.StreamListener, router.addListener((Message.MessageListener) this); router.addListener((Iq.IqListener) this); router.startParsing(); + logger.info("STREAM ROUTER START"); } catch (IOException e) { logger.log(Level.SEVERE, "router failed", e); } @@ -240,5 +244,6 @@ public class ConnectionRouter implements Stream.StreamListener, @Override public void onStreamFail(Exception ex) { logger.log(Level.SEVERE, "STREAM ROUTER (FAIL)", ex); + reconnect(); } } -- cgit v1.2.3 From a6b07685219dee3b48a816e562fdbe93f2419779 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 29 Jun 2016 15:15:15 +0300 Subject: add juick bot protocol WIP --- .../com/juick/server/protocol/JuickProtocol.java | 375 +++++++++++++++++++++ .../com/juick/server/protocol/ProtocolReply.java | 23 ++ .../server/protocol/annotation/UserCommand.java | 31 ++ 3 files changed, 429 insertions(+) create mode 100644 src/main/java/com/juick/server/protocol/JuickProtocol.java create mode 100644 src/main/java/com/juick/server/protocol/ProtocolReply.java create mode 100644 src/main/java/com/juick/server/protocol/annotation/UserCommand.java (limited to 'src/main') diff --git a/src/main/java/com/juick/server/protocol/JuickProtocol.java b/src/main/java/com/juick/server/protocol/JuickProtocol.java new file mode 100644 index 00000000..923c404e --- /dev/null +++ b/src/main/java/com/juick/server/protocol/JuickProtocol.java @@ -0,0 +1,375 @@ +package com.juick.server.protocol; + +import com.juick.*; +import com.juick.json.MessageSerializer; +import com.juick.server.*; +import com.juick.server.protocol.annotation.UserCommand; +import com.juick.xmpp.extensions.JuickMessage; +import org.springframework.jdbc.core.JdbcTemplate; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +/** + * Created by oxpa on 22.03.16. + */ + +public class JuickProtocol { + MessageSerializer json = new MessageSerializer(); + JdbcTemplate sql; + String baseUri; + + public JuickProtocol(JdbcTemplate sql, String baseUri) { + this.sql = sql; + this.baseUri = baseUri; + } + + /** + * find command by pattern and invoke + * @param user who send command + * @param userInput given by user + * @return command result + * @throws InvocationTargetException + * @throws IllegalAccessException + * @throws NoSuchMethodException + */ + public ProtocolReply getReply(User user, String userInput) throws InvocationTargetException, + IllegalAccessException, NoSuchMethodException { + Optional cmd = Arrays.asList(getClass().getDeclaredMethods()).stream() + .filter(m -> m.isAnnotationPresent(UserCommand.class)) + .filter(m -> Pattern.compile(m.getAnnotation(UserCommand.class).pattern(), + m.getAnnotation(UserCommand.class).patternFlags()).matcher(userInput).matches()) + .findFirst(); + if (!cmd.isPresent()) { + // default command - post as new message + return postMessage(user, userInput); + } else { + Matcher matcher = Pattern.compile(cmd.get().getAnnotation(UserCommand.class).pattern(), + cmd.get().getAnnotation(UserCommand.class).patternFlags()).matcher(userInput); + List groups = new ArrayList<>(); + while (matcher.find()) { + for (int i = 1; i <= matcher.groupCount(); i++) { + groups.add(matcher.group(i)); + } + } + return (ProtocolReply) getClass().getMethod(cmd.get().getName(), User.class, String[].class) + .invoke(this, user, groups.toArray(new String[groups.size()])); + } + } + + public ProtocolReply postMessage(User user, String input) { + List tags = TagQueries.fromString(sql, input, false); + String body = input.substring(TagQueries.toString(tags).length()); + int mid = MessagesQueries.createMessage(sql, user.getUID(), body, null, tags); + //app.events().publishEvent(new JuickMessageEvent(app.messages().getMessage(mid))); + return new ProtocolReply("New message posted.\n#" + mid + " " + baseUri + mid, + Optional.of(json.serializeList(Collections.singletonList(MessagesQueries.getMessage(sql, mid))))); + } + + @UserCommand(pattern = "^#(\\++)$", help = "#+ - Show last Juick messages (#++ - second page, ...)") + public ProtocolReply commandLast(User user, String... arguments) { + // number of + is the page count + int page = arguments[0].length(); + List mids = MessagesQueries.getAll(sql, user.getUID(), page); + List messages = MessagesQueries.getMessages(sql, mids); + // TODO: message toString + return new ProtocolReply("Last messages: \n" + String.join("\n", messages.stream().map(Object::toString) + .collect(Collectors.toList())), Optional.of(json.serializeList(messages))); + } + + @UserCommand(pattern = "^\\s*bl\\s*$", patternFlags = Pattern.CASE_INSENSITIVE, + help = "BL - Show your blacklist") + public ProtocolReply commandBL(User user_from, String... arguments) { + List blusers; + List bltags; + + blusers = UserQueries.getUserBLUsers(sql, user_from.getUID()); + bltags = TagQueries.getUserBLTags(sql, user_from.getUID()); + + + String txt = ""; + if (bltags.size() > 0) { + for (String bltag : bltags) { + txt += "*" + bltag + "\n"; + } + + if (blusers.size() > 0) { + txt += "\n"; + } + } + if (blusers.size() > 0) { + for (User bluser : blusers) { + txt += "@" + bluser.getUName() + "\n"; + } + } + if (txt.isEmpty()) { + txt = "You don't have any users or tags in your blacklist."; + } + return new ProtocolReply(txt, Optional.empty()); + } + + @UserCommand(pattern = "^\\@([^\\s\\n\\+]+)(\\+?)$", + help = "@username+ - Show user's info and last 10 messages (@username++ - second page, ..)") + public ProtocolReply commandUser(User user, String... arguments) { + User blogUser = UserQueries.getUserByName(sql, arguments[0]); + int page = arguments[1].length(); + if (blogUser.getUID() > 0) { + List mids = MessagesQueries.getUserBlog(sql, blogUser.getUID(), 0, page); + List messages = MessagesQueries.getMessages(sql, mids); + return new ProtocolReply(String.format("Last messages from @%s:\n%s", arguments[0], + String.join("\n", messages.stream() + .map(Object::toString).collect(Collectors.toList()))), + Optional.of(json.serializeList(messages))); + } + return new ProtocolReply("User not found", Optional.empty()); + } + + @UserCommand(pattern = "^\\s*d\\s*\\#([0-9]+)\\s*$", patternFlags = Pattern.CASE_INSENSITIVE, + help = "D #12345 - delete the message") + public ProtocolReply commandDel(User user, String... args) { + try { + int mid = Integer.parseInt(args[0]); + if (MessagesQueries.deleteMessage(sql, user.getUID(), mid)) { + return new ProtocolReply(String.format("Message %s deleted", mid), Optional.empty()); + } + } catch (NumberFormatException e) { + return new ProtocolReply("Error", Optional.empty()); + } + return new ProtocolReply("Error", Optional.empty()); + } + + @UserCommand(pattern = "^\\s*login\\s*$", patternFlags = Pattern.CASE_INSENSITIVE, + help = "LOGIN - log in to Juick website") + public ProtocolReply commandLogin(User user, String... arguments) { + return new ProtocolReply(baseUri + "?" + UserQueries.getHashByUID(sql, user.getUID()), + Optional.empty()); + } + + @UserCommand(pattern = "^(#+)$", help = "# - Show last messages from your feed (## - second page, ...)") + public ProtocolReply commandMyFeed(User user, String... arguments) { + // number of # is the page count + int page = arguments[0].length(); + List mids = MessagesQueries.getMyFeed(sql, user.getUID(), page); + List messages = MessagesQueries.getMessages(sql, mids); + // TODO: add instructions for empty feed + return new ProtocolReply("Your feed: \n" + String.join("\n", + messages.stream().map(Object::toString).collect(Collectors.toList())), + Optional.of(json.serializeList(messages))); + } + + @UserCommand(pattern = "^\\s*(on|off)\\s*$", patternFlags = Pattern.CASE_INSENSITIVE, + help = "ON/OFF - Enable/disable subscriptions delivery") + public ProtocolReply commandOnOff(User user, String[] input) { + UserQueries.ActiveStatus newStatus; + String retValUpdated; + if (input[0].toLowerCase().equals("on")) { + newStatus = UserQueries.ActiveStatus.Active; + retValUpdated = "Notifications are activated for " + user.getJID(); + } else { + newStatus = UserQueries.ActiveStatus.Inactive; + retValUpdated = "Notifications are disabled for " + user.getJID(); + } + + if (UserQueries.setActiveStatusForJID(sql, user.getJID(), newStatus)) { + return new ProtocolReply(retValUpdated, Optional.empty()); + } else { + return new ProtocolReply(String.format("Subscriptions status for %s was not changed", user.getJID()), + Optional.empty()); + } + } + + @UserCommand(pattern = "^\\s*ping\\s*$", patternFlags = Pattern.CASE_INSENSITIVE, + help = "PING - returns you a PONG") + public ProtocolReply commandPing(User user, String[] input) { + return new ProtocolReply("PONG", Optional.empty()); + } + + @UserCommand(pattern = "^\\@(\\S+)\\s+([\\s\\S]+)$", help = "@username message - send PM to username") + public ProtocolReply commandPM(User user_from, String... arguments) { + String user_to = arguments[0]; + String body = arguments[1]; + int ret = 0; + + int uid_to = 0; + String jid_to = null; + boolean haveInRoster = false; + + if (user_to.indexOf('@') > 0) { + uid_to = UserQueries.getUIDbyJID(sql, user_to); + } else { + uid_to = UserQueries.getUIDbyName(sql, user_to); + } + + if (uid_to > 0) { + if (!UserQueries.isInBLAny(sql, uid_to, user_from.getUID())) { + if (PMQueries.createPM(sql, user_from.getUID(), uid_to, body)) { + jid_to = UserQueries.getJIDsbyUID(sql, uid_to); + if (jid_to != null) { + haveInRoster = PMQueries.havePMinRoster(sql, user_from.getUID(), jid_to); + } + ret = 200; + } else { + ret = 500; + } + } else { + ret = 403; + } + } else { + ret = 404; + } + + + if (ret == 200) { + JuickMessage jmsg = new JuickMessage(); + jmsg.setUser(user_from); + jmsg.setText(body); + // TODO: add PM payload + app.events().publishEvent(new JuickMessageEvent(jmsg)); + /* TODO: move to XMPP component + if (jid_to != null) { + Message mm = new Message(); + mm.to = new JID(jid_to); + mm.type = Message.Type.chat; + if (haveInRoster) { + mm.from = new JID(user_from.getUName(), getDomain(), "Juick"); + mm.body = body; + } else { + mm.from = new JID("juick", getDomain(), "Juick"); + mm.body = "Private message from @" + user_from.getUName() + ":\n" + body; + } + return Collections.singletonList(mm); + } + */ + } + if (ret == 200) { + return new ProtocolReply("Private message sent", Optional.empty()); + } else { + return new ProtocolReply("Error " + ret, Optional.empty()); + } + } + + @UserCommand(pattern = "^#(\\d+)(\\+?)$", help = "#1234 - Show message (#1234+ - message with replies)") + public ProtocolReply commandShow(User user, String... arguments) { + boolean showReplies = arguments[1].length() > 0; + int mid; + try { + mid = Integer.parseInt(arguments[0]); + } catch (NumberFormatException e) { + return new ProtocolReply("Error", Optional.empty()); + } + Message msg = MessagesQueries.getMessage(sql, mid); + if (showReplies) { + List replies = MessagesQueries.getReplies(sql, mid); + replies.add(0, msg); + return new ProtocolReply(String.join("\n", + replies.stream().map(Object::toString).collect(Collectors.toList())), + Optional.of(json.serializeList(replies))); + } + return new ProtocolReply(msg.toString(), Optional.of(json.serializeList(Collections.singletonList(msg)))); + } + @UserCommand(pattern = "^(#|\\.)(\\d+)((\\.|\\-|\\/)(\\d+))?\\s([\\s\\S]+)", + help = "#1234 *tag *tag2 - edit tags\n#1234 text - reply to message") + public ProtocolReply EditOrReply(User user, String... args) { + int mid; + try { + mid = Integer.parseInt(args[1]); + } catch (NumberFormatException e) { + return new ProtocolReply("Error", Optional.empty()); + } + int rid; + try { + rid = Integer.parseInt(args[4]); + } catch (NumberFormatException e) { + rid = 0; + } + String txt = args[5]; + List messageTags = TagQueries.fromString(sql, txt, true); + if (messageTags.size() > 0) { + if (user.getUID() != MessagesQueries.getMessageAuthor(sql, mid).getUID()) { + return new ProtocolReply("It is not your message", Optional.empty()); + } + TagQueries.updateTags(sql, mid, messageTags); + return new ProtocolReply("Tags are updated", Optional.empty()); + } else { + int newrid = MessagesQueries.createReply(sql, mid, rid, user.getUID(), txt, null); + return new ProtocolReply("Reply posted.\n#" + mid + "/" + newrid + " " + + baseUri + mid + "/" + newrid, + Optional.of(json.serializeList(Collections.singletonList(MessagesQueries.getReply(sql, mid, newrid))))); + } + } + + @UserCommand(pattern = "^(s|u)\\s+#(\\d+)$", help = "S #1234 - subscribe to comments", + patternFlags = Pattern.CASE_INSENSITIVE) + public ProtocolReply commandSubscribeMessage(User user, String... args) { + boolean subscribe = args[0].equalsIgnoreCase("s"); + int mid; + try { + mid = Integer.parseInt(args[1]); + } catch (NumberFormatException e) { + return new ProtocolReply("Error", Optional.empty()); + } + if (subscribe) { + if (SubscriptionsQueries.subscribeMessage(sql, mid, user.getUID())) { + return new ProtocolReply("Subscribed", Optional.empty()); + } + } else { + if (SubscriptionsQueries.unSubscribeMessage(sql, mid, user.getUID())) { + return new ProtocolReply("Unsubscribed from #" + mid, Optional.empty()); + } + return new ProtocolReply("You was not subscribed to #" + mid, Optional.empty()); + } + return new ProtocolReply("Error", Optional.empty()); + } + @UserCommand(pattern = "^(s|u)\\s+\\@(\\S+)$", help = "S @user - subscribe to user's posts", + patternFlags = Pattern.CASE_INSENSITIVE) + public ProtocolReply commandSubscribeUser(User user, String... args) { + boolean subscribe = args[0].equalsIgnoreCase("s"); + User toUser = UserQueries.getUserByName(sql, args[1]); + if (toUser.getUID() > 0) { + if (subscribe) { + if (SubscriptionsQueries.subscribeUser(sql, user, toUser)) { + return new ProtocolReply("Subscribed", Optional.empty()); + // TODO: notification + // TODO: already subscribed case + } + } else { + if (SubscriptionsQueries.unSubscribeUser(sql, user, toUser)) { + return new ProtocolReply("Unsubscribed from @" + toUser.getUName(), Optional.empty()); + } + return new ProtocolReply("You was not subscribed to @" + toUser.getUName(), Optional.empty()); + } + } + return new ProtocolReply("Error", Optional.empty()); + } + @UserCommand(pattern = "^(s|u)\\s+\\*(\\S+)$", help = "S *tag - subscribe to tag" + + "\nU *tag - unsubscribe from tag", patternFlags = Pattern.CASE_INSENSITIVE) + public ProtocolReply commandSubscribeTag(User user, String... args) { + boolean subscribe = args[0].equalsIgnoreCase("s"); + Tag tag = TagQueries.getTag(sql, args[1], true); + if (subscribe) { + if (SubscriptionsQueries.subscribeTag(sql, user, tag)) { + return new ProtocolReply("Subscribed", Optional.empty()); + } + } else { + if (SubscriptionsQueries.unSubscribeTag(sql, user, tag)) { + return new ProtocolReply("Unsubscribed from " + tag.Name, Optional.empty()); + } + return new ProtocolReply("You was not subscribed to " + tag.Name, Optional.empty()); + } + return new ProtocolReply("Error", Optional.empty()); + } + + @UserCommand(pattern = "^\\s*help\\s*$", patternFlags = Pattern.CASE_INSENSITIVE, + help = "HELP - returns this help message") + public ProtocolReply commandHelp(User user, String[] input) { + List commandsHelp = Arrays.asList(getClass().getDeclaredMethods()).stream() + .filter(m -> m.isAnnotationPresent(UserCommand.class)) + .map(m -> m.getAnnotation(UserCommand.class).help()) + .collect(Collectors.toList()); + return new ProtocolReply(String.join("\n", commandsHelp), Optional.empty()); + } +} diff --git a/src/main/java/com/juick/server/protocol/ProtocolReply.java b/src/main/java/com/juick/server/protocol/ProtocolReply.java new file mode 100644 index 00000000..d9d36a5d --- /dev/null +++ b/src/main/java/com/juick/server/protocol/ProtocolReply.java @@ -0,0 +1,23 @@ +package com.juick.server.protocol; + +import java.util.Optional; + +/** + * Created by vitalyster on 08.04.2016. + */ +public class ProtocolReply { + + private Optional json; + private String description; + + public ProtocolReply(String text, Optional json) { + this.description = text; + this.json = json; + } + public String getDescription() { + return description; + } + public Optional getJson() { + return json; + } +} diff --git a/src/main/java/com/juick/server/protocol/annotation/UserCommand.java b/src/main/java/com/juick/server/protocol/annotation/UserCommand.java new file mode 100644 index 00000000..af7c4924 --- /dev/null +++ b/src/main/java/com/juick/server/protocol/annotation/UserCommand.java @@ -0,0 +1,31 @@ +package com.juick.server.protocol.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Created by oxpa on 22.03.16. + */ +@Target({ElementType.TYPE, ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface UserCommand { + /** + * + * @return a command pattern + */ + String pattern() default ""; + + /** + * + * @return pattern flags + */ + int patternFlags() default 0; + + /** + * + * @return a string used in HELP command output. Basically, only 1 string + */ + String help() default ""; +} -- cgit v1.2.3 From 210690e7aefbd4d82e269277d621b5e6efc5c909 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 1 Jul 2016 14:12:09 +0300 Subject: comment juickprotocol for now --- src/main/java/com/juick/server/protocol/JuickProtocol.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/protocol/JuickProtocol.java b/src/main/java/com/juick/server/protocol/JuickProtocol.java index 923c404e..1a2e5333 100644 --- a/src/main/java/com/juick/server/protocol/JuickProtocol.java +++ b/src/main/java/com/juick/server/protocol/JuickProtocol.java @@ -207,7 +207,7 @@ public class JuickProtocol { if (uid_to > 0) { if (!UserQueries.isInBLAny(sql, uid_to, user_from.getUID())) { if (PMQueries.createPM(sql, user_from.getUID(), uid_to, body)) { - jid_to = UserQueries.getJIDsbyUID(sql, uid_to); + //jid_to = UserQueries.getJIDsbyUID(sql, uid_to); if (jid_to != null) { haveInRoster = PMQueries.havePMinRoster(sql, user_from.getUID(), jid_to); } @@ -228,7 +228,7 @@ public class JuickProtocol { jmsg.setUser(user_from); jmsg.setText(body); // TODO: add PM payload - app.events().publishEvent(new JuickMessageEvent(jmsg)); + //app.events().publishEvent(new JuickMessageEvent(jmsg)); /* TODO: move to XMPP component if (jid_to != null) { Message mm = new Message(); -- cgit v1.2.3 From d48e3b71c27d6782a63dda84aced58780b54f309 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 6 Jul 2016 18:48:28 +0300 Subject: JdbcTemplate is thread-safe --- .../java/com/juick/xmpp/s2s/ConnectionRouter.java | 52 +++--- src/main/java/com/juick/xmpp/s2s/JuickBot.java | 182 +++++++++------------ .../java/com/juick/xmpp/s2s/XMPPComponent.java | 1 - 3 files changed, 100 insertions(+), 135 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java b/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java index 95878edd..0030834a 100644 --- a/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java +++ b/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java @@ -55,15 +55,13 @@ public class ConnectionRouter implements Stream.StreamListener, public void sendJuickMessage(JuickMessage jmsg) { List jids = new ArrayList<>(); - synchronized (XMPPComponent.sqlSync) { - if (jmsg.FriendsOnly) { - jids = SubscriptionsQueries.getJIDSubscribedToUser(XMPPComponent.sql, jmsg.getUser().getUID(), jmsg.FriendsOnly); - } else { - List users = SubscriptionsQueries.getSubscribedUsers(XMPPComponent.sql, jmsg.getUser().getUID(), jmsg.getMID()); - for (User user : users) { - for (String jid : UserQueries.getJIDsbyUID(XMPPComponent.sql, user.getUID())) { - jids.add(jid); - } + if (jmsg.FriendsOnly) { + jids = SubscriptionsQueries.getJIDSubscribedToUser(XMPPComponent.sql, jmsg.getUser().getUID(), jmsg.FriendsOnly); + } else { + List users = SubscriptionsQueries.getSubscribedUsers(XMPPComponent.sql, jmsg.getUser().getUID(), jmsg.getMID()); + for (User user : users) { + for (String jid : UserQueries.getJIDsbyUID(XMPPComponent.sql, user.getUID())) { + jids.add(jid); } } } @@ -103,13 +101,11 @@ public class ConnectionRouter implements Stream.StreamListener, String replyQuote; String replyTo; - synchronized (XMPPComponent.sqlSync) { - users = SubscriptionsQueries.getUsersSubscribedToComments(XMPPComponent.sql, jmsg.getMID(), jmsg.getUser().getUID()); - com.juick.Message replyMessage = jmsg.ReplyTo > 0 ? MessagesQueries.getReply(XMPPComponent.sql, jmsg.getMID(), jmsg.ReplyTo) - : MessagesQueries.getMessage(XMPPComponent.sql, jmsg.getMID()); - replyTo = replyMessage.getUser().getUName(); - replyQuote = getReplyQuote(replyMessage); - } + users = SubscriptionsQueries.getUsersSubscribedToComments(XMPPComponent.sql, jmsg.getMID(), jmsg.getUser().getUID()); + com.juick.Message replyMessage = jmsg.ReplyTo > 0 ? MessagesQueries.getReply(XMPPComponent.sql, jmsg.getMID(), jmsg.ReplyTo) + : MessagesQueries.getMessage(XMPPComponent.sql, jmsg.getMID()); + replyTo = replyMessage.getUser().getUName(); + replyQuote = getReplyQuote(replyMessage); String txt = "Reply by @" + jmsg.getUser().getUName() + ":\n" + replyQuote + "\n@" + replyTo + " "; String attachment = jmsg.getAttachmentURL(); @@ -124,11 +120,9 @@ public class ConnectionRouter implements Stream.StreamListener, msg.type = Message.Type.chat; msg.addChild(jmsg); for (User user : users) { - synchronized (XMPPComponent.sqlSync) { - for (String jid : UserQueries.getJIDsbyUID(XMPPComponent.sql, user.getUID())) { - msg.to = new JID(jid); - XMPPComponent.sendOut(msg); - } + for (String jid : UserQueries.getJIDsbyUID(XMPPComponent.sql, user.getUID())) { + msg.to = new JID(jid); + XMPPComponent.sendOut(msg); } } } @@ -146,11 +140,9 @@ public class ConnectionRouter implements Stream.StreamListener, public void sendJuickRecommendation(JuickMessage recomm) { List users; JuickMessage jmsg; - synchronized (XMPPComponent.sqlSync) { - jmsg = new JuickMessage(MessagesQueries.getMessage(XMPPComponent.sql, recomm.getMID())); - users = SubscriptionsQueries.getUsersSubscribedToUserRecommendations(XMPPComponent.sql, - recomm.getUser().getUID(), recomm.getMID(), jmsg.getUser().getUID()); - } + jmsg = new JuickMessage(MessagesQueries.getMessage(XMPPComponent.sql, recomm.getMID())); + users = SubscriptionsQueries.getUsersSubscribedToUserRecommendations(XMPPComponent.sql, + recomm.getUser().getUID(), recomm.getMID(), jmsg.getUser().getUID()); String txt = "Recommended by @" + recomm.getUser().getUName() + ":\n"; txt += "@" + jmsg.getUser().getUName() + ":" + jmsg.getTagsString() + "\n"; @@ -186,11 +178,9 @@ public class ConnectionRouter implements Stream.StreamListener, } for (User user : users) { - synchronized (XMPPComponent.sqlSync) { - for (String jid : UserQueries.getJIDsbyUID(XMPPComponent.sql, user.getUID())) { - msg.to = new JID(jid); - XMPPComponent.sendOut(msg); - } + for (String jid : UserQueries.getJIDsbyUID(XMPPComponent.sql, user.getUID())) { + msg.to = new JID(jid); + XMPPComponent.sendOut(msg); } } } diff --git a/src/main/java/com/juick/xmpp/s2s/JuickBot.java b/src/main/java/com/juick/xmpp/s2s/JuickBot.java index 1172d75f..357b070f 100644 --- a/src/main/java/com/juick/xmpp/s2s/JuickBot.java +++ b/src/main/java/com/juick/xmpp/s2s/JuickBot.java @@ -69,9 +69,7 @@ public class JuickBot { } else if (p.type.equals(Presence.Type.probe)) { int uid_to = 0; if (!toJuick) { - synchronized (XMPPComponent.sqlSync) { - uid_to = UserQueries.getUIDbyName(XMPPComponent.sql, username); - } + uid_to = UserQueries.getUIDbyName(XMPPComponent.sql, username); } if (toJuick || uid_to > 0) { @@ -94,12 +92,10 @@ public class JuickBot { if (toJuick) { canSubscribe = true; } else { - synchronized (XMPPComponent.sqlSync) { - int uid_to = UserQueries.getUIDbyName(XMPPComponent.sql, username); - if (uid_to > 0) { - PMQueries.addPMinRoster(XMPPComponent.sql, uid_to, p.from.Bare()); - canSubscribe = true; - } + int uid_to = UserQueries.getUIDbyName(XMPPComponent.sql, username); + if (uid_to > 0) { + PMQueries.addPMinRoster(XMPPComponent.sql, uid_to, p.from.Bare()); + canSubscribe = true; } } @@ -122,11 +118,9 @@ public class JuickBot { } } else if (p.type.equals(Presence.Type.unsubscribe)) { if (!toJuick) { - synchronized (XMPPComponent.sqlSync) { - int uid_to = UserQueries.getUIDbyName(XMPPComponent.sql, username); - if (uid_to > 0) { - PMQueries.removePMinRoster(XMPPComponent.sql, uid_to, p.from.Bare()); - } + int uid_to = UserQueries.getUIDbyName(XMPPComponent.sql, username); + if (uid_to > 0) { + PMQueries.removePMinRoster(XMPPComponent.sql, uid_to, p.from.Bare()); } } @@ -146,11 +140,9 @@ public class JuickBot { User user_from = null; String signuphash = ""; - synchronized (XMPPComponent.sqlSync) { - user_from = UserQueries.getUserByJID(XMPPComponent.sql, msg.from.Bare()); - if (user_from == null) { - signuphash = UserQueries.getSignUpHashByJID(XMPPComponent.sql, msg.from.Bare()); - } + user_from = UserQueries.getUserByJID(XMPPComponent.sql, msg.from.Bare()); + if (user_from == null) { + signuphash = UserQueries.getSignUpHashByJID(XMPPComponent.sql, msg.from.Bare()); } if (user_from == null) { @@ -168,10 +160,7 @@ public class JuickBot { return incomingMessageJuick(user_from, msg); } - int uid_to = 0; - synchronized (XMPPComponent.sqlSync) { - uid_to = UserQueries.getUIDbyName(XMPPComponent.sql, username); - } + int uid_to = UserQueries.getUIDbyName(XMPPComponent.sql, username); if (uid_to == 0) { Message reply = new Message(msg.to, msg.from, Message.Type.error); @@ -182,10 +171,8 @@ public class JuickBot { } boolean success = false; - synchronized (XMPPComponent.sqlSync) { - if (!UserQueries.isInBLAny(XMPPComponent.sql, uid_to, user_from.getUID())) { - success = PMQueries.createPM(XMPPComponent.sql, user_from.getUID(), uid_to, msg.body); - } + if (!UserQueries.isInBLAny(XMPPComponent.sql, uid_to, user_from.getUID())) { + success = PMQueries.createPM(XMPPComponent.sql, user_from.getUID(), uid_to, msg.body); } if (success) { @@ -193,9 +180,7 @@ public class JuickBot { m.from = new JID("juick", "juick.com", null); m.to = new JID(Integer.toString(uid_to), "push.juick.com", null); JuickMessage jmsg = new JuickMessage(); - synchronized (XMPPComponent.sqlSync) { - jmsg.setUser(user_from); - } + jmsg.setUser(user_from); jmsg.setText(msg.body); m.childs.add(jmsg); XMPPComponent.connRouter.router.send(m.toString()); @@ -205,22 +190,20 @@ public class JuickBot { List jids; boolean inroster = false; - synchronized (XMPPComponent.sqlSync) { - jids = UserQueries.getJIDsbyUID(XMPPComponent.sql, uid_to); - for (String jid : jids) { - Message mm = new Message(); - mm.to = new JID(jid); - mm.type = Message.Type.chat; - inroster = PMQueries.havePMinRoster(XMPPComponent.sql, user_from.getUID(), jid); - if (inroster) { - mm.from = new JID(jmsg.getUser().getUName(), "juick.com", "Juick"); - mm.body = msg.body; - } else { - mm.from = new JID("juick", "juick.com", "Juick"); - mm.body = "Private message from @" + jmsg.getUser().getUName() + ":\n" + msg.body; - } - XMPPComponent.sendOut(mm); + jids = UserQueries.getJIDsbyUID(XMPPComponent.sql, uid_to); + for (String jid : jids) { + Message mm = new Message(); + mm.to = new JID(jid); + mm.type = Message.Type.chat; + inroster = PMQueries.havePMinRoster(XMPPComponent.sql, user_from.getUID(), jid); + if (inroster) { + mm.from = new JID(jmsg.getUser().getUName(), "juick.com", "Juick"); + mm.body = msg.body; + } else { + mm.from = new JID("juick", "juick.com", "Juick"); + mm.body = "Private message from @" + jmsg.getUser().getUName() + ":\n" + msg.body; } + XMPPComponent.sendOut(mm); } } else { Message reply = new Message(msg.to, msg.from, Message.Type.error); @@ -299,77 +282,70 @@ public class JuickBot { List jids_to = null; boolean haveInRoster = false; - synchronized (XMPPComponent.sqlSync) { - if (user_to.indexOf('@') > 0) { - uid_to = UserQueries.getUIDbyJID(XMPPComponent.sql, user_to); - } else { - uid_to = UserQueries.getUIDbyName(XMPPComponent.sql, user_to); - } + if (user_to.indexOf('@') > 0) { + uid_to = UserQueries.getUIDbyJID(XMPPComponent.sql, user_to); + } else { + uid_to = UserQueries.getUIDbyName(XMPPComponent.sql, user_to); + } - if (uid_to > 0) { - if (!UserQueries.isInBLAny(XMPPComponent.sql, uid_to, user_from.getUID())) { - if (PMQueries.createPM(XMPPComponent.sql, user_from.getUID(), uid_to, body)) { - jids_to = UserQueries.getJIDsbyUID(XMPPComponent.sql, uid_to); - ret = 200; - } else { - ret = 500; - } + if (uid_to > 0) { + if (!UserQueries.isInBLAny(XMPPComponent.sql, uid_to, user_from.getUID())) { + if (PMQueries.createPM(XMPPComponent.sql, user_from.getUID(), uid_to, body)) { + jids_to = UserQueries.getJIDsbyUID(XMPPComponent.sql, uid_to); + ret = 200; } else { - ret = 403; + ret = 500; } } else { - ret = 404; + ret = 403; } + } else { + ret = 404; + } - if (ret == 200) { - Message msg = new Message(); - msg.from = new JID("juick", "juick.com", null); - msg.to = new JID(Integer.toString(uid_to), "push.juick.com", null); - JuickMessage jmsg = new JuickMessage(); - jmsg.setUser(user_from); - jmsg.setText(body); - msg.childs.add(jmsg); - XMPPComponent.connRouter.router.send(msg.toString()); - - msg.to.Host = "ws.juick.com"; - XMPPComponent.connRouter.router.send(msg.toString()); - - for (String jid : jids_to) { - Message mm = new Message(); - mm.to = new JID(jid); - mm.type = Message.Type.chat; - haveInRoster = PMQueries.havePMinRoster(XMPPComponent.sql, user_from.getUID(), jid); - if (haveInRoster) { - mm.from = new JID(user_from.getUName(), "juick.com", "Juick"); - mm.body = body; - } else { - mm.from = new JID("juick", "juick.com", "Juick"); - mm.body = "Private message from @" + user_from.getUName() + ":\n" + body; - } - XMPPComponent.sendOut(mm); + if (ret == 200) { + Message msg = new Message(); + msg.from = new JID("juick", "juick.com", null); + msg.to = new JID(Integer.toString(uid_to), "push.juick.com", null); + JuickMessage jmsg = new JuickMessage(); + jmsg.setUser(user_from); + jmsg.setText(body); + msg.childs.add(jmsg); + XMPPComponent.connRouter.router.send(msg.toString()); + + msg.to.Host = "ws.juick.com"; + XMPPComponent.connRouter.router.send(msg.toString()); + + for (String jid : jids_to) { + Message mm = new Message(); + mm.to = new JID(jid); + mm.type = Message.Type.chat; + haveInRoster = PMQueries.havePMinRoster(XMPPComponent.sql, user_from.getUID(), jid); + if (haveInRoster) { + mm.from = new JID(user_from.getUName(), "juick.com", "Juick"); + mm.body = body; + } else { + mm.from = new JID("juick", "juick.com", "Juick"); + mm.body = "Private message from @" + user_from.getUName() + ":\n" + body; } + XMPPComponent.sendOut(mm); } + } - Message reply = new Message(m.to, m.from); - if (ret == 200) { - reply.type = m.type; - reply.body = "Private message sent"; - } else { - reply.type = Message.Type.error; - reply.body = "Error " + ret; - } - XMPPComponent.sendOut(reply); + Message reply = new Message(m.to, m.from); + if (ret == 200) { + reply.type = m.type; + reply.body = "Private message sent"; + } else { + reply.type = Message.Type.error; + reply.body = "Error " + ret; } + XMPPComponent.sendOut(reply); } private static void commandBLShow(Message m, User user_from) throws Exception { - List blusers; - List bltags; - - synchronized (XMPPComponent.sqlSync) { - blusers = UserQueries.getUserBLUsers(XMPPComponent.sql, user_from.getUID()); - bltags = TagQueries.getUserBLTags(XMPPComponent.sql, user_from.getUID()); - } + List blusers = UserQueries.getUserBLUsers(XMPPComponent.sql, user_from.getUID()); + List bltags = TagQueries.getUserBLTags(XMPPComponent.sql, user_from.getUID()); String txt = ""; if (bltags.size() > 0) { diff --git a/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java b/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java index 70f01553..bb736b5e 100644 --- a/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java +++ b/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java @@ -43,7 +43,6 @@ public class XMPPComponent implements ServletContextListener { static final List inConnections = Collections.synchronizedList(new ArrayList<>()); static final List outConnections = Collections.synchronizedList(new ArrayList<>()); static final List outCache = Collections.synchronizedList(new ArrayList<>()); - static final Integer sqlSync = 0; static JdbcTemplate sql; final public static HashMap childParsers = new HashMap<>(); -- cgit v1.2.3 From 02fd70cf59c30e3a9702eaf60d9e51f460efd8ca Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 6 Jul 2016 19:04:41 +0300 Subject: static components are evil --- src/main/java/com/juick/xmpp/s2s/CleaningUp.java | 25 +++-- src/main/java/com/juick/xmpp/s2s/Connection.java | 10 +- src/main/java/com/juick/xmpp/s2s/ConnectionIn.java | 44 +++++---- .../com/juick/xmpp/s2s/ConnectionListener.java | 12 ++- .../java/com/juick/xmpp/s2s/ConnectionOut.java | 34 +++---- .../java/com/juick/xmpp/s2s/ConnectionRouter.java | 36 +++---- src/main/java/com/juick/xmpp/s2s/JuickBot.java | 106 +++++++++++---------- .../java/com/juick/xmpp/s2s/XMPPComponent.java | 66 ++++++------- 8 files changed, 179 insertions(+), 154 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/xmpp/s2s/CleaningUp.java b/src/main/java/com/juick/xmpp/s2s/CleaningUp.java index 48771580..14d97ed8 100644 --- a/src/main/java/com/juick/xmpp/s2s/CleaningUp.java +++ b/src/main/java/com/juick/xmpp/s2s/CleaningUp.java @@ -10,19 +10,24 @@ import java.util.Iterator; * @author ugnich */ public class CleaningUp implements Runnable { + XMPPComponent xmpp; + + public CleaningUp(XMPPComponent xmpp) { + this.xmpp = xmpp; + } @Override public void run() { while (true) { try { - PrintWriter statsFile = new PrintWriter(XMPPComponent.STATSFILE, "UTF-8"); + PrintWriter statsFile = new PrintWriter(xmpp.STATSFILE, "UTF-8"); statsFile.write("

Threads: " + Thread.activeCount() + "

"); - statsFile.write("

Out (" + XMPPComponent.outConnections.size() + ")

"); + statsFile.write("

Out (" + xmpp.outConnections.size() + ")

tosidinactiveout packetsout bytes
"); long now = System.currentTimeMillis(); - synchronized (XMPPComponent.outConnections) { - for (Iterator i = XMPPComponent.outConnections.iterator(); i.hasNext();) { + synchronized (xmpp.outConnections) { + for (Iterator i = xmpp.outConnections.iterator(); i.hasNext();) { ConnectionOut c = i.next(); int inactive = (int) ((double) (now - c.tsLocalData) / 1000.0); if (inactive > 900) { @@ -40,10 +45,10 @@ public class CleaningUp implements Runnable { } } - statsFile.write("
tosidinactiveout packetsout bytes

In (" + XMPPComponent.inConnections.size() + ")

"); + statsFile.write("
fromsidinactivein packets

In (" + xmpp.inConnections.size() + ")

"); - synchronized (XMPPComponent.inConnections) { - for (Iterator i = XMPPComponent.inConnections.iterator(); i.hasNext();) { + synchronized (xmpp.inConnections) { + for (Iterator i = xmpp.inConnections.iterator(); i.hasNext();) { ConnectionIn c = i.next(); int inactive = (int) ((double) (now - c.tsRemoteData) / 1000.0); if (inactive > 900) { @@ -73,10 +78,10 @@ public class CleaningUp implements Runnable { } } - statsFile.write("
fromsidinactivein packets

Cache (" + XMPPComponent.outCache.size() + ")

"); + statsFile.write("
hostlivesize

Cache (" + xmpp.outCache.size() + ")

"); - synchronized (XMPPComponent.outCache) { - for (Iterator i = XMPPComponent.outCache.iterator(); i.hasNext();) { + synchronized (xmpp.outCache) { + for (Iterator i = xmpp.outCache.iterator(); i.hasNext();) { CacheEntry c = i.next(); int inactive = (int) ((double) (now - c.tsCreated) / 1000.0); if (inactive > 600) { diff --git a/src/main/java/com/juick/xmpp/s2s/Connection.java b/src/main/java/com/juick/xmpp/s2s/Connection.java index c1bbf22e..eae6efaa 100644 --- a/src/main/java/com/juick/xmpp/s2s/Connection.java +++ b/src/main/java/com/juick/xmpp/s2s/Connection.java @@ -30,6 +30,7 @@ public class Connection { public long tsLocalData = 0; public long bytesLocal = 0; public long packetsLocal = 0; + XMPPComponent xmpp; Socket socket; public static final String NS_DB = "jabber:server:dialback"; public static final String NS_TLS = "urn:ietf:params:xml:ns:xmpp-tls"; @@ -52,17 +53,18 @@ public class Connection { }; - public Connection() throws XmlPullParserException, KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException, UnrecoverableKeyException, KeyManagementException { + public Connection(XMPPComponent xmpp) throws XmlPullParserException, KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException, UnrecoverableKeyException, KeyManagementException { + this.xmpp = xmpp; tsCreated = System.currentTimeMillis(); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); KeyStore ks = KeyStore.getInstance("JKS"); - try (InputStream ksIs = new FileInputStream(XMPPComponent.keystore)) { - ks.load(ksIs, XMPPComponent.keystorePassword.toCharArray()); + try (InputStream ksIs = new FileInputStream(xmpp.keystore)) { + ks.load(ksIs, xmpp.keystorePassword.toCharArray()); } KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory .getDefaultAlgorithm()); - kmf.init(ks, XMPPComponent.keystorePassword.toCharArray()); + kmf.init(ks, xmpp.keystorePassword.toCharArray()); sc = SSLContext.getInstance("TLSv1.2"); sc.init(kmf.getKeyManagers(), trustAllCerts, new SecureRandom()); diff --git a/src/main/java/com/juick/xmpp/s2s/ConnectionIn.java b/src/main/java/com/juick/xmpp/s2s/ConnectionIn.java index 93e761a0..ea1cf0b1 100644 --- a/src/main/java/com/juick/xmpp/s2s/ConnectionIn.java +++ b/src/main/java/com/juick/xmpp/s2s/ConnectionIn.java @@ -31,9 +31,13 @@ public class ConnectionIn extends Connection implements Runnable { final public List from = new ArrayList<>(); public long tsRemoteData = 0; public long packetsRemote = 0; + XMPPComponent xmpp; + JuickBot bot; - public ConnectionIn(Socket socket) throws Exception { - super(); + public ConnectionIn(XMPPComponent xmpp, JuickBot bot, Socket socket) throws Exception { + super(xmpp); + this.xmpp = xmpp; + this.bot = bot; this.socket = socket; streamID = UUID.randomUUID().toString(); restartParser(); @@ -69,19 +73,19 @@ public class ConnectionIn extends Connection implements Runnable { String dfrom = parser.getAttributeValue(null, "from"); String to = parser.getAttributeValue(null, "to"); LOGGER.info("STREAM FROM " + dfrom + " TO " + to + " " + streamID + " ASKING FOR DIALBACK"); - if (dfrom.endsWith(XMPPComponent.HOSTNAME) && (dfrom.equals(XMPPComponent.HOSTNAME) || dfrom.endsWith("." + XMPPComponent.HOSTNAME))) { + if (dfrom.endsWith(xmpp.HOSTNAME) && (dfrom.equals(xmpp.HOSTNAME) || dfrom.endsWith("." + xmpp.HOSTNAME))) { break; } - if (to != null && to.equals(XMPPComponent.HOSTNAME)) { + if (to != null && to.equals(xmpp.HOSTNAME)) { String dbKey = XmlUtils.getTagText(parser); updateTsRemoteData(); - ConnectionOut c = XMPPComponent.getConnectionOut(dfrom, false); + ConnectionOut c = xmpp.getConnectionOut(dfrom, false); if (c != null) { c.sendDialbackVerify(streamID, dbKey); } else { - c = new ConnectionOut(dfrom, streamID, dbKey); - XMPPComponent.executorService.submit(c); + c = new ConnectionOut(xmpp, dfrom, streamID, dbKey); + xmpp.executorService.submit(c); } } else { throw new HostUnknownException("STREAM FROM " + dfrom + " " + streamID + " INVALID TO " + to); @@ -107,15 +111,15 @@ public class ConnectionIn extends Connection implements Runnable { } else if (tag.equals("presence") && checkFromTo(parser)) { Presence p = Presence.parse(parser, null); if (p != null && (p.type == null || !p.type.equals(Presence.Type.error))) { - JuickBot.incomingPresence(p); + bot.incomingPresence(p); } } else if (tag.equals("message") && checkFromTo(parser)) { updateTsRemoteData(); - Message msg = Message.parse(parser, XMPPComponent.childParsers); + Message msg = Message.parse(parser, xmpp.childParsers); if (msg != null && (msg.type == null || !msg.type.equals(Message.Type.error))) { LOGGER.info("STREAM " + streamID + ": " + msg.toString()); - if (!JuickBot.incomingMessage(msg)) { - XMPPComponent.connRouter.router.send(msg.toString()); + if (!bot.incomingMessage(msg)) { + xmpp.connRouter.router.send(msg.toString()); } } } else if (tag.equals("iq") && checkFromTo(parser)) { @@ -124,7 +128,7 @@ public class ConnectionIn extends Connection implements Runnable { String xml = XmlUtils.parseToString(parser, true); if (type == null || !type.equals(Iq.Type.error)) { LOGGER.info("STREAM " + streamID + ": " + xml); - XMPPComponent.connRouter.router.send(xml); + xmpp.connRouter.router.send(xml); } } else if (!isSecured() && tag.equals("starttls")) { LOGGER.info("STREAM " + streamID + " SECURING"); @@ -140,7 +144,7 @@ public class ConnectionIn extends Connection implements Runnable { } catch (SSLException sex) { LOGGER.warning("STREAM " + streamID + " SSL ERROR"); sendStanza(""); - XMPPComponent.removeConnectionIn(this); + xmpp.removeConnectionIn(this); closeConnection(); } } else if (isSecured() && tag.equals("stream") && parser.getNamespace().equals(NS_STREAM)) { @@ -150,17 +154,17 @@ public class ConnectionIn extends Connection implements Runnable { } } LOGGER.warning("STREAM " + streamID + " FINISHED"); - XMPPComponent.removeConnectionIn(this); + xmpp.removeConnectionIn(this); closeConnection(); } catch (EOFException ex) { LOGGER.info(String.format("STREAM %s CLOSED (dirty)", streamID)); - XMPPComponent.removeConnectionIn(this); + xmpp.removeConnectionIn(this); closeConnection(); } catch (HostUnknownException e) { LOGGER.warning(e.getMessage()); } catch (Exception e) { LOGGER.log(Level.WARNING, "STREAM " + streamID + " ERROR", e); - XMPPComponent.removeConnectionIn(this); + xmpp.removeConnectionIn(this); closeConnection(); } } @@ -172,10 +176,10 @@ public class ConnectionIn extends Connection implements Runnable { void sendOpenStream(String from, boolean xmppversionnew) throws IOException { String openStream = ""; + xmpp.HOSTNAME + "' id='" + streamID + "' version='1.0'>"; if (xmppversionnew) { openStream += ""; - if (!isSecured() && !XMPPComponent.brokenSSLhosts.contains(from)) { + if (!isSecured() && !xmpp.brokenSSLhosts.contains(from)) { openStream += ""; } openStream += ""; @@ -185,7 +189,7 @@ public class ConnectionIn extends Connection implements Runnable { public void sendDialbackResult(String sfrom, String type) { try { - sendStanza(""); + sendStanza(""); if (type.equals("valid")) { from.add(sfrom); LOGGER.info("STREAM FROM " + sfrom + " " + streamID + " READY"); @@ -200,7 +204,7 @@ public class ConnectionIn extends Connection implements Runnable { String cto = parser.getAttributeValue(null, "to"); if (cfrom != null && cto != null && !cfrom.isEmpty() && !cto.isEmpty()) { JID jidto = new JID(cto); - if (jidto.Host != null && jidto.Username != null && jidto.Host.equals(XMPPComponent.HOSTNAME) && jidto.Username.matches("^[a-zA-Z0-9\\-]{2,16}$")) { + if (jidto.Host != null && jidto.Username != null && jidto.Host.equals(xmpp.HOSTNAME) && jidto.Username.matches("^[a-zA-Z0-9\\-]{2,16}$")) { JID jidfrom = new JID(cfrom); int size = from.size(); for (int i = 0; i < size; i++) { diff --git a/src/main/java/com/juick/xmpp/s2s/ConnectionListener.java b/src/main/java/com/juick/xmpp/s2s/ConnectionListener.java index 569d3db0..607397a8 100644 --- a/src/main/java/com/juick/xmpp/s2s/ConnectionListener.java +++ b/src/main/java/com/juick/xmpp/s2s/ConnectionListener.java @@ -13,6 +13,12 @@ public class ConnectionListener implements Runnable { private static final Logger logger = Logger.getLogger(ConnectionListener.class.getName()); + XMPPComponent xmpp; + + public ConnectionListener(XMPPComponent xmpp) { + this.xmpp = xmpp; + } + @Override public void run() { try { @@ -21,9 +27,9 @@ public class ConnectionListener implements Runnable { while (true) { try { Socket socket = listener.accept(); - ConnectionIn client = new ConnectionIn(socket); - XMPPComponent.addConnectionIn(client); - XMPPComponent.executorService.submit(client); + ConnectionIn client = new ConnectionIn(xmpp, new JuickBot(xmpp), socket); + xmpp.addConnectionIn(client); + xmpp.executorService.submit(client); } catch (Exception e) { logger.log(Level.SEVERE, "s2s error", e); } diff --git a/src/main/java/com/juick/xmpp/s2s/ConnectionOut.java b/src/main/java/com/juick/xmpp/s2s/ConnectionOut.java index d3a10406..b79cb49a 100644 --- a/src/main/java/com/juick/xmpp/s2s/ConnectionOut.java +++ b/src/main/java/com/juick/xmpp/s2s/ConnectionOut.java @@ -26,16 +26,18 @@ public class ConnectionOut extends Connection implements Runnable { public boolean streamReady = false; public String to; + XMPPComponent xmpp; String checkSID = null; String dbKey = null; - public ConnectionOut(String hostname) throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, XmlPullParserException, KeyManagementException, KeyStoreException, IOException { - super(); + public ConnectionOut(XMPPComponent xmpp, String hostname) throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, XmlPullParserException, KeyManagementException, KeyStoreException, IOException { + super(xmpp); + this.xmpp = xmpp; to = hostname; } - public ConnectionOut(String hostname, String checkSID, String dbKey) throws Exception { - super(); + public ConnectionOut(XMPPComponent xmpp, String hostname, String checkSID, String dbKey) throws Exception { + super(xmpp); to = hostname; this.checkSID = checkSID; this.dbKey = dbKey; @@ -44,15 +46,15 @@ public class ConnectionOut extends Connection implements Runnable { void sendOpenStream() throws IOException { sendStanza(""); + xmpp.HOSTNAME + "' to='" + to + "' version='1.0'>"); } void processDialback() throws Exception { if (checkSID != null) { sendDialbackVerify(checkSID, dbKey); } - sendStanza("" + - generateDialbackKey(to, XMPPComponent.HOSTNAME, streamID) + ""); + sendStanza("" + + generateDialbackKey(to, xmpp.HOSTNAME, streamID) + ""); } @Override @@ -76,7 +78,7 @@ public class ConnectionOut extends Connection implements Runnable { } LOGGER.info("STREAM TO " + to + " " + streamID + " OPEN"); - XMPPComponent.addConnectionOut(ConnectionOut.this); + xmpp.addConnectionOut(ConnectionOut.this); boolean xmppversionnew = parser.getAttributeValue(null, "version") != null; if (!xmppversionnew) { processDialback(); @@ -95,7 +97,7 @@ public class ConnectionOut extends Connection implements Runnable { streamReady = true; LOGGER.info("STREAM TO " + to + " " + streamID + " READY"); - String cache = XMPPComponent.getFromCache(to); + String cache = xmpp.getFromCache(to); if (cache != null) { LOGGER.info("STREAM TO " + to + " " + streamID + " SENDING CACHE"); sendStanza(cache); @@ -110,7 +112,7 @@ public class ConnectionOut extends Connection implements Runnable { String type = parser.getAttributeValue(null, "type"); String sid = parser.getAttributeValue(null, "id"); if (from != null && from.equals(to) && sid != null && !sid.isEmpty() && type != null) { - ConnectionIn c = XMPPComponent.getConnectionIn(sid); + ConnectionIn c = xmpp.getConnectionIn(sid); if (c != null) { c.sendDialbackResult(from, type); } @@ -118,7 +120,7 @@ public class ConnectionOut extends Connection implements Runnable { XmlUtils.skip(parser); } else if (tag.equals("features") && parser.getNamespace().equals(NS_STREAM)) { StreamFeatures features = StreamFeatures.parse(parser); - if (!isSecured() && features.STARTTLS >= 0 && !XMPPComponent.brokenSSLhosts.contains(to)) { + if (!isSecured() && features.STARTTLS >= 0 && !xmpp.brokenSSLhosts.contains(to)) { System.out.println("STREAM TO " + to + " " + streamID + " SECURING"); sendStanza(""); } else { @@ -136,7 +138,7 @@ public class ConnectionOut extends Connection implements Runnable { } catch (SSLException sex) { LOGGER.log(Level.SEVERE, String.format("s2s ssl error: %s %s", to, streamID), sex); sendStanza(""); - XMPPComponent.removeConnectionOut(this); + xmpp.removeConnectionOut(this); closeConnection(); } } else if (isSecured() && tag.equals("stream") && parser.getNamespace().equals(NS_STREAM)) { @@ -147,22 +149,22 @@ public class ConnectionOut extends Connection implements Runnable { } LOGGER.warning("STREAM TO " + to + " " + streamID + " FINISHED"); - XMPPComponent.removeConnectionOut(ConnectionOut.this); + xmpp.removeConnectionOut(ConnectionOut.this); closeConnection(); } catch (EOFException eofex) { LOGGER.info(String.format("STREAM %s %s CLOSED (dirty)", to, streamID)); - XMPPComponent.removeConnectionOut(ConnectionOut.this); + xmpp.removeConnectionOut(ConnectionOut.this); closeConnection(); } catch (Exception e) { LOGGER.log(Level.SEVERE, String.format("s2s out exception: %s %s", to, streamID), e); - XMPPComponent.removeConnectionOut(ConnectionOut.this); + xmpp.removeConnectionOut(ConnectionOut.this); closeConnection(); } } public void sendDialbackVerify(String sid, String key) { try { - sendStanza("" + key + ""); + sendStanza("" + key + ""); } catch (IOException e) { LOGGER.log(Level.WARNING, "STREAM TO " + to + " " + streamID + " ERROR", e); } diff --git a/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java b/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java index 0030834a..588cfe2c 100644 --- a/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java +++ b/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java @@ -27,8 +27,10 @@ public class ConnectionRouter implements Stream.StreamListener, private String password; Stream router; Socket socket; + XMPPComponent xmpp; - ConnectionRouter(String componentName, String password) { + ConnectionRouter(XMPPComponent s2s, String componentName, String password) { + this.xmpp = s2s; this.componentName = componentName; this.password = password; reconnect(); @@ -56,11 +58,11 @@ public class ConnectionRouter implements Stream.StreamListener, List jids = new ArrayList<>(); if (jmsg.FriendsOnly) { - jids = SubscriptionsQueries.getJIDSubscribedToUser(XMPPComponent.sql, jmsg.getUser().getUID(), jmsg.FriendsOnly); + jids = SubscriptionsQueries.getJIDSubscribedToUser(xmpp.sql, jmsg.getUser().getUID(), jmsg.FriendsOnly); } else { - List users = SubscriptionsQueries.getSubscribedUsers(XMPPComponent.sql, jmsg.getUser().getUID(), jmsg.getMID()); + List users = SubscriptionsQueries.getSubscribedUsers(xmpp.sql, jmsg.getUser().getUID(), jmsg.getMID()); for (User user : users) { - for (String jid : UserQueries.getJIDsbyUID(XMPPComponent.sql, user.getUID())) { + for (String jid : UserQueries.getJIDsbyUID(xmpp.sql, user.getUID())) { jids.add(jid); } } @@ -92,7 +94,7 @@ public class ConnectionRouter implements Stream.StreamListener, for (String jid : jids) { msg.to = new JID(jid); - XMPPComponent.sendOut(msg); + xmpp.sendOut(msg); } } @@ -101,9 +103,9 @@ public class ConnectionRouter implements Stream.StreamListener, String replyQuote; String replyTo; - users = SubscriptionsQueries.getUsersSubscribedToComments(XMPPComponent.sql, jmsg.getMID(), jmsg.getUser().getUID()); - com.juick.Message replyMessage = jmsg.ReplyTo > 0 ? MessagesQueries.getReply(XMPPComponent.sql, jmsg.getMID(), jmsg.ReplyTo) - : MessagesQueries.getMessage(XMPPComponent.sql, jmsg.getMID()); + users = SubscriptionsQueries.getUsersSubscribedToComments(xmpp.sql, jmsg.getMID(), jmsg.getUser().getUID()); + com.juick.Message replyMessage = jmsg.ReplyTo > 0 ? MessagesQueries.getReply(xmpp.sql, jmsg.getMID(), jmsg.ReplyTo) + : MessagesQueries.getMessage(xmpp.sql, jmsg.getMID()); replyTo = replyMessage.getUser().getUName(); replyQuote = getReplyQuote(replyMessage); @@ -120,9 +122,9 @@ public class ConnectionRouter implements Stream.StreamListener, msg.type = Message.Type.chat; msg.addChild(jmsg); for (User user : users) { - for (String jid : UserQueries.getJIDsbyUID(XMPPComponent.sql, user.getUID())) { + for (String jid : UserQueries.getJIDsbyUID(xmpp.sql, user.getUID())) { msg.to = new JID(jid); - XMPPComponent.sendOut(msg); + xmpp.sendOut(msg); } } } @@ -140,8 +142,8 @@ public class ConnectionRouter implements Stream.StreamListener, public void sendJuickRecommendation(JuickMessage recomm) { List users; JuickMessage jmsg; - jmsg = new JuickMessage(MessagesQueries.getMessage(XMPPComponent.sql, recomm.getMID())); - users = SubscriptionsQueries.getUsersSubscribedToUserRecommendations(XMPPComponent.sql, + jmsg = new JuickMessage(MessagesQueries.getMessage(xmpp.sql, recomm.getMID())); + users = SubscriptionsQueries.getUsersSubscribedToUserRecommendations(xmpp.sql, recomm.getUser().getUID(), recomm.getMID(), jmsg.getUser().getUID()); String txt = "Recommended by @" + recomm.getUser().getUName() + ":\n"; @@ -178,9 +180,9 @@ public class ConnectionRouter implements Stream.StreamListener, } for (User user : users) { - for (String jid : UserQueries.getJIDsbyUID(XMPPComponent.sql, user.getUID())) { + for (String jid : UserQueries.getJIDsbyUID(xmpp.sql, user.getUID())) { msg.to = new JID(jid); - XMPPComponent.sendOut(msg); + xmpp.sendOut(msg); } } } @@ -190,7 +192,7 @@ public class ConnectionRouter implements Stream.StreamListener, JID jid = iq.to; if (!jid.Host.equals(componentName)) { logger.info("STREAM ROUTER (IQ): " + iq.toString()); - XMPPComponent.sendOut(iq); + xmpp.sendOut(iq); } return false; } @@ -213,7 +215,7 @@ public class ConnectionRouter implements Stream.StreamListener, } } } else { - XMPPComponent.sendOut(xmsg); + xmpp.sendOut(xmsg); } } @@ -222,7 +224,7 @@ public class ConnectionRouter implements Stream.StreamListener, JID jid = presence.to; if (!jid.Host.equals(componentName)) { logger.info("STREAM ROUTER (PRESENCE): " + presence.toString()); - XMPPComponent.sendOut(presence); + xmpp.sendOut(presence); } } diff --git a/src/main/java/com/juick/xmpp/s2s/JuickBot.java b/src/main/java/com/juick/xmpp/s2s/JuickBot.java index 357b070f..18e7580d 100644 --- a/src/main/java/com/juick/xmpp/s2s/JuickBot.java +++ b/src/main/java/com/juick/xmpp/s2s/JuickBot.java @@ -19,6 +19,10 @@ import java.util.regex.Pattern; * @author ugnich */ public class JuickBot { + XMPPComponent xmpp; + public JuickBot(XMPPComponent xmpp) { + this.xmpp = xmpp; + } public static final JID JuickJID = new JID("juick", "juick.com", "Juick"); private static final String HELPTEXT = @@ -55,7 +59,7 @@ public class JuickBot { + "\n" + "Read more: http://juick.com/help/"; - public static boolean incomingPresence(Presence p) throws Exception { + public boolean incomingPresence(Presence p) throws Exception { final String username = p.to.Username.toLowerCase(); final boolean toJuick = username.equals("juick"); @@ -64,12 +68,12 @@ public class JuickBot { reply.from = new JID(p.to.Username, p.to.Host, null); reply.to = new JID(p.from.Username, p.from.Host, null); reply.type = Presence.Type.unsubscribe; - XMPPComponent.sendOut(reply); + xmpp.sendOut(reply); return true; } else if (p.type.equals(Presence.Type.probe)) { int uid_to = 0; if (!toJuick) { - uid_to = UserQueries.getUIDbyName(XMPPComponent.sql, username); + uid_to = UserQueries.getUIDbyName(xmpp.sql, username); } if (toJuick || uid_to > 0) { @@ -78,12 +82,12 @@ public class JuickBot { reply.from.Resource = "Juick"; reply.to = p.from; reply.priority = 10; - XMPPComponent.sendOut(reply); + xmpp.sendOut(reply); } else { Presence reply = new Presence(p.to, p.from, Presence.Type.error); reply.id = p.id; reply.addChild(new Error(Error.Type.cancel, "item-not-found")); - XMPPComponent.sendOut(reply); + xmpp.sendOut(reply); return true; } return true; @@ -92,46 +96,46 @@ public class JuickBot { if (toJuick) { canSubscribe = true; } else { - int uid_to = UserQueries.getUIDbyName(XMPPComponent.sql, username); + int uid_to = UserQueries.getUIDbyName(xmpp.sql, username); if (uid_to > 0) { - PMQueries.addPMinRoster(XMPPComponent.sql, uid_to, p.from.Bare()); + PMQueries.addPMinRoster(xmpp.sql, uid_to, p.from.Bare()); canSubscribe = true; } } if (canSubscribe) { Presence reply = new Presence(p.to, p.from, Presence.Type.subscribed); - XMPPComponent.sendOut(reply); + xmpp.sendOut(reply); reply.from.Resource = "Juick"; reply.priority = 10; reply.type = null; - XMPPComponent.sendOut(reply); + xmpp.sendOut(reply); return true; } else { Presence reply = new Presence(p.to, p.from, Presence.Type.error); reply.id = p.id; reply.addChild(new Error(Error.Type.cancel, "item-not-found")); - XMPPComponent.sendOut(reply); + xmpp.sendOut(reply); return true; } } else if (p.type.equals(Presence.Type.unsubscribe)) { if (!toJuick) { - int uid_to = UserQueries.getUIDbyName(XMPPComponent.sql, username); + int uid_to = UserQueries.getUIDbyName(xmpp.sql, username); if (uid_to > 0) { - PMQueries.removePMinRoster(XMPPComponent.sql, uid_to, p.from.Bare()); + PMQueries.removePMinRoster(xmpp.sql, uid_to, p.from.Bare()); } } Presence reply = new Presence(p.to, p.from, Presence.Type.unsubscribed); - XMPPComponent.sendOut(reply); + xmpp.sendOut(reply); } return false; } - public static boolean incomingMessage(Message msg) throws Exception { + public boolean incomingMessage(Message msg) throws Exception { if (msg.body == null || msg.body.isEmpty()) { return true; } @@ -140,9 +144,9 @@ public class JuickBot { User user_from = null; String signuphash = ""; - user_from = UserQueries.getUserByJID(XMPPComponent.sql, msg.from.Bare()); + user_from = UserQueries.getUserByJID(xmpp.sql, msg.from.Bare()); if (user_from == null) { - signuphash = UserQueries.getSignUpHashByJID(XMPPComponent.sql, msg.from.Bare()); + signuphash = UserQueries.getSignUpHashByJID(xmpp.sql, msg.from.Bare()); } if (user_from == null) { @@ -152,7 +156,7 @@ public class JuickBot { } else { reply.body = "Внимание, системное сообщение!\nВаш JabberID не обнаружен в списке доверенных. Для того, чтобы отправить сообщение пользователю " + username + "@juick.com, пожалуйста зарегистрируйте свой JabberID в системе: http://juick.com/signup?type=xmpp&hash=" + signuphash + "\nЕсли у вас уже есть учетная запись на Juick, вы сможете присоединить этот JabberID к ней.\n\nWarning, system message!\nYour JabberID is not found in our server's white list. To send a message to " + username + "@juick.com, please sign up: http://juick.com/signup?type=xmpp&hash=" + signuphash + "\nIf you already have an account on Juick, you will be proposed to attach this JabberID to your existing account."; } - XMPPComponent.sendOut(reply); + xmpp.sendOut(reply); return true; } @@ -160,19 +164,19 @@ public class JuickBot { return incomingMessageJuick(user_from, msg); } - int uid_to = UserQueries.getUIDbyName(XMPPComponent.sql, username); + int uid_to = UserQueries.getUIDbyName(xmpp.sql, username); if (uid_to == 0) { Message reply = new Message(msg.to, msg.from, Message.Type.error); reply.id = msg.id; reply.addChild(new Error(Error.Type.cancel, "item-not-found")); - XMPPComponent.sendOut(reply); + xmpp.sendOut(reply); return true; } boolean success = false; - if (!UserQueries.isInBLAny(XMPPComponent.sql, uid_to, user_from.getUID())) { - success = PMQueries.createPM(XMPPComponent.sql, user_from.getUID(), uid_to, msg.body); + if (!UserQueries.isInBLAny(xmpp.sql, uid_to, user_from.getUID())) { + success = PMQueries.createPM(xmpp.sql, user_from.getUID(), uid_to, msg.body); } if (success) { @@ -183,19 +187,19 @@ public class JuickBot { jmsg.setUser(user_from); jmsg.setText(msg.body); m.childs.add(jmsg); - XMPPComponent.connRouter.router.send(m.toString()); + xmpp.connRouter.router.send(m.toString()); m.to.Host = "ws.juick.com"; - XMPPComponent.connRouter.router.send(m.toString()); + xmpp.connRouter.router.send(m.toString()); List jids; boolean inroster = false; - jids = UserQueries.getJIDsbyUID(XMPPComponent.sql, uid_to); + jids = UserQueries.getJIDsbyUID(xmpp.sql, uid_to); for (String jid : jids) { Message mm = new Message(); mm.to = new JID(jid); mm.type = Message.Type.chat; - inroster = PMQueries.havePMinRoster(XMPPComponent.sql, user_from.getUID(), jid); + inroster = PMQueries.havePMinRoster(xmpp.sql, user_from.getUID(), jid); if (inroster) { mm.from = new JID(jmsg.getUser().getUName(), "juick.com", "Juick"); mm.body = msg.body; @@ -203,20 +207,20 @@ public class JuickBot { mm.from = new JID("juick", "juick.com", "Juick"); mm.body = "Private message from @" + jmsg.getUser().getUName() + ":\n" + msg.body; } - XMPPComponent.sendOut(mm); + xmpp.sendOut(mm); } } else { Message reply = new Message(msg.to, msg.from, Message.Type.error); reply.id = msg.id; reply.addChild(new Error(Error.Type.cancel, "not-allowed")); - XMPPComponent.sendOut(reply); + xmpp.sendOut(reply); } return true; } private static Pattern regexPM = Pattern.compile("^\\@(\\S+)\\s+([\\s\\S]+)$"); - public static boolean incomingMessageJuick(User user_from, Message msg) throws Exception { + public boolean incomingMessageJuick(User user_from, Message msg) throws Exception { String command = msg.body.trim(); int commandlen = command.length(); @@ -253,29 +257,29 @@ public class JuickBot { return false; } - private static void commandPing(Message m) throws Exception { + private void commandPing(Message m) throws Exception { Presence p = new Presence(JuickJID, m.from); p.priority = 10; - XMPPComponent.sendOut(p); + xmpp.sendOut(p); Message reply = new Message(JuickJID, m.from, Message.Type.chat); reply.body = "PONG"; - XMPPComponent.sendOut(reply); + xmpp.sendOut(reply); } - private static void commandHelp(Message m) throws Exception { + private void commandHelp(Message m) throws Exception { Message reply = new Message(JuickJID, m.from, Message.Type.chat); reply.body = HELPTEXT; - XMPPComponent.sendOut(reply); + xmpp.sendOut(reply); } - private static void commandLogin(Message m, User user_from) throws Exception { + private void commandLogin(Message m, User user_from) throws Exception { Message reply = new Message(JuickJID, m.from, Message.Type.chat); - reply.body = "http://juick.com/login?" + UserQueries.getHashByUID(XMPPComponent.sql, user_from.getUID()); - XMPPComponent.sendOut(reply); + reply.body = "http://juick.com/login?" + UserQueries.getHashByUID(xmpp.sql, user_from.getUID()); + xmpp.sendOut(reply); } - private static void commandPM(Message m, User user_from, String user_to, String body) throws Exception { + private void commandPM(Message m, User user_from, String user_to, String body) throws Exception { int ret = 0; int uid_to = 0; @@ -283,15 +287,15 @@ public class JuickBot { boolean haveInRoster = false; if (user_to.indexOf('@') > 0) { - uid_to = UserQueries.getUIDbyJID(XMPPComponent.sql, user_to); + uid_to = UserQueries.getUIDbyJID(xmpp.sql, user_to); } else { - uid_to = UserQueries.getUIDbyName(XMPPComponent.sql, user_to); + uid_to = UserQueries.getUIDbyName(xmpp.sql, user_to); } if (uid_to > 0) { - if (!UserQueries.isInBLAny(XMPPComponent.sql, uid_to, user_from.getUID())) { - if (PMQueries.createPM(XMPPComponent.sql, user_from.getUID(), uid_to, body)) { - jids_to = UserQueries.getJIDsbyUID(XMPPComponent.sql, uid_to); + if (!UserQueries.isInBLAny(xmpp.sql, uid_to, user_from.getUID())) { + if (PMQueries.createPM(xmpp.sql, user_from.getUID(), uid_to, body)) { + jids_to = UserQueries.getJIDsbyUID(xmpp.sql, uid_to); ret = 200; } else { ret = 500; @@ -311,16 +315,16 @@ public class JuickBot { jmsg.setUser(user_from); jmsg.setText(body); msg.childs.add(jmsg); - XMPPComponent.connRouter.router.send(msg.toString()); + xmpp.connRouter.router.send(msg.toString()); msg.to.Host = "ws.juick.com"; - XMPPComponent.connRouter.router.send(msg.toString()); + xmpp.connRouter.router.send(msg.toString()); for (String jid : jids_to) { Message mm = new Message(); mm.to = new JID(jid); mm.type = Message.Type.chat; - haveInRoster = PMQueries.havePMinRoster(XMPPComponent.sql, user_from.getUID(), jid); + haveInRoster = PMQueries.havePMinRoster(xmpp.sql, user_from.getUID(), jid); if (haveInRoster) { mm.from = new JID(user_from.getUName(), "juick.com", "Juick"); mm.body = body; @@ -328,7 +332,7 @@ public class JuickBot { mm.from = new JID("juick", "juick.com", "Juick"); mm.body = "Private message from @" + user_from.getUName() + ":\n" + body; } - XMPPComponent.sendOut(mm); + xmpp.sendOut(mm); } } @@ -340,12 +344,12 @@ public class JuickBot { reply.type = Message.Type.error; reply.body = "Error " + ret; } - XMPPComponent.sendOut(reply); + xmpp.sendOut(reply); } - private static void commandBLShow(Message m, User user_from) throws Exception { - List blusers = UserQueries.getUserBLUsers(XMPPComponent.sql, user_from.getUID()); - List bltags = TagQueries.getUserBLTags(XMPPComponent.sql, user_from.getUID()); + private void commandBLShow(Message m, User user_from) throws Exception { + List blusers = UserQueries.getUserBLUsers(xmpp.sql, user_from.getUID()); + List bltags = TagQueries.getUserBLTags(xmpp.sql, user_from.getUID()); String txt = ""; if (bltags.size() > 0) { @@ -368,6 +372,6 @@ public class JuickBot { Message reply = new Message(JuickJID, m.from, Message.Type.chat); reply.body = txt; - XMPPComponent.sendOut(reply); + xmpp.sendOut(reply); } } diff --git a/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java b/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java index bb736b5e..2b75fef1 100644 --- a/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java +++ b/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java @@ -32,45 +32,45 @@ public class XMPPComponent implements ServletContextListener { private static final Logger LOGGER = Logger.getLogger(XMPPComponent.class.getName()); - public static final ExecutorService executorService = Executors.newCachedThreadPool(); - - public static String HOSTNAME = null; - public static String STATSFILE = null; - public static String keystore; - public static String keystorePassword; - public static List brokenSSLhosts; - public static ConnectionRouter connRouter; - static final List inConnections = Collections.synchronizedList(new ArrayList<>()); - static final List outConnections = Collections.synchronizedList(new ArrayList<>()); - static final List outCache = Collections.synchronizedList(new ArrayList<>()); - static JdbcTemplate sql; - final public static HashMap childParsers = new HashMap<>(); - - public static void addConnectionIn(ConnectionIn c) { + public final ExecutorService executorService = Executors.newCachedThreadPool(); + + public String HOSTNAME = null; + public String STATSFILE = null; + public String keystore; + public String keystorePassword; + public List brokenSSLhosts; + public ConnectionRouter connRouter; + final List inConnections = Collections.synchronizedList(new ArrayList<>()); + final List outConnections = Collections.synchronizedList(new ArrayList<>()); + final List outCache = Collections.synchronizedList(new ArrayList<>()); + JdbcTemplate sql; + final public HashMap childParsers = new HashMap<>(); + + public void addConnectionIn(ConnectionIn c) { synchronized (inConnections) { inConnections.add(c); } } - public static void addConnectionOut(ConnectionOut c) { + public void addConnectionOut(ConnectionOut c) { synchronized (outConnections) { outConnections.add(c); } } - public static void removeConnectionIn(ConnectionIn c) { + public void removeConnectionIn(ConnectionIn c) { synchronized (inConnections) { inConnections.remove(c); } } - public static void removeConnectionOut(ConnectionOut c) { + public void removeConnectionOut(ConnectionOut c) { synchronized (outConnections) { outConnections.remove(c); } } - public static String getFromCache(String hostname) { + public String getFromCache(String hostname) { CacheEntry ret = null; synchronized (outCache) { for (Iterator i = outCache.iterator(); i.hasNext();) { @@ -85,7 +85,7 @@ public class XMPPComponent implements ServletContextListener { return (ret != null) ? ret.xml : null; } - public static ConnectionOut getConnectionOut(String hostname, boolean needReady) { + public ConnectionOut getConnectionOut(String hostname, boolean needReady) { synchronized (outConnections) { for (ConnectionOut c : outConnections) { if (c.to != null && c.to.equals(hostname) && (!needReady || c.streamReady)) { @@ -96,7 +96,7 @@ public class XMPPComponent implements ServletContextListener { return null; } - public static ConnectionIn getConnectionIn(String streamID) { + public ConnectionIn getConnectionIn(String streamID) { synchronized (inConnections) { for (ConnectionIn c : inConnections) { if (c.streamID != null && c.streamID.equals(streamID)) { @@ -107,11 +107,11 @@ public class XMPPComponent implements ServletContextListener { return null; } - public static void sendOut(Stanza s) { + public void sendOut(Stanza s) { sendOut(s.to.Host, s.toString()); } - public static void sendOut(String hostname, String xml) { + public void sendOut(String hostname, String xml) { boolean haveAnyConn = false; ConnectionOut connOut = null; @@ -155,8 +155,8 @@ public class XMPPComponent implements ServletContextListener { if (!haveAnyConn) { ConnectionOut connectionOut = null; try { - connectionOut = new ConnectionOut(hostname); - XMPPComponent.executorService.submit(connectionOut); + connectionOut = new ConnectionOut(this, hostname); + executorService.submit(connectionOut); } catch (CertificateException | UnrecoverableKeyException | NoSuchAlgorithmException | XmlPullParserException | KeyStoreException | KeyManagementException | IOException e) { LOGGER.log(Level.SEVERE, "s2s out error", e); } @@ -185,9 +185,9 @@ public class XMPPComponent implements ServletContextListener { sql = new JdbcTemplate(dataSource); childParsers.put(JuickMessage.XMLNS, new JuickMessage()); - executorService.submit(() -> connRouter = new ConnectionRouter(componentName, conf.getProperty("xmpp_password"))); - executorService.submit(new ConnectionListener()); - executorService.submit(new CleaningUp()); + executorService.submit(() -> connRouter = new ConnectionRouter(this, componentName, conf.getProperty("xmpp_password"))); + executorService.submit(new ConnectionListener(this)); + executorService.submit(new CleaningUp(this)); } catch (Exception e) { LOGGER.log(Level.SEVERE, "XMPPComponent error", e); } @@ -198,16 +198,16 @@ public class XMPPComponent implements ServletContextListener { @Override public void contextDestroyed(ServletContextEvent sce) { - synchronized (XMPPComponent.outConnections) { - for (Iterator i = XMPPComponent.outConnections.iterator(); i.hasNext();) { + synchronized (outConnections) { + for (Iterator i = outConnections.iterator(); i.hasNext();) { ConnectionOut c = i.next(); c.closeConnection(); i.remove(); } } - synchronized (XMPPComponent.inConnections) { - for (Iterator i = XMPPComponent.inConnections.iterator(); i.hasNext();) { + synchronized (inConnections) { + for (Iterator i = inConnections.iterator(); i.hasNext();) { ConnectionIn c = i.next(); c.closeConnection(); i.remove(); @@ -215,7 +215,7 @@ public class XMPPComponent implements ServletContextListener { } try { - XMPPComponent.connRouter.closeConnection(); + connRouter.closeConnection(); } catch (IOException e) { LOGGER.log(Level.WARNING, "router warning", e); } -- cgit v1.2.3 From 32b3acd9a2cd20ea51e1375756bd95583d7e3dd7 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 6 Jul 2016 19:08:21 +0300 Subject: s2s: connection out must have stream id --- src/main/java/com/juick/xmpp/s2s/ConnectionOut.java | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/main') diff --git a/src/main/java/com/juick/xmpp/s2s/ConnectionOut.java b/src/main/java/com/juick/xmpp/s2s/ConnectionOut.java index b79cb49a..581881a6 100644 --- a/src/main/java/com/juick/xmpp/s2s/ConnectionOut.java +++ b/src/main/java/com/juick/xmpp/s2s/ConnectionOut.java @@ -17,6 +17,7 @@ import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.UnrecoverableKeyException; import java.security.cert.CertificateException; +import java.util.UUID; import java.util.logging.Level; /** @@ -41,6 +42,7 @@ public class ConnectionOut extends Connection implements Runnable { to = hostname; this.checkSID = checkSID; this.dbKey = dbKey; + streamID = UUID.randomUUID().toString(); } void sendOpenStream() throws IOException { -- cgit v1.2.3 From 152f36927fa373c619778ca84c2f9532cc3f561b Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 6 Jul 2016 19:15:38 +0300 Subject: cleanup --- src/main/java/com/juick/xmpp/s2s/XMPPComponent.java | 2 -- 1 file changed, 2 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java b/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java index 2b75fef1..245038d4 100644 --- a/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java +++ b/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java @@ -180,8 +180,6 @@ public class XMPPComponent implements ServletContextListener { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName(conf.getProperty("datasource_driver", "com.mysql.jdbc.Driver")); dataSource.setUrl(conf.getProperty("datasource_url")); - DriverManagerDataSource dataSourceSearch = new DriverManagerDataSource(); - dataSourceSearch.setDriverClassName(conf.getProperty("datasource_driver", "com.mysql.jdbc.Driver")); sql = new JdbcTemplate(dataSource); childParsers.put(JuickMessage.XMLNS, new JuickMessage()); -- cgit v1.2.3 From 95869ef683a517eb48db1784150ded7661294f8a Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 6 Jul 2016 19:18:04 +0300 Subject: drop recursive dependency --- .../com/juick/xmpp/s2s/ConnectionListener.java | 41 ---------------------- .../java/com/juick/xmpp/s2s/XMPPComponent.java | 15 +++++++- 2 files changed, 14 insertions(+), 42 deletions(-) delete mode 100644 src/main/java/com/juick/xmpp/s2s/ConnectionListener.java (limited to 'src/main') diff --git a/src/main/java/com/juick/xmpp/s2s/ConnectionListener.java b/src/main/java/com/juick/xmpp/s2s/ConnectionListener.java deleted file mode 100644 index 607397a8..00000000 --- a/src/main/java/com/juick/xmpp/s2s/ConnectionListener.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.juick.xmpp.s2s; - -import java.net.ServerSocket; -import java.net.Socket; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * - * @author vt - */ -public class ConnectionListener implements Runnable { - - private static final Logger logger = Logger.getLogger(ConnectionListener.class.getName()); - - XMPPComponent xmpp; - - public ConnectionListener(XMPPComponent xmpp) { - this.xmpp = xmpp; - } - - @Override - public void run() { - try { - final ServerSocket listener = new ServerSocket(5269); - logger.info("s2s listener ready"); - while (true) { - try { - Socket socket = listener.accept(); - ConnectionIn client = new ConnectionIn(xmpp, new JuickBot(xmpp), socket); - xmpp.addConnectionIn(client); - xmpp.executorService.submit(client); - } catch (Exception e) { - logger.log(Level.SEVERE, "s2s error", e); - } - } - } catch (Exception e) { - logger.log(Level.SEVERE, "s2s listener exception", e); - } - } -} diff --git a/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java b/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java index 245038d4..db7b11ac 100644 --- a/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java +++ b/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java @@ -10,6 +10,8 @@ import org.xmlpull.v1.XmlPullParserException; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; import java.security.KeyManagementException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; @@ -184,8 +186,19 @@ public class XMPPComponent implements ServletContextListener { childParsers.put(JuickMessage.XMLNS, new JuickMessage()); executorService.submit(() -> connRouter = new ConnectionRouter(this, componentName, conf.getProperty("xmpp_password"))); - executorService.submit(new ConnectionListener(this)); executorService.submit(new CleaningUp(this)); + final ServerSocket listener = new ServerSocket(5269); + LOGGER.info("s2s listener ready"); + while (true) { + try { + Socket socket = listener.accept(); + ConnectionIn client = new ConnectionIn(this, new JuickBot(this), socket); + addConnectionIn(client); + executorService.submit(client); + } catch (Exception e) { + LOGGER.log(Level.SEVERE, "s2s error", e); + } + } } catch (Exception e) { LOGGER.log(Level.SEVERE, "XMPPComponent error", e); } -- cgit v1.2.3 From 2ebd7a050acf477b68a72397395a026ec63d89dd Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 6 Jul 2016 19:34:54 +0300 Subject: separate thread for xmpp component --- src/main/java/com/juick/xmpp/s2s/XMPPComponent.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java b/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java index db7b11ac..5cdce82a 100644 --- a/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java +++ b/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java @@ -35,6 +35,7 @@ public class XMPPComponent implements ServletContextListener { private static final Logger LOGGER = Logger.getLogger(XMPPComponent.class.getName()); public final ExecutorService executorService = Executors.newCachedThreadPool(); + ExecutorService xmppService = Executors.newSingleThreadExecutor(); public String HOSTNAME = null; public String STATSFILE = null; @@ -169,7 +170,7 @@ public class XMPPComponent implements ServletContextListener { public void contextInitialized(ServletContextEvent sce) { LOGGER.info("component initialized"); - executorService.submit(() -> { + xmppService.submit(() -> { Properties conf = new Properties(); try { conf.load(sce.getServletContext().getResourceAsStream("/WEB-INF/juick.conf")); -- cgit v1.2.3 From 3f0044b6d7ed4533da2a9dd0480787b8a4ef9d01 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 6 Jul 2016 19:37:37 +0300 Subject: cleanup --- src/main/java/com/juick/xmpp/s2s/ConnectionOut.java | 2 -- 1 file changed, 2 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/xmpp/s2s/ConnectionOut.java b/src/main/java/com/juick/xmpp/s2s/ConnectionOut.java index 581881a6..fede701e 100644 --- a/src/main/java/com/juick/xmpp/s2s/ConnectionOut.java +++ b/src/main/java/com/juick/xmpp/s2s/ConnectionOut.java @@ -27,13 +27,11 @@ public class ConnectionOut extends Connection implements Runnable { public boolean streamReady = false; public String to; - XMPPComponent xmpp; String checkSID = null; String dbKey = null; public ConnectionOut(XMPPComponent xmpp, String hostname) throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, XmlPullParserException, KeyManagementException, KeyStoreException, IOException { super(xmpp); - this.xmpp = xmpp; to = hostname; } -- cgit v1.2.3 From 448c3854ff3ebed5fd38193c71a2bc7b9217fe02 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 6 Jul 2016 19:39:45 +0300 Subject: cleanup --- src/main/java/com/juick/xmpp/s2s/ConnectionIn.java | 2 -- 1 file changed, 2 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/xmpp/s2s/ConnectionIn.java b/src/main/java/com/juick/xmpp/s2s/ConnectionIn.java index ea1cf0b1..bfc561b4 100644 --- a/src/main/java/com/juick/xmpp/s2s/ConnectionIn.java +++ b/src/main/java/com/juick/xmpp/s2s/ConnectionIn.java @@ -31,12 +31,10 @@ public class ConnectionIn extends Connection implements Runnable { final public List from = new ArrayList<>(); public long tsRemoteData = 0; public long packetsRemote = 0; - XMPPComponent xmpp; JuickBot bot; public ConnectionIn(XMPPComponent xmpp, JuickBot bot, Socket socket) throws Exception { super(xmpp); - this.xmpp = xmpp; this.bot = bot; this.socket = socket; streamID = UUID.randomUUID().toString(); -- cgit v1.2.3 From 7cbf108b84682fb17af15f528df3f0bd952fc58d Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 6 Jul 2016 19:47:18 +0300 Subject: refactoring --- .../java/com/juick/xmpp/s2s/XMPPComponent.java | 41 +++++++++++----------- 1 file changed, 21 insertions(+), 20 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java b/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java index 5cdce82a..8ebdb76a 100644 --- a/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java +++ b/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java @@ -170,23 +170,23 @@ public class XMPPComponent implements ServletContextListener { public void contextInitialized(ServletContextEvent sce) { LOGGER.info("component initialized"); - xmppService.submit(() -> { - Properties conf = new Properties(); - try { - conf.load(sce.getServletContext().getResourceAsStream("/WEB-INF/juick.conf")); - HOSTNAME = conf.getProperty("hostname"); - String componentName = conf.getProperty("componentname"); - STATSFILE = conf.getProperty("statsfile"); - keystore = conf.getProperty("keystore"); - keystorePassword = conf.getProperty("keystore_password"); - brokenSSLhosts = Arrays.asList(conf.getProperty("broken_ssl_hosts", "").split(",")); - DriverManagerDataSource dataSource = new DriverManagerDataSource(); - dataSource.setDriverClassName(conf.getProperty("datasource_driver", "com.mysql.jdbc.Driver")); - dataSource.setUrl(conf.getProperty("datasource_url")); - sql = new JdbcTemplate(dataSource); + Properties conf = new Properties(); + try { + conf.load(sce.getServletContext().getResourceAsStream("/WEB-INF/juick.conf")); + HOSTNAME = conf.getProperty("hostname"); + String componentName = conf.getProperty("componentname"); + STATSFILE = conf.getProperty("statsfile"); + keystore = conf.getProperty("keystore"); + keystorePassword = conf.getProperty("keystore_password"); + brokenSSLhosts = Arrays.asList(conf.getProperty("broken_ssl_hosts", "").split(",")); + DriverManagerDataSource dataSource = new DriverManagerDataSource(); + dataSource.setDriverClassName(conf.getProperty("datasource_driver", "com.mysql.jdbc.Driver")); + dataSource.setUrl(conf.getProperty("datasource_url")); + sql = new JdbcTemplate(dataSource); - childParsers.put(JuickMessage.XMLNS, new JuickMessage()); - executorService.submit(() -> connRouter = new ConnectionRouter(this, componentName, conf.getProperty("xmpp_password"))); + childParsers.put(JuickMessage.XMLNS, new JuickMessage()); + xmppService.submit(() -> { + connRouter = new ConnectionRouter(this, componentName, conf.getProperty("xmpp_password")); executorService.submit(new CleaningUp(this)); final ServerSocket listener = new ServerSocket(5269); LOGGER.info("s2s listener ready"); @@ -200,10 +200,11 @@ public class XMPPComponent implements ServletContextListener { LOGGER.log(Level.SEVERE, "s2s error", e); } } - } catch (Exception e) { - LOGGER.log(Level.SEVERE, "XMPPComponent error", e); - } - }); + }); + + } catch (Exception e) { + LOGGER.log(Level.SEVERE, "XMPPComponent error", e); + } } -- cgit v1.2.3 From 8c8200933f01074b001b0f0f5bf7113ed6e29de6 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 6 Jul 2016 19:51:55 +0300 Subject: cleanup --- src/main/java/com/juick/CrosspostComponent.java | 20 -------------------- src/main/java/com/juick/PushComponent.java | 20 -------------------- src/main/java/com/juick/xmpp/s2s/XMPPComponent.java | 20 -------------------- 3 files changed, 60 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/CrosspostComponent.java b/src/main/java/com/juick/CrosspostComponent.java index d7ee8724..eb4e2207 100644 --- a/src/main/java/com/juick/CrosspostComponent.java +++ b/src/main/java/com/juick/CrosspostComponent.java @@ -92,26 +92,6 @@ public class CrosspostComponent implements ServletContextListener, Stream.Strea @Override public void contextDestroyed(ServletContextEvent sce) { - // Now deregister JDBC drivers in this context's ClassLoader: - // Get the webapp's ClassLoader - ClassLoader cl = Thread.currentThread().getContextClassLoader(); - // Loop through all drivers - Enumeration drivers = DriverManager.getDrivers(); - while (drivers.hasMoreElements()) { - Driver driver = drivers.nextElement(); - if (driver.getClass().getClassLoader() == cl) { - // This driver was registered by the webapp's ClassLoader, so deregister it: - try { - logger.info(String.format("Deregistering JDBC driver %s", driver.toString())); - DriverManager.deregisterDriver(driver); - } catch (SQLException ex) { - logger.log(Level.SEVERE, String.format("Error deregistering JDBC driver %s", driver), ex); - } - } else { - // driver was not registered by the webapp's ClassLoader and may be in use elsewhere - logger.log(Level.SEVERE, String.format("Not deregistering JDBC driver %s as it does not belong to this webapp's ClassLoader", driver)); - } - } executorService.shutdown(); logger.info("component destroyed"); } diff --git a/src/main/java/com/juick/PushComponent.java b/src/main/java/com/juick/PushComponent.java index e33d5fc3..d8401e51 100644 --- a/src/main/java/com/juick/PushComponent.java +++ b/src/main/java/com/juick/PushComponent.java @@ -107,26 +107,6 @@ public class PushComponent implements ServletContextListener, Stream.StreamListe @Override public void contextDestroyed(ServletContextEvent sce) { - // Now deregister JDBC drivers in this context's ClassLoader: - // Get the webapp's ClassLoader - ClassLoader cl = Thread.currentThread().getContextClassLoader(); - // Loop through all drivers - Enumeration drivers = DriverManager.getDrivers(); - while (drivers.hasMoreElements()) { - Driver driver = drivers.nextElement(); - if (driver.getClass().getClassLoader() == cl) { - // This driver was registered by the webapp's ClassLoader, so deregister it: - try { - logger.info(String.format("Deregistering JDBC driver %s", driver.toString())); - DriverManager.deregisterDriver(driver); - } catch (SQLException ex) { - logger.log(Level.SEVERE, String.format("Error deregistering JDBC driver %s", driver), ex); - } - } else { - // driver was not registered by the webapp's ClassLoader and may be in use elsewhere - logger.log(Level.SEVERE, String.format("Not deregistering JDBC driver %s as it does not belong to this webapp's ClassLoader", driver)); - } - } executorService.shutdown(); logger.info("component destroyed"); } diff --git a/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java b/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java index 8ebdb76a..ecfe1671 100644 --- a/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java +++ b/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java @@ -232,26 +232,6 @@ public class XMPPComponent implements ServletContextListener { } catch (IOException e) { LOGGER.log(Level.WARNING, "router warning", e); } - // Now deregister JDBC drivers in this context's ClassLoader: - // Get the webapp's ClassLoader - ClassLoader cl = Thread.currentThread().getContextClassLoader(); - // Loop through all drivers - Enumeration drivers = DriverManager.getDrivers(); - while (drivers.hasMoreElements()) { - Driver driver = drivers.nextElement(); - if (driver.getClass().getClassLoader() == cl) { - // This driver was registered by the webapp's ClassLoader, so deregister it: - try { - LOGGER.info(String.format("Deregistering JDBC driver %s", driver.toString())); - DriverManager.deregisterDriver(driver); - } catch (SQLException ex) { - LOGGER.log(Level.SEVERE, String.format("Error deregistering JDBC driver %s", driver), ex); - } - } else { - // driver was not registered by the webapp's ClassLoader and may be in use elsewhere - LOGGER.log(Level.SEVERE, String.format("Not deregistering JDBC driver %s as it does not belong to this webapp's ClassLoader", driver)); - } - } executorService.shutdown(); LOGGER.info("component destroyed"); } -- cgit v1.2.3 From 6933a80515e454b45e03c1d3609093ad1abcef92 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 6 Jul 2016 20:07:45 +0300 Subject: merge router and component --- src/main/java/com/juick/CrosspostComponent.java | 4 - src/main/java/com/juick/PushComponent.java | 11 +- src/main/java/com/juick/xmpp/s2s/ConnectionIn.java | 4 +- .../java/com/juick/xmpp/s2s/ConnectionRouter.java | 241 -------------------- src/main/java/com/juick/xmpp/s2s/JuickBot.java | 13 +- .../java/com/juick/xmpp/s2s/XMPPComponent.java | 244 +++++++++++++++++++-- 6 files changed, 234 insertions(+), 283 deletions(-) delete mode 100644 src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java (limited to 'src/main') diff --git a/src/main/java/com/juick/CrosspostComponent.java b/src/main/java/com/juick/CrosspostComponent.java index eb4e2207..8b07583f 100644 --- a/src/main/java/com/juick/CrosspostComponent.java +++ b/src/main/java/com/juick/CrosspostComponent.java @@ -38,10 +38,6 @@ import java.net.Socket; import java.net.URL; import java.net.URLEncoder; import java.security.Key; -import java.sql.Driver; -import java.sql.DriverManager; -import java.sql.SQLException; -import java.util.Enumeration; import java.util.Properties; import java.util.UUID; import java.util.concurrent.ExecutorService; diff --git a/src/main/java/com/juick/PushComponent.java b/src/main/java/com/juick/PushComponent.java index d8401e51..3bb7cdc5 100644 --- a/src/main/java/com/juick/PushComponent.java +++ b/src/main/java/com/juick/PushComponent.java @@ -22,10 +22,8 @@ import com.google.android.gcm.server.MulticastResult; import com.google.android.gcm.server.Result; import com.google.android.gcm.server.Sender; import com.juick.json.MessageSerializer; -import com.juick.server.MessagesQueries; import com.juick.server.PushQueries; import com.juick.server.SubscriptionsQueries; -import com.juick.server.UserQueries; import com.juick.xmpp.JID; import com.juick.xmpp.Message.MessageListener; import com.juick.xmpp.Stream; @@ -54,11 +52,10 @@ import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import java.io.IOException; import java.net.Socket; -import java.sql.Connection; -import java.sql.Driver; -import java.sql.DriverManager; -import java.sql.SQLException; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Properties; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.logging.Level; diff --git a/src/main/java/com/juick/xmpp/s2s/ConnectionIn.java b/src/main/java/com/juick/xmpp/s2s/ConnectionIn.java index bfc561b4..345caea9 100644 --- a/src/main/java/com/juick/xmpp/s2s/ConnectionIn.java +++ b/src/main/java/com/juick/xmpp/s2s/ConnectionIn.java @@ -117,7 +117,7 @@ public class ConnectionIn extends Connection implements Runnable { if (msg != null && (msg.type == null || !msg.type.equals(Message.Type.error))) { LOGGER.info("STREAM " + streamID + ": " + msg.toString()); if (!bot.incomingMessage(msg)) { - xmpp.connRouter.router.send(msg.toString()); + xmpp.router.send(msg.toString()); } } } else if (tag.equals("iq") && checkFromTo(parser)) { @@ -126,7 +126,7 @@ public class ConnectionIn extends Connection implements Runnable { String xml = XmlUtils.parseToString(parser, true); if (type == null || !type.equals(Iq.Type.error)) { LOGGER.info("STREAM " + streamID + ": " + xml); - xmpp.connRouter.router.send(xml); + xmpp.router.send(xml); } } else if (!isSecured() && tag.equals("starttls")) { LOGGER.info("STREAM " + streamID + " SECURING"); diff --git a/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java b/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java deleted file mode 100644 index 588cfe2c..00000000 --- a/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java +++ /dev/null @@ -1,241 +0,0 @@ -package com.juick.xmpp.s2s; - -import com.juick.User; -import com.juick.server.MessagesQueries; -import com.juick.server.SubscriptionsQueries; -import com.juick.server.UserQueries; -import com.juick.xmpp.*; -import com.juick.xmpp.extensions.JuickMessage; -import com.juick.xmpp.extensions.Nickname; -import com.juick.xmpp.extensions.XOOB; - -import java.io.IOException; -import java.net.Socket; -import java.util.ArrayList; -import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * @author ugnich - */ -public class ConnectionRouter implements Stream.StreamListener, - Message.MessageListener, Iq.IqListener, Presence.PresenceListener { - private static final Logger logger = Logger.getLogger(ConnectionRouter.class.getName()); - - private String componentName; - private String password; - Stream router; - Socket socket; - XMPPComponent xmpp; - - ConnectionRouter(XMPPComponent s2s, String componentName, String password) { - this.xmpp = s2s; - this.componentName = componentName; - this.password = password; - reconnect(); - } - public void reconnect() { - try { - socket = new Socket("localhost", 5347); - router = new StreamComponent(new JID("s2s"), socket.getInputStream(), socket.getOutputStream(), password); - router.addChildParser(new JuickMessage()); - router.addListener((Stream.StreamListener) this); - router.addListener((Message.MessageListener) this); - router.addListener((Iq.IqListener) this); - router.startParsing(); - logger.info("STREAM ROUTER START"); - } catch (IOException e) { - logger.log(Level.SEVERE, "router failed", e); - } - } - public void closeConnection() throws IOException { - router.logoff(); - socket.close(); - } - - public void sendJuickMessage(JuickMessage jmsg) { - List jids = new ArrayList<>(); - - if (jmsg.FriendsOnly) { - jids = SubscriptionsQueries.getJIDSubscribedToUser(xmpp.sql, jmsg.getUser().getUID(), jmsg.FriendsOnly); - } else { - List users = SubscriptionsQueries.getSubscribedUsers(xmpp.sql, jmsg.getUser().getUID(), jmsg.getMID()); - for (User user : users) { - for (String jid : UserQueries.getJIDsbyUID(xmpp.sql, user.getUID())) { - jids.add(jid); - } - } - } - - String txt = "@" + jmsg.getUser().getUName() + ":" + jmsg.getTagsString() + "\n"; - String attachment = jmsg.getAttachmentURL(); - if (attachment != null) { - txt += attachment + "\n"; - } - txt += jmsg.getText() + "\n\n"; - txt += "#" + jmsg.getMID() + " http://juick.com/" + jmsg.getMID(); - - Nickname nick = new Nickname(); - nick.Nickname = "@" + jmsg.getUser().getUName(); - - com.juick.xmpp.Message msg = new com.juick.xmpp.Message(); - msg.from = JuickBot.JuickJID; - msg.body = txt; - msg.type = Message.Type.chat; - msg.thread = "juick-" + jmsg.getMID(); - msg.addChild(jmsg); - msg.addChild(nick); - if (attachment != null) { - XOOB oob = new XOOB(); - oob.URL = attachment; - msg.addChild(oob); - } - - for (String jid : jids) { - msg.to = new JID(jid); - xmpp.sendOut(msg); - } - } - - public void sendJuickComment(JuickMessage jmsg) { - List users; - String replyQuote; - String replyTo; - - users = SubscriptionsQueries.getUsersSubscribedToComments(xmpp.sql, jmsg.getMID(), jmsg.getUser().getUID()); - com.juick.Message replyMessage = jmsg.ReplyTo > 0 ? MessagesQueries.getReply(xmpp.sql, jmsg.getMID(), jmsg.ReplyTo) - : MessagesQueries.getMessage(xmpp.sql, jmsg.getMID()); - replyTo = replyMessage.getUser().getUName(); - replyQuote = getReplyQuote(replyMessage); - - String txt = "Reply by @" + jmsg.getUser().getUName() + ":\n" + replyQuote + "\n@" + replyTo + " "; - String attachment = jmsg.getAttachmentURL(); - if (attachment != null) { - txt += attachment + "\n"; - } - txt += jmsg.getText() + "\n\n" + "#" + jmsg.getMID() + "/" + jmsg.getRID() + " http://juick.com/" + jmsg.getMID() + "#" + jmsg.getRID(); - - com.juick.xmpp.Message msg = new com.juick.xmpp.Message(); - msg.from = JuickBot.JuickJID; - msg.body = txt; - msg.type = Message.Type.chat; - msg.addChild(jmsg); - for (User user : users) { - for (String jid : UserQueries.getJIDsbyUID(xmpp.sql, user.getUID())) { - msg.to = new JID(jid); - xmpp.sendOut(msg); - } - } - } - - private String getReplyQuote(com.juick.Message q) { - String quote = q.getText(); - if (quote.length() > 50) { - quote = ">" + quote.substring(0, 47).replace('\n', ' ') + "...\n"; - } else if (quote.length() > 0) { - quote = ">" + quote.replace('\n', ' ') + "\n"; - } - return quote; - } - - public void sendJuickRecommendation(JuickMessage recomm) { - List users; - JuickMessage jmsg; - jmsg = new JuickMessage(MessagesQueries.getMessage(xmpp.sql, recomm.getMID())); - users = SubscriptionsQueries.getUsersSubscribedToUserRecommendations(xmpp.sql, - recomm.getUser().getUID(), recomm.getMID(), jmsg.getUser().getUID()); - - String txt = "Recommended by @" + recomm.getUser().getUName() + ":\n"; - txt += "@" + jmsg.getUser().getUName() + ":" + jmsg.getTagsString() + "\n"; - String attachment = jmsg.getAttachmentURL(); - if (attachment != null) { - txt += attachment + "\n"; - } - txt += jmsg.getText() + "\n\n"; - txt += "#" + jmsg.getMID(); - if (jmsg.Replies > 0) { - if (jmsg.Replies % 10 == 1 && jmsg.Replies % 100 != 11) { - txt += " (" + jmsg.Replies + " reply)"; - } else { - txt += " (" + jmsg.Replies + " replies)"; - } - } - txt += " http://juick.com/" + jmsg.getMID(); - - Nickname nick = new Nickname(); - nick.Nickname = "@" + jmsg.getUser().getUName(); - - com.juick.xmpp.Message msg = new com.juick.xmpp.Message(); - msg.from = JuickBot.JuickJID; - msg.body = txt; - msg.type = Message.Type.chat; - msg.thread = "juick-" + jmsg.getMID(); - msg.addChild(jmsg); - msg.addChild(nick); - if (attachment != null) { - XOOB oob = new XOOB(); - oob.URL = attachment; - msg.addChild(oob); - } - - for (User user : users) { - for (String jid : UserQueries.getJIDsbyUID(xmpp.sql, user.getUID())) { - msg.to = new JID(jid); - xmpp.sendOut(msg); - } - } - } - - @Override - public boolean onIq(Iq iq) { - JID jid = iq.to; - if (!jid.Host.equals(componentName)) { - logger.info("STREAM ROUTER (IQ): " + iq.toString()); - xmpp.sendOut(iq); - } - return false; - } - - @Override - public void onMessage(Message xmsg) { - logger.info("STREAM ROUTER (PROCESS): " + xmsg.toString()); - JuickMessage jmsg = (JuickMessage) xmsg.getChild(JuickMessage.XMLNS); - JID jid = xmsg.to; - if (jid.Host.equals(componentName)) { - if (jmsg != null) { - if (jid.Username != null && jid.Username.equals("recomm")) { - sendJuickRecommendation(jmsg); - } else { - if (jmsg.getRID() > 0) { - sendJuickComment(jmsg); - } else if (jmsg.getMID() > 0) { - sendJuickMessage(jmsg); - } - } - } - } else { - xmpp.sendOut(xmsg); - } - } - - @Override - public void onPresence(Presence presence) { - JID jid = presence.to; - if (!jid.Host.equals(componentName)) { - logger.info("STREAM ROUTER (PRESENCE): " + presence.toString()); - xmpp.sendOut(presence); - } - } - - @Override - public void onStreamReady() { - logger.info("STREAM ROUTER (READY)"); - } - - @Override - public void onStreamFail(Exception ex) { - logger.log(Level.SEVERE, "STREAM ROUTER (FAIL)", ex); - reconnect(); - } -} diff --git a/src/main/java/com/juick/xmpp/s2s/JuickBot.java b/src/main/java/com/juick/xmpp/s2s/JuickBot.java index 18e7580d..f0b71689 100644 --- a/src/main/java/com/juick/xmpp/s2s/JuickBot.java +++ b/src/main/java/com/juick/xmpp/s2s/JuickBot.java @@ -20,11 +20,12 @@ import java.util.regex.Pattern; */ public class JuickBot { XMPPComponent xmpp; - public JuickBot(XMPPComponent xmpp) { + public JuickBot(XMPPComponent xmpp, JID JuickJID) { this.xmpp = xmpp; + this.JuickJID = JuickJID; } - public static final JID JuickJID = new JID("juick", "juick.com", "Juick"); + public final JID JuickJID; private static final String HELPTEXT = "@username text - Send private message\n" + "*tagname Blah-blah-blah - Post a message with tag 'tagname'\n" @@ -187,10 +188,10 @@ public class JuickBot { jmsg.setUser(user_from); jmsg.setText(msg.body); m.childs.add(jmsg); - xmpp.connRouter.router.send(m.toString()); + xmpp.router.send(m.toString()); m.to.Host = "ws.juick.com"; - xmpp.connRouter.router.send(m.toString()); + xmpp.router.send(m.toString()); List jids; boolean inroster = false; @@ -315,10 +316,10 @@ public class JuickBot { jmsg.setUser(user_from); jmsg.setText(body); msg.childs.add(jmsg); - xmpp.connRouter.router.send(msg.toString()); + xmpp.router.send(msg.toString()); msg.to.Host = "ws.juick.com"; - xmpp.connRouter.router.send(msg.toString()); + xmpp.router.send(msg.toString()); for (String jid : jids_to) { Message mm = new Message(); diff --git a/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java b/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java index ecfe1671..9b8bb07e 100644 --- a/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java +++ b/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java @@ -1,8 +1,13 @@ package com.juick.xmpp.s2s; -import com.juick.xmpp.Stanza; -import com.juick.xmpp.StanzaChild; +import com.juick.User; +import com.juick.server.MessagesQueries; +import com.juick.server.SubscriptionsQueries; +import com.juick.server.UserQueries; +import com.juick.xmpp.*; import com.juick.xmpp.extensions.JuickMessage; +import com.juick.xmpp.extensions.Nickname; +import com.juick.xmpp.extensions.XOOB; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.xmlpull.v1.XmlPullParserException; @@ -17,9 +22,6 @@ import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.UnrecoverableKeyException; import java.security.cert.CertificateException; -import java.sql.Driver; -import java.sql.DriverManager; -import java.sql.SQLException; import java.util.*; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -30,19 +32,20 @@ import java.util.logging.Logger; * * @author ugnich */ -public class XMPPComponent implements ServletContextListener { +public class XMPPComponent implements ServletContextListener, Stream.StreamListener, + Message.MessageListener, Iq.IqListener, Presence.PresenceListener { - private static final Logger LOGGER = Logger.getLogger(XMPPComponent.class.getName()); + private static final Logger logger = Logger.getLogger(XMPPComponent.class.getName()); public final ExecutorService executorService = Executors.newCachedThreadPool(); - ExecutorService xmppService = Executors.newSingleThreadExecutor(); + StreamComponent router; + JuickBot bot; - public String HOSTNAME = null; + public String HOSTNAME, componentName; public String STATSFILE = null; public String keystore; public String keystorePassword; public List brokenSSLhosts; - public ConnectionRouter connRouter; final List inConnections = Collections.synchronizedList(new ArrayList<>()); final List outConnections = Collections.synchronizedList(new ArrayList<>()); final List outCache = Collections.synchronizedList(new ArrayList<>()); @@ -135,7 +138,7 @@ public class XMPPComponent implements ServletContextListener { try { connOut.sendStanza(xml); } catch (IOException e) { - LOGGER.warning("STREAM TO " + connOut.to + " " + connOut.streamID + " ERROR: " + e.toString()); + logger.warning("STREAM TO " + connOut.to + " " + connOut.streamID + " ERROR: " + e.toString()); } return; } @@ -161,7 +164,7 @@ public class XMPPComponent implements ServletContextListener { connectionOut = new ConnectionOut(this, hostname); executorService.submit(connectionOut); } catch (CertificateException | UnrecoverableKeyException | NoSuchAlgorithmException | XmlPullParserException | KeyStoreException | KeyManagementException | IOException e) { - LOGGER.log(Level.SEVERE, "s2s out error", e); + logger.log(Level.SEVERE, "s2s out error", e); } } } @@ -169,12 +172,13 @@ public class XMPPComponent implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { - LOGGER.info("component initialized"); + logger.info("component initialized"); Properties conf = new Properties(); try { conf.load(sce.getServletContext().getResourceAsStream("/WEB-INF/juick.conf")); HOSTNAME = conf.getProperty("hostname"); - String componentName = conf.getProperty("componentname"); + componentName = conf.getProperty("componentname"); + JID Jid = new JID(conf.getProperty("xmppbot_jid")); STATSFILE = conf.getProperty("statsfile"); keystore = conf.getProperty("keystore"); keystorePassword = conf.getProperty("keystore_password"); @@ -183,27 +187,34 @@ public class XMPPComponent implements ServletContextListener { dataSource.setDriverClassName(conf.getProperty("datasource_driver", "com.mysql.jdbc.Driver")); dataSource.setUrl(conf.getProperty("datasource_url")); sql = new JdbcTemplate(dataSource); + bot = new JuickBot(this, Jid); childParsers.put(JuickMessage.XMLNS, new JuickMessage()); - xmppService.submit(() -> { - connRouter = new ConnectionRouter(this, componentName, conf.getProperty("xmpp_password")); + Socket routerSocket = new Socket("localhost", 5347); + router = new StreamComponent(new JID("s2s"), routerSocket.getInputStream(), routerSocket.getOutputStream(), conf.getProperty("xmpp_password")); + router.addChildParser(new JuickMessage()); + router.addListener((Stream.StreamListener) this); + router.addListener((Message.MessageListener) this); + router.addListener((Iq.IqListener) this); + executorService.submit(() -> { + router.startParsing(); executorService.submit(new CleaningUp(this)); final ServerSocket listener = new ServerSocket(5269); - LOGGER.info("s2s listener ready"); + logger.info("s2s listener ready"); while (true) { try { Socket socket = listener.accept(); - ConnectionIn client = new ConnectionIn(this, new JuickBot(this), socket); + ConnectionIn client = new ConnectionIn(this, bot, socket); addConnectionIn(client); executorService.submit(client); } catch (Exception e) { - LOGGER.log(Level.SEVERE, "s2s error", e); + logger.log(Level.SEVERE, "s2s error", e); } } }); } catch (Exception e) { - LOGGER.log(Level.SEVERE, "XMPPComponent error", e); + logger.log(Level.SEVERE, "XMPPComponent error", e); } } @@ -228,11 +239,198 @@ public class XMPPComponent implements ServletContextListener { } try { - connRouter.closeConnection(); + closeRouterConnection(); } catch (IOException e) { - LOGGER.log(Level.WARNING, "router warning", e); + logger.log(Level.WARNING, "router warning", e); } executorService.shutdown(); - LOGGER.info("component destroyed"); + logger.info("component destroyed"); + } + public void closeRouterConnection() throws IOException { + router.logoff(); + } + + public void sendJuickMessage(JuickMessage jmsg) { + List jids = new ArrayList<>(); + + if (jmsg.FriendsOnly) { + jids = SubscriptionsQueries.getJIDSubscribedToUser(sql, jmsg.getUser().getUID(), jmsg.FriendsOnly); + } else { + List users = SubscriptionsQueries.getSubscribedUsers(sql, jmsg.getUser().getUID(), jmsg.getMID()); + for (User user : users) { + for (String jid : UserQueries.getJIDsbyUID(sql, user.getUID())) { + jids.add(jid); + } + } + } + + String txt = "@" + jmsg.getUser().getUName() + ":" + jmsg.getTagsString() + "\n"; + String attachment = jmsg.getAttachmentURL(); + if (attachment != null) { + txt += attachment + "\n"; + } + txt += jmsg.getText() + "\n\n"; + txt += "#" + jmsg.getMID() + " http://juick.com/" + jmsg.getMID(); + + Nickname nick = new Nickname(); + nick.Nickname = "@" + jmsg.getUser().getUName(); + + com.juick.xmpp.Message msg = new com.juick.xmpp.Message(); + msg.from = bot.JuickJID; + msg.body = txt; + msg.type = Message.Type.chat; + msg.thread = "juick-" + jmsg.getMID(); + msg.addChild(jmsg); + msg.addChild(nick); + if (attachment != null) { + XOOB oob = new XOOB(); + oob.URL = attachment; + msg.addChild(oob); + } + + for (String jid : jids) { + msg.to = new JID(jid); + sendOut(msg); + } + } + + public void sendJuickComment(JuickMessage jmsg) { + List users; + String replyQuote; + String replyTo; + + users = SubscriptionsQueries.getUsersSubscribedToComments(sql, jmsg.getMID(), jmsg.getUser().getUID()); + com.juick.Message replyMessage = jmsg.ReplyTo > 0 ? MessagesQueries.getReply(sql, jmsg.getMID(), jmsg.ReplyTo) + : MessagesQueries.getMessage(sql, jmsg.getMID()); + replyTo = replyMessage.getUser().getUName(); + replyQuote = getReplyQuote(replyMessage); + + String txt = "Reply by @" + jmsg.getUser().getUName() + ":\n" + replyQuote + "\n@" + replyTo + " "; + String attachment = jmsg.getAttachmentURL(); + if (attachment != null) { + txt += attachment + "\n"; + } + txt += jmsg.getText() + "\n\n" + "#" + jmsg.getMID() + "/" + jmsg.getRID() + " http://juick.com/" + jmsg.getMID() + "#" + jmsg.getRID(); + + com.juick.xmpp.Message msg = new com.juick.xmpp.Message(); + msg.from = bot.JuickJID; + msg.body = txt; + msg.type = Message.Type.chat; + msg.addChild(jmsg); + for (User user : users) { + for (String jid : UserQueries.getJIDsbyUID(sql, user.getUID())) { + msg.to = new JID(jid); + sendOut(msg); + } + } + } + + private String getReplyQuote(com.juick.Message q) { + String quote = q.getText(); + if (quote.length() > 50) { + quote = ">" + quote.substring(0, 47).replace('\n', ' ') + "...\n"; + } else if (quote.length() > 0) { + quote = ">" + quote.replace('\n', ' ') + "\n"; + } + return quote; + } + + public void sendJuickRecommendation(JuickMessage recomm) { + List users; + JuickMessage jmsg; + jmsg = new JuickMessage(MessagesQueries.getMessage(sql, recomm.getMID())); + users = SubscriptionsQueries.getUsersSubscribedToUserRecommendations(sql, + recomm.getUser().getUID(), recomm.getMID(), jmsg.getUser().getUID()); + + String txt = "Recommended by @" + recomm.getUser().getUName() + ":\n"; + txt += "@" + jmsg.getUser().getUName() + ":" + jmsg.getTagsString() + "\n"; + String attachment = jmsg.getAttachmentURL(); + if (attachment != null) { + txt += attachment + "\n"; + } + txt += jmsg.getText() + "\n\n"; + txt += "#" + jmsg.getMID(); + if (jmsg.Replies > 0) { + if (jmsg.Replies % 10 == 1 && jmsg.Replies % 100 != 11) { + txt += " (" + jmsg.Replies + " reply)"; + } else { + txt += " (" + jmsg.Replies + " replies)"; + } + } + txt += " http://juick.com/" + jmsg.getMID(); + + Nickname nick = new Nickname(); + nick.Nickname = "@" + jmsg.getUser().getUName(); + + com.juick.xmpp.Message msg = new com.juick.xmpp.Message(); + msg.from = bot.JuickJID; + msg.body = txt; + msg.type = Message.Type.chat; + msg.thread = "juick-" + jmsg.getMID(); + msg.addChild(jmsg); + msg.addChild(nick); + if (attachment != null) { + XOOB oob = new XOOB(); + oob.URL = attachment; + msg.addChild(oob); + } + + for (User user : users) { + for (String jid : UserQueries.getJIDsbyUID(sql, user.getUID())) { + msg.to = new JID(jid); + sendOut(msg); + } + } + } + + @Override + public boolean onIq(Iq iq) { + JID jid = iq.to; + if (!jid.Host.equals(componentName)) { + logger.info("STREAM ROUTER (IQ): " + iq.toString()); + sendOut(iq); + } + return false; + } + + @Override + public void onMessage(Message xmsg) { + logger.info("STREAM ROUTER (PROCESS): " + xmsg.toString()); + JuickMessage jmsg = (JuickMessage) xmsg.getChild(JuickMessage.XMLNS); + JID jid = xmsg.to; + if (jid.Host.equals(componentName)) { + if (jmsg != null) { + if (jid.Username != null && jid.Username.equals("recomm")) { + sendJuickRecommendation(jmsg); + } else { + if (jmsg.getRID() > 0) { + sendJuickComment(jmsg); + } else if (jmsg.getMID() > 0) { + sendJuickMessage(jmsg); + } + } + } + } else { + sendOut(xmsg); + } + } + + @Override + public void onPresence(Presence presence) { + JID jid = presence.to; + if (!jid.Host.equals(componentName)) { + logger.info("STREAM ROUTER (PRESENCE): " + presence.toString()); + sendOut(presence); + } + } + + @Override + public void onStreamReady() { + logger.info("STREAM ROUTER (READY)"); + } + + @Override + public void onStreamFail(Exception ex) { + logger.log(Level.SEVERE, "STREAM ROUTER (FAIL)", ex); } } -- cgit v1.2.3 From 5005833a6f4437c7ad0d3d36bbd8b2da99a904ee Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 6 Jul 2016 20:13:23 +0300 Subject: fix initialization --- src/main/java/com/juick/xmpp/s2s/XMPPComponent.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java b/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java index 9b8bb07e..21efaeab 100644 --- a/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java +++ b/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java @@ -195,7 +195,8 @@ public class XMPPComponent implements ServletContextListener, Stream.StreamListe router.addChildParser(new JuickMessage()); router.addListener((Stream.StreamListener) this); router.addListener((Message.MessageListener) this); - router.addListener((Iq.IqListener) this); + router.addListener((Iq.IqListener) this); + executorService.submit(() -> router.startParsing()); executorService.submit(() -> { router.startParsing(); executorService.submit(new CleaningUp(this)); -- cgit v1.2.3 From a33e6c3926b41a29a5828677cbad265f7c503d57 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 6 Jul 2016 20:18:49 +0300 Subject: refactor --- .../java/com/juick/xmpp/s2s/XMPPComponent.java | 28 +++++++++++++--------- 1 file changed, 17 insertions(+), 11 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java b/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java index 21efaeab..d0b231e2 100644 --- a/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java +++ b/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java @@ -159,9 +159,8 @@ public class XMPPComponent implements ServletContextListener, Stream.StreamListe } if (!haveAnyConn) { - ConnectionOut connectionOut = null; try { - connectionOut = new ConnectionOut(this, hostname); + ConnectionOut connectionOut = new ConnectionOut(this, hostname); executorService.submit(connectionOut); } catch (CertificateException | UnrecoverableKeyException | NoSuchAlgorithmException | XmlPullParserException | KeyStoreException | KeyManagementException | IOException e) { logger.log(Level.SEVERE, "s2s out error", e); @@ -190,16 +189,22 @@ public class XMPPComponent implements ServletContextListener, Stream.StreamListe bot = new JuickBot(this, Jid); childParsers.put(JuickMessage.XMLNS, new JuickMessage()); - Socket routerSocket = new Socket("localhost", 5347); - router = new StreamComponent(new JID("s2s"), routerSocket.getInputStream(), routerSocket.getOutputStream(), conf.getProperty("xmpp_password")); - router.addChildParser(new JuickMessage()); - router.addListener((Stream.StreamListener) this); - router.addListener((Message.MessageListener) this); - router.addListener((Iq.IqListener) this); - executorService.submit(() -> router.startParsing()); + + executorService.submit(() -> { + Socket routerSocket = null; + try { + routerSocket = new Socket("localhost", 5347); + router = new StreamComponent(new JID("s2s"), routerSocket.getInputStream(), routerSocket.getOutputStream(), conf.getProperty("xmpp_password")); + router.addChildParser(new JuickMessage()); + router.addListener((Stream.StreamListener) this); + router.addListener((Message.MessageListener) this); + router.addListener((Iq.IqListener) this); + router.startParsing(); + } catch (IOException e) { + logger.log(Level.SEVERE, "router error", e); + } + }); executorService.submit(() -> { - router.startParsing(); - executorService.submit(new CleaningUp(this)); final ServerSocket listener = new ServerSocket(5269); logger.info("s2s listener ready"); while (true) { @@ -213,6 +218,7 @@ public class XMPPComponent implements ServletContextListener, Stream.StreamListe } } }); + executorService.submit(new CleaningUp(this)); } catch (Exception e) { logger.log(Level.SEVERE, "XMPPComponent error", e); -- cgit v1.2.3 From c4f9f29274ab9700272ba1064d05ccf7eefe91e2 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 7 Jul 2016 10:22:34 +0300 Subject: filter top and photos by bl_users --- deps/com.juick.server | 2 +- src/main/java/com/juick/http/www/Home.java | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 'src/main') diff --git a/deps/com.juick.server b/deps/com.juick.server index 67d53fe2..a0d038f5 160000 --- a/deps/com.juick.server +++ b/deps/com.juick.server @@ -1 +1 @@ -Subproject commit 67d53fe24081ade138b5d9756fd835575eb4ae79 +Subproject commit a0d038f5e6aa28ef67162c591abe64d057a19fa5 diff --git a/src/main/java/com/juick/http/www/Home.java b/src/main/java/com/juick/http/www/Home.java index 89d4005f..d5c1bb7b 100644 --- a/src/main/java/com/juick/http/www/Home.java +++ b/src/main/java/com/juick/http/www/Home.java @@ -62,10 +62,12 @@ public class Home { } else if (paramShow == null) { if (visitor != null) { title = "Популярные"; + mids = MessagesQueries.getPopular(sql, visitor.getUID(), paramBefore); } else { title = "Микроблоги Juick: популярные записи"; + mids = MessagesQueries.getPopular(sql, 0, paramBefore); } - mids = MessagesQueries.getPopular(sql, paramBefore); + } else if (paramShow.equals("top")) { Utils.sendPermanentRedirect(response, "/"); return; -- cgit v1.2.3 From 058c77d435985cf177efd094816d01cf887d46f2 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 7 Jul 2016 10:26:58 +0300 Subject: fix pm sorting --- src/main/java/com/juick/http/www/PM.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/http/www/PM.java b/src/main/java/com/juick/http/www/PM.java index b351eb0e..932d1baf 100644 --- a/src/main/java/com/juick/http/www/PM.java +++ b/src/main/java/com/juick/http/www/PM.java @@ -63,8 +63,7 @@ public class PM { if (!msgs.isEmpty()) { out.println("
    "); - for (int i = msgs.size() - 1; i >= 0; i--) { - com.juick.Message msg = msgs.get(i); + for (com.juick.Message msg : msgs) { String txt = PageTemplates.formatMessage(msg.getText()); @@ -137,8 +136,7 @@ public class PM { if (!msgs.isEmpty()) { out.println("
      "); - for (int i = msgs.size() - 1; i >= 0; i--) { - com.juick.Message msg = msgs.get(i); + for (com.juick.Message msg : msgs) { String txt = PageTemplates.formatMessage(msg.getText()); -- cgit v1.2.3 From 857a930d31c425185c9b9405f5a7820e6cec9491 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 7 Jul 2016 10:55:54 +0300 Subject: fix ts parsing --- src/main/java/com/juick/xmpp/extensions/JuickMessage.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/xmpp/extensions/JuickMessage.java b/src/main/java/com/juick/xmpp/extensions/JuickMessage.java index 220caba7..885b2375 100644 --- a/src/main/java/com/juick/xmpp/extensions/JuickMessage.java +++ b/src/main/java/com/juick/xmpp/extensions/JuickMessage.java @@ -81,7 +81,10 @@ public class JuickMessage extends com.juick.Message implements StanzaChild { if (sReadOnly != null) { jmsg.ReadOnly = true; } - jmsg.setDate(df.parse(parser.getAttributeValue(null, "ts"))); + String ts = parser.getAttributeValue(null, "timestamp"); + if (ts != null) { + jmsg.setDate(df.parse(ts)); + } jmsg.AttachmentType = parser.getAttributeValue(null, "attach"); while (parser.next() == XmlPullParser.START_TAG) { -- cgit v1.2.3
hostlivesize