aboutsummaryrefslogtreecommitdiff
path: root/juick-notifications/src/main/java/com
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2016-11-15 15:56:06 +0300
committerGravatar Vitaly Takmazov2016-11-15 15:56:06 +0300
commit2cfabeab2e64a1b8b6db32a0135a2fce06bc87ab (patch)
tree65790951a6b43e436f8bfc742cc3a81cbeeb7b2c /juick-notifications/src/main/java/com
parent1c65b6d29c0fc69d861ffa0e624f73981b62cca6 (diff)
all components using babbler now, only juick-xmpp uses ugnich xmpp
Diffstat (limited to 'juick-notifications/src/main/java/com')
-rw-r--r--juick-notifications/src/main/java/com/juick/components/Notifications.java269
-rw-r--r--juick-notifications/src/main/java/com/juick/components/configuration/NotificationsAppConfiguration.java9
2 files changed, 122 insertions, 156 deletions
diff --git a/juick-notifications/src/main/java/com/juick/components/Notifications.java b/juick-notifications/src/main/java/com/juick/components/Notifications.java
index d37f7a25..e449abef 100644
--- a/juick-notifications/src/main/java/com/juick/components/Notifications.java
+++ b/juick-notifications/src/main/java/com/juick/components/Notifications.java
@@ -18,16 +18,12 @@
package com.juick.components;
import com.google.android.gcm.server.*;
+import com.google.android.gcm.server.Message;
import com.juick.json.MessageSerializer;
import com.juick.util.ThreadHelper;
-import com.juick.xmpp.JID;
-import com.juick.xmpp.Message.MessageListener;
-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.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.http.Consts;
import org.apache.http.Header;
@@ -50,24 +46,24 @@ import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpMethod;
import org.springframework.web.client.RestTemplate;
+import rocks.xmpp.core.XmppException;
+import rocks.xmpp.core.session.Extension;
+import rocks.xmpp.core.session.XmppSessionConfiguration;
+import rocks.xmpp.extensions.component.accept.ExternalComponent;
import java.io.IOException;
-import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
-import java.util.concurrent.ExecutorService;
/**
* @author Ugnich Anton
*/
-public class Notifications implements InitializingBean, DisposableBean, Stream.StreamListener, MessageListener {
+public class Notifications implements InitializingBean {
private static Logger logger = LoggerFactory.getLogger(Notifications.class);
private final RestTemplate rest;
- private final ExecutorService service;
- private Socket socket;
- private Stream xmpp;
+ private ExternalComponent xmpp;
private final Sender GCMSender;
private final String wns_application_sip;
@@ -78,8 +74,7 @@ public class Notifications implements InitializingBean, DisposableBean, Stream.S
private final String xmppPushPassword;
- public Notifications(final Environment env, final ExecutorService service, final RestTemplate rest) {
- this.service = service;
+ public Notifications(final Environment env, final RestTemplate rest) {
this.rest = rest;
wns_application_sip = env.getProperty("wns_application_sip", "");
@@ -92,150 +87,128 @@ public class Notifications implements InitializingBean, DisposableBean, Stream.S
}
@Override
- public void afterPropertiesSet() throws Exception {
- try {
- socket = new Socket(xmppHost, xmppPort);
- xmpp = new StreamComponent(new JID("", pushJid, ""), socket.getInputStream(), socket.getOutputStream(), xmppPushPassword);
- xmpp.addChildParser(new JuickMessage());
- xmpp.addListener((Stream.StreamListener) this);
- xmpp.addListener((MessageListener) this);
-
- service.submit(() -> xmpp.startParsing());
-
- logger.info("Notifications initialized");
- } catch (Exception e) {
- logger.error("Notifications initialization error", e);
- }
- }
-
- @Override
- public void destroy() {
- ThreadHelper.shutdownAndAwaitTermination(service);
-
- logger.info("component destroyed");
- }
-
- @Override
- public void onStreamReady() {
- logger.info("XMPP STREAM READY");
- }
-
- @Override
- public void onStreamFail(final Exception e) {
- logger.error("XMPP STREAM FAIL", e);
- }
-
- @Override
- public void onMessage(final com.juick.xmpp.Message msg) {
- JuickMessage jmsg = (JuickMessage) msg.getChild(JuickMessage.XMLNS);
- boolean isPM = jmsg.getMid() == 0;
- boolean isReply = jmsg.getRid() > 0;
- int pmTo = 0;
+ public void afterPropertiesSet() {
+ XmppSessionConfiguration configuration = XmppSessionConfiguration.builder()
+ .extensions(Extension.of(com.juick.Message.class))
+ .build();
+ xmpp = ExternalComponent.create(pushJid, xmppPushPassword, configuration, xmppHost, xmppPort);
+ xmpp.addInboundMessageListener(e -> {
+ rocks.xmpp.core.stanza.model.Message msg = e.getMessage();
+ com.juick.Message jmsg = msg.getExtension(com.juick.Message.class);
+ boolean isPM = jmsg.getMid() == 0;
+ boolean isReply = jmsg.getRid() > 0;
+ int pmTo = 0;
- /*** ANDROID ***/
- final List<String> regids = new ArrayList<>();
- if (isPM) {
- 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 {
- 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());
- }
+ /*** ANDROID ***/
+ final List<String> regids = new ArrayList<>();
+ if (isPM) {
+ 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 {
+ 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()) {
- MessageSerializer messageSerializer = new MessageSerializer();
- String json = messageSerializer.serialize(jmsg).toString();
- logger.info(json);
- Message message = new Message.Builder().addData("message", json).build();
- try {
- MulticastResult result = GCMSender.send(message, regids, 3);
- List<Result> results = result.getResults();
- for (int i = 0; i < results.size(); i++) {
- logger.info("RES " + i + ": " + results.get(i).toString());
+ if (!regids.isEmpty()) {
+ MessageSerializer messageSerializer = new MessageSerializer();
+ String json = messageSerializer.serialize(jmsg).toString();
+ logger.info(json);
+ Message message = new Message.Builder().addData("message", json).build();
+ try {
+ MulticastResult result = GCMSender.send(message, regids, 3);
+ List<Result> results = result.getResults();
+ for (int i = 0; i < results.size(); i++) {
+ logger.info("RES " + i + ": " + results.get(i).toString());
+ }
+ } catch (IOException ex) {
+ logger.error(ex.getMessage(), ex);
+ } catch (IllegalArgumentException err) {
+ logger.warn("Android: Invalid API Key");
}
- } catch (IOException e) {
- logger.error(e.getMessage(), e);
- } catch (IllegalArgumentException err) {
- logger.warn("Android: Invalid API Key");
+ } else {
+ logger.info("GMS: no recipients");
}
- } else {
- logger.info("GMS: no recipients");
- }
- /*** WinPhone ***/
- final List<String> urls = new ArrayList<>();
- if (isPM) {
- 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 {
- 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());
- }
+ /*** WinPhone ***/
+ final List<String> urls = new ArrayList<>();
+ if (isPM) {
+ 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 {
+ 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());
+ }
- if (urls.isEmpty()) {
- logger.info("WNS: no recipients");
- } else {
- try {
- String wnsToken = getWnsAccessToken();
- String text1 = "@" + jmsg.getUser().getName();
- if (!jmsg.getTags().isEmpty()) {
- text1 += ":" + XmlUtils.escape(jmsg.getTagsString());
- }
- String text2 = XmlUtils.escape(jmsg.getText());
- String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
- + "<toast>"
- + "<visual>"
- + "<binding template=\"ToastImageAndText02\">"
- + "<image id=\"1\" src=\"http://i.juick.com/as/" + jmsg.getUser().getUid() + ".png\" />"
- + "<text id=\"1\">" + text1 + "</text>"
- + "<text id=\"2\">" + text2 + "</text>"
- + "</binding>"
- + "</visual>"
- + "<commands>"
- + "<command arguments=\"?mid=" + jmsg.getMid() + "\" />"
- + "</commands>"
- + "</toast>";
- logger.trace(xml);
- for (String url : urls) {
- logger.info("WNS: " + url);
- sendWNS(wnsToken, url, xml);
+ if (urls.isEmpty()) {
+ logger.info("WNS: no recipients");
+ } else {
+ try {
+ String wnsToken = getWnsAccessToken();
+ String text1 = "@" + jmsg.getUser().getName();
+ if (!jmsg.getTags().isEmpty()) {
+ text1 += ":" + StringEscapeUtils.escapeXml11(jmsg.getTagsString());
+ }
+ String text2 = StringEscapeUtils.escapeXml11(jmsg.getText());
+ String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ + "<toast>"
+ + "<visual>"
+ + "<binding template=\"ToastImageAndText02\">"
+ + "<image id=\"1\" src=\"http://i.juick.com/as/" + jmsg.getUser().getUid() + ".png\" />"
+ + "<text id=\"1\">" + text1 + "</text>"
+ + "<text id=\"2\">" + text2 + "</text>"
+ + "</binding>"
+ + "</visual>"
+ + "<commands>"
+ + "<command arguments=\"?mid=" + jmsg.getMid() + "\" />"
+ + "</commands>"
+ + "</toast>";
+ logger.trace(xml);
+ for (String url : urls) {
+ logger.info("WNS: " + url);
+ sendWNS(wnsToken, url, xml);
+ }
+ } catch (IOException | IllegalStateException ex) {
+ logger.error("WNS: ", ex);
}
- } catch (IOException | IllegalStateException e) {
- logger.error("WNS: ", e);
}
- }
- /*** iOS ***/
- final List<String> tokens = new ArrayList<>();
- if (isPM) {
- 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 {
- 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")
- .withSandboxDestination().build();
- for (String token : tokens) {
- String payload = APNS.newPayload().alertTitle("@" + jmsg.getUser().getName()).alertBody(jmsg.getText()).build();
- logger.info("APNS: " + token);
- service.push(token, payload);
+ /*** iOS ***/
+ final List<String> tokens = new ArrayList<>();
+ if (isPM) {
+ 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 {
+ 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());
}
- } else {
- logger.info("APNS: no recipients");
+ if (!tokens.isEmpty()) {
+ ApnsService service = APNS.newService().withCert("/etc/juick/ios.p12", "juick")
+ .withSandboxDestination().build();
+ for (String token : tokens) {
+ String payload = APNS.newPayload().alertTitle("@" + jmsg.getUser().getName()).alertBody(jmsg.getText()).build();
+ logger.info("APNS: " + token);
+ service.push(token, payload);
+ }
+ } else {
+ logger.info("APNS: no recipients");
+ }
+ });
+ try {
+ xmpp.connect();
+ } catch (XmppException e) {
+ logger.warn("xmpp extension", e);
}
}
diff --git a/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsAppConfiguration.java b/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsAppConfiguration.java
index 1974830a..c17587ac 100644
--- a/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsAppConfiguration.java
+++ b/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsAppConfiguration.java
@@ -10,8 +10,6 @@ import org.springframework.core.env.Environment;
import org.springframework.web.client.RestTemplate;
import javax.inject.Inject;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
/**
* Created by aalexeev on 11/12/16.
@@ -30,11 +28,6 @@ public class NotificationsAppConfiguration {
@Bean
public Notifications push() {
- return new Notifications(env, service(), rest());
- }
-
- @Bean
- public ExecutorService service() {
- return Executors.newCachedThreadPool();
+ return new Notifications(env, rest());
}
}