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() 

Source Link

Document

Create a new instance of the HttpComponentsClientHttpRequestFactory with a default HttpClient based on system properties.

Usage

From source file:fi.helsinki.opintoni.config.CoursePageConfiguration.java

private ClientHttpRequestFactory clientHttpRequestFactory() {
    HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
    factory.setReadTimeout(appConfiguration.getInteger("httpClient.readTimeout"));
    factory.setConnectTimeout(appConfiguration.getInteger("httpClient.connectTimeout"));

    PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager();
    poolingHttpClientConnectionManager.setMaxTotal(appConfiguration.getInteger("httpClient.maxTotal"));
    poolingHttpClientConnectionManager//from w w w  .  j ava2 s .c om
            .setDefaultMaxPerRoute(appConfiguration.getInteger("httpClient.defaultMaxPerRoute"));

    CloseableHttpClient httpClient = HttpClientBuilder.create()
            .setConnectionManager(poolingHttpClientConnectionManager).build();

    factory.setHttpClient(httpClient);

    return factory;
}

From source file:org.cloudfoundry.identity.uaa.login.RemoteUaaAuthenticationManager.java

public RemoteUaaAuthenticationManager() {
    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() {
        protected boolean hasError(HttpStatus statusCode) {
            return statusCode.series() == HttpStatus.Series.SERVER_ERROR;
        }/*from ww w  .j  ava 2s .  com*/
    });
    this.restTemplate = restTemplate;
}

From source file:fi.helsinki.opintoni.config.OodiConfiguration.java

private ClientHttpRequestFactory clientHttpRequestFactory() {
    HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();

    factory.setReadTimeout(appConfiguration.getInteger("httpClient.readTimeout"));
    factory.setConnectTimeout(appConfiguration.getInteger("httpClient.connectTimeout"));

    PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager();
    poolingHttpClientConnectionManager.setMaxTotal(appConfiguration.getInteger("httpClient.maxTotal"));
    poolingHttpClientConnectionManager//from   www. j  a  v a  2s. co m
            .setDefaultMaxPerRoute(appConfiguration.getInteger("httpClient.defaultMaxPerRoute"));

    CloseableHttpClient httpClient = HttpClientBuilder.create()
            .setConnectionManager(poolingHttpClientConnectionManager).build();

    factory.setHttpClient(httpClient);

    return new BufferingClientHttpRequestFactory(factory);
}

From source file:com.navercorp.pinpoint.plugin.spring.web.RestTemplateIT.java

@Test
public void test2() throws Exception {
    RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
    String forObject = restTemplate.getForObject(webServer.getCallHttpUrl(), String.class);

    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();//from w ww  .java2s .  c o  m

    verifier.verifyTrace(event("REST_TEMPLATE", RestTemplate.class.getConstructor()));
    verifier.verifyTrace(event("REST_TEMPLATE", AbstractClientHttpRequest.class.getMethod("execute"),
            annotation("http.status.code", 200)));
}

From source file:com.angelmmg90.consumerservicespotify.configuration.SpringWebConfig.java

@Bean
public static RestTemplate getTemplate() throws IOException {
    if (template == null) {
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(new AuthScope(PROXY_HOST, PROXY_PORT),
                new UsernamePasswordCredentials(PROXY_USER, PROXY_PASSWORD));

        Header[] h = new Header[3];
        h[0] = new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json");
        h[1] = new BasicHeader(HttpHeaders.AUTHORIZATION, "Bearer " + ACCESS_TOKEN);

        List<Header> headers = new ArrayList<>(Arrays.asList(h));

        HttpClientBuilder clientBuilder = HttpClientBuilder.create();

        clientBuilder.useSystemProperties();
        clientBuilder.setProxy(new HttpHost(PROXY_HOST, PROXY_PORT));
        clientBuilder.setDefaultCredentialsProvider(credentialsProvider);
        clientBuilder.setDefaultHeaders(headers).build();
        String SAMPLE_URL = "https://api.spotify.com/v1/users/yourUserName/playlists/7HHFd1tNiIFIwYwva5MTNv";

        HttpUriRequest request = RequestBuilder.get().setUri(SAMPLE_URL).build();

        clientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy());

        CloseableHttpClient client = clientBuilder.build();
        client.execute(request);/*from  w w  w .j  av  a  2 s  .c  o  m*/

        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
        factory.setHttpClient(client);

        template = new RestTemplate();
        template.setRequestFactory(factory);
    }

    return template;
}

