Example usage for org.springframework.http.client HttpComponentsClientHttpRequestFactory HttpComponentsClientHttpRequestFactory

List of usage examples for org.springframework.http.client HttpComponentsClientHttpRequestFactory HttpComponentsClientHttpRequestFactory

Introduction

In this page you can find the example usage for org.springframework.http.client HttpComponentsClientHttpRequestFactory HttpComponentsClientHttpRequestFactory.

Prototype

public HttpComponentsClientHttpRequestFactory(HttpClient httpClient) 

Source Link

Document

Create a new instance of the HttpComponentsClientHttpRequestFactory with the given HttpClient instance.

Usage

From source file:org.venice.beachfront.bfapi.BfApiConfig.java

@Bean(name = "rest-template-no-follow-redirect")
public RestTemplate getHttpClientWithoutRedirects() {
    return new RestTemplate(new HttpComponentsClientHttpRequestFactory(
            HttpClientBuilder.create().disableRedirectHandling().build()));
}

From source file:org.drugis.addis.config.MainConfig.java

@Bean
public RestTemplate restTemplate(RequestConfig requestConfig)
        throws CertificateException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException,
        KeyManagementException, IOException {
    RestTemplate restTemplate = new RestTemplate(
            new HttpComponentsClientHttpRequestFactory(httpClient(requestConfig)));
    restTemplate.getMessageConverters().add(new JenaGraphMessageConverter());
    return restTemplate;
}

From source file:org.thingsboard.server.msa.AbstractContainerTest.java

private static HttpComponentsClientHttpRequestFactory getRequestFactoryForSelfSignedCert() throws Exception {
    SSLContextBuilder builder = SSLContexts.custom();
    builder.loadTrustMaterial(null, (TrustStrategy) (chain, authType) -> true);
    SSLContext sslContext = builder.build();
    SSLConnectionSocketFactory sslSelfSigned = new SSLConnectionSocketFactory(sslContext,
            new X509HostnameVerifier() {
                @Override/*  w w w.  jav  a 2s  . c  o m*/
                public void verify(String host, SSLSocket ssl) {
                }

                @Override
                public void verify(String host, X509Certificate cert) {
                }

                @Override
                public void verify(String host, String[] cns, String[] subjectAlts) {
                }

                @Override
                public boolean verify(String s, SSLSession sslSession) {
                    return true;
                }
            });

    Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("https", sslSelfSigned).build();

    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
    CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();
    return new HttpComponentsClientHttpRequestFactory(httpClient);
}

From source file:org.springframework.security.kerberos.client.KerberosRestTemplate.java

/**
 * Instantiates a new kerberos rest template.
 *
 * @param keyTabLocation the key tab location
 * @param userPrincipal the user principal
 * @param password the password//from  w ww.  j  a  v a  2  s . c  o m
 * @param loginOptions the login options
 * @param httpClient the http client
 */
private KerberosRestTemplate(String keyTabLocation, String userPrincipal, String password,
        Map<String, Object> loginOptions, HttpClient httpClient) {
    super(new HttpComponentsClientHttpRequestFactory(httpClient));
    this.keyTabLocation = keyTabLocation;
    this.userPrincipal = userPrincipal;
    this.password = password;
    this.loginOptions = loginOptions;
}

From source file:org.springframework.cloud.dataflow.shell.command.ConfigCommands.java

@CliCommand(value = {
        "dataflow config server" }, help = "Configure the Spring Cloud Data Flow REST server to use")
