
fluximpl.TwitterActionImpl Maven / Gradle / Ivy
package fluximpl;
import flux.*;
import flux.logging.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.scribe.builder.ServiceBuilder;
import org.scribe.builder.api.TwitterApi;
import org.scribe.model.*;
import org.scribe.oauth.OAuthService;
import org.scribe.utils.StreamUtils;
import org.scribe.utils.URLUtils;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
/**
* Implementation for TwitterAction.
*
* @author [email protected]
*/
public class TwitterActionImpl extends ActionImpl implements TwitterAction {
private static final String ACTION_VARIABLE = "TWITTER_ACTION_VARIABLE";
private static final String AUTHORIZE_URL = "https://twitter.com/oauth/authorize";
private static final String SEARCH_URL = "http://search.twitter.com/search.json";
public TwitterActionImpl() {
super(new FlowChartImpl(), "Twitter Action");
}
public TwitterActionImpl(FlowChartImpl fc, String name) {
super(fc, name);
}
public Set getHiddenVariableNames() {
// The returned raw type is a Set of String.
@SuppressWarnings("unchecked")
Set set = super.getHiddenVariableNames();
set.add(ACTION_VARIABLE);
return set;
}
public String getConsumerKey() {
return getVariable().consumerKey;
}
@Override
public void setConsumerKey(String apiKey) {
TwitterVariable var = getVariable();
var.consumerKey = apiKey;
putVariable(var);
}
public int getCount() {
return getVariable().count;
}
@Override
public void setCount(int count) {
TwitterVariable var = getVariable();
var.count = count;
putVariable(var);
}
public int getPage() {
return getVariable().page;
}
@Override
public void setPage(int page) {
TwitterVariable var = getVariable();
var.page = page;
putVariable(var);
}
public String getConsumerSecret() {
return getVariable().consumerSecret;
}
public Properties getParameters() {
return getVariable().parameters;
}
@Override
public void setParameters(Properties parameters) {
TwitterVariable var = getVariable();
var.parameters = parameters;
putVariable(var);
}
public StringMapEntry getIndexedParameters(int index) {
Properties bodyProperties = getVariable().parameters;
return BodyPropertiesHelper.getIndexedBodyProperties(index, bodyProperties);
}
public StringMapEntry[] getIndexedParameters() {
Properties queryParams = getVariable().parameters;
return BodyPropertiesHelper.getIndexedBodyProperties(queryParams);
}
public void setIndexedParameters(StringMapEntry[] queryParameters) throws EngineException {
TwitterVariable var = getVariable();
Properties variableQueryParameters = var.parameters;
BodyPropertiesHelper.setIndexedBodyProperties(queryParameters, variableQueryParameters);
putVariable(var);
}
public void setIndexedParameters(int index, StringMapEntry arg) throws EngineException {
TwitterVariable var = getVariable();
Properties queryParams = var.parameters;
BodyPropertiesHelper.setIndexedBodyProperties(index, arg, queryParams);
putVariable(var);
}
@Override
public void setConsumerSecret(String apiSecret) {
TwitterVariable var = getVariable();
var.consumerSecret = apiSecret;
putVariable(var);
}
public boolean getOAuthEnabled() {
return getVariable().oAuthEnabled;
}
@Override
public void setOAuthEnabled(boolean oAuthEnabled) {
TwitterVariable var = getVariable();
var.oAuthEnabled = oAuthEnabled;
putVariable(var);
}
public String getSearchQuery() {
return getVariable().searchQuery;
}
@Override
public void setSearchQuery(String searchQuery) {
TwitterVariable var = getVariable();
var.searchQuery = searchQuery;
putVariable(var);
}
public RestActionType getRequestMethod() {
return RestActionType.valueOf(getVariable().requestMethod);
}
@Override
public void setRequestMethod(RestActionType actionType) {
TwitterVariable var = getVariable();
var.requestMethod = actionType.toString();
putVariable(var);
}
public String getResourceUrl() {
return getVariable().resourceUrl;
}
@Override
public void setResourceUrl(String resourceUrl) {
TwitterVariable var = getVariable();
var.resourceUrl = resourceUrl;
putVariable(var);
}
public String getUsername() {
return getVariable().username;
}
@Override
public void setUsername(String username) {
TwitterVariable var = getVariable();
var.username = username;
putVariable(var);
}
public String getPassword() {
Password password = getVariable().password;
if (password != null) {
return password.getEncryptedPassword();
}
return null;
}
@Override
public void setPassword(String password) {
TwitterVariable var = getVariable();
if (password != null) {
var.password = Password.makePassword(password);
}
putVariable(var);
}
private boolean isSearchRequest() {
return getResourceUrl().equals(SEARCH_URL) && getRequestMethod().equals(RestActionType.GET);
}
@Override
public Object execute(FlowContext flowContext) throws Exception {
TwitterActionResult result;
if (getOAuthEnabled()) {
result = processOAuthRequest(flowContext);
} else {
Map queryParams = new HashMap();
if (isSearchRequest()) {
queryParams.put("q", getSearchQuery());
queryParams.put("rpp", Integer.toString(getCount()));
queryParams.put("page", Integer.toString(getPage()));
result = processRequest(flowContext, queryParams);
} else {
queryParams.clear();
queryParams.put("count", Integer.toString(getCount()));
queryParams.put("page", Integer.toString(getPage()));
result = processRequest(flowContext, queryParams);
}
}
if (!StringUtil.isNullOrEmpty(result.response))
flowContext.sendToAuditTrail("CustomEvent", result.response);
return result;
}
private TwitterActionResult processRequest(FlowContext flowContext, Map queryParams) throws EngineException {
Logger log = flowContext.getLogger();
TwitterActionResult result = new TwitterActionResult();
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) new URL(getResourceUrl()).openConnection();
connection.setRequestMethod(getRequestMethod().name());
String content = URLUtils.formURLEncodeMap(queryParams);
connection.setRequestProperty("Content-Length", String.valueOf(content.getBytes().length));
connection.setDoOutput(true);
connection.getOutputStream().write(content.getBytes());
connection.connect();
int code = connection.getResponseCode();
String body = StreamUtils.getStreamContents(connection.getInputStream());
result.responseCode = code;
result.response = body;
} catch (Exception e) {
log.severe("Failed to process request. " + e.getMessage());
} finally {
if (connection != null)
connection.disconnect();
}
return result;
}
private TwitterActionResult processOAuthRequest(FlowContext flowContext) throws EngineException {
Logger log = flowContext.getLogger();
OAuthService service = new ServiceBuilder()
.provider(TwitterApi.class)
.apiKey(getConsumerKey())
.apiSecret(getConsumerSecret())
.build();
Token requestToken = service.getRequestToken();
String oauthToken = requestToken.getToken();
TwitterActionResult result = new TwitterActionResult();
HttpURLConnection connection = null;
try {
//Authorize Step1: GET
connection = (HttpURLConnection) new URL(AUTHORIZE_URL).openConnection();
connection.setRequestMethod(Verb.GET.name());
Map bodyParams = new HashMap();
bodyParams.put("oauth_token", oauthToken);
String content = URLUtils.formURLEncodeMap(bodyParams);
connection.setRequestProperty("Content-Length", String.valueOf(content.getBytes().length));
connection.setDoOutput(true);
connection.getOutputStream().write(content.getBytes());
// Perform Authorization using oauth_token
connection.connect();
int code = connection.getResponseCode();
log.info("Authorize GET Response code : " + code);
String body = StreamUtils.getStreamContents(connection.getInputStream());
log.finest("Authorize POST Response body : " + body);
String authenticityToken = getAuthenticityTokenFromTwitterResponse(body);
if (StringUtil.isNullOrEmpty(authenticityToken)) {
throw new EngineException("Error in authorizing consumer.");
}
//Authorize Step2: POST
connection = (HttpURLConnection) new URL(AUTHORIZE_URL).openConnection();
connection.setRequestMethod(Verb.POST.name());
bodyParams = new HashMap();
bodyParams.put("authenticity_token", authenticityToken);
bodyParams.put("oauth_token", oauthToken);
String password = getVariable().password.getClearTextPassword();
bodyParams.put("session[password]", password);
bodyParams.put("session[username_or_email]", getUsername());
content = URLUtils.formURLEncodeMap(bodyParams);
connection.setRequestProperty("Content-Length", String.valueOf(content.getBytes().length));
connection.setDoOutput(true);
connection.getOutputStream().write(content.getBytes());
// Perform Authorize using authenticity_token, oauth_token, username, password
connection.connect();
code = connection.getResponseCode();
log.info("Authorize POST Response code : " + code);
body = StreamUtils.getStreamContents(connection.getInputStream());
log.finest("Authorize POST Response body : " + body);
String pin = getPinFromTwitterResponse(body, log);
Verifier verifier = new Verifier(pin);
//Retrieve accessToken using verifier PIN.
Token accessToken = service.getAccessToken(requestToken, verifier);
String endpoint = null;
Map queryParams = null;
if (getRequestMethod().equals(RestActionType.GET)) {
queryParams = new HashMap();
queryParams.put("count", Integer.toString(getCount()));
queryParams.put("page", Integer.toString(getPage()));
endpoint = getResourceUrl()+"?"+URLUtils.formURLEncodeMap(queryParams);
} else if (getRequestMethod().equals(RestActionType.POST)) {
queryParams = new HashMap();
for (Map.Entry
© 2015 - 2025 Weber Informatics LLC | Privacy Policy