Java Code Examples for org.springframework.http.client.OkHttp3ClientHttpRequestFactory

The following examples show how to use org.springframework.http.client.OkHttp3ClientHttpRequestFactory. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: soul   Source File: HttpSyncDataService.java    License: Apache License 2.0 6 votes vote down vote up
private void start(final HttpConfig httpConfig) {
    // init RestTemplate
    OkHttp3ClientHttpRequestFactory factory = new OkHttp3ClientHttpRequestFactory();
    factory.setConnectTimeout((int) this.connectionTimeout.toMillis());
    factory.setReadTimeout((int) HttpConstants.CLIENT_POLLING_READ_TIMEOUT);
    this.httpClient = new RestTemplate(factory);
    // It could be initialized multiple times, so you need to control that.
    if (RUNNING.compareAndSet(false, true)) {
        // fetch all group configs.
        this.fetchGroupConfig(ConfigGroupEnum.values());
        // one thread for listener, another one for fetch configuration data.
        this.executor = new ThreadPoolExecutor(3, 3, 0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(),
                SoulThreadFactory.create("http-long-polling", true));
        // start long polling.
        this.executor.execute(new HttpLongPollingTask());
    } else {
        log.info("soul http long polling was started, executor=[{}]", executor);
    }
}
 
Example 2
@Bean
@Lazy
@ConditionalOnMissingBean
public RestTemplateBuilder restTemplateBuilder(ObjectProvider<HttpMessageConverters> messageConverters,
                                               ObjectProvider<RestTemplateCustomizer> restTemplateCustomizers,
                                               ObjectProvider<RestTemplateRequestCustomizer<?>> restTemplateRequestCustomizers,
                                               OkHttpClient okHttpClient) {
    RestTemplateBuilder builder = new RestTemplateBuilder();
    HttpMessageConverters converters = messageConverters.getIfUnique();
    if (converters != null) {
        builder = builder.messageConverters(converters.getConverters());
    }
    builder = addCustomizers(builder, restTemplateCustomizers, RestTemplateBuilder::customizers);
    builder = addCustomizers(builder, restTemplateRequestCustomizers, RestTemplateBuilder::requestCustomizers);

    builder = builder.requestFactory(() -> new OkHttp3ClientHttpRequestFactory(okHttpClient));

    return builder;
}
 
Example 3
@Bean
@Lazy
@ConditionalOnMissingBean
public RestTemplateBuilder restTemplateBuilder(ObjectProvider<HttpMessageConverters> messageConverters,
                                               ObjectProvider<RestTemplateCustomizer> restTemplateCustomizers,
                                               ObjectProvider<RestTemplateRequestCustomizer<?>> restTemplateRequestCustomizers,
                                               OkHttpClient okHttpClient) {
    RestTemplateBuilder builder = new RestTemplateBuilder();
    HttpMessageConverters converters = messageConverters.getIfUnique();
    if (converters != null) {
        builder = builder.messageConverters(converters.getConverters());
    }
    builder = addCustomizers(builder, restTemplateCustomizers, RestTemplateBuilder::customizers);
    builder = addCustomizers(builder, restTemplateRequestCustomizers, RestTemplateBuilder::requestCustomizers);

    builder = builder.requestFactory(() -> new OkHttp3ClientHttpRequestFactory(okHttpClient));

    return builder;
}
 
Example 4
Source Project: onetwo   Source File: DefaultRestExecutorFactory.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public RestExecutor createRestExecutor() {
	RestExecutorConfig config = this.restExecutorConfig;
	if(config==null){
		config = new RestExecutorConfig();
	}
	ExtRestTemplate restTemplate = null;
	if(RestUtils.isOkHttp3Present()){
		OkHttp3ClientHttpRequestFactory requestFactory = new OkHttp3ClientHttpRequestFactory();
		requestFactory.setConnectTimeout(config.getConnectTimeout());
		requestFactory.setReadTimeout(config.getReadTimeout());
		requestFactory.setWriteTimeout(config.getWriteTimeout());
		restTemplate = new ExtRestTemplate(requestFactory);
	}else{
		restTemplate = new ExtRestTemplate();
	}
	return restTemplate;
}
 
Example 5
@SuppressWarnings("deprecation")
@Parameters
public static Iterable<? extends ClientHttpRequestFactory> data() {
	return Arrays.asList(
			new SimpleClientHttpRequestFactory(),
			new HttpComponentsClientHttpRequestFactory(),
			new org.springframework.http.client.Netty4ClientHttpRequestFactory(),
			new OkHttp3ClientHttpRequestFactory()
	);
}
 