public String target(@CliOption(mandatory = false, key = { "",
        "uri" }, help = "the location of the Spring Cloud Data Flow REST endpoint", unspecifiedDefaultValue = Target.DEFAULT_TARGET) String targetUriString,
        @CliOption(mandatory = false, key = {
                "username" }, help = "the username for authenticated access to the Admin REST endpoint", unspecifiedDefaultValue = Target.DEFAULT_USERNAME) String targetUsername,
        @CliOption(mandatory = false, key = {
                "password" }, help = "the password for authenticated access to the Admin REST endpoint (valid only with a username)", specifiedDefaultValue = Target.DEFAULT_SPECIFIED_PASSWORD, unspecifiedDefaultValue = Target.DEFAULT_UNSPECIFIED_PASSWORD) String targetPassword) {

    if (!StringUtils.isEmpty(targetPassword) && StringUtils.isEmpty(targetUsername)) {
        return "A password may be specified only together with a username";
    }//from w  w w  .j  a  v  a  2  s.c  o  m
    if (StringUtils.isEmpty(targetPassword) && !StringUtils.isEmpty(targetUsername)) {
        // read password from the command line
        targetPassword = userInput.prompt("Password", "", false);
    }

    try {
        this.targetHolder.setTarget(new Target(targetUriString, targetUsername, targetPassword));

        if (StringUtils.hasText(targetUsername) && StringUtils.hasText(targetPassword)) {
            final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(AuthScope.ANY,
                    new UsernamePasswordCredentials(
                            targetHolder.getTarget().getTargetCredentials().getUsername(),
                            targetHolder.getTarget().getTargetCredentials().getPassword()));
            final CloseableHttpClient httpClient = HttpClientBuilder.create()
                    .setDefaultCredentialsProvider(credentialsProvider).build();
            final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(
                    httpClient);

            this.restTemplate.setRequestFactory(requestFactory);
        }

        this.shell.setDataFlowOperations(
                new DataFlowTemplate(targetHolder.getTarget().getTargetUri(), this.restTemplate));
        return (String.format("Successfully targeted %s", targetUriString));
    } catch (Exception e) {
        this.targetHolder.getTarget().setTargetException(e);
        this.shell.setDataFlowOperations(null);
        if (e instanceof DataFlowServerException) {
            String message = String.format("Unable to parse server response: %s - at URI '%s'.", e.getMessage(),
                    targetUriString);
            if (logger.isDebugEnabled()) {
                logger.debug(message, e);
            } else {
                logger.warn(message);
            }
            return message;
        } else {
            return (String.format("Unable to contact Data Flow Server at '%s': '%s'.", targetUriString,
                    e.toString()));
        }
    }
}

From source file:com.neiljbrown.brighttalk.channels.reportingapi.client.spring.AppConfig.java

/**
 * @return The instance of {@link ClientHttpRequestFactory} to be used to create HTTP requests.
 *//* w  ww  .j  a v a 2 s  .c  om*/
@Bean
public ClientHttpRequestFactory clientHttpRequestFactory() {
    // Use the Apache HttpComponents implementation of ClientHttpRequestFactory as it provides an HTTP client that
    // supports the use of authentication, connection pooling and returning HTTP response status codes for RESTful
    // errors without exceptions being thrown.
    return new HttpComponentsClientHttpRequestFactory(this.httpClient());
}

From source file:com.gooddata.GoodData.java

static RestTemplate createRestTemplate(GoodDataEndpoint endpoint, HttpClient httpClient) {
    notNull(endpoint, "endpoint");
    notNull(httpClient, "httpClient");

    final UriPrefixingClientHttpRequestFactory factory = new UriPrefixingClientHttpRequestFactory(
            new HttpComponentsClientHttpRequestFactory(httpClient), endpoint.toUri());
    final RestTemplate restTemplate = new RestTemplate(factory);
    restTemplate.setInterceptors(//from w  ww . java 2s . com
            Arrays.asList(new HeaderSettingRequestInterceptor(singletonMap("Accept", getAcceptHeaderValue()))));

    restTemplate.setErrorHandler(new ResponseErrorHandler(restTemplate.getMessageConverters()));

    return restTemplate;
}

From source file:org.springframework.cloud.config.server.environment.ConfigurableHttpConnectionFactoryIntegrationTests.java

private void makeRequest(HttpClient httpClient, String url) {
    RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory(httpClient));
    restTemplate.getForObject(url, String.class);
}