Java Code Examples for org.springframework.web.client.RestTemplate#getRequestFactory()

The following examples show how to use org.springframework.web.client.RestTemplate#getRequestFactory() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: spring-vault   File: VaultInitializer.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Create a new {@link VaultInitializer} with the given {@link SslConfiguration} and
 * {@link VaultEndpoint}.
 * @param sslConfiguration must not be {@literal null}.
 * @param vaultEndpoint must not be {@literal null}.
 */
public VaultInitializer(SslConfiguration sslConfiguration, VaultEndpoint vaultEndpoint) {

	Assert.notNull(sslConfiguration, "SslConfiguration must not be null");
	Assert.notNull(vaultEndpoint, "VaultEndpoint must not be null");

	RestTemplate restTemplate = TestRestTemplateFactory.create(sslConfiguration);
	WebClient webClient = TestWebClientFactory.create(sslConfiguration);

	VaultTemplate vaultTemplate = new VaultTemplate(TestRestTemplateFactory.TEST_VAULT_ENDPOINT,
			restTemplate.getRequestFactory(), new PreparingSessionManager());

	this.token = Settings.token();

	this.prepareVault = new PrepareVault(webClient, TestRestTemplateFactory.create(sslConfiguration),
			vaultTemplate);
	this.vaultEndpoint = vaultEndpoint;
}
 
Example 2
Source Project: Profiles-Blog   File: PayloadSender.java    License: MIT License 6 votes vote down vote up
@PostConstruct
public void initialize() {
    restTemplate = new RestTemplate();

    // Loose JSON serialization defaults
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    objectMapper.setSerializationInclusion(Include.NON_NULL);

    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(objectMapper);
    restTemplate.setMessageConverters(Arrays.asList(converter));

    // Timeout settings
    SimpleClientHttpRequestFactory requestFactory = (SimpleClientHttpRequestFactory) restTemplate.getRequestFactory();
    int tenSeconds = 10000;
    requestFactory.setReadTimeout(tenSeconds);
    requestFactory.setConnectTimeout(tenSeconds);
}
 
Example 3
Source Project: rqueue   File: HttpUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static <T> T readUrl(String url, Class<T> clazz) {
  try {
    RestTemplate restTemplate = new RestTemplate();
    SimpleClientHttpRequestFactory rf =
        (SimpleClientHttpRequestFactory) restTemplate.getRequestFactory();
    rf.setReadTimeout(2 * Constants.ONE_MILLI_INT);
    rf.setConnectTimeout(2 * Constants.ONE_MILLI_INT);
    return restTemplate.getForObject(url, clazz);
  } catch (Exception e) {
    log.error("GET call failed for {}", url, e);
    return null;
  }
}
 
Example 4
private void setTimeout(RestTemplate restTemplate, int connectTimeout, int readTimeout) {
    restTemplate.setRequestFactory(new SimpleClientHttpRequestFactory());
    SimpleClientHttpRequestFactory rf = (SimpleClientHttpRequestFactory) restTemplate
            .getRequestFactory();
    rf.setReadTimeout(readTimeout);
    rf.setConnectTimeout(connectTimeout);
}
 
Example 5
private void setTimeout(RestTemplate restTemplate, int connectTimeout, int readTimeout) {
    restTemplate.setRequestFactory(new SimpleClientHttpRequestFactory());
    SimpleClientHttpRequestFactory rf = (SimpleClientHttpRequestFactory) restTemplate
            .getRequestFactory();
    rf.setReadTimeout(readTimeout);
    rf.setConnectTimeout(connectTimeout);
}
 
Example 6
private static void setTimeout(RestTemplate restTemplate, int connectTimeout, int readTimeout) {
    restTemplate.setRequestFactory(new SimpleClientHttpRequestFactory());
    SimpleClientHttpRequestFactory rf = (SimpleClientHttpRequestFactory) restTemplate
            .getRequestFactory();
    rf.setReadTimeout(readTimeout);
    rf.setConnectTimeout(connectTimeout);
}
 
Example 7
@Test
public void serverDoesNotBuffer() {
	// gh-50
	ArtifactoryServer server = this.artifactory.server("https://example.com", null, null);
	RestTemplate restTemplate = (RestTemplate) ReflectionTestUtils.getField(server, "restTemplate");
	SimpleClientHttpRequestFactory requestFactory = (SimpleClientHttpRequestFactory) restTemplate
			.getRequestFactory();
	assertThat(ReflectionTestUtils.getField(requestFactory, "bufferRequestBody")).isEqualTo(Boolean.FALSE);
	assertThat(ReflectionTestUtils.getField(requestFactory, "outputStreaming")).isEqualTo(Boolean.TRUE);
}
 
Example 8
Source Project: apollo   File: CtripMQService.java    License: Apache License 2.0 5 votes vote down vote up
@PostConstruct
public void init() {
  restTemplate = new RestTemplate();

  SimpleClientHttpRequestFactory rf = (SimpleClientHttpRequestFactory) restTemplate.getRequestFactory();
  rf.setReadTimeout(portalConfig.readTimeout());
  rf.setConnectTimeout(portalConfig.connectTimeout());

  MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
  converter.setSupportedMediaTypes(
      Arrays.asList(MediaType.APPLICATION_JSON_UTF8, MediaType.APPLICATION_OCTET_STREAM));

  restTemplate.setMessageConverters(Arrays.asList(converter, new FormHttpMessageConverter()));

}
 
Example 9
@Bean
@ConditionalOnClass(SSLContextBuilder.class)
@ConditionalOnProperty(value = "wiremock.rest-template-ssl-enabled",
		matchIfMissing = true)
public RestTemplateCustomizer wiremockRestTemplateCustomizer() {
	return new RestTemplateCustomizer() {
		@Override
		public void customize(RestTemplate restTemplate) {
			if (restTemplate
					.getRequestFactory() instanceof HttpComponentsClientHttpRequestFactory) {
				HttpComponentsClientHttpRequestFactory factory = (HttpComponentsClientHttpRequestFactory) restTemplate
						.getRequestFactory();
				factory.setHttpClient(createSslHttpClient());
			}
		}

		private HttpClient createSslHttpClient() {
			try {
				SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
						new SSLContextBuilder().loadTrustMaterial(null,
								TrustSelfSignedStrategy.INSTANCE).build(),
						NoopHostnameVerifier.INSTANCE);
				return HttpClients.custom().setSSLSocketFactory(socketFactory)
						.build();
			}
			catch (Exception ex) {
				throw new IllegalStateException("Unable to create SSL HttpClient",
						ex);
			}
		}
	};
}
 
Example 10
private void setAuthorizingRequestFactory(RestTemplate restTemplate, CloudCredentials credentials, OAuthClient oAuthClient) {
    ClientHttpRequestFactory requestFactory = restTemplate.getRequestFactory();
    if (!(requestFactory instanceof CloudControllerRestClientHttpRequestFactory)) {
        restTemplate.setRequestFactory(new CloudControllerRestClientHttpRequestFactory(requestFactory, credentials, oAuthClient));
    }
}
 
Example 11
@Before
public void setup() {
	RestTemplate restTemplate = new RestTemplate();
	this.server = MockRestServiceServer.createServer(restTemplate);
	this.factory = restTemplate.getRequestFactory();
}
 
Example 12
Source Project: raml-tester   File: RamlRestTemplate.java    License: Apache License 2.0 4 votes vote down vote up
private RamlRestTemplate(RamlChecker ramlChecker, boolean notSending, ReportStore reportStore, RestTemplate restTemplate) {
    this(ramlChecker, notSending, reportStore, restTemplate.getRequestFactory());
    init(restTemplate);
}