Search in sources :

Example 1 with Task

use of aQute.bnd.service.progress.ProgressPlugin.Task in project bnd by bndtools.

the class HttpClient method getTask.

ProgressPlugin.Task getTask(final HttpRequest<?> request) {
    final String name = (request.upload == null ? "Download " : "Upload ") + request.url;
    final int size = 100;
    final ProgressPlugin.Task task;
    final List<ProgressPlugin> progressPlugins = registry != null ? registry.getPlugins(ProgressPlugin.class) : null;
    if (progressPlugins != null && progressPlugins.size() > 1) {
        final List<ProgressPlugin.Task> multiplexedTasks = new ArrayList<>();
        for (ProgressPlugin progressPlugin : progressPlugins) {
            multiplexedTasks.add(progressPlugin.startTask(name, size));
        }
        task = new ProgressPlugin.Task() {

            @Override
            public void worked(int units) {
                for (ProgressPlugin.Task task : multiplexedTasks) {
                    task.worked(units);
                }
            }

            @Override
            public void done(String message, Throwable e) {
                for (ProgressPlugin.Task task : multiplexedTasks) {
                    task.done(message, e);
                }
            }

            @Override
            public boolean isCanceled() {
                for (ProgressPlugin.Task task : multiplexedTasks) {
                    if (task.isCanceled()) {
                        return true;
                    }
                }
                return false;
            }
        };
    } else if (progressPlugins != null && progressPlugins.size() == 1) {
        task = progressPlugins.get(0).startTask(name, size);
    } else {
        task = new ProgressPlugin.Task() {

            @Override
            public void worked(int units) {
            }

            @Override
            public void done(String message, Throwable e) {
            }

            @Override
            public boolean isCanceled() {
                return Thread.currentThread().isInterrupted();
            }
        };
    }
    return task;
}
Also used : Task(aQute.bnd.service.progress.ProgressPlugin.Task) Task(aQute.bnd.service.progress.ProgressPlugin.Task) ProgressPlugin(aQute.bnd.service.progress.ProgressPlugin) ArrayList(java.util.ArrayList)

Example 2 with Task

use of aQute.bnd.service.progress.ProgressPlugin.Task in project bnd by bndtools.

the class HttpClient method send0.

public TaggedData send0(final HttpRequest<?> request) throws Exception {
    final ProxySetup proxy = getProxySetup(request.url);
    final URLConnection con = getProxiedAndConfiguredConnection(request.url, proxy);
    final HttpURLConnection hcon = (HttpURLConnection) (con instanceof HttpURLConnection ? con : null);
    if (request.ifNoneMatch != null) {
        request.headers.put("If-None-Match", entitytag(request.ifNoneMatch));
    }
    if (request.ifMatch != null) {
        request.headers.put("If-Match", "\"" + entitytag(request.ifMatch));
    }
    if (request.ifModifiedSince > 0) {
        request.headers.put("If-Modified-Since", httpDateFormat().format(new Date(request.ifModifiedSince)));
    }
    if (request.ifUnmodifiedSince != 0) {
        request.headers.put("If-Unmodified-Since", httpDateFormat().format(new Date(request.ifUnmodifiedSince)));
    }
    setHeaders(request.headers, con);
    configureHttpConnection(request.verb, hcon);
    final ProgressPlugin.Task task = getTask(request);
    try {
        TaggedData td = connectWithProxy(proxy, new Callable<TaggedData>() {

            @Override
            public TaggedData call() throws Exception {
                return doConnect(request.upload, request.download, con, hcon, request, task);
            }
        });
        logger.debug("result {}", td);
        return td;
    } catch (Throwable t) {
        task.done("Failed " + t, t);
        throw t;
    }
}
Also used : Task(aQute.bnd.service.progress.ProgressPlugin.Task) HttpURLConnection(java.net.HttpURLConnection) ProgressPlugin(aQute.bnd.service.progress.ProgressPlugin) ProxySetup(aQute.bnd.service.url.ProxyHandler.ProxySetup) TaggedData(aQute.bnd.service.url.TaggedData) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) FileNotFoundException(java.io.FileNotFoundException) SocketTimeoutException(java.net.SocketTimeoutException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ProtocolException(java.net.ProtocolException)

Aggregations

ProgressPlugin (aQute.bnd.service.progress.ProgressPlugin)2 Task (aQute.bnd.service.progress.ProgressPlugin.Task)2 ProxySetup (aQute.bnd.service.url.ProxyHandler.ProxySetup)1 TaggedData (aQute.bnd.service.url.TaggedData)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 HttpURLConnection (java.net.HttpURLConnection)1 MalformedURLException (java.net.MalformedURLException)1 ProtocolException (java.net.ProtocolException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 URISyntaxException (java.net.URISyntaxException)1 URLConnection (java.net.URLConnection)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1