aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/CrosspostComponent.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/juick/CrosspostComponent.java')
-rw-r--r--src/main/java/com/juick/CrosspostComponent.java41
1 files changed, 19 insertions, 22 deletions
diff --git a/src/main/java/com/juick/CrosspostComponent.java b/src/main/java/com/juick/CrosspostComponent.java
index d5f13ab2..d7ee8724 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,7 +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;
@@ -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", ""));
+ 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);
@@ -115,15 +116,11 @@ public class CrosspostComponent implements ServletContextListener, Stream.Strea
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 +158,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 +195,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 +206,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 +232,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());