use of com.azure.android.core.test.models.RecordingRedactor in project azure-sdk-for-android by Azure.
the class RecordNetworkCallPolicy method extractResponseData.
private Map<String, String> extractResponseData(final HttpResponse response) {
final Map<String, String> responseData = new HashMap<>();
responseData.put(STATUS_CODE, Integer.toString(response.getStatusCode()));
boolean addedRetryAfter = false;
for (HttpHeader header : response.getHeaders()) {
String headerValueToStore = header.getValue();
if (header.getName().equalsIgnoreCase("retry-after")) {
headerValueToStore = "0";
addedRetryAfter = true;
} else if (header.getName().equalsIgnoreCase(X_MS_ENCRYPTION_KEY_SHA256)) {
// The encryption key is sensitive information so capture it with a hidden value.
headerValueToStore = "REDACTED";
}
responseData.put(header.getName(), headerValueToStore);
}
if (!addedRetryAfter) {
responseData.put("retry-after", "0");
}
String contentType = response.getHeaderValue(CONTENT_TYPE);
if (contentType == null) {
final byte[] bytes = response.getBodyAsByteArray();
if (bytes != null && bytes.length != 0) {
String content = new String(bytes, StandardCharsets.UTF_8);
responseData.put(CONTENT_LENGTH, Integer.toString(content.length()));
responseData.put(BODY, content);
}
return responseData;
} else if (contentType.equalsIgnoreCase("application/octet-stream") || contentType.equalsIgnoreCase("avro/binary")) {
final byte[] bytes = response.getBodyAsByteArray();
if (bytes != null && bytes.length != 0) {
responseData.put(BODY, Base64.getEncoder().encodeToString(bytes));
}
return responseData;
} else if (contentType.contains("json") || response.getHeaderValue(CONTENT_ENCODING) == null) {
String content = response.getBodyAsString(StandardCharsets.UTF_8);
if (content == null || content.length() == 0) {
content = "";
}
responseData.put(BODY, new RecordingRedactor().redact(content));
return responseData;
} else {
final byte[] bytes = response.getBodyAsByteArray();
if (bytes == null || bytes.length == 0) {
return responseData;
}
String content;
if ("gzip".equalsIgnoreCase(response.getHeaderValue(CONTENT_ENCODING))) {
try (GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(bytes));
ByteArrayOutputStream output = new ByteArrayOutputStream()) {
byte[] buffer = new byte[DEFAULT_BUFFER_LENGTH];
int position = 0;
int bytesRead = gis.read(buffer, position, buffer.length);
while (bytesRead != -1) {
output.write(buffer, 0, bytesRead);
position += bytesRead;
bytesRead = gis.read(buffer, position, buffer.length);
}
content = output.toString("UTF-8");
} catch (IOException e) {
throw logger.logExceptionAsWarning(new RuntimeException(e));
}
} else {
content = new String(bytes, StandardCharsets.UTF_8);
}
responseData.remove(CONTENT_ENCODING);
responseData.put(CONTENT_LENGTH, Integer.toString(content.length()));
responseData.put(BODY, content);
return responseData;
}
}
Aggregations