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);
}
}
Aggregations