use of aQute.bnd.service.url.TaggedData in project bnd by bndtools.
the class HttpClientProxyTest method assertSocks5Proxy.
@SuppressWarnings("resource")
void assertSocks5Proxy(String password, boolean authenticationCalled) throws MalformedURLException, Exception {
Processor p = new Processor();
p.setProperty("-connectionsettings", "" + false);
HttpClient hc = new HttpClient();
ConnectionSettings cs = new ConnectionSettings(p, hc);
if (socks5Proxy != null) {
ProxyDTO proxy = new ProxyDTO();
proxy.active = true;
proxy.host = "localhost";
proxy.port = socksProxyPort;
proxy.protocol = Type.SOCKS;
if (password != null) {
proxy.username = "proxyuser";
proxy.password = password;
}
hc.addProxyHandler(ConnectionSettings.createProxyHandler(proxy));
}
ServerDTO server = new ServerDTO();
server.id = httpTestServer.getBaseURI().toString();
server.verify = false;
server.trust = Strings.join(httpTestServer.getTrustedCertificateFiles(IO.getFile("generated")));
cs.add(server);
URL url = new URL(httpTestServer.getBaseURI() + "/get-tag/ABCDEFGH");
TaggedData tag = hc.connectTagged(url);
assertNotNull(tag);
assertEquals("ABCDEFGH", tag.getTag());
String s = IO.collect(tag.getInputStream());
assertNotNull(s);
assertTrue(s.trim().startsWith("{"));
// assertTrue(this.authenticationCalled.get() == authenticationCalled);
}
use of aQute.bnd.service.url.TaggedData in project bnd by bndtools.
the class HttpClientTest method testCachingMultipleFetch.
public void testCachingMultipleFetch() throws Exception {
try (HttpClient hc = new HttpClient()) {
hc.setCache(tmp);
Promise<TaggedData> a = hc.build().useCache().age(1, TimeUnit.DAYS).asTag().async(httpServer.getBaseURI("mftch"));
Thread.sleep(100);
Promise<TaggedData> b = hc.build().useCache().age(1, TimeUnit.DAYS).asTag().async(httpServer.getBaseURI("mftch"));
TaggedData ta = a.getValue();
assertEquals("FOO", ta.getTag());
assertEquals(200, ta.getResponseCode());
assertEquals(true, ta.hasPayload());
assertEquals(true, ta.isOk());
assertEquals(State.UPDATED, ta.getState());
TaggedData tb = b.getValue();
assertEquals("FOO", tb.getTag());
assertEquals(304, tb.getResponseCode());
assertEquals(false, tb.hasPayload());
assertEquals(State.UNMODIFIED, tb.getState());
System.out.println(a.getValue());
System.out.println(b.getValue());
}
}
use of aQute.bnd.service.url.TaggedData in project bnd by bndtools.
the class HttpClientCacheTest method testPrivateFile.
/**
* Use the cached form but use our own file, not one from the central cache
*
* @throws Exception
*/
public void testPrivateFile() throws Exception {
try (HttpClient client = new HttpClient()) {
client.setCache(new File(tmp, "cache"));
etag = "1234";
File t1 = new File(tmp, "abc.txt");
//
// File does not exist
//
assertFalse("File should not exist", t1.isFile());
TaggedData tag = client.build().useCache(t1).asTag().go(new URI(httpServer.getBaseURI() + "/testetag"));
assertTrue("Expected the file to be created (not unmodified)", tag.isOk());
assertTrue("Just created the file", t1.isFile());
assertEquals("Should be the tag we set", "1234", tag.getTag());
tag = client.build().useCache(t1, 100000).asTag().go(new URI(httpServer.getBaseURI() + "/testetag"));
assertEquals("Expected file to be 'fresh'", State.UNMODIFIED, tag.getState());
tag = client.build().useCache(t1).asTag().go(new URI(httpServer.getBaseURI() + "/testetag"));
assertTrue("Should have checked so we should have unmodified", tag.isNotModified());
etag = "5678";
tag = client.build().useCache(t1, 100000).asTag().go(new URI(httpServer.getBaseURI() + "/testetag"));
assertEquals("Since it is still fresh, we expect no new fetch", State.UNMODIFIED, tag.getState());
tag = client.build().useCache(t1).asTag().go(new URI(httpServer.getBaseURI() + "/testetag"));
assertEquals("We should have fetched it with the new etag", "5678", tag.getTag());
assertTrue("And it should have been modified", tag.isOk());
String s = client.build().useCache(t1).get(String.class).go(new URI(httpServer.getBaseURI() + "/testetag"));
assertEquals("Content check", "5678", s);
byte[] b = client.build().useCache(t1).get(byte[].class).go(new URI(httpServer.getBaseURI() + "/testetag"));
assertTrue("Content check", Arrays.equals("5678".getBytes(), b));
}
}
use of aQute.bnd.service.url.TaggedData in project bnd by bndtools.
the class HttpClientProxyTest method assertHttpProxy.
@SuppressWarnings("resource")
void assertHttpProxy(String password, boolean authenticationCalled) throws MalformedURLException, Exception {
Processor p = new Processor();
p.setProperty("-connectionsettings", "" + false);
HttpClient hc = new HttpClient();
ConnectionSettings cs = new ConnectionSettings(p, hc);
if (httpProxy != null) {
ProxyDTO proxy = new ProxyDTO();
proxy.active = true;
proxy.host = "localhost";
proxy.port = httpProxyPort;
proxy.protocol = Type.HTTP;
if (password != null) {
proxy.username = "proxyuser";
proxy.password = password;
}
hc.addProxyHandler(ConnectionSettings.createProxyHandler(proxy));
}
ServerDTO server = new ServerDTO();
server.id = httpTestServer.getBaseURI().toString();
server.verify = false;
server.trust = Strings.join(httpTestServer.getTrustedCertificateFiles(IO.getFile("generated")));
cs.add(server);
URL url = new URL(httpTestServer.getBaseURI() + "/get-tag/ABCDEFGH");
TaggedData tag = hc.connectTagged(url);
assertNotNull(tag);
if (tag.getState() != State.OTHER)
assertEquals("ABCDEFGH", tag.getTag());
String s = IO.collect(tag.getInputStream());
assertNotNull(s);
assertTrue(s.trim().startsWith("{"));
assertTrue(this.authenticationCalled.get() == authenticationCalled);
}
use of aQute.bnd.service.url.TaggedData in project bnd by bndtools.
the class DefaultURLConnector method connectTagged.
public TaggedData connectTagged(URL url, String tag, Set<String> loopDetect) throws IOException {
try {
TaggedData result;
loopDetect.add(url.toString());
URLConnection connection = url.openConnection();
try {
if (disableServerVerify)
HttpsUtil.disableServerVerification(connection);
} catch (GeneralSecurityException e) {
if (reporter != null)
reporter.error("Error attempting to disable SSL server certificate verification: %s", e);
throw new IOException("Error attempting to disable SSL server certificate verification.");
}
if (connection instanceof HttpURLConnection) {
// Turn on caching and send the ETag
HttpURLConnection httpConnection = (HttpURLConnection) connection;
httpConnection.setUseCaches(true);
if (tag != null)
httpConnection.setRequestProperty(HEADER_IF_NONE_MATCH, tag);
httpConnection.setInstanceFollowRedirects(false);
httpConnection.connect();
int responseCode = httpConnection.getResponseCode();
if (responseCode == RESPONSE_NOT_MODIFIED) {
result = null;
httpConnection.disconnect();
} else if (responseCode >= 300 && responseCode < 400) {
String location = httpConnection.getHeaderField(HEADER_LOCATION);
if (location == null)
throw new IOException("HTTP server returned redirect status but Location header was missing.");
try {
URL resolved = url.toURI().resolve(location).toURL();
if (reporter != null)
reporter.warning("HTTP address redirected from %s to %s", url.toString(), resolved.toString());
if (loopDetect.contains(resolved.toString()))
throw new IOException(String.format("Detected loop in HTTP redirect from '%s' to '%s'.", url, resolved));
if (Thread.currentThread().isInterrupted())
throw new IOException("Interrupted");
result = connectTagged(resolved, tag, loopDetect);
} catch (URISyntaxException e) {
throw new IOException(String.format("Failed to resolve location '%s' against origin URL: %s", location, url), e);
}
} else {
String responseTag = httpConnection.getHeaderField(HEADER_ETAG);
// TODO: get content-size from the http header
InputStream stream = createProgressWrappedStream(connection.getInputStream(), "Downloading " + url, -1);
result = new TaggedData(connection, stream);
}
} else {
// Non-HTTP so ignore all this tagging malarky
InputStream stream = createProgressWrappedStream(connection.getInputStream(), "Downloading " + url, -1);
result = new TaggedData(connection, stream);
}
return result;
} catch (IOException e) {
throw e;
} catch (Exception e) {
throw new IOException(e);
}
}
Aggregations