use of org.eclipse.mylyn.commons.net.WebLocation in project eclipse-integration-commons by spring-projects.
the class HttpClientTransportService method stream.
/**
* Read a web-based resource at the specified location using the given
* processor.
*
* @param location the web location of the content
* @param processor the processor that will handle content
* @param progressMonitor the monitor
* @throws IOException if a network or IO problem occurs
*/
public InputStream stream(java.net.URI uri, IProgressMonitor progressMonitor) throws CoreException {
WebLocation location = new WebLocation(uri.toString());
SubMonitor monitor = SubMonitor.convert(progressMonitor);
monitor.subTask(NLS.bind("Fetching {0}", location.getUrl()));
try {
HttpClient client = new HttpClient();
// $NON-NLS-1$
org.eclipse.mylyn.commons.net.WebUtil.configureHttpClient(client, "");
boolean success = false;
GetMethod method = new GetMethod(location.getUrl());
try {
HostConfiguration hostConfiguration = org.eclipse.mylyn.commons.net.WebUtil.createHostConfiguration(client, location, monitor);
int result = org.eclipse.mylyn.commons.net.WebUtil.execute(client, hostConfiguration, method, monitor);
if (result == HttpStatus.SC_OK) {
InputStream in = org.eclipse.mylyn.commons.net.WebUtil.getResponseBodyAsStream(method, monitor);
success = true;
return in;
} else {
throw toException(location, result);
}
} catch (IOException e) {
throw toException(location, e);
} finally {
if (!success) {
method.releaseConnection();
}
}
} finally {
monitor.done();
}
}
use of org.eclipse.mylyn.commons.net.WebLocation in project eclipse-integration-commons by spring-projects.
the class HttpClientTransportService method getLastModified.
/**
* Verify availability of resources at the given web locations. Normally
* this would be done using an HTTP HEAD.
*
* @param locations the locations of the resource to verify
* @param one indicate if only one of the resources must exist
* @param progressMonitor the monitor
* @return true if the resource exists
*/
public long getLastModified(java.net.URI uri, IProgressMonitor progressMonitor) throws CoreException {
WebLocation location = new WebLocation(uri.toString());
SubMonitor monitor = SubMonitor.convert(progressMonitor);
monitor.subTask(NLS.bind("Fetching {0}", location.getUrl()));
try {
HttpClient client = new HttpClient();
// $NON-NLS-1$
org.eclipse.mylyn.commons.net.WebUtil.configureHttpClient(client, "");
HeadMethod method = new HeadMethod(location.getUrl());
try {
HostConfiguration hostConfiguration = org.eclipse.mylyn.commons.net.WebUtil.createHostConfiguration(client, location, monitor);
int result = org.eclipse.mylyn.commons.net.WebUtil.execute(client, hostConfiguration, method, monitor);
if (result == HttpStatus.SC_OK) {
// $NON-NLS-1$
Header lastModified = method.getResponseHeader("Last-Modified");
if (lastModified != null) {
try {
return DateUtil.parseDate(lastModified.getValue()).getTime();
} catch (DateParseException e) {
// fall through
}
}
return 0;
} else {
throw toException(location, result);
}
} catch (IOException e) {
throw toException(location, e);
} finally {
method.releaseConnection();
}
} finally {
monitor.done();
}
}
use of org.eclipse.mylyn.commons.net.WebLocation in project eclipse-integration-commons by spring-projects.
the class HttpClientTransportService method download.
/**
* Download an HTTP-based resource
*
* @param target the target file to which the content is saved
* @param location the web location of the content
* @param monitor the monitor
* @throws IOException if a network or IO problem occurs
*/
public void download(java.net.URI uri, OutputStream out, IProgressMonitor progressMonitor) throws CoreException {
WebLocation location = new WebLocation(uri.toString());
SubMonitor monitor = SubMonitor.convert(progressMonitor);
monitor.subTask(NLS.bind("Fetching {0}", location.getUrl()));
try {
HttpClient client = new HttpClient();
// $NON-NLS-1$
org.eclipse.mylyn.commons.net.WebUtil.configureHttpClient(client, "");
GetMethod method = new GetMethod(location.getUrl());
try {
HostConfiguration hostConfiguration = org.eclipse.mylyn.commons.net.WebUtil.createHostConfiguration(client, location, monitor);
int result = org.eclipse.mylyn.commons.net.WebUtil.execute(client, hostConfiguration, method, monitor);
if (result == HttpStatus.SC_OK) {
long total = method.getResponseContentLength();
if (total != -1) {
monitor.setWorkRemaining((int) total);
}
InputStream in = org.eclipse.mylyn.commons.net.WebUtil.getResponseBodyAsStream(method, monitor);
try {
in = new BufferedInputStream(in);
byte[] buffer = new byte[BUFFER_SIZE];
int len;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
if (total != -1) {
monitor.worked(len);
} else {
monitor.worked(1);
monitor.setWorkRemaining(10000);
}
if (monitor.isCanceled()) {
// cancellation
throw new OperationCanceledException();
}
}
} catch (OperationCanceledException e) {
// the download
throw toException(location, result);
} catch (IOException e) {
// the network
throw toException(location, 500);
} finally {
in.close();
}
} else {
throw toException(location, result);
}
} finally {
method.releaseConnection();
}
} catch (IOException e) {
throw toException(location, e);
} finally {
monitor.done();
}
}
Aggregations