Example 6
@SuppressWarnings("deprecation")
@Parameters
public static Iterable<? extends ClientHttpRequestFactory> data() {
	return Arrays.asList(
			new SimpleClientHttpRequestFactory(),
			new HttpComponentsClientHttpRequestFactory(),
			new Netty4ClientHttpRequestFactory(),
			new OkHttp3ClientHttpRequestFactory()
	);
}
 
Example 7
@Parameterized.Parameters()
public static Iterable<Supplier<ClientHttpRequestFactory>> data() {
    return Arrays.asList(
        SimpleClientHttpRequestFactory::new,
        OkHttp3ClientHttpRequestFactory::new,
        HttpComponentsClientHttpRequestFactory::new);
}
 
Example 8
@Test
public void createClientHttpRequestFactory() {
    ClientOptions clientOptions = mock(ClientOptions.class);
    SslConfiguration sslConfiguration = mock(SslConfiguration.class);

    SslConfiguration.KeyStoreConfiguration keyStoreConfiguration = mock(SslConfiguration.KeyStoreConfiguration.class);
    when(sslConfiguration.getKeyStoreConfiguration()).thenReturn(keyStoreConfiguration);
    when(sslConfiguration.getTrustStoreConfiguration()).thenReturn(keyStoreConfiguration);

    when(clientOptions.getConnectionTimeout()).thenReturn(Duration.ZERO);
    when(clientOptions.getReadTimeout()).thenReturn(Duration.ZERO);

    ClientHttpRequestFactory result = util.createClientHttpRequestFactory(clientOptions, sslConfiguration);

    assertThat(result).isInstanceOf(OkHttp3ClientHttpRequestFactory.class);
}
 
Example 9
@Test
public void okHttp3ClientCreated() throws Exception {
	ClientHttpRequestFactory factory = OkHttp3.usingOkHttp3(new ClientOptions());

	assertThat(factory).isInstanceOf(OkHttp3ClientHttpRequestFactory.class);

	((DisposableBean) factory).destroy();
}
 
Example 10
@Override
public ClientHttpRequestFactory createRequestFactory(TimeoutConfiguration configuration) {
    OkHttp3ClientHttpRequestFactory requestFactory = new OkHttp3ClientHttpRequestFactory(httpClient);
    requestFactory.setConnectTimeout(toMillis(configuration.getConnection()));
    requestFactory.setReadTimeout(toMillis(configuration.getRead()));
    requestFactory.setWriteTimeout(toMillis(configuration.getWrite()));
    return requestFactory;
}
 
