Permalink
Browse files

Switch from java.net.HttpURLConnection to Apache HttpClient

  • Loading branch information...
1 parent f1d8fb9 commit c004d99b5cb77dbf1435291e5d69bed12bbea9ae @phbernard phbernard committed Jul 24, 2011
View
20 pom.xml
@@ -3,7 +3,7 @@
<groupId>com.flattr4android.rest</groupId>
<artifactId>java-flattr-rest</artifactId>
<packaging>jar</packaging>
- <version>0.0.2.4</version>
+ <version>0.0.3.0</version>
<name>java-flattr-rest</name>
<description>
Java library for the Flattr Rest API.
@@ -56,10 +56,28 @@
<version>1.2.1.1</version>
</dependency>
<dependency>
+ <groupId>oauth.signpost</groupId>
+ <artifactId>signpost-commonshttp4</artifactId>
+ <version>1.2.1.1</version>
+ <scope>compile</scope>
+ </dependency>
+ <dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.1</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.httpcomponents</groupId>
+ <artifactId>httpcore</artifactId>
+ <version>4.0.1</version>
+ <scope>compile</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.httpcomponents</groupId>
+ <artifactId>httpclient</artifactId>
+ <version>4.0.1</version>
+ <scope>compile</scope>
+ </dependency>
</dependencies>
<!-- build settings -->
@@ -15,7 +15,8 @@
package com.flattr4android.rest;
import java.io.IOException;
-import java.net.HttpURLConnection;
+
+import org.apache.http.HttpResponse;
@SuppressWarnings("serial")
public class AuthenticationException extends FlattrServerResponseException {
@@ -24,9 +25,8 @@ public AuthenticationException(int status, String msg) {
super(status, msg);
}
- public AuthenticationException(HttpURLConnection connection)
- throws IOException {
- super(connection);
+ public AuthenticationException(HttpResponse response) throws IOException {
+ super(response);
}
}
@@ -14,10 +14,10 @@
*/
package com.flattr4android.rest;
-import oauth.signpost.basic.DefaultOAuthConsumer;
+import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
@SuppressWarnings("serial")
-public class FlattrOAuthConsumer extends DefaultOAuthConsumer {
+public class FlattrOAuthConsumer extends CommonsHttpOAuthConsumer {
public FlattrOAuthConsumer(String consumerKey, String consumerSecret) {
super(consumerKey, consumerSecret);
@@ -19,12 +19,12 @@
import java.net.MalformedURLException;
import java.net.URL;
-import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.basic.HttpURLConnectionRequestAdapter;
+import oauth.signpost.commonshttp.CommonsHttpOAuthProvider;
import oauth.signpost.http.HttpRequest;
@SuppressWarnings("serial")
-public class FlattrOAuthProvider extends DefaultOAuthProvider {
+public class FlattrOAuthProvider extends CommonsHttpOAuthProvider {
public static final String REQUEST_TOKEN_ENDPOINT_URL = "http://api.flattr.com/oauth/request_token";
public static final String ACCESS_TOKEN_ENDPOINT_URL = "http://api.flattr.com/oauth/access_token";
@@ -16,9 +16,7 @@
import java.io.IOException;
import java.io.InputStream;
-import java.io.PrintWriter;
-import java.net.HttpURLConnection;
-import java.net.URL;
+import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
@@ -27,11 +25,17 @@
import javax.xml.parsers.ParserConfigurationException;
import oauth.signpost.OAuthConsumer;
-import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpRequestBase;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.DefaultHttpClient;
import org.xml.sax.SAXException;
import com.flattr4android.rest.demo.SampleThing;
@@ -49,14 +53,15 @@
private boolean demoMode = false;
private Thing demoSampleThing;
private ThingCache thingCache = new ThingCache();
+ private DefaultHttpClient client;
public FlattrRestClient(OAuthConsumer consumer) {
this.consumer = consumer;
}
public FlattrRestClient(String consumerKey, String consumerSecret,
String accessToken, String tokenSecret) {
- consumer = new DefaultOAuthConsumer(consumerKey, consumerSecret);
+ consumer = new FlattrOAuthConsumer(consumerKey, consumerSecret);
consumer.setTokenWithSecret(accessToken, tokenSecret);
}
@@ -437,55 +442,55 @@ public void register(Thing thing) throws OAuthMessageSignerException,
+ "feed/categories"));
}
- protected HttpURLConnection sendRequest(String uri)
+ protected HttpResponse sendRequest(String uri)
throws OAuthMessageSignerException,
OAuthExpectationFailedException, OAuthCommunicationException,
FlattrServerResponseException, IOException {
return sendRequest(uri, "GET", null);
}
- protected HttpURLConnection sendRequest(String uri, String method,
- String content) throws OAuthMessageSignerException,
+ protected HttpResponse sendRequest(String uri, String method, String content)
+ throws OAuthMessageSignerException,
OAuthExpectationFailedException, OAuthCommunicationException,
IOException, FlattrServerResponseException {
- URL url = new URL("http://api.flattr.com" + uri);
- HttpURLConnection request = (HttpURLConnection) url.openConnection();
- consumer.sign(request);
- request.setRequestMethod(method);
- if (content != null) {
- request.setDoOutput(true);
- PrintWriter pw = new PrintWriter(request.getOutputStream());
- pw.write(content);
- }
- request.connect();
- int reqCode;
- try {
- reqCode = request.getResponseCode();
- } catch (IOException e) {
- // See
- // http://stackoverflow.com/questions/1357372/ioexception-received-authentication-challenge-is-null-apache-harmony-android
- if (e.getMessage().equals(
- "Received authentication challenge is null")) {
- throw new AuthenticationException(request);
- } else {
- throw e;
+ HttpRequestBase req;
+ if (method.equals("GET")) {
+ req = new HttpGet("http://api.flattr.com" + uri);
+ } else {
+ req = new HttpPost("http://api.flattr.com" + uri);
+ if (content != null) {
+ StringEntity body = new StringEntity("data="
+ + URLEncoder.encode(content, "utf-8"));
+ body.setContentType("application/x-www-form-urlencoded");
+ ((HttpPost) req).setEntity(body);
}
}
+ consumer.sign(req);
+ HttpResponse resp = getHttpClient().execute(req);
+ int reqCode;
+ reqCode = resp.getStatusLine().getStatusCode();
if (reqCode != 200) {
if (reqCode == 401) {
- throw new AuthenticationException(request);
+ throw new AuthenticationException(resp);
} else {
- throw new FlattrServerResponseException(request);
+ throw new FlattrServerResponseException(resp);
}
}
- return request;
+ return resp;
}
protected InputStream getResourceInputStream(String uri)
throws IOException, OAuthMessageSignerException,
OAuthExpectationFailedException, OAuthCommunicationException,
FlattrServerResponseException {
- return (InputStream) sendRequest(uri).getContent();
+ return (InputStream) sendRequest(uri).getEntity().getContent();
+ }
+
+ private HttpClient getHttpClient() {
+ if (client == null) {
+ client = new DefaultHttpClient();
+ }
+ return client;
}
}
@@ -15,7 +15,8 @@
package com.flattr4android.rest;
import java.io.IOException;
-import java.net.HttpURLConnection;
+
+import org.apache.http.HttpResponse;
@SuppressWarnings("serial")
public class FlattrServerResponseException extends FlattrRestException {
@@ -27,9 +28,10 @@ public FlattrServerResponseException(int status, String msg) {
super("Server response: " + status + " (" + msg + ")");
}
- public FlattrServerResponseException(HttpURLConnection connection)
+ public FlattrServerResponseException(HttpResponse response)
throws IOException {
- this(connection.getResponseCode(), connection.getResponseMessage());
+ this(response.getStatusLine().getStatusCode(), response.getStatusLine()
+ .getReasonPhrase());
}
public int getStatus() {

0 comments on commit c004d99

Please sign in to comment.