diff options
Diffstat (limited to 'src/main/java/com/juick/CrosspostComponent.java')
-rw-r--r-- | src/main/java/com/juick/CrosspostComponent.java | 52 |
1 files changed, 11 insertions, 41 deletions
diff --git a/src/main/java/com/juick/CrosspostComponent.java b/src/main/java/com/juick/CrosspostComponent.java index 22de81b6..b188fa16 100644 --- a/src/main/java/com/juick/CrosspostComponent.java +++ b/src/main/java/com/juick/CrosspostComponent.java @@ -24,6 +24,8 @@ 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; +import org.springframework.jdbc.core.JdbcTemplate; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; @@ -61,7 +63,7 @@ public class CrosspostComponent implements ServletContextListener, Stream.Strea 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"; - Connection sql; + JdbcTemplate sql; Stream xmpp; String twitter_consumer_key; String twitter_consumer_secret; @@ -80,8 +82,7 @@ public class CrosspostComponent implements ServletContextListener, Stream.Strea twitter_consumer_key = conf.getProperty("twitter_consumer_key", ""); twitter_consumer_secret = conf.getProperty("twitter_consumer_secret", ""); - setupSql(conf.getProperty("mysql_host"), conf.getProperty("mysql_username"), - conf.getProperty("mysql_password", ""), conf.getProperty("mysql_database", "")); + sql = (JdbcTemplate) sce.getServletContext().getAttribute("sql"); setupXmppComponent(conf.getProperty("xmpp_password", "")); } catch (Exception e) { logger.log(Level.SEVERE, e.getMessage(), e); @@ -91,41 +92,10 @@ public class CrosspostComponent implements ServletContextListener, Stream.Strea @Override public void contextDestroyed(ServletContextEvent sce) { - // Now deregister JDBC drivers in this context's ClassLoader: - // Get the webapp's ClassLoader - ClassLoader cl = Thread.currentThread().getContextClassLoader(); - // Loop through all drivers - Enumeration<Driver> drivers = DriverManager.getDrivers(); - while (drivers.hasMoreElements()) { - Driver driver = drivers.nextElement(); - if (driver.getClass().getClassLoader() == cl) { - // This driver was registered by the webapp's ClassLoader, so deregister it: - try { - logger.info(String.format("Deregistering JDBC driver %s", driver.toString())); - DriverManager.deregisterDriver(driver); - } catch (SQLException ex) { - logger.log(Level.SEVERE, String.format("Error deregistering JDBC driver %s", driver), ex); - } - } else { - // driver was not registered by the webapp's ClassLoader and may be in use elsewhere - logger.log(Level.SEVERE, String.format("Not deregistering JDBC driver %s as it does not belong to this webapp's ClassLoader", driver)); - } - } executorService.shutdown(); logger.info("component destroyed"); } - public void setupSql(String host, String username, String password, String database) { - try { - Class.forName("com.mysql.jdbc.Driver"); - sql = DriverManager.getConnection( - String.format("jdbc:mysql://%s/%s?autoReconnect=true&user=%s&password=%s", - host, database, username, password)); - } catch (SQLException | ClassNotFoundException e) { - logger.log(Level.SEVERE, e.getMessage(), e); - } - } - public void setupXmppComponent(String password) { try { Socket socket = new Socket("localhost", 5347); @@ -198,8 +168,8 @@ public class CrosspostComponent implements ServletContextListener, Stream.Strea } public boolean vkontaktePost(com.juick.Message jmsg) { - String tokens[] = CrosspostQueries.getVKTokens(sql, jmsg.getUser().getUID()); - if (tokens == null || tokens.length != 2) { + Pair<String, String> tokens = CrosspostQueries.getVKTokens(sql, jmsg.getUser().getUID()); + if (tokens == null) { return false; } @@ -209,7 +179,7 @@ public class CrosspostComponent implements ServletContextListener, Stream.Strea boolean ret = false; try { - String body = "owner_id=" + tokens[0] + "&access_token=" + URLEncoder.encode(tokens[1], "UTF-8") + "&from_group=1&message=" + URLEncoder.encode(status, "UTF-8"); + String body = "owner_id=" + tokens.getLeft() + "&access_token=" + URLEncoder.encode(tokens.getRight(), "UTF-8") + "&from_group=1&message=" + URLEncoder.encode(status, "UTF-8"); HttpsURLConnection conn = (HttpsURLConnection) new URL(VKURL).openConnection(); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); @@ -235,12 +205,12 @@ public class CrosspostComponent implements ServletContextListener, Stream.Strea } public boolean twitterPost(com.juick.Message jmsg) { - String tokens[] = CrosspostQueries.getTwitterTokens(sql, jmsg.getUser().getUID()); - if (tokens == null || tokens.length != 2) { + Pair<String, String> tokens = CrosspostQueries.getTwitterTokens(sql, jmsg.getUser().getUID()); + if (tokens == null) { return false; } - String token = percentEncode(tokens[0]); - String token_secret = percentEncode(tokens[1]); + String token = percentEncode(tokens.getLeft()); + String token_secret = percentEncode(tokens.getRight()); logger.info("TWITTER: #" + jmsg.getMID()); |