Search in sources :

Example 11 with HttpGetResult

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);
}
Also used : HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CaseInsensitiveHeaders(org.odk.collect.android.openrosa.CaseInsensitiveHeaders) Headers(okhttp3.Headers) CaseInsensitiveEmptyHeaders(org.odk.collect.android.openrosa.CaseInsensitiveEmptyHeaders) Request(okhttp3.Request) Date(java.util.Date) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) OpenRosaServerClient(org.odk.collect.android.openrosa.OpenRosaServerClient) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpGetResult(org.odk.collect.android.openrosa.HttpGetResult) MediaType(okhttp3.MediaType) NonNull(androidx.annotation.NonNull)

Aggregations

HashMap (java.util.HashMap)11 HttpGetResult (org.odk.collect.android.openrosa.HttpGetResult)11 Test (org.junit.Test)10 OpenRosaFormSource (org.odk.collect.android.openrosa.OpenRosaFormSource)10 FormSourceException (org.odk.collect.forms.FormSourceException)9 ByteArrayInputStream (java.io.ByteArrayInputStream)6 NonNull (androidx.annotation.NonNull)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 Date (java.util.Date)1 Headers (okhttp3.Headers)1 MediaType (okhttp3.MediaType)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1 ResponseBody (okhttp3.ResponseBody)1 CaseInsensitiveEmptyHeaders (org.odk.collect.android.openrosa.CaseInsensitiveEmptyHeaders)1 CaseInsensitiveHeaders (org.odk.collect.android.openrosa.CaseInsensitiveHeaders)1 OpenRosaServerClient (org.odk.collect.android.openrosa.OpenRosaServerClient)1