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

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

Introduction

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

Prototype

SimpleClientHttpRequestFactory

Source Link

Usage

From source file:org.zalando.boot.etcd.EtcdClient.java

/**
 * {@inheritDoc}//  ww w.j a va2s.  c  om
 * 
 * @see InitializingBean#afterPropertiesSet()
 */
@Override
public void afterPropertiesSet() throws Exception {
    if (this.requestFactory == null) {
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(1000);
        requestFactory.setReadTimeout(3000);
        this.requestFactory = requestFactory;
    }

    template = new RestTemplate(this.requestFactory);
    template.setMessageConverters(Arrays.asList(requestConverter, responseConverter));

    if (locationUpdaterEnabled) {
        Runnable worker = new Runnable() {
            @Override
            public void run() {
                updateMembers();
            }
        };
        locationUpdater.scheduleAtFixedRate(worker, 5000, 5000, TimeUnit.MILLISECONDS);
    }
}

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

@Test
public void connectTimeoutCanBeConfiguredOnAWrappedRequestFactory() {
    SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
    this.builder.requestFactory(new BufferingClientHttpRequestFactory(requestFactory)).setConnectTimeout(1234)
            .build();/* ww  w.j  ava 2 s  . c  om*/
    assertThat(ReflectionTestUtils.getField(requestFactory, "connectTimeout")).isEqualTo(1234);
}

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

@Test
public void readTimeoutCanBeConfiguredOnAWrappedRequestFactory() {
    SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
    this.builder.requestFactory(new BufferingClientHttpRequestFactory(requestFactory)).setReadTimeout(1234)
            .build();//  w  ww.j ava 2s. com
    assertThat(ReflectionTestUtils.getField(requestFactory, "readTimeout")).isEqualTo(1234);
}

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

@Test
public void unwrappingDoesNotAffectRequestFactoryThatIsSetOnTheBuiltTemplate() {
    SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
    RestTemplate template = this.builder.requestFactory(new BufferingClientHttpRequestFactory(requestFactory))
            .build();/* ww w .j a  va2s  .co  m*/
    assertThat(template.getRequestFactory()).isInstanceOf(BufferingClientHttpRequestFactory.class);
}

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

private ClientHttpRequestFactory detectRequestFactory() {
    for (Map.Entry<String, String> candidate : REQUEST_FACTORY_CANDIDATES.entrySet()) {
        ClassLoader classLoader = getClass().getClassLoader();
        if (ClassUtils.isPresent(candidate.getKey(), classLoader)) {
            Class<?> factoryClass = ClassUtils.resolveClassName(candidate.getValue(), classLoader);
            return (ClientHttpRequestFactory) BeanUtils.instantiateClass(factoryClass);
        }//from www  . j ava  2s . c  o  m
    }
    return new SimpleClientHttpRequestFactory();
}

From source file:org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfigurationTests.java

public boolean hasHeader(String url, int port, String header) throws Exception {
    SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
    ClientHttpRequest request = clientHttpRequestFactory
            .createRequest(new URI("http://localhost:" + port + url), HttpMethod.GET);
    ClientHttpResponse response = request.execute();
    return response.getHeaders().containsKey(header);
}

From source file:org.apache.geode.management.internal.web.http.support.HttpRequester.java

HttpRequester(Properties securityProperties, RestTemplate restTemplate) {
    final SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
    this.securityProperties = securityProperties;
    if (restTemplate == null) {
        this.restTemplate = new RestTemplate(clientHttpRequestFactory);
    } else {/* w  w w .  j a  v a2s.c  o m*/
        this.restTemplate = restTemplate;
    }

    // add our custom HttpMessageConverter for serializing DTO Objects into the HTTP request message
    // body and de-serializing HTTP response message body content back into DTO Objects
    List<HttpMessageConverter<?>> converters = this.restTemplate.getMessageConverters();
    // remove the MappingJacksonHttpConverter
    for (int i = converters.size() - 1; i >= 0; i--) {
        HttpMessageConverter converter = converters.get(i);
        if (converter instanceof MappingJackson2HttpMessageConverter) {
            converters.remove(converter);
        }
    }
    converters.add(new SerializableObjectHttpMessageConverter());

    this.restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
        @Override
        public void handleError(final ClientHttpResponse response) throws IOException {
            String body = IOUtils.toString(response.getBody(), StandardCharsets.UTF_8);
            final String message = String.format("The HTTP request failed with: %1$d - %2$s.",
                    response.getRawStatusCode(), body);

            if (response.getRawStatusCode() == 401) {
                throw new AuthenticationFailedException(message);
            } else if (response.getRawStatusCode() == 403) {
                throw new NotAuthorizedException(message);
            } else {
                throw new RuntimeException(message);
            }
        }
    });
}

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

public RestOperations createRestTemplate() {
    RestTemplate client = new RestTemplate();
    client.setRequestFactory(new SimpleClientHttpRequestFactory() {
        @Override//from ww w. j a  v  a  2 s  . c  o m
        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.app.integration.ServerRunning.java

public RestOperations createRestTemplate() {
    RestTemplate client = new RestTemplate(new SimpleClientHttpRequestFactory() {
        @Override// ww w.  j a v  a2  s.  co m
        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;
}