Search in sources :

Example 36 with HttpClient

use of aQute.bnd.http.HttpClient in project bnd by bndtools.

the class OSGiRepositoryTest method testSimple.

public void testSimple() throws Exception {
    try (OSGiRepository r = new OSGiRepository()) {
        Map<String, String> map = new HashMap<>();
        map.put("locations", fnx.getBaseURI("/repo/minir5.xml").toString());
        map.put("cache", cache.getPath());
        map.put("max.stale", "10000");
        r.setProperties(map);
        Processor p = new Processor();
        HttpClient httpClient = new HttpClient();
        httpClient.setCache(cache);
        httpClient.setRegistry(p);
        p.addBasicPlugin(httpClient);
        p.setBase(ws);
        p.addBasicPlugin(Workspace.createStandaloneWorkspace(p, ws.toURI()));
        r.setRegistry(p);
        final AtomicInteger tasks = new AtomicInteger();
        p.addBasicPlugin(new ProgressPlugin() {

            @Override
            public Task startTask(final String name, int size) {
                System.out.println("Starting " + name);
                tasks.incrementAndGet();
                return new Task() {

                    @Override
                    public void worked(int units) {
                        System.out.println("Worked " + name + " " + units);
                    }

                    @Override
                    public void done(String message, Throwable e) {
                        System.out.println("Done " + name + " " + message);
                    }

                    @Override
                    public boolean isCanceled() {
                        return false;
                    }
                };
            }
        });
        assertEquals(0, tasks.get());
        File file = r.get("dummybundle", new Version("0"), null);
        assertNotNull(file);
        // 2 = index + file
        assertEquals(2, tasks.get());
        File file2 = r.get("dummybundle", new Version("0"), null);
        assertNotNull(file2);
        // second one should not have downloaded
        assertEquals(2, tasks.get());
        r.refresh();
        File file3 = r.get("dummybundle", new Version("0"), null);
        assertNotNull(file3);
        // second one should not have downloaded
        assertEquals(2, tasks.get());
    }
}
Also used : Processor(aQute.bnd.osgi.Processor) HashMap(java.util.HashMap) ProgressPlugin(aQute.bnd.service.progress.ProgressPlugin) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Version(aQute.bnd.version.Version) HttpClient(aQute.bnd.http.HttpClient) File(java.io.File)

Example 37 with HttpClient

use of aQute.bnd.http.HttpClient 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)

Example 38 with HttpClient

use of aQute.bnd.http.HttpClient in project bnd by bndtools.

the class HttpClientTest method testModifiedSince.

public void testModifiedSince() throws Exception {
    try (HttpClient hc = new HttpClient()) {
        TaggedData data = hc.build().get(TaggedData.class).ifModifiedSince(10000).go(httpServer.getBaseURI("etag/1234/20000"));
        assertNotNull(data);
        assertEquals("1234", data.getTag());
        assertEquals(200, data.getResponseCode());
    }
    try (HttpClient hc = new HttpClient()) {
        TaggedData data = hc.build().get(TaggedData.class).ifModifiedSince(20000).go(httpServer.getBaseURI("etag/1234/10000"));
        assertNotNull(data);
        assertEquals("1234", data.getTag());
        assertEquals(304, data.getResponseCode());
    }
}
Also used : HttpClient(aQute.bnd.http.HttpClient) TaggedData(aQute.bnd.service.url.TaggedData)

Example 39 with HttpClient

use of aQute.bnd.http.HttpClient in project bnd by bndtools.

the class IndexedReposWithComms method testBasicWorkspace.

public void testBasicWorkspace() throws Exception {
    HttpTestServer ht = http();
    try {
        createSecureSocks5();
        Workspace ws = Workspace.getWorkspace(aQute.lib.io.IO.getFile("workspaces/basic"));
        assertNotNull(ws);
        List<URLConnector> connectors = ws.getPlugins(URLConnector.class);
        assertNotNull(connectors);
        assertEquals(1, connectors.size());
        assertTrue(connectors.get(0) instanceof HttpClient);
        HttpClient hc = (HttpClient) connectors.get(0);
        InputStream connect = hc.connect(new URL(ht.getBaseURI() + "/basic-auth/user/good"));
        assertNotNull(connect);
        aQute.lib.io.IO.copy(connect, System.out);
        connect.close();
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.toString());
    } finally {
        ht.close();
    }
}
Also used : InputStream(java.io.InputStream) HttpClient(aQute.bnd.http.HttpClient) URLConnector(aQute.bnd.service.url.URLConnector) HttpTestServer(aQute.http.testservers.HttpTestServer) URL(java.net.URL) CloseSessionException(sockslib.server.listener.CloseSessionException) IOException(java.io.IOException) AuthenticationException(sockslib.common.AuthenticationException) Workspace(aQute.bnd.build.Workspace)

Example 40 with HttpClient

use of aQute.bnd.http.HttpClient in project bnd by bndtools.

the class HttpClientTest method testRedirectTooMany.

public void testRedirectTooMany() throws Exception {
    try (HttpClient hc = new HttpClient()) {
        TaggedData tag = hc.build().maxRedirects(3).get(TaggedData.class).go(httpServer.getBaseURI("redirect/200/200"));
        assertEquals(3, tag.getResponseCode() / 100);
    }
}
Also used : HttpClient(aQute.bnd.http.HttpClient) TaggedData(aQute.bnd.service.url.TaggedData)

Aggregations

HttpClient (aQute.bnd.http.HttpClient)46 TaggedData (aQute.bnd.service.url.TaggedData)18 File (java.io.File)14 Processor (aQute.bnd.osgi.Processor)13 URI (java.net.URI)9 ProgressPlugin (aQute.bnd.service.progress.ProgressPlugin)6 Version (aQute.bnd.version.Version)6 IOException (java.io.IOException)6 URL (java.net.URL)6 HashMap (java.util.HashMap)6 ConnectionSettings (aQute.bnd.connection.settings.ConnectionSettings)5 Workspace (aQute.bnd.build.Workspace)4 ServerDTO (aQute.bnd.connection.settings.ServerDTO)4 RepositoryPlugin (aQute.bnd.service.RepositoryPlugin)4 Config (aQute.http.testservers.HttpTestServer.Config)4 Resource (org.osgi.resource.Resource)4 Jar (aQute.bnd.osgi.Jar)3 Slf4jReporter (aQute.libg.reporter.slf4j.Slf4jReporter)3 MavenRepository (aQute.maven.provider.MavenRepository)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3