|
19 | 19 | package org.apache.hadoop.hdfs.web.oauth2;
|
20 | 20 |
|
21 | 21 | import java.io.IOException;
|
| 22 | +import java.nio.charset.StandardCharsets; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.List; |
22 | 25 | import java.util.Map;
|
23 |
| -import java.util.concurrent.TimeUnit; |
24 |
| - |
25 |
| -import okhttp3.OkHttpClient; |
26 |
| -import okhttp3.Request; |
27 |
| -import okhttp3.RequestBody; |
28 |
| -import okhttp3.Response; |
29 | 26 |
|
30 | 27 | import org.apache.hadoop.classification.InterfaceAudience;
|
31 | 28 | import org.apache.hadoop.classification.InterfaceStability;
|
32 | 29 | import org.apache.hadoop.conf.Configuration;
|
33 | 30 | import org.apache.hadoop.hdfs.web.URLConnectionFactory;
|
34 | 31 | import org.apache.hadoop.util.JsonSerialization;
|
35 | 32 | import org.apache.hadoop.util.Timer;
|
| 33 | +import org.apache.http.HttpHeaders; |
36 | 34 | import org.apache.http.HttpStatus;
|
| 35 | +import org.apache.http.NameValuePair; |
| 36 | +import org.apache.http.client.config.RequestConfig; |
| 37 | +import org.apache.http.client.entity.UrlEncodedFormEntity; |
| 38 | +import org.apache.http.client.methods.CloseableHttpResponse; |
| 39 | +import org.apache.http.client.methods.HttpPost; |
| 40 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 41 | +import org.apache.http.impl.client.HttpClientBuilder; |
| 42 | +import org.apache.http.message.BasicNameValuePair; |
| 43 | +import org.apache.http.util.EntityUtils; |
37 | 44 |
|
38 | 45 | import static org.apache.hadoop.hdfs.client.HdfsClientConfigKeys.OAUTH_CLIENT_ID_KEY;
|
39 | 46 | import static org.apache.hadoop.hdfs.client.HdfsClientConfigKeys.OAUTH_REFRESH_URL_KEY;
|
@@ -97,38 +104,37 @@ public synchronized String getAccessToken() throws IOException {
|
97 | 104 | }
|
98 | 105 |
|
99 | 106 | void refresh() throws IOException {
|
100 |
| - OkHttpClient client = new OkHttpClient.Builder() |
101 |
| - .connectTimeout(URLConnectionFactory.DEFAULT_SOCKET_TIMEOUT, TimeUnit.MILLISECONDS) |
102 |
| - .readTimeout(URLConnectionFactory.DEFAULT_SOCKET_TIMEOUT, TimeUnit.MILLISECONDS) |
103 |
| - .build(); |
104 |
| - |
105 |
| - String bodyString = Utils.postBody(CLIENT_SECRET, getCredential(), |
106 |
| - GRANT_TYPE, CLIENT_CREDENTIALS, |
107 |
| - CLIENT_ID, clientId); |
108 |
| - |
109 |
| - RequestBody body = RequestBody.create(bodyString, URLENCODED); |
110 |
| - |
111 |
| - Request request = new Request.Builder() |
112 |
| - .url(refreshURL) |
113 |
| - .post(body) |
| 107 | + final List<NameValuePair> pairs = new ArrayList<>(); |
| 108 | + pairs.add(new BasicNameValuePair(CLIENT_SECRET, getCredential())); |
| 109 | + pairs.add(new BasicNameValuePair(GRANT_TYPE, CLIENT_CREDENTIALS)); |
| 110 | + pairs.add(new BasicNameValuePair(CLIENT_ID, clientId)); |
| 111 | + final RequestConfig config = RequestConfig.custom() |
| 112 | + .setConnectTimeout(URLConnectionFactory.DEFAULT_SOCKET_TIMEOUT) |
| 113 | + .setConnectionRequestTimeout(URLConnectionFactory.DEFAULT_SOCKET_TIMEOUT) |
| 114 | + .setSocketTimeout(URLConnectionFactory.DEFAULT_SOCKET_TIMEOUT) |
114 | 115 | .build();
|
115 |
| - try (Response response = client.newCall(request).execute()) { |
116 |
| - if (!response.isSuccessful()) { |
117 |
| - throw new IOException("Unexpected code " + response); |
118 |
| - } |
119 |
| - |
120 |
| - if (response.code() != HttpStatus.SC_OK) { |
121 |
| - throw new IllegalArgumentException("Received invalid http response: " |
122 |
| - + response.code() + ", text = " + response.toString()); |
| 116 | + try (CloseableHttpClient client = |
| 117 | + HttpClientBuilder.create().setDefaultRequestConfig(config).build()) { |
| 118 | + final HttpPost httpPost = new HttpPost(refreshURL); |
| 119 | + httpPost.setEntity(new UrlEncodedFormEntity(pairs, StandardCharsets.UTF_8)); |
| 120 | + httpPost.setHeader(HttpHeaders.CONTENT_TYPE, URLENCODED); |
| 121 | + try (CloseableHttpResponse response = client.execute(httpPost)) { |
| 122 | + final int statusCode = response.getStatusLine().getStatusCode(); |
| 123 | + if (statusCode != HttpStatus.SC_OK) { |
| 124 | + throw new IllegalArgumentException( |
| 125 | + "Received invalid http response: " + statusCode + ", text = " + |
| 126 | + EntityUtils.toString(response.getEntity())); |
| 127 | + } |
| 128 | + Map<?, ?> responseBody = JsonSerialization.mapReader().readValue( |
| 129 | + EntityUtils.toString(response.getEntity())); |
| 130 | + |
| 131 | + String newExpiresIn = responseBody.get(EXPIRES_IN).toString(); |
| 132 | + timer.setExpiresIn(newExpiresIn); |
| 133 | + |
| 134 | + accessToken = responseBody.get(ACCESS_TOKEN).toString(); |
123 | 135 | }
|
124 |
| - |
125 |
| - Map<?, ?> responseBody = JsonSerialization.mapReader().readValue( |
126 |
| - response.body().string()); |
127 |
| - |
128 |
| - String newExpiresIn = responseBody.get(EXPIRES_IN).toString(); |
129 |
| - timer.setExpiresIn(newExpiresIn); |
130 |
| - |
131 |
| - accessToken = responseBody.get(ACCESS_TOKEN).toString(); |
| 136 | + } catch (RuntimeException e) { |
| 137 | + throw new IOException("Unable to obtain access token from credential", e); |
132 | 138 | } catch (Exception e) {
|
133 | 139 | throw new IOException("Unable to obtain access token from credential", e);
|
134 | 140 | }
|
|
0 commit comments