AuthScope.ANY_REALM, "BASIC");
cred = new UsernamePasswordCredentials(username + ":" + password);
urlToUse = new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getFile()).toExternalForm();
}
} catch (MalformedURLException e) {
throw new FileUpdaterException("Failed to configure base URL for authentication: " + e.getMessage(),
e);
}
if (doauth) {
client.getParams().setAuthenticationPreemptive(true);
client.getState().setCredentials(authscope, cred);
}
interaction.setClient(client);
interaction.setMethod(new GetMethod(urlToUse));
interaction.setFollowRedirects(true);
if (null != acceptHeader) {
interaction.setRequestHeader("Accept", acceptHeader);
} else {
interaction.setRequestHeader("Accept", "*/*");
}
if (useCaching) {
applyCacheHeaders(cacheProperties, interaction);
}
logger.debug("Making remote request: " + cleanUrl);
try {
resultCode = interaction.executeMethod();
reasonCode = interaction.getStatusText();
if (useCaching && HttpStatus.SC_NOT_MODIFIED == resultCode) {
logger.debug("Content NOT MODIFIED: file up to date");
} else if (HttpStatus.SC_OK == resultCode) {
determineContentType(interaction);
//write to file
FileOutputStream output=new FileOutputStream(destinationFile);
try{
Streams.copyStream(interaction.getResponseBodyAsStream(), output);
}finally{
output.close();
}
if (destinationFile.length() < 1) {
//file was empty!
if(!destinationFile.delete()) {
logger.warn("Failed to remove empty file: " + destinationFile.getAbsolutePath());
}
}
if (useCaching) {
cacheResponseInfo(interaction, cacheMetadata);
}
} else {
throw new FileUpdaterException(
"Unable to retrieve content: result code: " + resultCode + " " + reasonCode);
}
} catch (HttpException e) {
throw new FileUpdaterException(e);
} catch (IOException e) {
throw new FileUpdaterException(e);
} finally {
interaction.releaseConnection();
}
}