aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/PushComponent.java
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2016-07-07 11:25:23 +0300
committerGravatar Vitaly Takmazov2016-07-07 11:25:23 +0300
commita93c85c5ea1bf6683f604bef2608053957577ff0 (patch)
treef80998a4cf339a2b85cd5f095f4386de9d46597b /src/main/java/com/juick/PushComponent.java
parent3e48ba7c9cae9027b0a045aa25ae40b533518865 (diff)
parent857a930d31c425185c9b9405f5a7820e6cec9491 (diff)
merge rebase_all
Diffstat (limited to 'src/main/java/com/juick/PushComponent.java')
-rw-r--r--src/main/java/com/juick/PushComponent.java119
1 files changed, 44 insertions, 75 deletions
diff --git a/src/main/java/com/juick/PushComponent.java b/src/main/java/com/juick/PushComponent.java
index 386448f4..3bb7cdc5 100644
--- a/src/main/java/com/juick/PushComponent.java
+++ b/src/main/java/com/juick/PushComponent.java
@@ -22,8 +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.xmpp.JID;
import com.juick.xmpp.Message.MessageListener;
import com.juick.xmpp.Stream;
@@ -45,21 +45,23 @@ 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;
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;
import java.util.logging.LogManager;
import java.util.logging.Logger;
+import java.util.stream.Collectors;
/**
*
@@ -72,7 +74,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 +93,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) {
@@ -103,39 +104,15 @@ 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<Driver> 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");
}
- 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) {
@@ -161,38 +138,32 @@ public class PushComponent implements ServletContextListener, Stream.StreamListe
@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 +187,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 +232,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")