Example 11
Source Project: fahrschein   Source File: Main.java    License: Apache License 2.0 5 votes vote down vote up
private static void subscriptionListenSpringAdapter(ObjectMapper objectMapper, Listener<SalesOrderPlaced> listener) throws IOException {

        final OkHttpClient client = new OkHttpClient.Builder()
                .readTimeout(60, TimeUnit.SECONDS)
                .connectTimeout(2, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .connectionPool(new ConnectionPool(2, 5*60, TimeUnit.SECONDS))
                .certificatePinner(new CertificatePinner.Builder()
                        .add(NAKADI_URI.getHost(), "sha256/KMUmME9xy7BKVUZ80VcmQ75zIZo16IZRTqVRYHVZeWY=")
                        .build())
                .build();

        final OkHttp3ClientHttpRequestFactory clientHttpRequestFactory = new OkHttp3ClientHttpRequestFactory(client);
        final SpringRequestFactory requestFactory = new SpringRequestFactory(clientHttpRequestFactory);

        final NakadiClient nakadiClient = NakadiClient.builder(NAKADI_URI)
                .withRequestFactory(requestFactory)
                .withAccessTokenProvider(new ZignAccessTokenProvider())
                .build();

        final Subscription subscription = nakadiClient.subscription("fahrschein-demo", SALES_ORDER_SERVICE_ORDER_PLACED)
                .withConsumerGroup("fahrschein-demo")
                .readFromEnd()
                .subscribe();

        nakadiClient.stream(subscription)
                .withObjectMapper(objectMapper)
                .listen(SalesOrderPlaced.class, listener);
    }
 
Example 12
protected RestTemplate createRestTemplate() {
    Timeout timeout = recaptcha.getValidation().getTimeout();
    OkHttp3ClientHttpRequestFactory requestFactory = new OkHttp3ClientHttpRequestFactory();
    requestFactory.setConnectTimeout(toMilliseconds(timeout.getConnect()));
    requestFactory.setReadTimeout(toMilliseconds(timeout.getRead()));
    requestFactory.setWriteTimeout(toMilliseconds(timeout.getWrite()));
    return new RestTemplate(requestFactory);
}
 
Example 13
Source Project: brave   Source File: RestTemplateBenchmarks.java    License: Apache License 2.0 5 votes vote down vote up
@Override protected RestTemplate newClient(HttpTracing httpTracing) {
  OkHttp3ClientHttpRequestFactory factory = new OkHttp3ClientHttpRequestFactory(ok);
  RestTemplate result = new RestTemplate(factory);
  result.setInterceptors(Collections.singletonList(
    TracingClientHttpRequestInterceptor.create(httpTracing
    )));
  return result;
}
 
Example 14
Source Project: brave   Source File: AsyncRestTemplateBenchmarks.java    License: Apache License 2.0 5 votes vote down vote up
@Override protected AsyncRestTemplate newClient(HttpTracing httpTracing) {
  OkHttp3ClientHttpRequestFactory factory = new OkHttp3ClientHttpRequestFactory(ok);
  AsyncRestTemplate result = new AsyncRestTemplate(factory);
  result.setInterceptors(Collections.singletonList(
    TracingAsyncClientHttpRequestInterceptor.create(httpTracing
    )));
  return result;
}
 
Example 15
@Bean
public RestTemplate restTemplate() {
	return new RestTemplate(new OkHttp3ClientHttpRequestFactory());
}
 
Example 16
@Bean
public RestTemplate restTemplate() {
	return new RestTemplate(new OkHttp3ClientHttpRequestFactory());
}
 
Example 17
static ClientHttpRequestFactory usingOkHttp3(ClientOptions options, SslConfiguration sslConfiguration)
		throws GeneralSecurityException, IOException {

	Builder builder = new Builder();

	if (hasSslConfiguration(sslConfiguration)) {

		TrustManager[] trustManagers = getTrustManagers(sslConfiguration);

		if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
			throw new IllegalStateException(
					"Unexpected default trust managers:" + Arrays.toString(trustManagers));
		}

		X509TrustManager trustManager = (X509TrustManager) trustManagers[0];
		SSLContext sslContext = getSSLContext(sslConfiguration, trustManagers);

		builder.sslSocketFactory(sslContext.getSocketFactory(), trustManager);
	}

	builder.connectTimeout(options.getConnectionTimeout().toMillis(), TimeUnit.MILLISECONDS)
			.readTimeout(options.getReadTimeout().toMillis(), TimeUnit.MILLISECONDS);

	return new OkHttp3ClientHttpRequestFactory(builder.build());
}
 
Example 18
Source Project: spring-boot-tutorials   Source File: RestTemplateConfig.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
public OkHttp3ClientHttpRequestFactory okHttp3ClientHttpRequestFactory(OkHttpClient okHttpClient) {
    OkHttp3ClientHttpRequestFactory factory = new OkHttp3ClientHttpRequestFactory(okHttpClient);
    return factory;
}
 
Example 19
Source Project: onetwo   Source File: ExtRestTemplate.java    License: Apache License 2.0 4 votes vote down vote up
public ExtRestTemplate(){
	this(RestUtils.isOkHttp3Present()?new OkHttp3ClientHttpRequestFactory():null);
}
 
Example 20
Source Project: brave   Source File: RestTemplateBenchmarks.java    License: Apache License 2.0 4 votes vote down vote up
@Override protected RestTemplate newClient() {
  return new RestTemplate(new OkHttp3ClientHttpRequestFactory(ok));
}
 
Example 21
Source Project: brave   Source File: AsyncRestTemplateBenchmarks.java    License: Apache License 2.0 4 votes vote down vote up
@Override protected AsyncRestTemplate newClient() {
  return new AsyncRestTemplate(new OkHttp3ClientHttpRequestFactory(ok));
}
 
Example 22
@Test
void okHttp3ClientShouldWork() throws Exception {

	ClientHttpRequestFactory factory = OkHttp3.usingOkHttp3(new ClientOptions(), Settings.createSslConfiguration());
	RestTemplate template = new RestTemplate(factory);

	String response = request(template);

	assertThat(factory).isInstanceOf(OkHttp3ClientHttpRequestFactory.class);
	assertThat(response).isNotNull().contains("initialized");

	((DisposableBean) factory).destroy();
}