}
closeQuietly(cacheOutputStream);
SchemeRegistry schemeRegistry = new SchemeRegistryBuilder(asConnectionParams).buildSchemeRegistry();
ClientConnectionManager httpConnectionManager = new BasicClientConnectionManager(schemeRegistry);
DefaultHttpClient httpClient = new DefaultHttpClient(httpConnectionManager);
HttpParams httpParams = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, SOCKET_CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParams, timeout);
if (credentials != null && !asConnectionParams.isClientcertAuthentication()) {
httpClient.getCredentialsProvider().setCredentials(
new AuthScope(asConnectionParams.getHost(), asConnectionParams.getPort()), credentials);
// If credentials were provided, we will first send a GET request to trigger the authentication challenge
// This allows to send the potentially big file only once to the server
// The typical resulting http exchange would be:
//
// GET without auth <- 401 (start auth challenge : the server will name the realm and the scheme)
// GET with auth <- 200
// POST big file
//
// Note this only works because we use SimpleHttpConnectionManager which maintains only one HttpConnection
//
// A better way to avoid uploading a big file twice would be to use the header "Expect: Continue"
// Unfortunately AS7 replies "100 Continue" even if authentication headers are not present yet
//
// There is no need to trigger digest authentication when client certification authentication is used
HttpGet triggerAuthRequest = new HttpGet(triggerAuthUri);
try {
// Send GET request in order to trigger authentication
// We don't check response code because we're not already uploading the file
httpClient.execute(triggerAuthRequest);
} catch (Exception ignore) {
// We don't stop trying upload if triggerAuthRequest raises exception
// See comment above
} finally {
triggerAuthRequest.abort();
}
}
String uploadURL = (asConnectionParams.isSecure() ? HTTPS_SCHEME : HTTP_SCHEME) + "://"
+ asConnectionParams.getHost() + ":" + asConnectionParams.getPort() + UPLOAD_URI;
HttpPost uploadRequest = new HttpPost(uploadUri);
try {
// Now upload file with multipart POST request
MultipartEntity multipartEntity = new MultipartEntity();
multipartEntity.addPart(filename, new FileBody(cacheFile));
uploadRequest.setEntity(multipartEntity);
HttpResponse uploadResponse = httpClient.execute(uploadRequest);
if (uploadResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
logUploadDoesNotEndWithHttpOkStatus(uploadResponse);
return null;
}
ObjectMapper objectMapper = new ObjectMapper();
InputStream responseBodyAsStream = uploadResponse.getEntity().getContent();
if (responseBodyAsStream == null) {
LOG.warn("POST request has no response body");
return objectMapper.readTree(EMPTY_JSON_TREE);
}
return objectMapper.readTree(responseBodyAsStream);
} catch (Exception e) {
LOG.error(e);
return null;
} finally {
// Release httpclient resources
uploadRequest.abort();
httpConnectionManager.shutdown();
// Delete cache file
deleteCacheFile();
}
}