diff options
Diffstat (limited to 'juick-crosspost/src/main/java/com/juick/components/Crosspost.java')
-rw-r--r-- | juick-crosspost/src/main/java/com/juick/components/Crosspost.java | 86 |
1 files changed, 50 insertions, 36 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 35623c40..905816fd 100644 --- a/juick-crosspost/src/main/java/com/juick/components/Crosspost.java +++ b/juick-crosspost/src/main/java/com/juick/components/Crosspost.java @@ -18,6 +18,7 @@ package com.juick.components; import com.juick.server.CrosspostQueries; +import com.juick.util.ThreadHelper; import com.juick.xmpp.JID; import com.juick.xmpp.Message; import com.juick.xmpp.Stream; @@ -29,8 +30,10 @@ 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.jdbc.core.JdbcTemplate; +import org.springframework.util.Assert; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; @@ -46,36 +49,40 @@ import java.util.concurrent.ExecutorService; /** * @author Ugnich Anton */ -public class Crosspost implements DisposableBean, Stream.StreamListener, Message.MessageListener { - public final static String TWITTERURL = "https://api.twitter.com/1.1/statuses/update.json"; - public final static String FBURL = "https://graph.facebook.com/me/feed"; - public final static String VKURL = "https://api.vk.com/method/wall.post"; +public class Crosspost implements InitializingBean, DisposableBean, Stream.StreamListener, Message.MessageListener { + 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"; private static Logger logger = LoggerFactory.getLogger(Crosspost.class); - JdbcTemplate jdbc; - Stream xmpp; - String twitter_consumer_key; - String twitter_consumer_secret; - ExecutorService service; + private final JdbcTemplate jdbc; + private final ExecutorService service; + private Stream xmpp; + + private final String twitter_consumer_key; + private final String twitter_consumer_secret; + private final String crosspostJid; + private final String password; + private final int port; + + public Crosspost(final Environment env, final ExecutorService service, final JdbcTemplate jdbc) { + Assert.notNull(env); + Assert.notNull(service); + Assert.notNull(jdbc); - public Crosspost(Environment env, ExecutorService service, JdbcTemplate jdbc) { this.service = service; this.jdbc = jdbc; - logger.info("component initialized"); - try { - twitter_consumer_key = env.getProperty("twitter_consumer_key", ""); - twitter_consumer_secret = env.getProperty("twitter_consumer_secret", ""); - setupXmppComponent(env.getProperty("crosspost_jid", "crosspost.juick.local"), - env.getProperty("xmpp_password", ""), NumberUtils.toInt(env.getProperty("xmpp_port", ""), 5347)); - service.submit(() -> xmpp.startParsing()); - } catch (Exception e) { - logger.error("crosspost exception", e); - } + twitter_consumer_key = env.getProperty("twitter_consumer_key", ""); + twitter_consumer_secret = env.getProperty("twitter_consumer_secret", ""); + + crosspostJid = env.getProperty("crosspost_jid", "crosspost.juick.local"); + password = env.getProperty("xmpp_password", ""); + port = NumberUtils.toInt(env.getProperty("xmpp_port"), 5347); } - public static String percentEncode(String s) { + public static String percentEncode(final String s) { String ret = ""; try { ret = URLEncoder.encode(s, "UTF-8").replace("+", "%20").replace("*", "%2A").replace("%7E", "~"); @@ -85,23 +92,30 @@ public class Crosspost implements DisposableBean, Stream.StreamListener, Message } @Override - public void destroy() { - logger.info("component destroyed"); - } - - public void setupXmppComponent(String jid, String password, int port) { + public void afterPropertiesSet() throws Exception { try { Socket socket = new Socket("localhost", port); - xmpp = new StreamComponent(new JID(jid), socket.getInputStream(), socket.getOutputStream(), password); + 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); - } catch (IOException e) { - logger.error(e.getMessage(), e); + + 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"); } @@ -112,7 +126,7 @@ public class Crosspost implements DisposableBean, Stream.StreamListener, Message } @Override - public void onMessage(com.juick.xmpp.Message msg) { + 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")) { @@ -125,7 +139,7 @@ public class Crosspost implements DisposableBean, Stream.StreamListener, Message } } - public boolean facebookPost(com.juick.Message jmsg) { + public boolean facebookPost(final com.juick.Message jmsg) { String token = CrosspostQueries.getFacebookToken(jdbc, jmsg.getUser().getUID()).orElse(""); if (token.isEmpty()) { return false; @@ -167,7 +181,7 @@ public class Crosspost implements DisposableBean, Stream.StreamListener, Message return ret; } - public boolean vkontaktePost(com.juick.Message jmsg) { + public boolean vkontaktePost(final com.juick.Message jmsg) { Pair<String, String> tokens = CrosspostQueries.getVKTokens(jdbc, jmsg.getUser().getUID()).orElse(Pair.of("", "")); if (tokens.getLeft().isEmpty() || tokens.getRight().isEmpty()) { return false; @@ -204,7 +218,7 @@ public class Crosspost implements DisposableBean, Stream.StreamListener, Message return ret; } - public boolean twitterPost(com.juick.Message jmsg) { + public boolean twitterPost(final com.juick.Message jmsg) { Pair<String, String> tokens = CrosspostQueries.getTwitterTokens(jdbc, jmsg.getUser().getUID()).orElse(Pair.of("", "")); if (tokens.getLeft().isEmpty() || tokens.getRight().isEmpty()) { return false; @@ -259,7 +273,7 @@ public class Crosspost implements DisposableBean, Stream.StreamListener, Message return ret; } - public String twitterSignature(String status, String nonce, String timestamp, String token, String token_secret) { + public String twitterSignature(final String status, final String nonce, final String timestamp, final String token, final String token_secret) { try { // ALPHABET-SORTED String params = "oauth_consumer_key=" + twitter_consumer_key @@ -285,7 +299,7 @@ public class Crosspost implements DisposableBean, Stream.StreamListener, Message return null; } - public String streamToString(InputStream is) { + public String streamToString(final InputStream is) { try { BufferedReader buf = new BufferedReader(new InputStreamReader(is)); StringBuilder str = new StringBuilder(); @@ -301,7 +315,7 @@ public class Crosspost implements DisposableBean, Stream.StreamListener, Message return null; } - public String getMessageHashTags(com.juick.Message jmsg) { + public String getMessageHashTags(final com.juick.Message jmsg) { String hashtags = ""; for (int i = 0; i < jmsg.Tags.size(); i++) { hashtags += "#" + jmsg.Tags.get(i) + " "; |