From 3e4807157e3c244820dd4d5149997970530e4fcb Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 23 Aug 2017 00:47:37 +0300 Subject: www: Twitter Cards --- .../main/java/com/juick/components/Crosspost.java | 36 ++++++---------------- 1 file changed, 9 insertions(+), 27 deletions(-) (limited to 'juick-crosspost/src/main') 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 6ad7f5d3..f481c348 100644 --- a/juick-crosspost/src/main/java/com/juick/components/Crosspost.java +++ b/juick-crosspost/src/main/java/com/juick/components/Crosspost.java @@ -19,9 +19,9 @@ package com.juick.components; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; import com.juick.Message; -import com.juick.Tag; import com.juick.service.CrosspostService; import com.juick.service.MessagesService; +import com.juick.util.MessageUtils; import org.apache.commons.codec.CharEncoding; import org.apache.commons.codec.binary.Base64; import org.apache.commons.io.IOUtils; @@ -40,7 +40,6 @@ import javax.crypto.spec.SecretKeySpec; import javax.inject.Inject; import javax.net.ssl.HttpsURLConnection; import java.io.OutputStreamWriter; -import java.io.UnsupportedEncodingException; import java.net.URL; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; @@ -80,15 +79,6 @@ public class Crosspost extends TextWebSocketHandler { ms.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT); } - public static String percentEncode(final String s) { - String ret = StringUtils.EMPTY; - try { - ret = URLEncoder.encode(s, CharEncoding.UTF_8).replace("+", "%20").replace("*", "%2A").replace("%7E", "~"); - } catch (UnsupportedEncodingException e) { - } - return ret; - } - @Override protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { Message msg = ms.readValue(message.asBytes(), Message.class); @@ -111,7 +101,7 @@ public class Crosspost extends TextWebSocketHandler { logger.info("FB: #{}", jmsg.getMid()); - String status = getMessageHashTags(jmsg) + "\n" + jmsg.getText(); + String status = MessageUtils.getMessageHashTags(jmsg) + "\n" + jmsg.getText(); boolean ret = false; try { @@ -153,7 +143,7 @@ public class Crosspost extends TextWebSocketHandler { logger.info("VK: #", jmsg.getMid()); - String status = getMessageHashTags(jmsg) + "\n" + jmsg.getText() + "\nhttp://juick.com/" + jmsg.getMid(); + String status = MessageUtils.getMessageHashTags(jmsg) + "\n" + jmsg.getText() + "\nhttp://juick.com/" + jmsg.getMid(); boolean ret = false; try { @@ -187,23 +177,23 @@ public class Crosspost extends TextWebSocketHandler { if (tokens.getLeft().isEmpty() || tokens.getRight().isEmpty()) { return false; } - String token = percentEncode(tokens.getLeft()); - String token_secret = percentEncode(tokens.getRight()); + String token = MessageUtils.percentEncode(tokens.getLeft()); + String token_secret = MessageUtils.percentEncode(tokens.getRight()); logger.info("TWITTER: #{}", jmsg.getMid()); - String status = getMessageHashTags(jmsg) + jmsg.getText(); + String status = MessageUtils.getMessageHashTags(jmsg) + jmsg.getText(); if (status.length() > 115) { status = status.substring(0, 114) + "…"; } status += " http://juick.com/" + jmsg.getMid(); - status = percentEncode(status); + status = MessageUtils.percentEncode(status); boolean ret = false; try { String nonce = UUID.randomUUID().toString(); String timestamp = Long.toString(System.currentTimeMillis() / 1000L); - String signature = percentEncode(twitterSignature(status, nonce, timestamp, token, token_secret)); + String signature = MessageUtils.percentEncode(twitterSignature(status, nonce, timestamp, token, token_secret)); String auth = "OAuth " + "oauth_consumer_key=\"" + twitter_consumer_key + "\", " + "oauth_nonce=\"" + nonce + "\", " @@ -248,7 +238,7 @@ public class Crosspost extends TextWebSocketHandler { + "&oauth_version=1.0" + "&status=" + status; - String base = "POST&" + percentEncode(TWITTERURL) + "&" + percentEncode(params); + String base = "POST&" + MessageUtils.percentEncode(TWITTERURL) + "&" + MessageUtils.percentEncode(params); String key = twitter_consumer_secret + "&" + token_secret; Key signingKey = new SecretKeySpec(key.getBytes(), "HmacSHA1"); @@ -262,12 +252,4 @@ public class Crosspost extends TextWebSocketHandler { } return null; } - - public String getMessageHashTags(final com.juick.Message jmsg) { - String hashtags = StringUtils.EMPTY; - for (Tag tag : jmsg.getTags()) { - hashtags += "#" + tag + " "; - } - return hashtags; - } } -- cgit v1.2.3