| 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.basic; |
| 12 |
|
| 13 |
import java.io.IOException; |
| 14 |
import java.net.HttpURLConnection; |
| 15 |
import java.net.MalformedURLException; |
| 16 |
import java.net.URL; |
| 17 |
|
| 18 |
import oauth.signpost.AbstractOAuthProvider; |
| 19 |
import oauth.signpost.http.HttpRequest; |
| 20 |
import oauth.signpost.http.HttpResponse; |
| 21 |
|
| 22 |
/** |
| 23 |
* This default implementation uses {@link java.net.HttpURLConnection} type GET |
| 24 |
* requests to receive tokens from a service provider. |
| 25 |
* |
| 26 |
* @author Matthias Kaeppler |
| 27 |
*/ |
| 28 |
public class DefaultOAuthProvider extends AbstractOAuthProvider { |
| 29 |
|
| 30 |
private static final long serialVersionUID = 1L; |
| 31 |
|
| 32 |
public DefaultOAuthProvider(String requestTokenEndpointUrl, String accessTokenEndpointUrl, |
| 33 |
String authorizationWebsiteUrl) { |
| 34 |
super(requestTokenEndpointUrl, accessTokenEndpointUrl, authorizationWebsiteUrl); |
| 35 |
} |
| 36 |
|
| 37 |
protected HttpRequest createRequest(String endpointUrl) throws MalformedURLException, |
| 38 |
IOException { |
| 39 |
HttpURLConnection connection = (HttpURLConnection) new URL(endpointUrl).openConnection(); |
| 40 |
connection.setRequestMethod("POST"); |
| 41 |
connection.setAllowUserInteraction(false); |
| 42 |
connection.setRequestProperty("Content-Length", "0"); |
| 43 |
return new HttpURLConnectionRequestAdapter(connection); |
| 44 |
} |
| 45 |
|
| 46 |
protected HttpResponse sendRequest(HttpRequest request) throws IOException { |
| 47 |
HttpURLConnection connection = (HttpURLConnection) request.unwrap(); |
| 48 |
connection.connect(); |
| 49 |
return new HttpURLConnectionResponseAdapter(connection); |
| 50 |
} |
| 51 |
|
| 52 |
@Override |
| 53 |
protected void closeConnection(HttpRequest request, HttpResponse response) { |
| 54 |
HttpURLConnection connection = (HttpURLConnection) request.unwrap(); |
| 55 |
if (connection != null) { |
| 56 |
connection.disconnect(); |
| 57 |
} |
| 58 |
} |
| 59 |
} |