try {
boolean append = false;
if ("http".equals(lUrl.getProtocol())) {
HttpClient hClient = new HttpClient();
// CB TODO, get this from core preferences.
hClient.getParams().setSoTimeout(
NetPropertiesHandler.timeoutValue);
HostConfiguration hc = hClient.getHostConfiguration();
hc = setProxySetttings(hClient, hc);
gMethod = new GetMethod(lUrl.toExternalForm());
// Add a byte range header asking for partial content.
if (pDownload.getAttachment().isIncomplete()) {
append = true;
addByteRangeHeader(pDownload.getAttachment(), gMethod);
gMethod.setFollowRedirects(true);
} else {
gMethod.setFollowRedirects(false);
}
// Need to do redirects manually to get the filename for
// storage. We follow the redirections until a 200 OK.
// we break in case of error.
boolean lContinue = true;
while (lContinue) {
int status = hClient.executeMethod(gMethod);
switch (status) { // Switch the result.
case HttpStatus.SC_OK: {
lContinue = false;
}
break;
case HttpStatus.SC_MOVED_PERMANENTLY:
case HttpStatus.SC_MOVED_TEMPORARILY:
case HttpStatus.SC_SEE_OTHER:
case HttpStatus.SC_TEMPORARY_REDIRECT: {
lRedirect = true;
// The redirection code fails for
// localhost, use IP address as a workaround.
String redirectLocation;
Header locationHeader = gMethod
.getResponseHeader("location");
if (locationHeader != null) {
redirectLocation = locationHeader.getValue();
gMethod.setFollowRedirects(true);
lUrl = new URL(redirectLocation);
} else {
}
}
break;
case HttpStatus.SC_PARTIAL_CONTENT:
// sLog.info("(1) Partial download granted for: "
// + pUrl.toExternalForm());
// sLog.info("(2) Start at byte: "
// + gMethod.getRequestHeader("Range")
// .getValue());
lContinue = false;
break;
case HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE: {
// 28-07-2006, Duh? OK let's try again without
// Range.
// sLog.warn("(1) Partial download denied for: "
// + pUrl.toExternalForm());
Header lRHeader = gMethod
.getResponseHeader("Content-Range");
if (lRHeader != null) {
// sLog.warn("(2) The content-range is: "
// + lRHeader.getValue());
}
// sLog.warn(gMethod.getResponseBodyAsString());
Header h = gMethod.getRequestHeader("Range");
gMethod.removeRequestHeader(h);
append = false;
}
break;
case HttpStatus.SC_UNAUTHORIZED: {
// Retry with password.
hc = hClient.getHostConfiguration();
hc = setProxySetttings(hClient, hc);
hClient.getState()
.setCredentials(
new AuthScope(AuthScope.ANY_HOST,
AuthScope.ANY_PORT,
AuthScope.ANY_REALM),
getCredentials(pDownload