@Before public void setupClient() { client = new CrnkClient("http://127.0.0.1:" + port + "/api"); client.findModules(); RestTemplateAdapter adapter = RestTemplateAdapter.newInstance(); adapter.setReceiveTimeout(30000, TimeUnit.MILLISECONDS); listener = Mockito.mock(RestTemplateAdapterListener.class); adapter.addListener(listener); adapter.addListener(new RestTemplateAdapterListenerBase()); adapter.getImplementation().setRequestFactory(new OkHttp3ClientHttpRequestFactory()); client.setHttpAdapter(adapter); scheduleRepo = client.getRepositoryForType(Schedule.class); taskRepo = client.getRepositoryForType(Task.class); projectRepo = client.getRepositoryForType(Project.class); relRepo = client.getRepositoryForType(Task.class, Project.class); scheduleTaskRepo = client.getRepositoryForType(Schedule.class, Task.class); taskScheduleRepo = client.getRepositoryForType(Task.class, Schedule.class); TestModule.clear(); }
static ClientHttpRequestFactory usingOkHttp3(ClientOptions options) throws IOException, GeneralSecurityException { SSLSocketFactory socketFactory = SSLContext.getDefault().getSocketFactory(); X509TrustManager trustManager = getTrustManager(); Builder builder = new Builder().sslSocketFactory(socketFactory, trustManager); if (options.getConnectionTimeout() != null) { builder.connectTimeout(options.getConnectionTimeout(), TimeUnit.MILLISECONDS); } if (options.getReadTimeout() != null) { builder.readTimeout(options.getReadTimeout(), TimeUnit.MILLISECONDS); } return new OkHttp3ClientHttpRequestFactory(builder.build()); }
static ClientHttpRequestFactory usingOkHttp3(ClientOptions options, SslConfiguration sslConfiguration) throws GeneralSecurityException, IOException { Builder builder = new Builder(); if (hasSslConfiguration(sslConfiguration)) { builder.sslSocketFactory(getSSLContext(sslConfiguration) .getSocketFactory()); } builder.connectTimeout(options.getConnectionTimeout().toMillis(), TimeUnit.MILLISECONDS).readTimeout( options.getReadTimeout().toMillis(), TimeUnit.MILLISECONDS); return new OkHttp3ClientHttpRequestFactory(builder.build()); }
@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(); } if(restExecutorInterceptors!=null){ List<ClientHttpRequestInterceptor> interList = restTemplate.getInterceptors(); if(interList==null){ interList = Lists.newArrayList(); restTemplate.setInterceptors(interList); } // interList.addAll(restExecutorRequestInterceptors); } return restTemplate; }
@Test public void okHttp3ClientCreated() throws Exception { ClientHttpRequestFactory factory = usingOkHttp3(new ClientOptions()); assertThat(factory, instanceOf(OkHttp3ClientHttpRequestFactory.class)); ((DisposableBean) factory).destroy(); }
@Test public 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(); }
@Test public void connectTimeoutCanBeConfiguredOnOkHttp3RequestFactory() { ClientHttpRequestFactory requestFactory = this.builder .requestFactory(OkHttp3ClientHttpRequestFactory.class) .setConnectTimeout(1234).build().getRequestFactory(); assertThat(ReflectionTestUtils.getField( ReflectionTestUtils.getField(requestFactory, "client"), "connectTimeout")) .isEqualTo(1234); }
@Test public void readTimeoutCanBeConfiguredOnOkHttp3RequestFactory() { ClientHttpRequestFactory requestFactory = this.builder .requestFactory(OkHttp3ClientHttpRequestFactory.class) .setReadTimeout(1234).build().getRequestFactory(); assertThat(ReflectionTestUtils.getField( ReflectionTestUtils.getField(requestFactory, "client"), "readTimeout")) .isEqualTo(1234); }
public String call() { // TODO zkusit pres HTTP/1.1, melo by to nejak vyhodit chybu,jelikoz tam mam jen 2.0 RestTemplate http2Template = new RestTemplate(new OkHttp3ClientHttpRequestFactory()); // RestTemplate http11Template = new RestTemplate(); // String http11Response = http11Template.getForObject("https://http2.akamai.com/", String.class); String http2Response = http2Template.getForObject("https://api.eetlite.cz", String.class); String resp = http2Response.contains("You are using HTTP/2 right now!") + "<br/>" + "HTTP/2 : " + http2Response.contains("You are using HTTP/2 right now!"); return ""; }
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); }
@Bean(name = "okHttpRestTemplate") public RestTemplate restTemplate() { return new RestTemplate(new OkHttp3ClientHttpRequestFactory()); }
@Bean @Order(2) @ConditionalOnBean(OkHttp3ClientHttpRequestFactory.class) public RestTemplateCustomizer okhttp3RestTemplateCustomizer(OkHttp3ClientHttpRequestFactory requestFactory) { return restTemplate -> restTemplate.setRequestFactory(requestFactory); }
@Bean @ConditionalOnMissingBean(OkHttp3ClientHttpRequestFactory.class) public OkHttp3ClientHttpRequestFactory okHttp3ClientHttpRequestFactory(OkHttpClient okHttpClient) { return new OkHttp3ClientHttpRequestFactory(okHttpClient); }
public ExtRestTemplate(){ this(RestUtils.isOkHttp3Present()?new OkHttp3ClientHttpRequestFactory():null); }
@Test public void testRestTemplate() { RestTemplate restTemplate = sut.restTemplate(); assertTrue(restTemplate.getRequestFactory() instanceof OkHttp3ClientHttpRequestFactory); }