Search in sources :

Example 1 with HttpsVerification

use of aQute.bnd.url.HttpsVerification in project bnd by bndtools.

the class HttpClientTest method testRedirectURL.

public void testRedirectURL() throws Exception {
    try (HttpClient hc = new HttpClient()) {
        HttpsVerification httpsVerification = new HttpsVerification(httpsServer.getCertificateChain(), false, hc.getReporter());
        hc.addURLConnectionHandler(httpsVerification);
        URI uri = httpsServer.getBaseURI("get");
        URL go = httpServer.getBaseURI("xlocation").toURL();
        TaggedData tag = hc.build().maxRedirects(3).get(TaggedData.class).headers("XLocation", uri.toString()).go(go);
        assertNotNull(tag);
        assertEquals(200, tag.getResponseCode());
    }
}
Also used : HttpsVerification(aQute.bnd.url.HttpsVerification) HttpClient(aQute.bnd.http.HttpClient) TaggedData(aQute.bnd.service.url.TaggedData) URI(java.net.URI) URL(java.net.URL)

Example 2 with HttpsVerification

use of aQute.bnd.url.HttpsVerification in project bnd by bndtools.

the class ConnectionSettings method createUrlConnectionHandler.

public URLConnectionHandler createUrlConnectionHandler(ServerDTO serverDTO) {
    final Glob match = new Glob(serverDTO.match == null ? serverDTO.id : serverDTO.match);
    final BasicAuthentication basic = getBasicAuthentication(serverDTO.username, serverDTO.password);
    final HttpsVerification https = new HttpsVerification(serverDTO.trust, serverDTO.verify, getParent());
    return new URLConnectionHandler() {

        @Override
        public boolean matches(URL url) {
            String scheme = url.getProtocol().toLowerCase();
            StringBuilder address = new StringBuilder();
            address.append(scheme).append("://").append(url.getHost());
            if (url.getPort() > 0 && url.getPort() != url.getDefaultPort())
                address.append(":").append(url.getPort());
            return match.matcher(address).matches();
        }

        @Override
        public void handle(URLConnection connection) throws Exception {
            if (basic != null)
                basic.handle(connection);
            if (isHttps(connection) && https != null) {
                https.handle(connection);
            }
        }

        boolean isHttps(URLConnection connection) {
            return "https".equalsIgnoreCase(connection.getURL().getProtocol());
        }

        public String toString() {
            return "Server [ match=" + match + ", basic=" + basic + ", https=" + https + "]";
        }
    };
}
Also used : URLConnectionHandler(aQute.bnd.service.url.URLConnectionHandler) HttpsVerification(aQute.bnd.url.HttpsVerification) Glob(aQute.libg.glob.Glob) BasicAuthentication(aQute.bnd.url.BasicAuthentication) URL(java.net.URL) URLConnection(java.net.URLConnection)

Aggregations

HttpsVerification (aQute.bnd.url.HttpsVerification)2 URL (java.net.URL)2 HttpClient (aQute.bnd.http.HttpClient)1 TaggedData (aQute.bnd.service.url.TaggedData)1 URLConnectionHandler (aQute.bnd.service.url.URLConnectionHandler)1 BasicAuthentication (aQute.bnd.url.BasicAuthentication)1 Glob (aQute.libg.glob.Glob)1 URI (java.net.URI)1 URLConnection (java.net.URLConnection)1