Search in sources :

Example 21 with TaggedData

use of aQute.bnd.service.url.TaggedData in project bnd by bndtools.

the class OSGiIndex method isStale.

/**
	 * Check any of the URL indexes are stale.
	 * 
	 * @return
	 * @throws Exception
	 */
boolean isStale() throws Exception {
    final Deferred<List<Void>> freshness = new Deferred<>();
    List<Promise<Void>> promises = new ArrayList<>(getURIs().size());
    for (final URI uri : getURIs()) {
        if (freshness.getPromise().isDone()) {
            // early exit if staleness already detected
            break;
        }
        try {
            Promise<TaggedData> async = client.build().useCache().asTag().async(uri);
            promises.add(async.then(new Success<TaggedData, Void>() {

                @Override
                public Promise<Void> call(Promise<TaggedData> resolved) throws Exception {
                    switch(resolved.getValue().getState()) {
                        case OTHER:
                            // in the offline case
                            // ignore might be best here
                            logger.debug("Could not verify {}", uri);
                            break;
                        case UNMODIFIED:
                            break;
                        case NOT_FOUND:
                        case UPDATED:
                        default:
                            logger.debug("Found {} to be stale", uri);
                            freshness.fail(new Exception("stale"));
                    }
                    return null;
                }
            }, new Failure() {

                @Override
                public void fail(Promise<?> resolved) throws Exception {
                    logger.debug("Could not verify {}: {}", uri, resolved.getFailure());
                    freshness.fail(resolved.getFailure());
                }
            }));
        } catch (Exception e) {
            logger.debug("Checking stale status: {}: {}", uri, e);
        }
    }
    // Resolve when all uris checked
    Promise<List<Void>> all = Promises.all(promises);
    freshness.resolveWith(all);
    // Block until freshness is resolved
    return freshness.getPromise().getFailure() != null;
}
Also used : Deferred(org.osgi.util.promise.Deferred) ArrayList(java.util.ArrayList) TaggedData(aQute.bnd.service.url.TaggedData) URI(java.net.URI) Success(org.osgi.util.promise.Success) Promise(org.osgi.util.promise.Promise) ArrayList(java.util.ArrayList) List(java.util.List) Failure(org.osgi.util.promise.Failure)

Example 22 with TaggedData

use of aQute.bnd.service.url.TaggedData in project bnd by bndtools.

the class HttpClient method doConnect.

