diff options
Diffstat (limited to 'src/main/java/com/juick/PushComponent.java')
-rw-r--r-- | src/main/java/com/juick/PushComponent.java | 92 |
1 files changed, 42 insertions, 50 deletions
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<User> 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<String> regids; + final List<String> 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<Integer> 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<String> urls; + final List<String> 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<Integer> 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<String> tokens; + final List<String> 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<Integer> 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") |