pjfanning commented on code in PR #6057:
URL: https://github.com/apache/hadoop/pull/6057#discussion_r1324791220
##########
hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/web/oauth2/CredentialBasedAccessTokenProvider.java:
##########
@@ -97,38 +104,37 @@ public synchronized String getAccessToken() throws
IOException {
}
void refresh() throws IOException {
- 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)
+ final List<NameValuePair> pairs = new ArrayList<>();
+ pairs.add(new BasicNameValuePair(CLIENT_SECRET, getCredential()));
+ pairs.add(new BasicNameValuePair(GRANT_TYPE, CLIENT_CREDENTIALS));
+ pairs.add(new BasicNameValuePair(CLIENT_ID, clientId));
+ final RequestConfig config = RequestConfig.custom()
+ .setConnectTimeout(URLConnectionFactory.DEFAULT_SOCKET_TIMEOUT)
+
.setConnectionRequestTimeout(URLConnectionFactory.DEFAULT_SOCKET_TIMEOUT)
+ .setSocketTimeout(URLConnectionFactory.DEFAULT_SOCKET_TIMEOUT)
.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: "
- + response.code() + ", text = " + response.toString());
+ try (CloseableHttpClient client =
+
HttpClientBuilder.create().setDefaultRequestConfig(config).build()) {
+ final HttpPost httpPost = new HttpPost(refreshURL);
+ httpPost.setEntity(new UrlEncodedFormEntity(pairs,
StandardCharsets.UTF_8));
+ httpPost.setHeader(HttpHeaders.CONTENT_TYPE, URLENCODED);
+ try (CloseableHttpResponse response = client.execute(httpPost)) {
+ final int statusCode = response.getStatusLine().getStatusCode();
+ if (statusCode != HttpStatus.SC_OK) {
+ throw new IllegalArgumentException(
+ "Received invalid http response: " + statusCode + ", text = " +
+ EntityUtils.toString(response.getEntity()));
+ }
+ Map<?, ?> responseBody = JsonSerialization.mapReader().readValue(
Review Comment:
The pre-existing code did not check the content-type of the return. It is
not uncommon for an API to return JSON but not to have `application/json`
Content-Type. I can make the change but I'm worried that the API we call may
not set the expected content-type on the response.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]