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:se.malmo.www.kontaktruta.service.ContactServiceImpl.java

@Autowired
public void setRestTemplate(RestTemplate restTemplate) {
    HostnameVerifier verifier = new NullHostnameVerifier();
    AppSimpleClientHttpRequestFactory factory = new AppSimpleClientHttpRequestFactory(verifier);
    restTemplate.setRequestFactory(factory);
    this.restTemplate = restTemplate;
}

From source file:com.github.ffremont.microservices.springboot.node.ApplicationConfiguration.java

@Bean
public RestTemplate getRestTemplate() {
    RestTemplate rTemplate = new RestTemplate();
    List<ClientHttpRequestInterceptor> interceptors = Collections.<ClientHttpRequestInterceptor>singletonList(
            new BasicAuthorizationInterceptor(this.username, this.password));
    rTemplate.setRequestFactory(
            new InterceptingClientHttpRequestFactory(rTemplate.getRequestFactory(), interceptors));

    return rTemplate;
}

From source file:com.concentricsky.android.khanacademy.data.remote.KAEntityFetcherTask.java

@Override
protected T doInBackground(Void... arg0) {
    // call  API and fetch an entity tree (commonly the tree rooted at the root topic)

    RestTemplate restTemplate = new RestTemplate();
    if (consumer != null) {
        restTemplate.setRequestFactory(new SpringRequestFactory(consumer));
    }/* w ww  .  j  a  v  a  2s.c  o m*/

    // TODO : Set up stream parsing.

    //      RequestCallback callback = new RequestCallback() {
    //
    //         public void doWithRequest(ClientHttpRequest request)
    //               throws IOException {
    //            // TODO Auto-generated method stub
    //            
    //         }
    //         
    //      };
    //      
    //      ResponseExtractor<T> extractor = new ResponseExtractor<T>() {
    //
    //         public T extractData(ClientHttpResponse response)
    //               throws IOException {
    //            
    //            InputStream stream = response.getBody();
    //            
    //            
    //            return null;
    //         }
    //         
    //      };
    //      
    //      restTemplate.execute(url, HttpMethod.GET, requestCallback, responseExtractor)

    // Provide a converter to the restTemplate to get automagic json --> pojo deserialization.
    // Provide a mapper to the converter so we can register the custom Video deserializer. Otherwise, the default ObjectMapper would do fine.
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new VideoDeserializerModule());
    MappingJacksonHttpMessageConverter converter = new MappingJacksonHttpMessageConverter();
    converter.setObjectMapper(mapper);
    restTemplate.getMessageConverters().add(converter);

    ModelBase entity = null;
    try {
        entity = restTemplate.getForObject(this.url, ModelBase.class);
    } catch (HttpClientErrorException e) {
        e.printStackTrace();
        exception = e;
        // meanwhile, entity is null, so we let that return naturally.
    } catch (ResourceAccessException e) {
        // This one happens on Nexus 7 if we have set a SpringRequestFactory but get no Auth challenge.
        // org.springframework.web.client.ResourceAccessException: I/O error: No authentication challenges found; nested exception is java.io.IOException: No authentication challenges found
        e.printStackTrace();
        Log.e(LOG_TAG, "url was " + url);
        exception = e;
    }

    T result;
    try {
        result = (T) entity;
    } catch (ClassCastException e) {
        e.printStackTrace();
        exception = e;
        result = null;
    }

    Log.d(LOG_TAG, "Response received. Returning entity.");
    return result;
}

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

/**
 * Creates and configures the instance of {@link RestTemplate} to be used by the API client.
 * //from  ww  w.j a va2s .  c o  m
 * @return The instance of {@link RestTemplate} to be used by the API client.
 */
@Bean
public RestTemplate apiClientRestTemplate() {
    // Use custom list of HttpMessageConverter rather than the default to allow the marshalling config to be customised
    RestTemplate restTemplate = new RestTemplate(this.httpMessageConverters());
    restTemplate.setRequestFactory(this.clientHttpRequestFactory());
    restTemplate.setErrorHandler(this.responseErrorHandler());
    return restTemplate;
}

From source file:gateway.Application.java

