use of org.odk.collect.android.openrosa.HttpGetResult in project collect by opendatakit.
the class OkHttpConnection method executeGetRequest.
@NonNull
@Override
public HttpGetResult executeGetRequest(@NonNull URI uri, @Nullable String contentType, @Nullable HttpCredentialsInterface credentials) throws Exception {
OpenRosaServerClient httpClient = clientFactory.get(uri.getScheme(), userAgent, credentials);
Request request = new Request.Builder().url(uri.toURL()).get().build();
Response response = httpClient.makeRequest(request, new Date());
int statusCode = response.code();
if (statusCode != HttpURLConnection.HTTP_OK) {
discardEntityBytes(response);
Timber.i("Error: %s (%s at %s", response.message(), String.valueOf(statusCode), uri.toString());
return new HttpGetResult(null, new HashMap<String, String>(), "", statusCode);
}
ResponseBody body = response.body();
if (body == null) {
throw new Exception("No entity body returned from: " + uri.toString());
}
if (contentType != null && contentType.length() > 0) {
MediaType type = body.contentType();
if (type != null && !type.toString().toLowerCase(Locale.ENGLISH).contains(contentType)) {
discardEntityBytes(response);
String error = "ContentType: " + type.toString() + " returned from: " + uri.toString() + " is not " + contentType + ". This is often caused by a network proxy. Do you need " + "to login to your network?";
throw new Exception(error);
}
}
InputStream downloadStream = body.byteStream();
String hash = "";
if (HTTP_CONTENT_TYPE_TEXT_XML.equals(contentType)) {
byte[] bytes = IOUtils.toByteArray(downloadStream);
downloadStream = new ByteArrayInputStream(bytes);
hash = Md5.getMd5Hash(new ByteArrayInputStream(bytes));
}
Map<String, String> responseHeaders = new HashMap<>();
Headers headers = response.headers();
for (int i = 0; i < headers.size(); i++) {
responseHeaders.put(headers.name(i), headers.value(i));
}
return new HttpGetResult(downloadStream, responseHeaders, hash, statusCode);
}
Aggregations