Example usage for org.springframework.web.client RestTemplate setRequestFactory

List of usage examples for org.springframework.web.client RestTemplate setRequestFactory

Introduction

In this page you can find the example usage for org.springframework.web.client RestTemplate setRequestFactory.

Prototype

@Override
public void setRequestFactory(ClientHttpRequestFactory requestFactory) 

Source Link

Usage

From source file:ingest.Application.java

@Bean
public RestTemplate restTemplate() {
    RestTemplate restTemplate = new RestTemplate();
    HttpClient httpClient = HttpClientBuilder.create().setMaxConnTotal(httpMaxTotal)
            .setMaxConnPerRoute(httpMaxRoute).setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {
                @Override//  w w w .j av a 2 s.  co  m
                public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
                    HeaderElementIterator it = new BasicHeaderElementIterator(
                            response.headerIterator(HTTP.CONN_KEEP_ALIVE));
                    while (it.hasNext()) {
                        HeaderElement headerElement = it.nextElement();
                        String param = headerElement.getName();
                        String value = headerElement.getValue();
                        if (value != null && param.equalsIgnoreCase("timeout")) {
                            return Long.parseLong(value) * 1000;
                        }
                    }
                    return (long) 5 * 1000;
                }
            }).build();
    restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient));
    return restTemplate;
}

From source file:org.springframework.boot.web.client.RestTemplateBuilder.java

private void configureRequestFactory(RestTemplate restTemplate) {
    ClientHttpRequestFactory requestFactory = null;
    if (this.requestFactory != null) {
        requestFactory = this.requestFactory;
    } else if (this.detectRequestFactory) {
        requestFactory = detectRequestFactory();
    }/*from   w  w w  .j a v  a  2  s.com*/
    if (requestFactory != null) {
        ClientHttpRequestFactory unwrappedRequestFactory = unwrapRequestFactoryIfNecessary(requestFactory);
        for (RequestFactoryCustomizer customizer : this.requestFactoryCustomizers) {
            customizer.customize(unwrappedRequestFactory);
        }
        restTemplate.setRequestFactory(requestFactory);
    }
}

From source file:org.springframework.boot.test.web.client.TestRestTemplate.java

/**
 * Creates a new {@code TestRestTemplate} with the same configuration as this one,
 * except that it will send basic authorization headers using the given
 * {@code username} and {@code password}.
 * @param username the username/*ww  w  .j a v  a2 s  . c  o  m*/
 * @param password the password
 * @return the new template
 * @since 1.4.1
 */
public TestRestTemplate withBasicAuth(String username, String password) {
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.setMessageConverters(getRestTemplate().getMessageConverters());
    restTemplate.setInterceptors(getRestTemplate().getInterceptors());
    restTemplate.setRequestFactory(getRestTemplate().getRequestFactory());
    restTemplate.setUriTemplateHandler(getRestTemplate().getUriTemplateHandler());
    TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplate, username, password,
            this.httpClientOptions);
    testRestTemplate.getRestTemplate().setErrorHandler(getRestTemplate().getErrorHandler());
    return testRestTemplate;
}

From source file:org.springframework.boot.test.web.client.TestRestTemplate.java

public TestRestTemplate(RestTemplate restTemplate, String username, String password,
        HttpClientOption... httpClientOptions) {
    Assert.notNull(restTemplate, "RestTemplate must not be null");
    this.httpClientOptions = httpClientOptions;
    if (ClassUtils.isPresent("org.apache.http.client.config.RequestConfig", null)) {
        restTemplate.setRequestFactory(new CustomHttpComponentsClientHttpRequestFactory(httpClientOptions));
    }// w w  w. j  av  a2 s.co  m
    addAuthentication(restTemplate, username, password);
    restTemplate.setErrorHandler(new NoOpResponseErrorHandler());
    this.restTemplate = restTemplate;
}

From source file:org.cloudfoundry.client.lib.rest.CloudControllerClientImpl.java

protected void configureCloudFoundryRequestFactory(RestTemplate restTemplate) {
    ClientHttpRequestFactory requestFactory = restTemplate.getRequestFactory();
    if (!(requestFactory instanceof CloudFoundryClientHttpRequestFactory)) {
        restTemplate.setRequestFactory(new CloudFoundryClientHttpRequestFactory(requestFactory));
    }//from  w w  w .  j  av a 2  s. co m
}

From source file:org.bytesoft.bytejta.supports.springcloud.SpringCloudConfiguration.java

@org.springframework.context.annotation.Bean("transactionRestTemplate")
public RestTemplate transactionTemplate(@Autowired ClientHttpRequestFactory requestFactory) {
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.setRequestFactory(requestFactory);
    return restTemplate;
}

From source file:org.cloudfoundry.identity.api.web.ServerRunning.java

public RestOperations createRestTemplate() {
    RestTemplate client = new RestTemplate();
    client.setRequestFactory(new SimpleClientHttpRequestFactory() {
        @Override//ww  w .j av  a 2s .  c  om
        protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {
            super.prepareConnection(connection, httpMethod);
            connection.setInstanceFollowRedirects(false);
        }
    });
    client.setErrorHandler(new ResponseErrorHandler() {
        // Pass errors through in response entity for status code analysis
        @Override
        public boolean hasError(ClientHttpResponse response) throws IOException {
            return false;
        }

        @Override
        public void handleError(ClientHttpResponse response) throws IOException {
        }
    });
    return client;
}

From source file:org.cloudfoundry.identity.uaa.authentication.manager.RestAuthenticationManager.java

public RestAuthenticationManager() {
    RestTemplate restTemplate = new RestTemplate();
    // The default java.net client doesn't allow you to handle 4xx responses
    restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
    restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
        @Override//from  ww  w  .  ja v  a  2 s. c  o m
        protected boolean hasError(HttpStatus statusCode) {
            return statusCode.series() == HttpStatus.Series.SERVER_ERROR;
        }
    });
    this.restTemplate = restTemplate;
}