Debian Codegraph @

signpost-commonshttp4/src/main/java/oauth/signpost/commonshttp/CommonsHttpOAuthProvider.java

raw
1 /*
2 * Copyright (c) 2009 Matthias Kaeppler Licensed under the Apache License,
3 * Version 2.0 (the "License"); you may not use this file except in compliance
4 * with the License. You may obtain a copy of the License at
5 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
6 * or agreed to in writing, software distributed under the License is
7 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8 * KIND, either express or implied. See the License for the specific language
9 * governing permissions and limitations under the License.
10 */
11 package oauth.signpost.commonshttp;
12
13 import java.io.IOException;
14
15 import oauth.signpost.AbstractOAuthProvider;
16 import oauth.signpost.http.HttpRequest;
17
18 import org.apache.http.HttpEntity;
19 import org.apache.http.HttpResponse;
20 import org.apache.http.client.HttpClient;
21 import org.apache.http.client.methods.HttpPost;
22 import org.apache.http.client.methods.HttpUriRequest;
23 import org.apache.http.impl.client.DefaultHttpClient;
24
25 /**
26 * This implementation uses the Apache Commons {@link HttpClient} 4.x HTTP
27 * implementation to fetch OAuth tokens from a service provider. Android users
28 * should use this provider implementation in favor of the default one, since
29 * the latter is known to cause problems with Android's Apache Harmony
30 * underpinnings.
31 *
32 * @author Matthias Kaeppler
33 */
34 public class CommonsHttpOAuthProvider extends AbstractOAuthProvider {
35
36 private static final long serialVersionUID = 1L;
37
38 private transient HttpClient httpClient;
39
40 public CommonsHttpOAuthProvider(String requestTokenEndpointUrl, String accessTokenEndpointUrl,
41 String authorizationWebsiteUrl) {
42 super(requestTokenEndpointUrl, accessTokenEndpointUrl, authorizationWebsiteUrl);
43 this.httpClient = new DefaultHttpClient();
44 }
45
46 public CommonsHttpOAuthProvider(String requestTokenEndpointUrl, String accessTokenEndpointUrl,
47 String authorizationWebsiteUrl, HttpClient httpClient) {
48 super(requestTokenEndpointUrl, accessTokenEndpointUrl, authorizationWebsiteUrl);
49 this.httpClient = httpClient;
50 }
51
52 public void setHttpClient(HttpClient httpClient) {
53 this.httpClient = httpClient;
54 }
55
56 @Override
57 protected HttpRequest createRequest(String endpointUrl) throws Exception {
58 HttpPost request = new HttpPost(endpointUrl);
59 return new HttpRequestAdapter(request);
60 }
61
62 @Override
63 protected oauth.signpost.http.HttpResponse sendRequest(HttpRequest request) throws Exception {
64 HttpResponse response = httpClient.execute((HttpUriRequest) request.unwrap());
65 return new HttpResponseAdapter(response);
66 }
67
68 @Override
69 protected void closeConnection(HttpRequest request, oauth.signpost.http.HttpResponse response)
70 throws Exception {
71 if (response != null) {
72 HttpEntity entity = ((HttpResponse) response.unwrap()).getEntity();
73 if (entity != null) {
74 try {
75 // free the connection
76 entity.consumeContent();
77 } catch (IOException e) {
78 // this means HTTP keep-alive is not possible
79 e.printStackTrace();
80 }
81 }
82 }
83 }
84 }