diff options
author | Vitaly Takmazov | 2016-02-03 12:35:59 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2016-02-03 12:35:59 +0300 |
commit | 380018da475ff41d3375e7f2bea0a192a4d9b178 (patch) | |
tree | 6e6e38e109c73ff5bc233681143ee4ac2bff9a96 /src/main/java/com/juick/CrosspostComponent.java | |
parent | 36f542dad713d173102a60a1aa7e336e6db31200 (diff) |
single xmpp component, WIP
Diffstat (limited to 'src/main/java/com/juick/CrosspostComponent.java')
-rw-r--r-- | src/main/java/com/juick/CrosspostComponent.java | 73 |
1 files changed, 7 insertions, 66 deletions
diff --git a/src/main/java/com/juick/CrosspostComponent.java b/src/main/java/com/juick/CrosspostComponent.java index b188fa16..6166f899 100644 --- a/src/main/java/com/juick/CrosspostComponent.java +++ b/src/main/java/com/juick/CrosspostComponent.java @@ -18,10 +18,6 @@ package com.juick; import com.juick.server.CrosspostQueries; -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 com.juick.xmpp.utils.Base64; import org.apache.commons.lang3.tuple.Pair; @@ -30,94 +26,39 @@ import org.springframework.jdbc.core.JdbcTemplate; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import javax.net.ssl.HttpsURLConnection; -import javax.servlet.ServletContextEvent; -import javax.servlet.ServletContextListener; import java.io.*; -import java.net.Socket; import java.net.URL; import java.net.URLEncoder; import java.security.Key; -import java.sql.Connection; -import java.sql.Driver; -import java.sql.DriverManager; -import java.sql.SQLException; -import java.util.Enumeration; import java.util.Properties; import java.util.UUID; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import java.util.logging.Level; -import java.util.logging.LogManager; import java.util.logging.Logger; /** * * @author Ugnich Anton */ -public class CrosspostComponent implements ServletContextListener, Stream.StreamListener, Message.MessageListener { +public class CrosspostComponent implements JuickComponent { private static Logger logger = Logger.getLogger(CrosspostComponent.class.getName()); - private ExecutorService executorService; - 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"; + JdbcTemplate sql; - Stream xmpp; String twitter_consumer_key; String twitter_consumer_secret; - @Override - public void contextInitialized(final ServletContextEvent sce) { + public CrosspostComponent(JdbcTemplate sql, Properties conf) { logger.info("component initialized"); - executorService = Executors.newSingleThreadExecutor(); - executorService.submit((Runnable) () -> { - try { - Properties conf = new Properties(); - conf.load(sce.getServletContext().getResourceAsStream("WEB-INF/juick.conf")); - - LogManager.getLogManager().readConfiguration( - sce.getServletContext().getResourceAsStream("WEB-INF/logging.properties")); - twitter_consumer_key = conf.getProperty("twitter_consumer_key", ""); - twitter_consumer_secret = conf.getProperty("twitter_consumer_secret", ""); - - sql = (JdbcTemplate) sce.getServletContext().getAttribute("sql"); - setupXmppComponent(conf.getProperty("xmpp_password", "")); - } catch (Exception e) { - logger.log(Level.SEVERE, e.getMessage(), e); - } - }); - } - - @Override - public void contextDestroyed(ServletContextEvent sce) { - executorService.shutdown(); - logger.info("component destroyed"); - } - - public void setupXmppComponent(String password) { - try { - Socket socket = new Socket("localhost", 5347); - xmpp = new StreamComponent(new JID("", "crosspost.juick.com", ""), socket.getInputStream(), socket.getOutputStream(), password); - xmpp.addChildParser(new JuickMessage()); - xmpp.addListener((Stream.StreamListener) this); - xmpp.addListener((Message.MessageListener) this); - xmpp.startParsing(); - } catch (IOException e) { - logger.log(Level.SEVERE, e.getMessage(), e); - } - } - - @Override - public void onStreamReady() { - logger.info("XMPP STREAM READY"); + twitter_consumer_key = conf.getProperty("twitter_consumer_key", ""); + twitter_consumer_secret = conf.getProperty("twitter_consumer_secret", ""); + this.sql = sql; } - - @Override - public void onStreamFail(Exception e) {logger.log(Level.SEVERE, "XMPP STREAM FAIL", e);} @Override - public void onMessage(com.juick.xmpp.Message msg) { + public void messageReceived(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")) { |