aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/CrosspostComponent.java
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2016-07-07 11:25:23 +0300
committerGravatar Vitaly Takmazov2016-07-07 11:25:23 +0300
commita93c85c5ea1bf6683f604bef2608053957577ff0 (patch)
treef80998a4cf339a2b85cd5f095f4386de9d46597b /src/main/java/com/juick/CrosspostComponent.java
parent3e48ba7c9cae9027b0a045aa25ae40b533518865 (diff)
parent857a930d31c425185c9b9405f5a7820e6cec9491 (diff)
merge rebase_all
Diffstat (limited to 'src/main/java/com/juick/CrosspostComponent.java')
-rw-r--r--src/main/java/com/juick/CrosspostComponent.java65
1 files changed, 19 insertions, 46 deletions
diff --git a/src/main/java/com/juick/CrosspostComponent.java b/src/main/java/com/juick/CrosspostComponent.java
index d5f13ab2..8b07583f 100644
--- a/src/main/java/com/juick/CrosspostComponent.java
+++ b/src/main/java/com/juick/CrosspostComponent.java
@@ -24,6 +24,9 @@ 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.tuple.Pair;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
@@ -35,11 +38,6 @@ 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;
@@ -61,7 +59,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 +78,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", ""));
+ setupSql(conf.getProperty("datasource_driver", "com.mysql.jdbc.Driver"), conf.getProperty("datasource_url", ""));
setupXmppComponent(conf.getProperty("xmpp_password", ""));
} catch (Exception e) {
logger.log(Level.SEVERE, e.getMessage(), e);
@@ -91,39 +88,15 @@ 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 setupSql(String driver, String url) {
+ DriverManagerDataSource dataSource = new DriverManagerDataSource();
+ dataSource.setDriverClassName(driver);
+ dataSource.setUrl(url);
+ sql = new JdbcTemplate(dataSource);
}
public void setupXmppComponent(String password) {
@@ -161,8 +134,8 @@ public class CrosspostComponent implements ServletContextListener, Stream.Strea
}
public boolean facebookPost(com.juick.Message jmsg) {
- String token = CrosspostQueries.getFacebookToken(sql, jmsg.getUser().getUID());
- if (token == null) {
+ String token = CrosspostQueries.getFacebookToken(sql, jmsg.getUser().getUID()).orElse("");
+ if (token.isEmpty()) {
return false;
}
@@ -198,8 +171,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()).orElse(Pair.of("", ""));
+ if (tokens.getLeft().isEmpty() || tokens.getRight().isEmpty()) {
return false;
}
@@ -209,7 +182,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 +208,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()).orElse(Pair.of("", ""));
+ if (tokens.getLeft().isEmpty() || tokens.getRight().isEmpty()) {
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());