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;
}
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"))) {
}
}
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);
}
}
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);
}
}
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);
}
Aggregations