From source file:com.insys.cfclient.nozzle.InfluxDBSender.java

@PostConstruct
public void postConstruct() throws Exception {
    if (properties.isSkipSslValidation()) {
        TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;

        SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
                .loadTrustMaterial(null, acceptingTrustStrategy).build();

        SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);

        CloseableHttpClient apacheHttpClient = HttpClients.custom().setSSLSocketFactory(csf).build();

        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();

        requestFactory.setHttpClient(apacheHttpClient);

        this.httpClient.setRequestFactory(requestFactory);
    }//from  w w  w .ja  va  2 s. co  m
}

From source file:some.test.AbstractTokenInfoResourceServerTokenServicesTest.java

protected RestOperations buildClient(final String token) {
    final AccessTokenProvider mockTokenProvider = mock(AccessTokenProvider.class);
    when(mockTokenProvider.obtainAccessToken(any(OAuth2ProtectedResourceDetails.class),
            any(AccessTokenRequest.class))).thenReturn(new DefaultOAuth2AccessToken(token));
    return new StupsOAuth2RestTemplate(mockTokenProvider, new HttpComponentsClientHttpRequestFactory());
}

From source file:com.garyclayburg.UserRestSmokeTest.java

@Ignore
@Test//  w  w  w.  ja v a  2 s .  c o m
public void testJsonApache() throws Exception {
    RestTemplate rest = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
    SimpleUser user1 = new SimpleUser();
    user1.setFirstname("Tommy");
    user1.setLastname("Deleteme");
    user1.setId("112" + (int) (Math.floor(Math.random() * 10000)));

    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.set("Content-Type", "application/json");
    //        HttpEntity<?> requestEntity = new HttpEntity(requestHeaders);
    HttpEntity<?> requestEntity = new HttpEntity(user1, requestHeaders);

    ResponseEntity<SimpleUser> simpleUserResponseEntity = rest.exchange(
            "http://" + endpoint + "/audited-users/auditedsave", HttpMethod.POST, requestEntity,
            SimpleUser.class);

    //        ResponseEntity<SimpleUser> userResponseEntity =
    //            rest.postForEntity("http://" + endpoint + "/audited-users/auditedsave",user1,SimpleUser.class);
    log.info("got a response");
    MatcherAssertionErrors.assertThat(simpleUserResponseEntity.getStatusCode(),
            Matchers.equalTo(HttpStatus.OK));

}

From source file:org.ow2.proactive.scheduling.api.graphql.service.AuthenticationService.java

@PostConstruct
protected void init() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {

    schedulerLoginFetchUrl = createLoginFetchUrl(schedulerRestUrl);

    sessionCache = CacheBuilder.newBuilder().maximumSize(Integer.parseInt(sessionCacheMaxSize))
            .expireAfterWrite(Integer.parseInt(sessionCacheExpireAfter), TimeUnit.MILLISECONDS)
            .build(new CacheLoader<String, String>() {
                @Override//from w  w w  .  j  av a  2s  .c om
                public String load(String sessionId) throws Exception {
                    return getLoginFromSessionId(sessionId);
                }
            });

    if (schedulerLoginFetchUrl.startsWith("https")) {
        CloseableHttpClient httpClient = HttpClients.custom().setSSLHostnameVerifier(new NoopHostnameVerifier())
                .setSSLContext(new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
                    public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
                        return true;
                    }
                }).build()).build();
        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
        requestFactory.setHttpClient(httpClient);

        restTemplate.setRequestFactory(requestFactory);
    }

}