Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error handling when token is chunked #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.Hashtable;
import java.util.UUID;
Expand Down Expand Up @@ -273,7 +275,7 @@ private static AzureADToken getTokenSingleCall(String authEndpoint, String paylo
long responseContentLength = conn.getHeaderFieldLong("Content-Length", 0);
requestId = requestId == null ? "" : requestId;

if (httpResponseCode == 200 && responseContentType.startsWith("application/json") && responseContentLength > 0) {
if (httpResponseCode == 200 && responseContentType.startsWith("application/json")) {
InputStream httpResponseStream = conn.getInputStream();
token = parseTokenFromStream(httpResponseStream);

Expand All @@ -284,13 +286,13 @@ private static AzureADToken getTokenSingleCall(String authEndpoint, String paylo
+ httpResponseCode + " " + conn.getResponseMessage()
+ " Content-Type: " + responseContentType
+ " Content-Length: " + responseContentLength
+ " Request ID: " + requestId.toString()
+ " Request ID: " + requestId
+ " Client Request Id: " + headers.get("client-request-id")
+ " Latency(ns) : " + totalTime;
log.debug(logMessage);
}
} else {
String responseBody = consumeInputStream(conn.getInputStream(), 1024);
String responseBody = new String(readAllBytes(conn.getInputStream()), StandardCharsets.UTF_8);
totalTime = System.nanoTime() - startTime;
String proxies = "none";
String httpProxy=System.getProperty("http.proxy");
Expand Down Expand Up @@ -369,20 +371,32 @@ private static AzureADToken parseTokenFromStream(InputStream httpResponseStream)
return token;
}

private static String consumeInputStream(InputStream inStream, int length) throws IOException {
byte[] b = new byte[length];
int totalBytesRead = 0;
int bytesRead = 0;
private static byte[] readAllBytes(InputStream inputStream) throws IOException {
final int bufLen = 4 * 0x400; // 4KB
byte[] buf = new byte[bufLen];
int readLen;
IOException exception = null;

do {
bytesRead = inStream.read(b, totalBytesRead, length - totalBytesRead);
if (bytesRead > 0) {
totalBytesRead += bytesRead;
}
} while (bytesRead >= 0 && totalBytesRead < length);
try {
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
while ((readLen = inputStream.read(buf, 0, bufLen)) != -1)
outputStream.write(buf, 0, readLen);

return new String(b, 0, totalBytesRead);
return outputStream.toByteArray();
}
} catch (IOException e) {
exception = e;
throw e;
} finally {
if (exception == null) inputStream.close();
else try {
inputStream.close();
} catch (IOException e) {
exception.addSuppressed(e);
}
}
}
}