diff options
Diffstat (limited to 'juick-crosspost/src/main/java/com/juick/components')
-rw-r--r-- | juick-crosspost/src/main/java/com/juick/components/Crosspost.java | 88 | ||||
-rw-r--r-- | juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostAppConfiguration.java | 10 |
2 files changed, 31 insertions, 67 deletions
diff --git a/juick-crosspost/src/main/java/com/juick/components/Crosspost.java b/juick-crosspost/src/main/java/com/juick/components/Crosspost.java index 8ccca67c..f14749eb 100644 --- a/juick-crosspost/src/main/java/com/juick/components/Crosspost.java +++ b/juick-crosspost/src/main/java/com/juick/components/Crosspost.java @@ -18,37 +18,34 @@ package com.juick.components; import com.juick.service.CrosspostService; -import com.juick.util.ThreadHelper; -import com.juick.xmpp.JID; -import com.juick.xmpp.Message; -import com.juick.xmpp.Stream; -import com.juick.xmpp.StreamComponent; -import com.juick.xmpp.extensions.JuickMessage; import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang3.math.NumberUtils; import org.apache.commons.lang3.tuple.Pair; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.core.env.Environment; import org.springframework.util.Assert; +import rocks.xmpp.core.XmppException; +import rocks.xmpp.core.session.Extension; +import rocks.xmpp.core.session.XmppSession; +import rocks.xmpp.core.session.XmppSessionConfiguration; +import rocks.xmpp.core.stanza.model.Message; +import rocks.xmpp.extensions.component.accept.ExternalComponent; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import javax.net.ssl.HttpsURLConnection; import java.io.*; -import java.net.Socket; import java.net.URL; import java.net.URLEncoder; import java.security.Key; import java.util.UUID; -import java.util.concurrent.ExecutorService; /** * @author Ugnich Anton */ -public class Crosspost implements InitializingBean, DisposableBean, Stream.StreamListener, Message.MessageListener { +public class Crosspost implements InitializingBean { final static String TWITTERURL = "https://api.twitter.com/1.1/statuses/update.json"; final static String FBURL = "https://graph.facebook.com/me/feed"; final static String VKURL = "https://api.vk.com/method/wall.post"; @@ -56,8 +53,7 @@ public class Crosspost implements InitializingBean, DisposableBean, Stream.Strea private static Logger logger = LoggerFactory.getLogger(Crosspost.class); private final CrosspostService crosspostService; - private final ExecutorService service; - private Stream xmpp; + private XmppSession xmpp; private final String twitter_consumer_key; private final String twitter_consumer_secret; @@ -65,12 +61,10 @@ public class Crosspost implements InitializingBean, DisposableBean, Stream.Strea private final String password; private final int port; - public Crosspost(final Environment env, final ExecutorService service, final CrosspostService crosspostService) { + public Crosspost(final Environment env, final CrosspostService crosspostService) { Assert.notNull(env); - Assert.notNull(service); Assert.notNull(crosspostService); - this.service = service; this.crosspostService = crosspostService; twitter_consumer_key = env.getProperty("twitter_consumer_key", ""); @@ -91,50 +85,28 @@ public class Crosspost implements InitializingBean, DisposableBean, Stream.Strea } @Override - public void afterPropertiesSet() throws Exception { - try { - Socket socket = new Socket("localhost", port); - xmpp = new StreamComponent(new JID(crosspostJid), socket.getInputStream(), socket.getOutputStream(), password); - xmpp.addChildParser(new JuickMessage()); - xmpp.addListener((Stream.StreamListener) this); - xmpp.addListener((Message.MessageListener) this); - - service.submit(() -> xmpp.startParsing()); - - logger.info("Crosspost initialized"); - } catch (Exception e) { - logger.error("Crosspost initialization exception", e); - } - } - - @Override - public void destroy() throws Exception { - ThreadHelper.shutdownAndAwaitTermination(service); - - logger.info("Crosspost destroyed"); - } - - @Override - public void onStreamReady() { - logger.info("XMPP STREAM READY"); - } - - @Override - public void onStreamFail(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); - if (msg.to != null && msg.to.Username != null && jmsg != null && jmsg.getRid() == 0) { - if (msg.to.Username.equals("twitter")) { - twitterPost(jmsg); - } else if (msg.to.Username.equals("fb")) { - facebookPost(jmsg); - } else if (msg.to.Username.equals("vk")) { - vkontaktePost(jmsg); + public void afterPropertiesSet() { + XmppSessionConfiguration configuration = XmppSessionConfiguration.builder() + .extensions(Extension.of(com.juick.Message.class)) + .build(); + xmpp = ExternalComponent.create(crosspostJid, password, configuration, "localhost", port); + xmpp.addInboundMessageListener(e -> { + Message msg = e.getMessage(); + com.juick.Message jmsg = msg.getExtension(com.juick.Message.class); + if (msg.getTo() != null && msg.getTo().getLocal() != null && jmsg != null && jmsg.getRid() == 0) { + if (msg.getTo().getLocal().equals("twitter")) { + twitterPost(jmsg); + } else if (msg.getTo().getLocal().equals("fb")) { + facebookPost(jmsg); + } else if (msg.getTo().getLocal().equals("vk")) { + vkontaktePost(jmsg); + } } + }); + try { + xmpp.connect(); + } catch (XmppException e) { + logger.warn("xmpp exception", e); } } diff --git a/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostAppConfiguration.java b/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostAppConfiguration.java index 603130b3..91a886b3 100644 --- a/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostAppConfiguration.java +++ b/juick-crosspost/src/main/java/com/juick/components/configuration/CrosspostAppConfiguration.java @@ -8,11 +8,8 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; -import org.springframework.jdbc.core.JdbcTemplate; import javax.inject.Inject; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; /** * Created by aalexeev on 11/12/16. @@ -28,11 +25,6 @@ public class CrosspostAppConfiguration { @Bean public Crosspost crosspost() { - return new Crosspost(env, service(), crosspostService); - } - - @Bean - public ExecutorService service() { - return Executors.newCachedThreadPool(); + return new Crosspost(env, crosspostService); } } |