Search in sources :

Example 1 with Config

use of org.openstack4j.core.transport.Config in project openstack4j by ContainX.

the class HttpCommand method populateHeaders.

private void populateHeaders() throws IOException {
    if (request.getConfig() != null && request.getConfig().getProxy() != null) {
        Config config = request.getConfig();
        Proxy proxy = new Proxy(Type.HTTP, new InetSocketAddress(config.getProxy().getRawHost(), config.getProxy().getPort()));
        connection = (HttpURLConnection) connectionUrl.openConnection(proxy);
    } else {
        connection = (HttpURLConnection) connectionUrl.openConnection();
    }
    connection.setRequestProperty("Content-Type", request.getContentType());
    connection.setRequestProperty("Accept", MediaType.JSON_UTF_8.toString());
    if (!request.hasHeaders()) {
        return;
    }
    for (Map.Entry<String, Object> h : request.getHeaders().entrySet()) {
        connection.setRequestProperty(h.getKey(), String.valueOf(h.getValue()));
    }
}
Also used : Config(org.openstack4j.core.transport.Config) Map(java.util.Map)

Example 2 with Config

use of org.openstack4j.core.transport.Config in project openstack4j by ContainX.

the class HttpCommand method initialize.

private void initialize() {
    OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();
    Config config = request.getConfig();
    if (config.getProxy() != null) {
        okHttpClientBuilder.proxy(new Proxy(Type.HTTP, new InetSocketAddress(config.getProxy().getRawHost(), config.getProxy().getPort())));
    }
    if (config.getConnectTimeout() > 0)
        okHttpClientBuilder.connectTimeout(config.getConnectTimeout(), TimeUnit.MILLISECONDS);
    if (config.getReadTimeout() > 0)
        okHttpClientBuilder.readTimeout(config.getReadTimeout(), TimeUnit.MILLISECONDS);
    if (config.isIgnoreSSLVerification()) {
        okHttpClientBuilder.hostnameVerifier(UntrustedSSL.getHostnameVerifier());
        okHttpClientBuilder.sslSocketFactory(UntrustedSSL.getSSLContext().getSocketFactory());
    }
    if (config.getSslContext() != null)
        okHttpClientBuilder.sslSocketFactory(config.getSslContext().getSocketFactory());
    if (config.getHostNameVerifier() != null)
        okHttpClientBuilder.hostnameVerifier(config.getHostNameVerifier());
    if (HttpLoggingFilter.isLoggingEnabled()) {
        okHttpClientBuilder.addInterceptor(new LoggingInterceptor());
    }
    client = okHttpClientBuilder.build();
    clientReq = new Request.Builder();
    populateHeaders(request);
    populateQueryParams(request);
}
Also used : Proxy(java.net.Proxy) OkHttpClient(okhttp3.OkHttpClient) Config(org.openstack4j.core.transport.Config) InetSocketAddress(java.net.InetSocketAddress) HttpRequest(org.openstack4j.core.transport.HttpRequest) Request(okhttp3.Request)

Example 3 with Config

use of org.openstack4j.core.transport.Config in project openstack4j by ContainX.

the class ServiceVersionTests method thatComputeV21IsReturned.

@Test
public void thatComputeV21IsReturned() {
    Config config = Config.newConfig().withResolver(LatestServiceVersionResolver.INSTANCE);
    assertTrue(session().useConfig(config).getEndpoint(ServiceType.COMPUTE).contains("/v2.1/"), "Endpoint was not version 2.1");
}
Also used : Config(org.openstack4j.core.transport.Config) AbstractTest(org.openstack4j.api.AbstractTest) Test(org.testng.annotations.Test)

Example 4 with Config

use of org.openstack4j.core.transport.Config in project openstack4j by ContainX.

the class ConfigTest method testUnequalConfigHostnameVerifier.

@Test
public void testUnequalConfigHostnameVerifier() {
    Config firstConfig = Config.newConfig();
    firstConfig.withHostnameVerifier(new HostnameVerifier() {

        @Override
        public boolean verify(String hostname, SSLSession session) {
            // TODO Auto-generated method stub
            return false;
        }
    });
    Config secondConfig = Config.newConfig();
    Assert.assertNotEquals(firstConfig, secondConfig);
}
Also used : Config(org.openstack4j.core.transport.Config) SSLSession(javax.net.ssl.SSLSession) HostnameVerifier(javax.net.ssl.HostnameVerifier) Test(org.testng.annotations.Test)

Example 5 with Config

use of org.openstack4j.core.transport.Config in project openstack4j by ContainX.

the class ConfigTest method testUnequalConfigSsl.

@Test
public void testUnequalConfigSsl() {
    Config firstConfig = Config.newConfig();
    try {
        SSLContext firstSslContext = SSLContext.getInstance("SSL");
        firstSslContext.init(null, new TrustManager[] { null }, new SecureRandom());
        firstConfig.withSSLContext(firstSslContext);
    } catch (Exception e) {
        e.printStackTrace();
    }
    Config secondConfig = Config.newConfig();
    try {
        SSLContext secondSslContext = SSLContext.getInstance("SSL");
        secondSslContext.init(null, new TrustManager[] { null }, new SecureRandom());
        secondConfig.withSSLContext(secondSslContext);
    } catch (Exception e) {
        e.printStackTrace();
    }
    Assert.assertNotEquals(firstConfig, secondConfig);
}
Also used : Config(org.openstack4j.core.transport.Config) SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) Test(org.testng.annotations.Test)

Aggregations

Config (org.openstack4j.core.transport.Config)5 Test (org.testng.annotations.Test)3 InetSocketAddress (java.net.InetSocketAddress)1 Proxy (java.net.Proxy)1 SecureRandom (java.security.SecureRandom)1 Map (java.util.Map)1 HostnameVerifier (javax.net.ssl.HostnameVerifier)1 SSLContext (javax.net.ssl.SSLContext)1 SSLSession (javax.net.ssl.SSLSession)1 OkHttpClient (okhttp3.OkHttpClient)1 Request (okhttp3.Request)1 AbstractTest (org.openstack4j.api.AbstractTest)1 HttpRequest (org.openstack4j.core.transport.HttpRequest)1