diff options
author | Vitaly Takmazov | 2016-10-24 17:11:52 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2016-10-24 17:12:41 +0300 |
commit | d1f27eff90719ed40d0d30d048f00908656b3028 (patch) | |
tree | a69a24b15d52613e662c4dcdaaff5b9453cd359c /juick-notifications/src/main/java/com/juick/notifications | |
parent | f2789e9891751e913c951dcaf1f215788306dfc3 (diff) |
notifications: jdbctemplate -> resttemplate
Diffstat (limited to 'juick-notifications/src/main/java/com/juick/notifications')
2 files changed, 25 insertions, 39 deletions
diff --git a/juick-notifications/src/main/java/com/juick/notifications/CloudNotifications.java b/juick-notifications/src/main/java/com/juick/notifications/CloudNotifications.java index 1ad1802f..e108ec03 100644 --- a/juick-notifications/src/main/java/com/juick/notifications/CloudNotifications.java +++ b/juick-notifications/src/main/java/com/juick/notifications/CloudNotifications.java @@ -19,8 +19,6 @@ package com.juick.notifications; import com.google.android.gcm.server.*; import com.juick.json.MessageSerializer; -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; @@ -44,9 +42,11 @@ import org.apache.http.util.EntityUtils; import org.apache.http.util.TextUtils; import org.json.JSONObject; import org.springframework.beans.factory.DisposableBean; +import org.springframework.core.ParameterizedTypeReference; import org.springframework.core.env.Environment; -import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.http.HttpMethod; import org.springframework.stereotype.Component; +import org.springframework.web.client.RestTemplate; import javax.inject.Inject; import java.io.IOException; @@ -57,7 +57,6 @@ import java.util.List; import java.util.concurrent.ExecutorService; import java.util.logging.Level; import java.util.logging.Logger; -import java.util.stream.Collectors; /** * @@ -71,7 +70,7 @@ public class CloudNotifications implements DisposableBean, Stream.StreamListener String wns_application_sip; String wns_client_secret; @Inject - JdbcTemplate jdbc; + RestTemplate rest; Socket socket; Stream xmpp; Sender GCMSender; @@ -118,31 +117,20 @@ public class CloudNotifications implements DisposableBean, Stream.StreamListener @Override public void onMessage(com.juick.xmpp.Message msg) { JuickMessage jmsg = (JuickMessage)msg.getChild(JuickMessage.XMLNS); - List<com.juick.User> subscribedUsers = new ArrayList<>(); boolean isPM = jmsg.getMID() == 0; boolean isReply = jmsg.getRID() > 0; int pmTo = 0; - if (isPM) { - pmTo = Integer.parseInt(msg.to.Username); - } else { - if (isReply) { - subscribedUsers = - SubscriptionsQueries.getUsersSubscribedToComments(jdbc, jmsg.getMID(), jmsg.getUser().getUID()); - } else { - // new message - subscribedUsers = SubscriptionsQueries.getSubscribedUsers(jdbc, jmsg.getUser().getUID(), jmsg.getMID()); - } - } /*** ANDROID ***/ final List<String> regids = new ArrayList<>(); if (isPM) { - regids.addAll(PushQueries.getAndroidRegID(jdbc, pmTo)); + regids.addAll(rest.exchange(String.format("http://api.juick.com/notifications?type=gcm&uid=%s", + jmsg.getUser().getUID()), + HttpMethod.GET, null, new ParameterizedTypeReference<List<String>>() {}).getBody()); } else { - List<Integer> uids = subscribedUsers.stream().map(com.juick.User::getUID).collect(Collectors.toList()); - if (uids.size() > 0) { - regids.addAll(PushQueries.getAndroidTokens(jdbc, uids)); - } + regids.addAll(rest.exchange(String.format("http://api.juick.com/notifications?type=gcm&uid=%s&mid=%s", + jmsg.getUser().getUID(), jmsg.getMID()), + HttpMethod.GET, null, new ParameterizedTypeReference<List<String>>() {}).getBody()); } if (!regids.isEmpty()) { @@ -168,12 +156,13 @@ public class CloudNotifications implements DisposableBean, Stream.StreamListener /*** WinPhone ***/ final List<String> urls = new ArrayList<>(); if (isPM) { - urls.addAll(PushQueries.getWinPhoneURL(jdbc, pmTo)); + urls.addAll(rest.exchange(String.format("http://api.juick.com/notifications?type=mpns&uid=%s", + jmsg.getUser().getUID()), + HttpMethod.GET, null, new ParameterizedTypeReference<List<String>>() {}).getBody()); } else { - List<Integer> uids = subscribedUsers.stream().map(com.juick.User::getUID).collect(Collectors.toList()); - if (uids.size() > 0) { - urls.addAll(PushQueries.getWindowsTokens(jdbc, uids)); - } + urls.addAll(rest.exchange(String.format("http://api.juick.com/notifications?type=mpns&uid=%s&mid=%s", + jmsg.getUser().getUID(), jmsg.getMID()), + HttpMethod.GET, null, new ParameterizedTypeReference<List<String>>() {}).getBody()); } @@ -213,12 +202,13 @@ public class CloudNotifications implements DisposableBean, Stream.StreamListener /*** iOS ***/ final List<String> tokens = new ArrayList<>(); if (isPM) { - tokens.addAll(PushQueries.getAPNSToken(jdbc, pmTo)); + tokens.addAll(rest.exchange(String.format("http://api.juick.com/notifications?type=apns&uid=%s", + jmsg.getUser().getUID()), + HttpMethod.GET, null, new ParameterizedTypeReference<List<String>>() {}).getBody()); } else { - List<Integer> uids = subscribedUsers.stream().map(com.juick.User::getUID).collect(Collectors.toList()); - if (uids.size() > 0) { - tokens.addAll(PushQueries.getAPNSTokens(jdbc, uids)); - } + tokens.addAll(rest.exchange(String.format("http://api.juick.com/notifications?type=apns&uid=%s&mid=%s", + jmsg.getUser().getUID(), jmsg.getMID()), + HttpMethod.GET, null, new ParameterizedTypeReference<List<String>>() {}).getBody()); } if (!tokens.isEmpty()) { ApnsService service = APNS.newService().withCert("/etc/juick/ios.p12", "juick") diff --git a/juick-notifications/src/main/java/com/juick/notifications/configuration/NotificationsConfiguration.java b/juick-notifications/src/main/java/com/juick/notifications/configuration/NotificationsConfiguration.java index e1267de0..fd122724 100644 --- a/juick-notifications/src/main/java/com/juick/notifications/configuration/NotificationsConfiguration.java +++ b/juick-notifications/src/main/java/com/juick/notifications/configuration/NotificationsConfiguration.java @@ -8,7 +8,6 @@ import com.mitchellbosecke.pebble.loader.Loader; import com.mitchellbosecke.pebble.loader.ServletLoader; import com.mitchellbosecke.pebble.spring4.PebbleViewResolver; import com.mitchellbosecke.pebble.spring4.extension.SpringExtension; -import org.apache.commons.dbcp2.BasicDataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @@ -17,7 +16,7 @@ import org.springframework.core.env.Environment; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; -import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.web.client.RestTemplate; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; @@ -42,11 +41,8 @@ public class NotificationsConfiguration extends WebMvcConfigurationSupport { ExecutorService service; @Bean - JdbcTemplate jdbc() { - BasicDataSource dataSource = new BasicDataSource(); - dataSource.setDriverClassName(env.getProperty("datasource_driver", "com.mysql.jdbc.Driver")); - dataSource.setUrl(env.getProperty("datasource_url")); - return new JdbcTemplate(dataSource); + RestTemplate rest() { + return new RestTemplate(); } @Inject private ServletContext servletContext; |