Search in sources :

Example 1 with OpenRosaServerClient

use of org.odk.collect.android.openrosa.OpenRosaServerClient in project collect by opendatakit.

the class OkHttpConnection method executeHeadRequest.

@NonNull
@Override
public HttpHeadResult executeHeadRequest(@NonNull URI uri, @Nullable HttpCredentialsInterface credentials) throws Exception {
    OpenRosaServerClient httpClient = clientFactory.get(uri.getScheme(), userAgent, credentials);
    Request request = new Request.Builder().url(uri.toURL()).head().build();
    Timber.i("Issuing HEAD request to: %s", uri.toString());
    Response response = httpClient.makeRequest(request, new Date());
    int statusCode = response.code();
    CaseInsensitiveHeaders responseHeaders = new CaseInsensitiveEmptyHeaders();
    if (statusCode == HttpURLConnection.HTTP_NO_CONTENT) {
        responseHeaders = new OkHttpCaseInsensitiveHeaders(response.headers());
    }
    discardEntityBytes(response);
    return new HttpHeadResult(statusCode, responseHeaders);
}
Also used : Response(okhttp3.Response) OpenRosaServerClient(org.odk.collect.android.openrosa.OpenRosaServerClient) CaseInsensitiveHeaders(org.odk.collect.android.openrosa.CaseInsensitiveHeaders) CaseInsensitiveEmptyHeaders(org.odk.collect.android.openrosa.CaseInsensitiveEmptyHeaders) Request(okhttp3.Request) HttpHeadResult(org.odk.collect.android.openrosa.HttpHeadResult) Date(java.util.Date) NonNull(androidx.annotation.NonNull)

Example 2 with OpenRosaServerClient

use of org.odk.collect.android.openrosa.OpenRosaServerClient in project collect by opendatakit.

the class OkHttpConnection method executePostRequest.

@NonNull
private HttpPostResult executePostRequest(@NonNull URI uri, @Nullable HttpCredentialsInterface credentials, MultipartBody multipartBody) throws Exception {
    OpenRosaServerClient httpClient = clientFactory.get(uri.getScheme(), userAgent, credentials);
    HttpPostResult postResult;
    Request request = new Request.Builder().url(uri.toURL()).post(multipartBody).build();
    Response response = httpClient.makeRequest(request, new Date());
    if (response.code() == 204) {
        throw new Exception();
    }
    postResult = new HttpPostResult(response.body().string(), response.code(), response.message());
    discardEntityBytes(response);
    return postResult;
}
Also used : Response(okhttp3.Response) OpenRosaServerClient(org.odk.collect.android.openrosa.OpenRosaServerClient) Request(okhttp3.Request) HttpPostResult(org.odk.collect.android.openrosa.HttpPostResult) Date(java.util.Date) NonNull(androidx.annotation.NonNull)

Example 3 with OpenRosaServerClient

use of org.odk.collect.android.openrosa.OpenRosaServerClient 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

NonNull (androidx.annotation.NonNull)3 Date (java.util.Date)3 Request (okhttp3.Request)3 Response (okhttp3.Response)3 OpenRosaServerClient (org.odk.collect.android.openrosa.OpenRosaServerClient)3 CaseInsensitiveEmptyHeaders (org.odk.collect.android.openrosa.CaseInsensitiveEmptyHeaders)2 CaseInsensitiveHeaders (org.odk.collect.android.openrosa.CaseInsensitiveHeaders)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 Headers (okhttp3.Headers)1 MediaType (okhttp3.MediaType)1 ResponseBody (okhttp3.ResponseBody)1 HttpGetResult (org.odk.collect.android.openrosa.HttpGetResult)1 HttpHeadResult (org.odk.collect.android.openrosa.HttpHeadResult)1 HttpPostResult (org.odk.collect.android.openrosa.HttpPostResult)1