Example usage for com.squareup.okhttp OkHttpClient OkHttpClient

List of usage examples for com.squareup.okhttp OkHttpClient OkHttpClient

Introduction

In this page you can find the example usage for com.squareup.okhttp OkHttpClient OkHttpClient.

Prototype

public OkHttpClient() 

Source Link

Usage

From source file:com.near.chimerarevo.utils.OkHttpUtils.java

License:Apache License

public static OkHttpClient getInstance() {
    if (client == null)
        client = new OkHttpClient();

    client.setConnectTimeout(15, TimeUnit.SECONDS);
    client.setReadTimeout(15, TimeUnit.SECONDS);
    client.setWriteTimeout(15, TimeUnit.SECONDS);

    return client;
}

From source file:com.netflix.spinnaker.clouddriver.artifacts.github.GitHubArtifactConfiguration.java

License:Apache License

@Bean
OkHttpClient okHttpClient() {
    return new OkHttpClient();
}

From source file:com.netflix.spinnaker.clouddriver.artifacts.maven.MavenArtifactCredentialsTest.java

License:Apache License

private void assertResolvable(WireMockServer server, String version, String expectedVersion,
        @Nullable String expectedSnapshotVersion) {
    String jarUrl = "/com/test/app/"
            + (expectedSnapshotVersion == null ? expectedVersion : expectedSnapshotVersion) + "/app-"
            + expectedVersion + ".jar";

    server.stubFor(any(urlEqualTo(jarUrl)).willReturn(aResponse().withBody(expectedVersion)));

    MavenArtifactAccount account = new MavenArtifactAccount();
    account.setRepositoryUrl(server.baseUrl());

    Artifact artifact = new Artifact();
    artifact.setReference("com.test:app:" + version);

    assertThat(new MavenArtifactCredentials(account, new OkHttpClient()).download(artifact))
            .hasSameContentAs(new ByteArrayInputStream(expectedVersion.getBytes(StandardCharsets.UTF_8)));

    assertThat(server.findUnmatchedRequests().getRequests()).isEmpty();
}

From source file:com.netflix.spinnaker.clouddriver.cloudfoundry.client.HttpCloudFoundryClient.java

License:Apache License

public HttpCloudFoundryClient(String account, String appsManagerUri, String metricsUri, String apiHost,
        String user, String password) {
    this.apiHost = apiHost;
    this.user = user;
    this.password = password;

    this.okHttpClient = new OkHttpClient();
    okHttpClient.interceptors().add(this::createRetryInterceptor);

    okHttpClient.setHostnameVerifier((s, sslSession) -> true);

    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        @Override/*from   www .  jav  a  2s  . c  om*/
        public void checkClientTrusted(X509Certificate[] x509Certificates, String s) {
        }

        @Override
        public void checkServerTrusted(X509Certificate[] x509Certificates, String s) {
        }

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }
    } };

    ObjectMapper mapper = new ObjectMapper();
    mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
    mapper.registerModule(new JavaTimeModule());

    this.jacksonConverter = new JacksonConverter(mapper);

    SSLContext sslContext;
    try {
        sslContext = SSLContext.getInstance("SSL");
        sslContext.init(null, trustAllCerts, new SecureRandom());
    } catch (KeyManagementException | NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }

    okHttpClient.setSslSocketFactory(sslContext.getSocketFactory());

    this.uaaService = new RestAdapter.Builder()
            .setEndpoint("https://" + apiHost.replaceAll("^api\\.", "login."))
            .setClient(new OkClient(okHttpClient)).setConverter(jacksonConverter).build()
            .create(AuthenticationService.class);

    this.organizations = new Organizations(createService(OrganizationService.class));
    this.spaces = new Spaces(createService(SpaceService.class), organizations);
    this.applications = new Applications(account, appsManagerUri, metricsUri,
            createService(ApplicationService.class), spaces);
    this.domains = new Domains(createService(DomainService.class), organizations);
    this.serviceInstances = new ServiceInstances(createService(ServiceInstanceService.class), organizations,
            spaces);
    this.routes = new Routes(account, createService(RouteService.class), applications, domains, spaces);
}

