protected AbstractIT() { clientFactory = Configuration.builder() .setBaseUri(System.getProperty("baseUrl")) .setClientHttpRequestFactory(new BufferingClientHttpRequestFactory( new HttpComponentsClientHttpRequestFactory())) .setRestTemplateConfigurer(new RestTemplateConfigurer() { @Override public void configure(RestTemplate restTemplate) { restTemplate.getInterceptors().addAll(asList( new LoggingClientHttpRequestInterceptor(), createdEntityRecordingInterceptor )); } }) .build() .buildClientFactory(); }
/** * Extension point for plugging in different HTTP factories. * @return Default is a {@link BufferingClientHttpRequestFactory} */ protected ClientHttpRequestFactory createHttpFactory( int connectTimeout, int requestTimeout) { SimpleClientHttpRequestFactory scrf = new SimpleClientHttpRequestFactory(); scrf.setConnectTimeout(connectTimeout); scrf.setReadTimeout(requestTimeout); // // Wrap the default request factory in a BufferingClientHttpRequestFactory // which allows us to read response bodies multiple times. This is needed // because some interceptors will need to consume the body before the final // response gets to the caller. // return new BufferingClientHttpRequestFactory(scrf); }
public static RestTemplate getTemplate(ClientHttpRequestInterceptor interceptor) { RestTemplate restTemplate = new RestTemplate(); List<ClientHttpRequestInterceptor> ris = new ArrayList<>(); ris.add(interceptor); restTemplate.setInterceptors(ris); SimpleClientHttpRequestFactory httpFactory = new SimpleClientHttpRequestFactory(); httpFactory.setOutputStreaming(false); restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(httpFactory)); restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8"))); return restTemplate; }
public static void disableSSL(RestTemplate restTemplate) { try { SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom() .loadTrustMaterial(null, (X509Certificate[] chain, String authType) -> true) .build(); CloseableHttpClient httpClient = HttpClients.custom() .setSSLSocketFactory(new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE)) .build(); HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); requestFactory.setHttpClient(httpClient); restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(requestFactory)); } catch (Exception e) { log.error("Exception occurred while creating http factory, error={}", e.getMessage(), e); } }
/** * Build the RestTemplate used to make HTTP requests. * @return RestTemplate */ protected RestTemplate buildRestTemplate() { RestTemplate restTemplate = new RestTemplate(); // This allows us to read the response more than once - Necessary for debugging. restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(restTemplate.getRequestFactory())); return restTemplate; }
private static LoggingRequestFactory interceptorToRequestFactory(LoggingRequestInterceptor lri) { List<ClientHttpRequestInterceptor> lInterceptors = new ArrayList<>(); lInterceptors.add(lri); SimpleClientHttpRequestFactory chrf = new SimpleClientHttpRequestFactory(); chrf.setOutputStreaming(false); return new LoggingRequestFactory( new BufferingClientHttpRequestFactory(chrf), lInterceptors ); }
@Override protected void interceptorsIntegration(List<ClientHttpRequestInterceptor> lInterceptors, Object sslConfiguration) { this.setInterceptors(lInterceptors); SimpleClientHttpRequestFactory chrf = new SimpleClientHttpRequestFactory(); chrf.setOutputStreaming(false); this.setRequestFactory( new InterceptingClientHttpRequestFactory( new BufferingClientHttpRequestFactory(chrf), lInterceptors ) ); }
/** * hard to check the logs provided by the interceptor when there's no error * however this unit test garantees the interceptor does not alter the reply * from the rest service. */ @Test public void testInterceptor() { List<ClientHttpRequestInterceptor> lInterceptors = new ArrayList<>(); //spring boot default log level is info lInterceptors.add(new LoggingRequestInterceptor(StandardCharsets.ISO_8859_1, 100, Level.ERROR)); SimpleClientHttpRequestFactory chrf = new SimpleClientHttpRequestFactory(); chrf.setOutputStreaming(false); rt.getRestTemplate().setRequestFactory(new InterceptingClientHttpRequestFactory( new BufferingClientHttpRequestFactory(chrf), lInterceptors )); ResponseEntity<String> resp = rt.getForEntity(MockedControllers.TEST_URL_GET, String.class); assertThat(resp.getBody()).isEqualTo(MockedControllers.TEST_RETURN_VALUE); }
@Bean public RestTemplate normalTemplate() { List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>(); interceptors.add(new ClientApiKeyHeaderInterceptor()); interceptors.add(new ClientLogInterceptor()); // Out-going serialization RestTemplate restTemplate = new RestTemplate(Collections.singletonList(getMessageConverter())); restTemplate.setInterceptors(interceptors); restTemplate.setRequestFactory( new BufferingClientHttpRequestFactory( new HttpComponentsClientHttpRequestFactory())); return restTemplate; }
@Test public void connectTimeoutCanBeConfiguredOnAWrappedRequestFactory() { SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); this.builder.requestFactory(new BufferingClientHttpRequestFactory(requestFactory)) .setConnectTimeout(1234).build(); assertThat(ReflectionTestUtils.getField(requestFactory, "connectTimeout")) .isEqualTo(1234); }
@Test public void readTimeoutCanBeConfiguredOnAWrappedRequestFactory() { SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); this.builder.requestFactory(new BufferingClientHttpRequestFactory(requestFactory)) .setReadTimeout(1234).build(); assertThat(ReflectionTestUtils.getField(requestFactory, "readTimeout")) .isEqualTo(1234); }
@Before public void setUp() { SlideShareTemplate slideShareTemplate = new SlideShareTemplate("key", "secret"); RestTemplate restTemplate = slideShareTemplate.getRestTemplate(); mockServer = MockRestServiceServer.createServer(restTemplate); // creating mockServer replaces RequestFactory in restTemplate to RequestMatcherClientHttpRequestFactory. // in order to support multiple reads in test, re-wrap the mock requestFactory and set to restTemplate. restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(restTemplate.getRequestFactory())); slideshowOperations = slideShareTemplate.slideshowOperations(); }
private RamlRestTemplate(RamlChecker ramlChecker, boolean notSending, ReportStore reportStore, ClientHttpRequestFactory requestFactory) { this.ramlChecker = ramlChecker; this.notSending = notSending; this.reportStore = reportStore; this.originalRequestFactory = requestFactory; final RamlRequestInterceptor interceptor = new RamlRequestInterceptor(ramlChecker, notSending, reportStore); setRequestFactory(new InterceptingClientHttpRequestFactory( new BufferingClientHttpRequestFactory(requestFactory), Collections.<ClientHttpRequestInterceptor>singletonList(interceptor))); }
@Override public void customize(RestTemplate restTemplate) { restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(restTemplate.getRequestFactory())); restTemplate.getInterceptors().add(new LoggingInterceptor(log)); }