@Bean
public RestTemplate restTemplate() {
    RestTemplate restTemplate = new RestTemplate();
    HttpClient httpClient = HttpClientBuilder.create().setMaxConnTotal(httpMaxTotal)
            .setMaxConnPerRoute(httpMaxRoute).build();
    restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient));
    return restTemplate;
}

From source file:sample.tomcat.X509ApplicationTests.java

@Test
public void testAuthenticatedHello() throws Exception {
    RestTemplate template = new TestRestTemplate();
    final HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(
            httpClient());/*from  w w w.  ja v a2  s.co  m*/
    template.setRequestFactory(factory);

    ResponseEntity<String> httpsEntity = template.getForEntity("https://localhost:" + this.port + "/hello",
            String.class);
    assertEquals(HttpStatus.OK, httpsEntity.getStatusCode());
    assertEquals("hello", httpsEntity.getBody());
}

From source file:sample.tomcat.SslApplicationTests.java

@Test
public void testAuthenticatedHello() throws Exception {
    RestTemplate template = new RestTemplate();
    final LocalhostClientHttpRequestFactory factory = new LocalhostClientHttpRequestFactory(
            secureSocketFactory());// www.jav  a 2  s  .com
    template.setRequestFactory(factory);

    ResponseEntity<String> httpsEntity = template.getForEntity("https://localhost:" + this.port + "/hello",
            String.class);
    assertEquals(HttpStatus.OK, httpsEntity.getStatusCode());
    assertEquals("hello", httpsEntity.getBody());
}

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

/**
 * Ensures that the passed-in {@link RestTemplate} is using the Apache HTTP Client. If the optional {@code username} AND
 * {@code password} are not empty, then a {@link BasicCredentialsProvider} will be added to the {@link CloseableHttpClient}.
 *
 * Furthermore, you can set the underlying {@link SSLContext} of the {@link HttpClient} allowing you to accept self-signed
 * certificates.//from   w  ww .ja  va 2s . c om
 *
 * @param restTemplate Must not be null
 * @param username Can be null
 * @param password Can be null
 * @param skipSslValidation Use with caution! If true certificate warnings will be ignored.
 */
public static void prepareRestTemplate(RestTemplate restTemplate, URI host, String username, String password,
        boolean skipSslValidation) {

    Assert.notNull(restTemplate, "The provided RestTemplate must not be null.");

    final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();

    if (StringUtils.hasText(username) && StringUtils.hasText(password)) {
        final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
        httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
    }

    if (skipSslValidation) {
        httpClientBuilder.setSSLContext(HttpClientUtils.buildCertificateIgnoringSslContext());
        httpClientBuilder.setSSLHostnameVerifier(new NoopHostnameVerifier());
    }

    final CloseableHttpClient httpClient = httpClientBuilder.build();
    final HttpHost targetHost = new HttpHost(host.getHost(), host.getPort(), host.getScheme());

    final HttpComponentsClientHttpRequestFactory requestFactory = new PreemptiveBasicAuthHttpComponentsClientHttpRequestFactory(
            httpClient, targetHost);
    restTemplate.setRequestFactory(requestFactory);
}

From source file:de.metas.ui.web.vaadin.VaadinClientApplication.java

@Bean
public RestTemplate restTemplate() {
    final List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
    messageConverters.add(new MappingJackson2HttpMessageConverter(JsonHelper.createObjectMapper()));
    final RestTemplate restTemplate = new RestTemplate(messageConverters);

    final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
    requestFactory.setReadTimeout(2000);
    requestFactory.setConnectTimeout(2000);
    restTemplate.setRequestFactory(requestFactory);

    return restTemplate;
}

From source file:fi.helsinki.moodi.config.OodiConfig.java

@Bean
public RestTemplate oodiRestTemplate() {
    final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(
            objectMapper());//from w  w w .jav a 2  s. co  m

    RestTemplate restTemplate = new RestTemplate(Collections.singletonList(converter));
    restTemplate.setInterceptors(newArrayList(new LoggingInterceptor(), new RequestTimingInterceptor()));
    if (useClientCert()) {
        final HttpClient client = HttpClients.custom().setSSLContext(sslContext()).build();
        restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(client));
    }
    return restTemplate;
}