private TaggedData doConnect(Object put, Type ref, final URLConnection con, final HttpURLConnection hcon, HttpRequest<?> request, ProgressPlugin.Task task) throws IOException, Exception {
    if (put != null) {
        task.worked(1);
        doOutput(put, con, request);
    } else
        logger.debug("{} {}", request.verb, request.url);
    if (request.timeout > 0) {
        con.setConnectTimeout((int) request.timeout * 10);
        con.setReadTimeout((int) (5000 > request.timeout ? request.timeout : 5000));
    } else {
        con.setConnectTimeout(120000);
        con.setReadTimeout(60000);
    }
    try {
        if (hcon == null) {
            // not http
            try {
                con.connect();
                InputStream in = con.getInputStream();
                return new TaggedData(con, in, request.useCacheFile);
            } catch (FileNotFoundException e) {
                task.done("file not found", e);
                return new TaggedData(con.getURL().toURI(), 404, request.useCacheFile);
            }
        }
        int code = hcon.getResponseCode();
        if (code == -1)
            System.out.println("WTF?");
        if (code == HttpURLConnection.HTTP_MOVED_TEMP || code == HttpURLConnection.HTTP_MOVED_PERM || code == HttpURLConnection.HTTP_SEE_OTHER) {
            if (request.redirects-- > 0) {
                String location = hcon.getHeaderField("Location");
                request.url = new URL(location);
                task.done("redirected", null);
                return send0(request);
            }
        }
        if (isUpdateInfo(con, request, code)) {
            File file = (File) request.upload;
            String etag = con.getHeaderField("ETag");
            try (Info info = cache.get(file, con.getURL().toURI())) {
                info.update(etag);
            }
        }
        if ((code / 100) != 2) {
            task.done("finished", null);
            return new TaggedData(con, null, request.useCacheFile);
        }
        // Do not enclose in resource try! InputStream is potentially
        // used
        // later
        InputStream xin = con.getInputStream();
        InputStream in = handleContentEncoding(hcon, xin);
        in = createProgressWrappedStream(in, con.toString(), con.getContentLength(), task, request.timeout);
        return new TaggedData(con, in, request.useCacheFile);
    } catch (SocketTimeoutException ste) {
        task.done(ste.toString(), null);
        return new TaggedData(request.url.toURI(), HttpURLConnection.HTTP_GATEWAY_TIMEOUT, request.useCacheFile);
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) GZIPInputStream(java.util.zip.GZIPInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) TaggedData(aQute.bnd.service.url.TaggedData) Info(aQute.bnd.http.URLCache.Info) File(java.io.File) URL(java.net.URL)

Example 23 with TaggedData

use of aQute.bnd.service.url.TaggedData in project bnd by bndtools.

the class MavenRepository method fetch.

private State fetch(List<MavenBackingRepository> mbrs, String remotePath, File file) throws Exception {
    State error = State.NOT_FOUND;
    for (MavenBackingRepository mbr : mbrs) {
        TaggedData fetch = mbr.fetch(remotePath, file);
        switch(fetch.getState()) {
            case NOT_FOUND:
                break;
            case OTHER:
                error = State.OTHER;
                logger.error("Fetching artifact gives error {}", remotePath);
                break;
            case UNMODIFIED:
            case UPDATED:
                return fetch.getState();
        }
    }
    return error;
}
Also used : State(aQute.bnd.service.url.State) TaggedData(aQute.bnd.service.url.TaggedData)

Example 24 with TaggedData

use of aQute.bnd.service.url.TaggedData in project bnd by bndtools.

the class HttpConnectorTest method testConnectTagModified.

public static void testConnectTagModified() throws Exception {
    DefaultURLConnector connector = new DefaultURLConnector();
    TaggedData data = connector.connectTagged(new URL(getUrl(true) + "bundles/dummybundle.jar"), "00000000");
    assertNotNull("Data should be non-null because ETag was different", data);
    data.getInputStream().close();
    assertEquals("ETag is incorrect", EXPECTED_ETAG, data.getTag());
}
Also used : TaggedData(aQute.bnd.service.url.TaggedData) URL(java.net.URL)

Example 25 with TaggedData

use of aQute.bnd.service.url.TaggedData in project bnd by bndtools.

the class MavenFileRepository method fetch.

@Override
public TaggedData fetch(String path, File dest) throws Exception {
    File source = getFile(path);
    if (source.isFile()) {
        IO.mkdirs(dest.getParentFile());
        IO.copy(source, dest);
        return new TaggedData(toURI(path), 200, dest);
    } else {
        return new TaggedData(toURI(path), 404, dest);
    }
}
Also used : TaggedData(aQute.bnd.service.url.TaggedData) File(java.io.File)

Aggregations

TaggedData (aQute.bnd.service.url.TaggedData)35 HttpClient (aQute.bnd.http.HttpClient)18 URL (java.net.URL)15 File (java.io.File)7 IOException (java.io.IOException)6 Processor (aQute.bnd.osgi.Processor)5 URISyntaxException (java.net.URISyntaxException)4 ConnectionSettings (aQute.bnd.connection.settings.ConnectionSettings)3 ServerDTO (aQute.bnd.connection.settings.ServerDTO)3 ProgressPlugin (aQute.bnd.service.progress.ProgressPlugin)3 FileNotFoundException (java.io.FileNotFoundException)3 URI (java.net.URI)3 ProxyDTO (aQute.bnd.connection.settings.ProxyDTO)2 HttpRequestException (aQute.bnd.http.HttpRequestException)2 Info (aQute.bnd.http.URLCache.Info)2 InputStream (java.io.InputStream)2 HttpURLConnection (java.net.HttpURLConnection)2 SocketTimeoutException (java.net.SocketTimeoutException)2 URLConnection (java.net.URLConnection)2 HashMap (java.util.HashMap)2