From 2cfabeab2e64a1b8b6db32a0135a2fce06bc87ab Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 15 Nov 2016 15:56:06 +0300 Subject: all components using babbler now, only juick-xmpp uses ugnich xmpp --- .../java/com/juick/components/Notifications.java | 269 +++++++++------------ .../NotificationsAppConfiguration.java | 9 +- 2 files changed, 122 insertions(+), 156 deletions(-) (limited to 'juick-notifications/src/main/java') 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 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>() { - }).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>() { - }).getBody()); - } + /*** ANDROID ***/ + final List 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>() { + }).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>() { + }).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 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 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 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>() { - }).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>() { - }).getBody()); - } + /*** WinPhone ***/ + final List 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>() { + }).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>() { + }).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 = "" - + "" - + "" - + "" - + "" - + "" + text1 + "" - + "" + text2 + "" - + "" - + "" - + "" - + "" - + "" - + ""; - 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 = "" + + "" + + "" + + "" + + "" + + "" + text1 + "" + + "" + text2 + "" + + "" + + "" + + "" + + "" + + "" + + ""; + 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 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>() { - }).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>() { - }).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 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>() { + }).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>() { + }).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()); } } -- cgit v1.2.3