/**
  * Create a new instance of the {@code AsyncRestTemplate} using the given {@link
  * AsyncTaskExecutor}.
  *
  * <p>This constructor uses a {@link SimpleClientHttpRequestFactory} in combination with the given
  * {@code AsyncTaskExecutor} for asynchronous execution.
  */
 public AsyncRestTemplate(AsyncListenableTaskExecutor taskExecutor) {
   Assert.notNull(taskExecutor, "AsyncTaskExecutor must not be null");
   SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
   requestFactory.setTaskExecutor(taskExecutor);
   this.syncTemplate = new RestTemplate(requestFactory);
   setAsyncRequestFactory(requestFactory);
 }
 public void assertContent(String url, int port, Object expected) throws Exception {
   SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
   ClientHttpRequest request =
       clientHttpRequestFactory.createRequest(
           new URI("http://localhost:" + port + url), HttpMethod.GET);
   try {
     ClientHttpResponse response = request.execute();
     try {
       String actual = StreamUtils.copyToString(response.getBody(), Charset.forName("UTF-8"));
       if (expected instanceof Matcher) {
         assertThat(actual).is(Matched.by((Matcher<?>) expected));
       } else {
         assertThat(actual).isEqualTo(expected);
       }
     } finally {
       response.close();
     }
   } catch (Exception ex) {
     if (expected == null) {
       if (SocketException.class.isInstance(ex) || FileNotFoundException.class.isInstance(ex)) {
         return;
       }
     }
     throw ex;
   }
 }
 public boolean hasHeader(String url, int port, String header) throws Exception {
   SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
   ClientHttpRequest request =
       clientHttpRequestFactory.createRequest(
           new URI("http://localhost:" + port + url), HttpMethod.GET);
   ClientHttpResponse response = request.execute();
   return response.getHeaders().containsKey(header);
 }
 @Test
 public void interceptShouldAddHeader() throws Exception {
   SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
   ClientHttpRequest request =
       requestFactory.createRequest(new URI("http://example.com"), HttpMethod.GET);
   ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class);
   byte[] body = new byte[] {};
   new BasicAuthorizationInterceptor("spring", "boot").intercept(request, body, execution);
   verify(execution).execute(request, body);
   assertEquals("Basic c3ByaW5nOmJvb3Q=", request.getHeaders().getFirst("Authorization"));
 }
 private void doTest(AnnotationConfigEmbeddedWebApplicationContext context, String resourcePath)
     throws Exception {
   SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
   ClientHttpRequest request =
       clientHttpRequestFactory.createRequest(
           new URI(
               "http://localhost:"
                   + context.getEmbeddedServletContainer().getPort()
                   + resourcePath),
           HttpMethod.GET);
   ClientHttpResponse response = request.execute();
   try {
     String actual = StreamUtils.copyToString(response.getBody(), Charset.forName("UTF-8"));
     assertThat(actual).isEqualTo("Hello World");
   } finally {
     response.close();
   }
 }
  @Bean
  @Qualifier("dnsClientHttpRequestFactory")
  public SimpleClientHttpRequestFactory dnsClientHttpRequestFactory() {
    SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();

    // Set timeouts (1 second to connect, unlimited - for response)
    clientHttpRequestFactory.setConnectTimeout(500);
    clientHttpRequestFactory.setReadTimeout(1000);

    // Setting the ThreadPoolTaskExecutor for the Async calls
    ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
    pool.setCorePoolSize(5);
    pool.setMaxPoolSize(10);
    pool.setWaitForTasksToCompleteOnShutdown(true);
    pool.initialize();

    // Setting the TaskExecutor to the ThreadPoolTaskExecutor
    clientHttpRequestFactory.setTaskExecutor(pool);

    return clientHttpRequestFactory;
  }