|
@@ -18,10 +18,15 @@
|
|
|
*/
|
|
|
package org.apache.hadoop.hdfs.web.oauth2;
|
|
|
|
|
|
-import com.squareup.okhttp.OkHttpClient;
|
|
|
-import com.squareup.okhttp.Request;
|
|
|
-import com.squareup.okhttp.RequestBody;
|
|
|
-import com.squareup.okhttp.Response;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+import okhttp3.OkHttpClient;
|
|
|
+import okhttp3.Request;
|
|
|
+import okhttp3.RequestBody;
|
|
|
+import okhttp3.Response;
|
|
|
+
|
|
|
import org.apache.hadoop.classification.InterfaceAudience;
|
|
|
import org.apache.hadoop.classification.InterfaceStability;
|
|
|
import org.apache.hadoop.conf.Configuration;
|
|
@@ -30,10 +35,6 @@ import org.apache.hadoop.util.JsonSerialization;
|
|
|
import org.apache.hadoop.util.Timer;
|
|
|
import org.apache.http.HttpStatus;
|
|
|
|
|
|
-import java.io.IOException;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
-
|
|
|
import static org.apache.hadoop.hdfs.client.HdfsClientConfigKeys.OAUTH_CLIENT_ID_KEY;
|
|
|
import static org.apache.hadoop.hdfs.client.HdfsClientConfigKeys.OAUTH_REFRESH_URL_KEY;
|
|
|
import static org.apache.hadoop.hdfs.web.oauth2.OAuth2Constants.ACCESS_TOKEN;
|
|
@@ -96,38 +97,38 @@ public abstract class CredentialBasedAccessTokenProvider
|
|
|
}
|
|
|
|
|
|
void refresh() throws IOException {
|
|
|
- try {
|
|
|
- OkHttpClient client = new OkHttpClient();
|
|
|
- client.setConnectTimeout(URLConnectionFactory.DEFAULT_SOCKET_TIMEOUT,
|
|
|
- TimeUnit.MILLISECONDS);
|
|
|
- client.setReadTimeout(URLConnectionFactory.DEFAULT_SOCKET_TIMEOUT,
|
|
|
- TimeUnit.MILLISECONDS);
|
|
|
-
|
|
|
- String bodyString = Utils.postBody(CLIENT_SECRET, getCredential(),
|
|
|
- GRANT_TYPE, CLIENT_CREDENTIALS,
|
|
|
- CLIENT_ID, clientId);
|
|
|
-
|
|
|
- RequestBody body = RequestBody.create(URLENCODED, bodyString);
|
|
|
-
|
|
|
- Request request = new Request.Builder()
|
|
|
- .url(refreshURL)
|
|
|
- .post(body)
|
|
|
- .build();
|
|
|
- Response responseBody = client.newCall(request).execute();
|
|
|
-
|
|
|
- if (responseBody.code() != HttpStatus.SC_OK) {
|
|
|
+ OkHttpClient client = new OkHttpClient.Builder()
|
|
|
+ .connectTimeout(URLConnectionFactory.DEFAULT_SOCKET_TIMEOUT, TimeUnit.MILLISECONDS)
|
|
|
+ .readTimeout(URLConnectionFactory.DEFAULT_SOCKET_TIMEOUT, TimeUnit.MILLISECONDS)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ String bodyString = Utils.postBody(CLIENT_SECRET, getCredential(),
|
|
|
+ GRANT_TYPE, CLIENT_CREDENTIALS,
|
|
|
+ CLIENT_ID, clientId);
|
|
|
+
|
|
|
+ RequestBody body = RequestBody.create(bodyString, URLENCODED);
|
|
|
+
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url(refreshURL)
|
|
|
+ .post(body)
|
|
|
+ .build();
|
|
|
+ try (Response response = client.newCall(request).execute()) {
|
|
|
+ if (!response.isSuccessful()) {
|
|
|
+ throw new IOException("Unexpected code " + response);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (response.code() != HttpStatus.SC_OK) {
|
|
|
throw new IllegalArgumentException("Received invalid http response: "
|
|
|
- + responseBody.code() + ", text = " + responseBody.toString());
|
|
|
+ + response.code() + ", text = " + response.toString());
|
|
|
}
|
|
|
|
|
|
- Map<?, ?> response = JsonSerialization.mapReader().readValue(
|
|
|
- responseBody.body().string());
|
|
|
+ Map<?, ?> responseBody = JsonSerialization.mapReader().readValue(
|
|
|
+ response.body().string());
|
|
|
|
|
|
- String newExpiresIn = response.get(EXPIRES_IN).toString();
|
|
|
+ String newExpiresIn = responseBody.get(EXPIRES_IN).toString();
|
|
|
timer.setExpiresIn(newExpiresIn);
|
|
|
|
|
|
- accessToken = response.get(ACCESS_TOKEN).toString();
|
|
|
-
|
|
|
+ accessToken = responseBody.get(ACCESS_TOKEN).toString();
|
|
|
} catch (Exception e) {
|
|
|
throw new IOException("Unable to obtain access token from credential", e);
|
|
|
}
|