diff options
author | Vitaly Takmazov | 2016-02-02 00:06:21 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2016-02-02 00:06:21 +0300 |
commit | f2a2660e753fa11478fd3edadfdff9c000b22cc9 (patch) | |
tree | 3204b0a1f1f599250b1602b9411060d7de676a0e /src/main/java/com/juick/PushComponent.java | |
parent | 9bd81dc9e69ba58d8fedd4adfb7e80245019eb18 (diff) |
minor cleanup
Diffstat (limited to 'src/main/java/com/juick/PushComponent.java')
-rw-r--r-- | src/main/java/com/juick/PushComponent.java | 61 |
1 files changed, 25 insertions, 36 deletions
diff --git a/src/main/java/com/juick/PushComponent.java b/src/main/java/com/juick/PushComponent.java index aab56778..0c3d70d7 100644 --- a/src/main/java/com/juick/PushComponent.java +++ b/src/main/java/com/juick/PushComponent.java @@ -22,14 +22,14 @@ 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.PushQueries; import com.juick.server.MessagesQueries; +import com.juick.server.PushQueries; import com.juick.xmpp.JID; import com.juick.xmpp.Message.MessageListener; -import com.juick.xmpp.utils.XmlUtils; import com.juick.xmpp.Stream; import com.juick.xmpp.StreamComponent; import com.juick.xmpp.extensions.JuickMessage; +import com.juick.xmpp.utils.XmlUtils; import com.notnoop.apns.APNS; import com.notnoop.apns.ApnsService; import org.apache.http.Consts; @@ -48,10 +48,7 @@ import org.json.JSONObject; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; import java.net.Socket; import java.sql.Connection; import java.sql.Driver; @@ -84,25 +81,22 @@ public class PushComponent implements ServletContextListener, Stream.StreamListe public void contextInitialized(final ServletContextEvent sce) { logger.info("component initialized"); executorService = Executors.newSingleThreadExecutor(); - executorService.submit(new Runnable() { - @Override - public void run() { - Properties conf = new Properties(); - try { - conf.load(sce.getServletContext().getResourceAsStream("WEB-INF/juick.conf")); - LogManager.getLogManager().readConfiguration( - sce.getServletContext().getResourceAsStream("WEB-INF/logging.properties")); - wns_application_sip = conf.getProperty("wns_application_sip", ""); - wns_client_secret = conf.getProperty("wns_client_secret", ""); - GCMSender = new Sender(conf.getProperty("gcm_key")); + executorService.submit((Runnable) () -> { + Properties conf = new Properties(); + try { + conf.load(sce.getServletContext().getResourceAsStream("WEB-INF/juick.conf")); + LogManager.getLogManager().readConfiguration( + sce.getServletContext().getResourceAsStream("WEB-INF/logging.properties")); + wns_application_sip = conf.getProperty("wns_application_sip", ""); + 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", "")); - 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) { - logger.log(Level.SEVERE, e.getMessage(), e); - } + setupSql(conf.getProperty("mysql_host"), conf.getProperty("mysql_username"), + conf.getProperty("mysql_password", ""), conf.getProperty("mysql_database", "")); + 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) { + logger.log(Level.SEVERE, e.getMessage(), e); } }); } @@ -139,9 +133,7 @@ public class PushComponent implements ServletContextListener, Stream.StreamListe sql = DriverManager.getConnection( String.format("jdbc:mysql://%s/%s?autoReconnect=true&user=%s&password=%s", host, database, username, password)); - } catch (SQLException e) { - logger.log(Level.SEVERE, e.getMessage(), e); - } catch (ClassNotFoundException e) { + } catch (SQLException | ClassNotFoundException e) { logger.log(Level.SEVERE, e.getMessage(), e); } } @@ -200,7 +192,7 @@ public class PushComponent implements ServletContextListener, Stream.StreamListe /*** ANDROID ***/ List<String> regids; if (isPM) { - regids = new ArrayList<String>(); + regids = new ArrayList<>(); String targetId = PushQueries.getAndroidRegID(sql, recipientID); if (targetId != null && !targetId.isEmpty()) { regids.add(targetId); @@ -232,9 +224,9 @@ public class PushComponent implements ServletContextListener, Stream.StreamListe /*** WinPhone ***/ List<String> urls; if (isPM) { - urls = new ArrayList<String>(); + urls = new ArrayList<>(); String targetURL = PushQueries.getWinPhoneURL(sql, recipientID); - if (targetURL != null && !targetURL.isEmpty()) { + if (!TextUtils.isEmpty(targetURL)) { urls.add(targetURL); } } else { @@ -266,14 +258,11 @@ public class PushComponent implements ServletContextListener, Stream.StreamListe + "</commands>" + "</toast>"; logger.fine(xml); - for (int i = 0; i < urls.size(); i++) { - String url = urls.get(i); + for (String url : urls) { logger.info("WNS: " + url); sendWNS(wnsToken, url, xml); } - } catch (IOException e) { - logger.log(Level.SEVERE, "WNS: ", e); - } catch (IllegalStateException e) { + } catch (IOException | IllegalStateException e) { logger.log(Level.SEVERE, "WNS: ", e); } } @@ -281,7 +270,7 @@ public class PushComponent implements ServletContextListener, Stream.StreamListe /*** iOS ***/ List<String> tokens; if (isPM) { - tokens = new ArrayList<String>(); + tokens = new ArrayList<>(); String targetToken = PushQueries.getAPNSToken(sql, recipientID); if (targetToken != null && !targetToken.isEmpty()) { tokens.add(targetToken); @@ -311,7 +300,7 @@ public class PushComponent implements ServletContextListener, Stream.StreamListe } HttpClient client = HttpClientBuilder.create().build(); String url = "https://login.live.com/accesstoken.srf"; - List<NameValuePair> formParams = new ArrayList<NameValuePair>(); + List<NameValuePair> formParams = new ArrayList<>(); formParams.add(new BasicNameValuePair("grant_type", "client_credentials")); formParams.add(new BasicNameValuePair("client_id", wns_application_sip)); formParams.add(new BasicNameValuePair("client_secret", wns_client_secret)); |