Skip to content

Commit

Permalink
lots of improvements to provider flexibility, in terms of how request…
Browse files Browse the repository at this point in the history
…s are sent to the server
  • Loading branch information
mttkay committed Mar 12, 2010
1 parent c8b96e1 commit 6132583
Show file tree
Hide file tree
Showing 18 changed files with 447 additions and 321 deletions.
@@ -1,35 +1,25 @@
/* 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.
/*
* 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.commonshttp;

import java.io.IOException;
import java.util.Map;

import oauth.signpost.AbstractOAuthProvider;
import oauth.signpost.OAuth;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import oauth.signpost.exception.OAuthNotAuthorizedException;
import oauth.signpost.http.HttpParameters;
import oauth.signpost.http.HttpRequest;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;

/**
Expand All @@ -45,83 +35,50 @@ public class CommonsHttpOAuthProvider extends AbstractOAuthProvider {

private static final long serialVersionUID = 1L;

private HttpClient httpClient;
private transient HttpClient httpClient;

public CommonsHttpOAuthProvider(String requestTokenEndpointUrl, String accessTokenEndpointUrl,
String authorizationWebsiteUrl) {
super(requestTokenEndpointUrl, accessTokenEndpointUrl, authorizationWebsiteUrl);
httpClient = new DefaultHttpClient();
this.httpClient = new DefaultHttpClient();
}

protected void retrieveToken(OAuthConsumer consumer, String endpointUrl)
throws OAuthMessageSignerException, OAuthCommunicationException,
OAuthNotAuthorizedException, OAuthExpectationFailedException {

Map<String, String> defaultHeaders = getRequestHeaders();
public CommonsHttpOAuthProvider(String requestTokenEndpointUrl, String accessTokenEndpointUrl,
String authorizationWebsiteUrl, HttpClient httpClient) {
super(requestTokenEndpointUrl, accessTokenEndpointUrl, authorizationWebsiteUrl);
this.httpClient = httpClient;
}

if (consumer.getConsumerKey() == null || consumer.getConsumerSecret() == null) {
throw new OAuthExpectationFailedException("Consumer key or secret not set");
}
public void setHttpClient(HttpClient httpClient) {
this.httpClient = httpClient;
}

@Override
protected HttpRequest createRequest(String endpointUrl) throws Exception {
HttpGet request = new HttpGet(endpointUrl);
for (String header : defaultHeaders.keySet()) {
request.setHeader(header, defaultHeaders.get(header));
}
HttpResponse response = null;

try {

consumer.sign(new HttpRequestAdapter(request));

response = httpClient.execute(request);

int statusCode = response.getStatusLine().getStatusCode();

if (statusCode == 401) {
throw new OAuthNotAuthorizedException();
}

HttpParameters responseParams = OAuth
.decodeForm(response.getEntity().getContent());

String token = responseParams.getFirst(OAuth.OAUTH_TOKEN);
String secret = responseParams.getFirst(OAuth.OAUTH_TOKEN_SECRET);
responseParams.remove(OAuth.OAUTH_TOKEN);
responseParams.remove(OAuth.OAUTH_TOKEN_SECRET);

setResponseParameters(responseParams);

if (token == null || secret == null) {
throw new OAuthExpectationFailedException(
"Request token or token secret not set in server reply. "
+ "The service provider you use is probably buggy.");
}
return new HttpRequestAdapter(request);
}

consumer.setTokenWithSecret(token, secret);
@Override
protected oauth.signpost.http.HttpResponse sendRequest(HttpRequest request) throws Exception {
HttpResponse response = httpClient.execute((HttpUriRequest) request.unwrap());
return new HttpResponseAdapter(response);
}

} catch (OAuthNotAuthorizedException e) {
throw e;
} catch (OAuthExpectationFailedException e) {
throw e;
} catch (Exception e) {
throw new OAuthCommunicationException(e);
} finally {
if (response != null) {
HttpEntity entity = response.getEntity();
if (entity != null) {
try {
// free the connection
entity.consumeContent();
} catch (IOException e) {
// this means HTTP keep-alive is not possible
e.printStackTrace();
}
@Override
protected void closeConnection(HttpRequest request, oauth.signpost.http.HttpResponse response)
throws Exception {
if (response != null) {
HttpEntity entity = ((HttpResponse) response.unwrap()).getEntity();
if (entity != null) {
try {
// free the connection
entity.consumeContent();
} catch (IOException e) {
// this means HTTP keep-alive is not possible
e.printStackTrace();
}
}
}
}

void setHttpClient(HttpClient httpClient) {
this.httpClient = httpClient;
}
}
Expand Up @@ -73,4 +73,8 @@ public InputStream getMessagePayload() throws IOException {
}
return entity.getContent();
}

public Object unwrap() {
return request;
}
}
@@ -0,0 +1,27 @@
package oauth.signpost.commonshttp;

import java.io.IOException;
import java.io.InputStream;

import oauth.signpost.http.HttpResponse;

public class HttpResponseAdapter implements HttpResponse {

private org.apache.http.HttpResponse response;

public HttpResponseAdapter(org.apache.http.HttpResponse response) {
this.response = response;
}

public InputStream getContent() throws IOException {
return response.getEntity().getContent();
}

public int getStatusCode() throws IOException {
return response.getStatusLine().getStatusCode();
}

public Object unwrap() {
return response;
}
}
@@ -0,0 +1,49 @@
package oauth.signpost.commonshttp;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import oauth.signpost.http.HttpRequest;
import oauth.signpost.mocks.OAuthProviderMock;

import org.apache.http.HttpResponse;
import org.apache.http.ProtocolVersion;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.message.BasicStatusLine;
import org.mockito.Mockito;

@SuppressWarnings("serial")
public class CommonHttpOAuthProviderMock extends CommonsHttpOAuthProvider implements
OAuthProviderMock {

private HttpClient httpClientMock;

public CommonHttpOAuthProviderMock(String requestTokenUrl, String accessTokenUrl,
String websiteUrl) {
super(requestTokenUrl, accessTokenUrl, websiteUrl);
}

@Override
protected oauth.signpost.http.HttpResponse sendRequest(HttpRequest request) throws Exception {
HttpResponse resp = httpClientMock.execute((HttpUriRequest) request.unwrap());
return new HttpResponseAdapter(resp);
}

public void mockConnection(String responseBody) throws Exception {
HttpResponse response = mock(HttpResponse.class);
this.httpClientMock = mock(HttpClient.class);
InputStream is = new ByteArrayInputStream(responseBody.getBytes());
InputStreamEntity entity = new InputStreamEntity(is, responseBody.length());
StatusLine statusLine = new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, "OK");

when(response.getStatusLine()).thenReturn(statusLine);
when(response.getEntity()).thenReturn(entity);
when(httpClientMock.execute(Mockito.any(HttpUriRequest.class))).thenReturn(response);
}
}
@@ -1,24 +1,10 @@
package oauth.signpost.commonshttp;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

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

import org.apache.http.HttpResponse;
import org.apache.http.ProtocolVersion;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.message.BasicStatusLine;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnit44Runner;

@RunWith(MockitoJUnit44Runner.class)
Expand All @@ -27,29 +13,13 @@ public class CommonsHttpOAuthProviderTest extends OAuthProviderTest {
@Override
protected OAuthProvider buildProvider(String requestTokenUrl, String accessTokenUrl,
String websiteUrl, boolean mockConnection) throws Exception {
OAuthProvider provider = new CommonsHttpOAuthProvider(requestTokenUrl, accessTokenUrl,
websiteUrl);
if (mockConnection) {
mockConnection(provider, OAuth.OAUTH_TOKEN + "=" + TOKEN + "&"
CommonHttpOAuthProviderMock provider = new CommonHttpOAuthProviderMock(requestTokenUrl,
accessTokenUrl, websiteUrl);
provider.mockConnection(OAuth.OAUTH_TOKEN + "=" + TOKEN + "&"
+ OAuth.OAUTH_TOKEN_SECRET + "=" + TOKEN_SECRET);
} else {
((CommonsHttpOAuthProvider) provider).setHttpClient(null);
return provider;
}
return provider;
}

@Override
protected void mockConnection(OAuthProvider provider, String responseBody) throws Exception {
HttpResponse response = mock(HttpResponse.class);
HttpClient client = mock(HttpClient.class);
InputStream is = new ByteArrayInputStream(responseBody.getBytes());
InputStreamEntity entity = new InputStreamEntity(is, responseBody.length());
StatusLine statusLine = new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, "OK");

when(response.getStatusLine()).thenReturn(statusLine);
when(response.getEntity()).thenReturn(entity);
when(client.execute(Mockito.any(HttpUriRequest.class))).thenReturn(response);

((CommonsHttpOAuthProvider) provider).setHttpClient(client);
return new CommonsHttpOAuthProvider(requestTokenUrl, accessTokenUrl, websiteUrl);
}
}

0 comments on commit 6132583

Please sign in to comment.