Skip to content
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
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ This means that all the power and flexibility of the underlying HTTP engine is s
### Modularity
Since version 1.1, Signpost comes in modules. Apart from the core module, which you always need, you can download additional modules to support other HTTP messaging libraries than the one coming with the standard Java platform (which would be [java.net.HttpURLConnection](https://docs.oracle.com/javase/8/docs/api/java/net/HttpURLConnection.html)).

Apart from HttpURLConnection, Signpost currently has modules for [Apache Commons HTTP](https://hc.apache.org/) version 4, and [Jetty HTTP Client](https://www.eclipse.org/jetty/documentation/current/http-client-api.html) version 6.
Apart from HttpURLConnection, Signpost currently has modules for [Apache Commons HTTP](https://hc.apache.org/) version 4 and version 5 as well as [Jetty HTTP Client](https://www.eclipse.org/jetty/documentation/current/http-client-api.html) version 6.

Limitations
------------
Expand Down
11 changes: 4 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<module>signpost-core</module>
<module>signpost-commonshttp3</module>
<module>signpost-commonshttp4</module>
<module>signpost-commonshttp5</module>
<module>signpost-jetty6</module>
</modules>

Expand Down Expand Up @@ -99,13 +100,13 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.5</version>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.0</version>
<artifactId>mockito-inline</artifactId>
<version>4.3.1</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand All @@ -115,10 +116,6 @@
<id>ossrh</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url>
</repository>
<snapshotRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import oauth.signpost.commonshttp3.Http3ResponseAdapter;
import oauth.signpost.commonshttp3.CommonsHttp3OAuthProvider;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
Expand All @@ -17,6 +15,8 @@

import org.mockito.Mockito;

import static org.mockito.Mockito.*;

@SuppressWarnings("serial")
public class CommonHttpOAuthProviderMock extends CommonsHttp3OAuthProvider implements
OAuthProviderMock {
Expand All @@ -42,8 +42,8 @@ public void mockConnection(String responseBody) throws Exception {

this.httpMethodMock = mock(HttpMethod.class);

when(httpMethodMock.getStatusLine()).thenReturn(statusLine);
when(httpMethodMock.getStatusCode()).thenReturn(200);
when(httpMethodMock.getResponseBodyAsStream()).thenReturn(is);
lenient().when(httpMethodMock.getStatusLine()).thenReturn(statusLine);
lenient().when(httpMethodMock.getStatusCode()).thenReturn(200);
lenient().when(httpMethodMock.getResponseBodyAsStream()).thenReturn(is);
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package oauth.signpost.commonshttp3;

import oauth.signpost.commonshttp3.CommonsHttp3OAuthProvider;
import oauth.signpost.OAuth;
import oauth.signpost.OAuthProvider;
import oauth.signpost.OAuthProviderTest;

import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnit44Runner;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnit44Runner.class)
@RunWith(MockitoJUnitRunner.class)
public class CommonsHttpOAuthProviderTest extends OAuthProviderTest {

@Override
protected OAuthProvider buildProvider(String requestTokenUrl, String accessTokenUrl,
String websiteUrl, boolean mockConnection) throws Exception {
String websiteUrl, boolean mockConnection) throws Exception {
if (mockConnection) {
CommonHttpOAuthProviderMock provider = new CommonHttpOAuthProviderMock(requestTokenUrl,
accessTokenUrl, websiteUrl);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package oauth.signpost.commonshttp3;

import oauth.signpost.commonshttp3.Http3RequestAdapter;
import oauth.signpost.basic.HttpRequestAdapterTestBase;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;

import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnit44Runner;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnit44Runner.class)
@RunWith(MockitoJUnitRunner.class)
public class HttpRequestAdapterTest extends HttpRequestAdapterTestBase {

@Override
public void prepareRequest() throws Exception {
PostMethod method = new PostMethod(URL);
method.setRequestHeader(HEADER_NAME, HEADER_VALUE);
RequestEntity body = new StringRequestEntity(PAYLOAD,CONTENT_TYPE,null);
RequestEntity body = new StringRequestEntity(PAYLOAD, CONTENT_TYPE, null);
method.setRequestEntity(body);
request = new Http3RequestAdapter(method);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
import oauth.signpost.OAuth;
import oauth.signpost.OAuthProvider;
import oauth.signpost.OAuthProviderTest;

import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnit44Runner;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnit44Runner.class)
@RunWith(MockitoJUnitRunner.class)
public class CommonsHttpOAuthProviderTest extends OAuthProviderTest {

@Override
protected OAuthProvider buildProvider(String requestTokenUrl, String accessTokenUrl,
String websiteUrl, boolean mockConnection) throws Exception {
String websiteUrl, boolean mockConnection) throws Exception {
if (mockConnection) {
CommonHttpOAuthProviderMock provider = new CommonHttpOAuthProviderMock(requestTokenUrl,
accessTokenUrl, websiteUrl);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package oauth.signpost.commonshttp;

import oauth.signpost.basic.HttpRequestAdapterTestBase;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnit44Runner;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnit44Runner.class)
@RunWith(MockitoJUnitRunner.class)
public class HttpRequestAdapterTest extends HttpRequestAdapterTestBase {

@Override
Expand Down
33 changes: 33 additions & 0 deletions signpost-commonshttp5/pom.xml
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>
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);
}

}
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;
Comment on lines +24 to +30

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 org.apache.hc.core5.http.Method;

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();
}
}
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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import java.util.Arrays;

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>();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
HashMap<String, String> headers = new HashMap<String, String>();
HashMap<String, String> headers = new HashMap<>();

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;
}
}
Loading