summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Wu <peter@lekensteyn.nl>2014-05-01 15:14:34 +0200
committerPeter Wu <peter@lekensteyn.nl>2014-05-01 15:14:34 +0200
commit69fec2b8561010b6d352a30a25914ab12fc27af0 (patch)
tree05adbd8ce3d01d8a2f8b2890502f2695df3b1d00
parenteda2701672851c54c6b6f2fe0b81d9785bbc1690 (diff)
downloadTwitterDataAnalytics-69fec2b8561010b6d352a30a25914ab12fc27af0.tar.gz
Fix signing for OAuth POST messages, fix search param
-rw-r--r--src/io/HttpURLConnectionPostRequestAdapter.java34
-rw-r--r--src/io/OAuthRequester.java8
-rw-r--r--src/io/StreamImpl.java6
3 files changed, 42 insertions, 6 deletions
diff --git a/src/io/HttpURLConnectionPostRequestAdapter.java b/src/io/HttpURLConnectionPostRequestAdapter.java
new file mode 100644
index 0000000..51cbc7c
--- /dev/null
+++ b/src/io/HttpURLConnectionPostRequestAdapter.java
@@ -0,0 +1,34 @@
+package io;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import oauth.signpost.basic.HttpURLConnectionRequestAdapter;
+import org.apache.commons.io.Charsets;
+
+/**
+ * Adapts a HttpUrlConnection instance with the POST method for use with OAuth
+ * signing.
+ *
+ * @author Peter Wu
+ */
+public class HttpURLConnectionPostRequestAdapter extends
+ HttpURLConnectionRequestAdapter {
+
+ private final String postData;
+
+ public HttpURLConnectionPostRequestAdapter(HttpURLConnection connection,
+ String postData) {
+ super(connection);
+ this.postData = postData;
+ }
+
+ @Override
+ public InputStream getMessagePayload() throws IOException {
+ // the super function returned null because it was unable to get the
+ // post data...
+ byte[] buf = postData.getBytes(Charsets.UTF_8);
+ return new ByteArrayInputStream(buf);
+ }
+}
diff --git a/src/io/OAuthRequester.java b/src/io/OAuthRequester.java
index 77c41c0..c3538ee 100644
--- a/src/io/OAuthRequester.java
+++ b/src/io/OAuthRequester.java
@@ -101,14 +101,16 @@ public class OAuthRequester extends AbstractRequester {
* Signs a HTTP request.
*
* @param conn An open HTTP connection with data ready to be sent.
+ * @param postData The same that got written to the output stream.
* @throws OAuthMessageSignerException
* @throws OAuthExpectationFailedException
* @throws OAuthCommunicationException
*/
- public void sign(HttpURLConnection conn) throws OAuthMessageSignerException,
- OAuthExpectationFailedException, OAuthCommunicationException {
+ public void sign(HttpURLConnection conn, String postData) throws
+ OAuthMessageSignerException, OAuthExpectationFailedException,
+ OAuthCommunicationException {
HttpURLConnectionRequestAdapter request;
- request = new HttpURLConnectionRequestAdapter(conn);
+ request = new HttpURLConnectionPostRequestAdapter(conn, postData);
consumer.sign(request);
}
diff --git a/src/io/StreamImpl.java b/src/io/StreamImpl.java
index 9a9b249..c378ea3 100644
--- a/src/io/StreamImpl.java
+++ b/src/io/StreamImpl.java
@@ -175,7 +175,7 @@ public class StreamImpl implements Stream {
}
private HttpURLConnection connect(String keywords) throws IOException {
- String postData = "follow=" + URLEncoder.encode(keywords, "UTF-8");
+ String postData = "track=" + URLEncoder.encode(keywords, "UTF-8");
HttpURLConnection conn;
conn = (HttpURLConnection) new URL(STREAM_URL).openConnection();
conn.setRequestMethod("POST");
@@ -185,12 +185,12 @@ public class StreamImpl implements Stream {
conn.addRequestProperty("Accept-Encoding", "gzip");
// connect and send request
conn.setDoOutput(true);
- conn.getOutputStream().write(postData.getBytes(Charsets.UTF_8));
try {
- oauth.sign(conn);
+ oauth.sign(conn, postData);
} catch (OAuthException ex) {
throw new IOException("Unable to sign request", ex);
}
+ conn.getOutputStream().write(postData.getBytes(Charsets.UTF_8));
int respCode = conn.getResponseCode();
if (respCode != 200) {
getLogger().severe("Response code " + respCode);