forked from scribejava/scribejava
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for HttpClient based connections
- Loading branch information
Mohan Kishore
committed
Apr 19, 2012
1 parent
1208fd8
commit 9e74946
Showing
10 changed files
with
251 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.scribe.model; | ||
|
||
import org.apache.http.Header; | ||
import org.apache.http.HeaderIterator; | ||
import org.apache.http.HttpResponse; | ||
import org.scribe.exceptions.OAuthException; | ||
|
||
/** | ||
* Provides an implementation of Scribe Response class that wraps an | ||
* Apache HttpResponse object (instead of the default URLConnection based | ||
* implementation) | ||
* | ||
* @author mkishor | ||
*/ | ||
public class HttpClientResponse extends Response { | ||
|
||
public HttpClientResponse(HttpResponse response) { | ||
this.code = response.getStatusLine().getStatusCode(); | ||
for (HeaderIterator iter = response.headerIterator(); iter.hasNext(); ) { | ||
Header header = iter.nextHeader(); | ||
headers.put(header.getName(), header.getValue()); | ||
} | ||
try { | ||
this.stream = response.getEntity().getContent(); | ||
} catch (Exception e) { | ||
throw new OAuthException("Error reading the response", e); | ||
} | ||
} | ||
|
||
// all the other methods are valid as-is - work off of the variables initialized | ||
// in the constructor... | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.scribe.model; | ||
|
||
import org.apache.http.HttpEntityEnclosingRequest; | ||
import org.apache.http.HttpResponse; | ||
import org.apache.http.client.HttpClient; | ||
import org.apache.http.client.methods.HttpDelete; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.client.methods.HttpPost; | ||
import org.apache.http.client.methods.HttpPut; | ||
import org.apache.http.client.methods.HttpUriRequest; | ||
import org.apache.http.entity.ByteArrayEntity; | ||
import org.scribe.exceptions.OAuthException; | ||
|
||
/** | ||
* This class allows us to use the HttpClient library to make the HTTP calls, | ||
* and the scribe library to provide an OAuth abstraction over it. | ||
* | ||
* @author mkishor | ||
*/ | ||
public class HttpClientStrategy implements HttpStrategy { | ||
private HttpClient httpClient; | ||
|
||
public HttpClientStrategy(HttpClient httpClient) { | ||
this.httpClient = httpClient; | ||
} | ||
|
||
public Response send(Request request) { | ||
try { | ||
String completeUrl = request.getCompleteUrl(); | ||
String verb = request.getVerb().name(); | ||
HttpUriRequest httpRequest = newHttpUriRequest(verb, completeUrl); | ||
addHeaders(request, httpRequest); | ||
if ("POST".equals(verb) || "PUT".equals(verb)) { | ||
ByteArrayEntity entity = new ByteArrayEntity(request.getByteBodyContents()); | ||
((HttpEntityEnclosingRequest) httpRequest).setEntity(entity); | ||
} | ||
HttpResponse httpResponse = httpClient.execute(httpRequest); | ||
return new HttpClientResponse(httpResponse); | ||
} catch (Exception e) { | ||
throw new OAuthException("Error making the request", e); | ||
} | ||
} | ||
|
||
private void addHeaders(Request request, HttpUriRequest httpRequest) { | ||
for (String key : request.getHeaders().keySet()) { | ||
httpRequest.addHeader(key, request.getHeaders().get(key)); | ||
} | ||
} | ||
|
||
protected HttpUriRequest newHttpUriRequest(String verb, String uri) { | ||
if ("GET".equals(verb)) { | ||
return new HttpGet(uri); | ||
} else if ("PUT".equals(verb)) { | ||
return new HttpPut(uri); | ||
} else if ("DELETE".equals(verb)) { | ||
return new HttpDelete(uri); | ||
} else { | ||
return new HttpPost(uri); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.scribe.model; | ||
|
||
/** | ||
* Allows the Request class to use a pluggable strategy for making | ||
* the actual HTTP Connection. | ||
* | ||
* @author mkishor | ||
*/ | ||
public interface HttpStrategy { | ||
public Response send(Request request); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.scribe.model; | ||
|
||
import java.io.IOException; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.net.UnknownHostException; | ||
|
||
import org.scribe.exceptions.OAuthException; | ||
|
||
public class URLConnectionStrategy implements HttpStrategy { | ||
protected static final String CONTENT_LENGTH = "Content-Length"; | ||
protected static final String CONTENT_TYPE = "Content-Type"; | ||
|
||
public Response send(Request request) { | ||
try { | ||
HttpURLConnection connection = createConnection(request); | ||
return doSend(request, connection); | ||
} catch (UnknownHostException uhe) { | ||
throw new OAuthException( | ||
"Could not reach the desired host. Check your network connection.", uhe); | ||
} catch (IOException ioe) { | ||
throw new OAuthException("Problems while creating connection.", ioe); | ||
} | ||
} | ||
|
||
protected HttpURLConnection createConnection(Request request) throws IOException { | ||
String completeUrl = request.getCompleteUrl(); | ||
System.setProperty("http.keepAlive", request.isConnectionKeepAlive() ? "true" : "false"); | ||
return (HttpURLConnection) new URL(completeUrl).openConnection(); | ||
} | ||
|
||
protected Response doSend(Request request, HttpURLConnection connection) throws IOException { | ||
connection.setRequestMethod(request.getVerb().name()); | ||
if (request.getConnectTimeout() != null) { | ||
connection.setConnectTimeout(request.getConnectTimeout().intValue()); | ||
} | ||
if (request.getReadTimeout() != null) { | ||
connection.setReadTimeout(request.getReadTimeout().intValue()); | ||
} | ||
addHeaders(request, connection); | ||
if (request.getVerb().equals(Verb.PUT) || request.getVerb().equals(Verb.POST)) { | ||
addBody(connection, request.getByteBodyContents()); | ||
} | ||
return new Response(connection); | ||
} | ||
|
||
protected void addHeaders(Request request, HttpURLConnection conn) { | ||
for (String key : request.getHeaders().keySet()) | ||
conn.setRequestProperty(key, request.getHeaders().get(key)); | ||
} | ||
|
||
protected void addBody(HttpURLConnection conn, byte[] content) throws IOException { | ||
conn.setRequestProperty(CONTENT_LENGTH, String.valueOf(content.length)); | ||
|
||
// Set default content type if none is set. | ||
if (conn.getRequestProperty(CONTENT_TYPE) == null) { | ||
conn.setRequestProperty(CONTENT_TYPE, Request.DEFAULT_CONTENT_TYPE); | ||
} | ||
conn.setDoOutput(true); | ||
conn.getOutputStream().write(content); | ||
} | ||
|
||
} |
Oops, something went wrong.
1 comment
on commit 9e74946
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is cool. I had some patches to use MultipartEntity from the same package to work with HttpURLConnection. I guess I'll wait until this gets merged to publish them.
good to know i wasn't the only one annoyed by this