From source file:com.netflix.spinnaker.clouddriver.docker.registry.api.v2.client.DefaultDockerOkClientProvider.java

License:Apache License

@Override
public OkClient provide(String address, long timeoutMs, boolean insecure) {
    OkHttpClient client = new OkHttpClient();
    client.setReadTimeout(timeoutMs, TimeUnit.MILLISECONDS);

    if (insecure) {
        SSLContext sslContext;// ww  w . j av  a  2 s  . co m
        try {
            sslContext = SSLContext.getInstance("SSL");
            TrustManager[] trustManagers = { new TrustAllX509TrustManager() };
            sslContext.init(null, trustManagers, new SecureRandom());
        } catch (NoSuchAlgorithmException | KeyManagementException e) {
            throw new IllegalStateException("Failed configuring insecure SslSocketFactory", e);
        }
        client.setSslSocketFactory(sslContext.getSocketFactory());
    }

    return new OkClient(client);
}

From source file:com.netflix.spinnaker.igor.concourse.client.OkHttpClientBuilder.java

License:Apache License

public static OkHttpClient retryingClient(Supplier<Token> refreshToken) {
    OkHttpClient okHttpClient = new OkHttpClient();
    okHttpClient.interceptors().add(chain -> OkHttpClientBuilder.createRetryInterceptor(chain, refreshToken));
    okHttpClient.setHostnameVerifier((s, sslSession) -> true);
    okHttpClient.setSslSocketFactory(getSslContext().getSocketFactory());
    okHttpClient.setConnectTimeout(15, TimeUnit.SECONDS);
    okHttpClient.setReadTimeout(15, TimeUnit.SECONDS);
    return okHttpClient;
}

From source file:com.netflix.spinnaker.igor.config.client.DefaultJenkinsOkHttpClientProvider.java

License:Apache License

@Override
public OkHttpClient provide(JenkinsProperties.JenkinsHost host) {
    return new OkHttpClient();
}

From source file:com.netflix.spinnaker.igor.config.GitlabCiConfig.java

License:Apache License

public static GitlabCiClient gitlabCiClient(String address, String privateToken, int timeout,
        ObjectMapper objectMapper) {//from w  w  w .j  a  v a 2  s .  c o m
    OkHttpClient client = new OkHttpClient();
    client.setReadTimeout(timeout, TimeUnit.MILLISECONDS);

    //Need this code because without FULL log level, fetching logs will fail. Ref https://github.com/square/retrofit/issues/953.
    RestAdapter.Log fooLog = message -> {
    };
    return new RestAdapter.Builder().setEndpoint(Endpoints.newFixedEndpoint(address))
            .setRequestInterceptor(new GitlabCiHeaders(privateToken)).setClient(new OkClient(client))
            .setLog(fooLog).setLogLevel(RestAdapter.LogLevel.FULL)
            .setConverter(new JacksonConverter(objectMapper)).build().create(GitlabCiClient.class);
}

From source file:com.netflix.spinnaker.igor.config.TravisConfig.java

License:Apache License

public static TravisClient travisClient(String address, int timeout, ObjectMapper objectMapper) {
    OkHttpClient client = new OkHttpClient();
    client.setReadTimeout(timeout, TimeUnit.MILLISECONDS);

    //Need this code because without FULL log level, fetching logs will fail. Ref https://github.com/square/retrofit/issues/953.
    RestAdapter.Log fooLog = message -> {
    };//  ww w  .  j a  va2  s . co  m

    return new RestAdapter.Builder().setEndpoint(Endpoints.newFixedEndpoint(address))
            .setRequestInterceptor(new TravisHeader()).setClient(new OkClient(client)).setLog(fooLog)
            .setLogLevel(RestAdapter.LogLevel.FULL).setConverter(new JacksonConverter(objectMapper)).build()
            .create(TravisClient.class);
}

From source file:com.nguyenmp.gauchospace.GauchoSpaceClient.java

License:Open Source License

public static OkHttpClient getClient(Session session) {
    OkHttpClient client = new OkHttpClient();
    CookieManager cookieManager = new CookieManager(session != null ? session.getCookies() : null,
            CookiePolicy.ACCEPT_ALL);
    client.setCookieHandler(cookieManager);
    return client;
}