Search in sources :

Example 1 with WebLocation

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();
    }
}
Also used : HostConfiguration(org.apache.commons.httpclient.HostConfiguration) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) WebLocation(org.eclipse.mylyn.commons.net.WebLocation) HttpClient(org.apache.commons.httpclient.HttpClient) SubMonitor(org.eclipse.core.runtime.SubMonitor) GetMethod(org.apache.commons.httpclient.methods.GetMethod) IOException(java.io.IOException)

Example 2 with WebLocation

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();
    }
}
Also used : HeadMethod(org.apache.commons.httpclient.methods.HeadMethod) Header(org.apache.commons.httpclient.Header) DateParseException(org.apache.commons.httpclient.util.DateParseException) HostConfiguration(org.apache.commons.httpclient.HostConfiguration) WebLocation(org.eclipse.mylyn.commons.net.WebLocation) HttpClient(org.apache.commons.httpclient.HttpClient) SubMonitor(org.eclipse.core.runtime.SubMonitor) IOException(java.io.IOException)

Example 3 with WebLocation

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();
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) HostConfiguration(org.apache.commons.httpclient.HostConfiguration) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) WebLocation(org.eclipse.mylyn.commons.net.WebLocation) HttpClient(org.apache.commons.httpclient.HttpClient) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) SubMonitor(org.eclipse.core.runtime.SubMonitor) GetMethod(org.apache.commons.httpclient.methods.GetMethod) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)3 HostConfiguration (org.apache.commons.httpclient.HostConfiguration)3 HttpClient (org.apache.commons.httpclient.HttpClient)3 SubMonitor (org.eclipse.core.runtime.SubMonitor)3 WebLocation (org.eclipse.mylyn.commons.net.WebLocation)3 BufferedInputStream (java.io.BufferedInputStream)2 InputStream (java.io.InputStream)2 GetMethod (org.apache.commons.httpclient.methods.GetMethod)2 Header (org.apache.commons.httpclient.Header)1 HeadMethod (org.apache.commons.httpclient.methods.HeadMethod)1 DateParseException (org.apache.commons.httpclient.util.DateParseException)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1