Search in sources :

Example 16 with TaggedData

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

the class MavenBackingRepository method getMetadata.

RevisionMetadata getMetadata(Revision revision) throws Exception {
    File metafile = IO.getFile(local, revision.metadata(id));
    RevisionMetadata metadata = revisions.get(revision);
    TaggedData tag = fetch(revision.metadata(), metafile);
    if (tag.getState() == State.NOT_FOUND || tag.getState() == State.OTHER) {
        if (metadata == null) {
            metadata = new RevisionMetadata();
            revisions.put(revision, metadata);
        }
        return metadata;
    }
    if (metadata == null || tag.getState() == State.UPDATED) {
        metadata = MetadataParser.parseRevisionMetadata(metafile);
        revisions.put(revision, metadata);
    }
    return metadata;
}
Also used : TaggedData(aQute.bnd.service.url.TaggedData) File(java.io.File) RevisionMetadata(aQute.maven.provider.MetadataParser.RevisionMetadata)

Example 17 with TaggedData

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

the class MavenRemoteRepository method store.

public void store(File file, String path) throws Exception {
    int n = 0;
    URL url = new URL(base + path);
    SHA1 sha1 = SHA1.digest(file);
    MD5 md5 = MD5.digest(file);
    try (TaggedData go = client.build().put().upload(file).updateTag().asTag().go(url)) {
        switch(go.getState()) {
            case NOT_FOUND:
            case OTHER:
                throw new IOException("Could not store " + path + " from " + file + " with " + go);
            case UNMODIFIED:
            case UPDATED:
            default:
                break;
        }
    }
    try (TaggedData tag = client.build().put().upload(sha1.asHex()).asTag().go(new URL(base + path + ".sha1"))) {
    }
    try (TaggedData tag = client.build().put().upload(md5.asHex()).asTag().go(new URL(base + path + ".md5"))) {
    }
}
Also used : SHA1(aQute.libg.cryptography.SHA1) TaggedData(aQute.bnd.service.url.TaggedData) IOException(java.io.IOException) URL(java.net.URL) MD5(aQute.libg.cryptography.MD5)

Example 18 with TaggedData

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

the class MavenRemoteRepository method fetch.

public TaggedData fetch(String path, File file) throws Exception {
    URL url = new URL(base + path);
    int n = 0;
    while (true) try {
        logger.debug("Fetching {}", path);
        TaggedData tag = client.build().headers("User-Agent", "Bnd").useCache(file, DEFAULT_MAX_STALE).asTag().go(url);
        logger.debug("Fetched {}", tag);
        if (tag.getState() == State.UPDATED) {
            if (!path.endsWith("/maven-metadata.xml")) {
                URL shaUrl = new URL(base + path + ".sha1");
                URL md5Url = new URL(base + path + ".md5");
                String sha = client.build().asString().timeout(15000).go(shaUrl);
                if (sha != null) {
                    String fileSha = SHA1.digest(file).asHex();
                    checkDigest(fileSha, sha, file);
                } else {
                    String md5 = client.build().asString().timeout(15000).go(md5Url);
                    if (md5 != null) {
                        String fileMD5 = MD5.digest(file).asHex();
                        checkDigest(fileMD5, md5, file);
                    }
                }
            }
        }
        return tag;
    } catch (Exception e) {
        n++;
        if (n > 3)
            throw e;
        Thread.sleep(1000 * n);
    }
}
Also used : TaggedData(aQute.bnd.service.url.TaggedData) URL(java.net.URL) IOException(java.io.IOException) HttpRequestException(aQute.bnd.http.HttpRequestException)

Example 19 with TaggedData

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

the class Cache method download.

public byte[] download(URI uri) throws IOException {
    byte[] data;
    try (HttpClient client = new HttpClient()) {
        Pair<String, byte[]> cachedTag = cache.get(uri);
        if (cachedTag == null) {
            // Not previously cached
            TaggedData td = client.connectTagged(uri.toURL());
            if (td == null || td.isNotFound())
                throw new FileNotFoundException("Not found");
            data = IO.read(td.getInputStream());
            if (td.getTag() != null)
                cache.put(uri, new Pair<String, byte[]>(td.getTag(), data));
        } else {
            // Previously cached with an ETag
            TaggedData td = client.connectTagged(uri.toURL(), cachedTag.getFirst());
            if (td == null || td.isNotFound())
                throw new FileNotFoundException("Not found");
            if (td.getResponseCode() == 304) {
                // unchanged
                data = cachedTag.getSecond();
            } else {
                // changed
                data = IO.read(td.getInputStream());
                if (td.getTag() == null) {
                    // server now not giving an etag -> remove from cache
                    cache.remove(uri);
                } else {
                    // replace cache entry with new tag
                    cache.put(uri, new Pair<String, byte[]>(td.getTag(), data));
                }
            }
        }
        return data;
    } catch (IOException e) {
        throw e;
    } catch (Exception e) {
        throw new IOException(e);
    }
}
Also used : HttpClient(aQute.bnd.http.HttpClient) FileNotFoundException(java.io.FileNotFoundException) TaggedData(aQute.bnd.service.url.TaggedData) IOException(java.io.IOException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Pair(aQute.libg.tuple.Pair)

Example 20 with TaggedData

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

the class HttpClientServerTest method assertOk.

@SuppressWarnings("resource")
private void assertOk(String password, boolean verify) throws Exception {
    File log = new File(tmp, "log");
    Processor p = new Processor();
    p.setProperty("-connection-log", log.toURI().getPath());
    HttpClient hc = new HttpClient();
    hc.setLog(log);
    ConnectionSettings cs = new ConnectionSettings(p, hc);
    ServerDTO server = new ServerDTO();
    server.id = httpServer.getBaseURI().toString();
    server.verify = verify;
    if (password != null) {
        server.username = "user";
        server.password = password;
    }
    server.trust = Strings.join(httpServer.getTrustedCertificateFiles(IO.getFile("generated")));
    cs.add(server);
    System.out.println(httpServer.getBaseURI());
    URL url = password == null ? new URL(httpServer.getBaseURI() + "/get") : new URL(httpServer.getBaseURI() + "/basic-auth/user/good");
    TaggedData tag = hc.connectTagged(url);
    assertNotNull(tag);
    String s = IO.collect(tag.getInputStream());
    assertNotNull(s);
    assertTrue(s.trim().startsWith("{"));
    IO.copy(log, System.out);
}
Also used : ServerDTO(aQute.bnd.connection.settings.ServerDTO) Processor(aQute.bnd.osgi.Processor) HttpClient(aQute.bnd.http.HttpClient) TaggedData(aQute.bnd.service.url.TaggedData) File(java.io.File) ConnectionSettings(aQute.bnd.connection.settings.ConnectionSettings) URL(java.net.URL)

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