Search in sources :

Example 1 with UncheckedFiles

use of org.opendatakit.briefcase.reused.UncheckedFiles in project briefcase by opendatakit.

the class CommonsHttp method uncheckedExecute.

private <T> Response<T> uncheckedExecute(Request<T> request, Executor executor) {
    if (request.ignoreCookies())
        cookieStore.clear();
    // Get an Apache Commons HTTPClient request and set some reasonable timeouts
    org.apache.http.client.fluent.Request commonsRequest = getCommonsRequest(request);
    // TODO v2.0 remove this header, since this is not a concern of this class
    if (!request.headers.containsKey("X-OpenRosa-Version")) {
        commonsRequest.addHeader("X-OpenRosa-Version", "1.0");
    }
    request.headers.forEach(commonsRequest::addHeader);
    // Set the request's body if it's a POST request
    if (request.getMethod() == POST) {
        HttpEntity body;
        if (request.isMultipart()) {
            MultipartEntityBuilder bodyBuilder = MultipartEntityBuilder.create();
            for (MultipartMessage part : request.multipartMessages) bodyBuilder = bodyBuilder.addPart(part.getName(), new InputStreamBody(part.getBody(), ContentType.create(part.getContentType()), part.getAttachmentName()));
            body = makeRepeatable(bodyBuilder.build());
        } else {
            body = makeRepeatable(buildBasicEntity(request.getBody()));
        }
        commonsRequest.body(body);
    }
    try {
        // Send the request and handle the response
        return executor.execute(commonsRequest).handleResponse(res -> Response.from(request, res));
    } catch (HttpHostConnectException e) {
        throw new HttpException("Connection refused", e);
    } catch (SocketTimeoutException | ConnectTimeoutException e) {
        throw new HttpException("The connection has timed out", e);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } finally {
        request.ifBody(UncheckedFiles::closeInputStream);
        if (request.multipartMessages != null)
            request.multipartMessages.stream().map(MultipartMessage::getBody).forEach(UncheckedFiles::closeInputStream);
    }
}
Also used : MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) HttpEntity(org.apache.http.HttpEntity) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) UncheckedFiles(org.opendatakit.briefcase.reused.UncheckedFiles) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) SocketTimeoutException(java.net.SocketTimeoutException) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) InputStreamBody(org.apache.http.entity.mime.content.InputStreamBody) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Aggregations

IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 HttpEntity (org.apache.http.HttpEntity)1 ConnectTimeoutException (org.apache.http.conn.ConnectTimeoutException)1 HttpHostConnectException (org.apache.http.conn.HttpHostConnectException)1 BasicHttpEntity (org.apache.http.entity.BasicHttpEntity)1 BufferedHttpEntity (org.apache.http.entity.BufferedHttpEntity)1 MultipartEntityBuilder (org.apache.http.entity.mime.MultipartEntityBuilder)1 InputStreamBody (org.apache.http.entity.mime.content.InputStreamBody)1 UncheckedFiles (org.opendatakit.briefcase.reused.UncheckedFiles)1