-
Notifications
You must be signed in to change notification settings - Fork 153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Apache httpclient5 support #33
Open
kjozsa
wants to merge
7
commits into
mttkay:master
Choose a base branch
from
kjozsa:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b2670ea
hcclient5 support
kjozsa 711770f
enforce absolute request uri on wrapped request
kjozsa 961cc48
update README with hcclient5 support
kjozsa ef7003b
fix unit test
kjozsa 7e4ea5a
hcclient5 async support
kjozsa c46609c
update README
kjozsa 4b9f885
remove temp snapshot repo
kjozsa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 3 additions & 5 deletions
8
...-commonshttp3/src/test/java/oauth/signpost/commonshttp3/CommonsHttpOAuthProviderTest.java
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
8 changes: 3 additions & 5 deletions
8
signpost-commonshttp3/src/test/java/oauth/signpost/commonshttp3/HttpRequestAdapterTest.java
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
5 changes: 2 additions & 3 deletions
5
signpost-commonshttp4/src/test/java/oauth/signpost/commonshttp/HttpRequestAdapterTest.java
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 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<parent> | ||
<artifactId>oauth-signpost</artifactId> | ||
<groupId>oauth.signpost</groupId> | ||
<version>2.1.2-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
<artifactId>signpost-commonshttp5</artifactId> | ||
<name>signpost-commonshttp5</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>oauth.signpost</groupId> | ||
<artifactId>signpost-core</artifactId> | ||
<version>${project.version}</version> | ||
<scope>compile</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.httpcomponents.client5</groupId> | ||
<artifactId>httpclient5</artifactId> | ||
<version>5.1.2</version> | ||
<scope>compile</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>oauth.signpost</groupId> | ||
<artifactId>signpost-core</artifactId> | ||
<version>${project.version}</version> | ||
<type>test-jar</type> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
43 changes: 43 additions & 0 deletions
43
...shttp5/src/main/java/oauth/signpost/commonshttp5/async/CommonsHttpAsyncOAuthConsumer.java
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,43 @@ | ||
/* Copyright (c) 2009 Matthias Kaeppler | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package oauth.signpost.commonshttp5.async; | ||
|
||
import oauth.signpost.AbstractOAuthConsumer; | ||
import oauth.signpost.http.HttpRequest; | ||
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest; | ||
|
||
/** | ||
* Supports signing HTTP requests of type {@link SimpleHttpRequest}. | ||
* | ||
* @author Kristof Jozsa | ||
*/ | ||
public class CommonsHttpAsyncOAuthConsumer extends AbstractOAuthConsumer { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
public CommonsHttpAsyncOAuthConsumer(String consumerKey, String consumerSecret) { | ||
super(consumerKey, consumerSecret); | ||
} | ||
|
||
@Override | ||
protected HttpRequest wrap(Object request) { | ||
if (!(request instanceof SimpleHttpRequest)) { | ||
throw new IllegalArgumentException("This consumer expects requests of type " + SimpleHttpRequest.class.getCanonicalName()); | ||
} | ||
|
||
return new HttpAsyncRequestAdapter((SimpleHttpRequest) request); | ||
} | ||
|
||
} |
91 changes: 91 additions & 0 deletions
91
...shttp5/src/main/java/oauth/signpost/commonshttp5/async/CommonsHttpAsyncOAuthProvider.java
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,91 @@ | ||
/* | ||
* Copyright (c) 2009 Matthias Kaeppler Licensed under the Apache License, | ||
* Version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law | ||
* or agreed to in writing, software distributed under the License is | ||
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
package oauth.signpost.commonshttp5.async; | ||
|
||
import oauth.signpost.AbstractOAuthProvider; | ||
import oauth.signpost.http.HttpRequest; | ||
import oauth.signpost.http.HttpResponse; | ||
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest; | ||
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse; | ||
import org.apache.hc.client5.http.async.methods.SimpleRequestProducer; | ||
import org.apache.hc.client5.http.async.methods.SimpleResponseConsumer; | ||
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient; | ||
import org.apache.hc.client5.http.impl.async.HttpAsyncClients; | ||
import org.apache.hc.client5.http.protocol.HttpClientContext; | ||
import org.apache.hc.core5.concurrent.FutureCallback; | ||
import org.apache.hc.core5.http.ClassicHttpResponse; | ||
import org.apache.hc.core5.http.HttpEntity; | ||
import org.apache.hc.core5.http.Method; | ||
import org.apache.hc.core5.http.io.entity.EntityUtils; | ||
import org.apache.hc.core5.http.message.BasicHttpResponse; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.util.concurrent.Future; | ||
|
||
/** | ||
* This implementation uses the Apache Commons {@link org.apache.hc.client5.http.async.HttpAsyncClient} 5.x HTTP | ||
* implementation to fetch OAuth tokens from a service provider. | ||
* | ||
* @author Kristof Jozsa | ||
*/ | ||
public class CommonsHttpAsyncOAuthProvider extends AbstractOAuthProvider { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
private transient CloseableHttpAsyncClient httpClient; | ||
|
||
public CommonsHttpAsyncOAuthProvider(String requestTokenEndpointUrl, String accessTokenEndpointUrl, | ||
String authorizationWebsiteUrl) { | ||
super(requestTokenEndpointUrl, accessTokenEndpointUrl, authorizationWebsiteUrl); | ||
this.httpClient = HttpAsyncClients.createDefault(); | ||
} | ||
|
||
public CommonsHttpAsyncOAuthProvider(String requestTokenEndpointUrl, String accessTokenEndpointUrl, | ||
String authorizationWebsiteUrl, CloseableHttpAsyncClient httpClient) { | ||
super(requestTokenEndpointUrl, accessTokenEndpointUrl, authorizationWebsiteUrl); | ||
this.httpClient = httpClient; | ||
} | ||
|
||
public void setHttpClient(CloseableHttpAsyncClient httpClient) { | ||
this.httpClient = httpClient; | ||
} | ||
|
||
@Override | ||
protected HttpRequest createRequest(String endpointUrl) throws Exception { | ||
return new HttpAsyncRequestAdapter(new SimpleHttpRequest(Method.POST, new URI(endpointUrl))); | ||
} | ||
|
||
@Override | ||
protected HttpResponse sendRequest(HttpRequest request) throws Exception { | ||
SimpleRequestProducer requestProducer = SimpleRequestProducer.create((SimpleHttpRequest) request.unwrap()); | ||
Future<SimpleHttpResponse> response = httpClient.execute(requestProducer, SimpleResponseConsumer.create(), HttpClientContext.create(), new FutureCallback<SimpleHttpResponse>() { | ||
@Override | ||
public void completed(SimpleHttpResponse simpleHttpResponse) { | ||
} | ||
|
||
@Override | ||
public void failed(Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
@Override | ||
public void cancelled() { | ||
} | ||
}); | ||
return new HttpAsyncResponseAdapter(response.get()); | ||
} | ||
|
||
@Override | ||
protected void closeConnection(HttpRequest request, oauth.signpost.http.HttpResponse response) throws Exception { | ||
httpClient.close(); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...commonshttp5/src/main/java/oauth/signpost/commonshttp5/async/HttpAsyncRequestAdapter.java
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,67 @@ | ||||||
package oauth.signpost.commonshttp5.async; | ||||||
|
||||||
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest; | ||||||
import org.apache.hc.core5.http.Header; | ||||||
|
||||||
import java.io.ByteArrayInputStream; | ||||||
import java.io.IOException; | ||||||
import java.io.InputStream; | ||||||
import java.util.Arrays; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
import java.util.HashMap; | ||||||
import java.util.Map; | ||||||
|
||||||
public class HttpAsyncRequestAdapter implements oauth.signpost.http.HttpRequest { | ||||||
|
||||||
private SimpleHttpRequest request; | ||||||
|
||||||
|
||||||
public HttpAsyncRequestAdapter(SimpleHttpRequest request) { | ||||||
this.request = request; | ||||||
this.request.setAbsoluteRequestUri(true); | ||||||
} | ||||||
|
||||||
public String getMethod() { | ||||||
return request.getMethod(); | ||||||
} | ||||||
|
||||||
public String getRequestUrl() { | ||||||
return request.getRequestUri(); | ||||||
} | ||||||
|
||||||
public void setRequestUrl(String url) { | ||||||
throw new RuntimeException(new UnsupportedOperationException()); | ||||||
} | ||||||
|
||||||
public String getHeader(String name) { | ||||||
Header header = request.getFirstHeader(name); | ||||||
if (header == null) { | ||||||
return null; | ||||||
} | ||||||
return header.getValue(); | ||||||
} | ||||||
|
||||||
public void setHeader(String name, String value) { | ||||||
request.setHeader(name, value); | ||||||
} | ||||||
|
||||||
public Map<String, String> getAllHeaders() { | ||||||
Header[] origHeaders = request.getHeaders(); | ||||||
HashMap<String, String> headers = new HashMap<String, String>(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
for (Header h : origHeaders) { | ||||||
headers.put(h.getName(), h.getValue()); | ||||||
} | ||||||
return headers; | ||||||
} | ||||||
|
||||||
public String getContentType() { | ||||||
return request.getContentType().toString(); | ||||||
} | ||||||
|
||||||
public InputStream getMessagePayload() throws IOException { | ||||||
return new ByteArrayInputStream(request.getBody().getBodyBytes()); | ||||||
} | ||||||
|
||||||
public Object unwrap() { | ||||||
return request; | ||||||
} | ||||||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.