Search in sources :

Example 1 with ClientHttpResponse

use of org.springframework.http.client.ClientHttpResponse in project spring-framework by spring-projects.

the class AsyncRestTemplate method doExecute.

/**
	 * Execute the given method on the provided URI. The
	 * {@link org.springframework.http.client.ClientHttpRequest}
	 * is processed using the {@link RequestCallback}; the response with
	 * the {@link ResponseExtractor}.
	 * @param url the fully-expanded URL to connect to
	 * @param method the HTTP method to execute (GET, POST, etc.)
	 * @param requestCallback object that prepares the request (can be {@code null})
	 * @param responseExtractor object that extracts the return value from the response (can
	 * be {@code null})
	 * @return an arbitrary object, as returned by the {@link ResponseExtractor}
	 */
protected <T> ListenableFuture<T> doExecute(URI url, HttpMethod method, AsyncRequestCallback requestCallback, ResponseExtractor<T> responseExtractor) throws RestClientException {
    Assert.notNull(url, "'url' must not be null");
    Assert.notNull(method, "'method' must not be null");
    try {
        AsyncClientHttpRequest request = createAsyncRequest(url, method);
        if (requestCallback != null) {
            requestCallback.doWithRequest(request);
        }
        ListenableFuture<ClientHttpResponse> responseFuture = request.executeAsync();
        return new ResponseExtractorFuture<>(method, url, responseFuture, responseExtractor);
    } catch (IOException ex) {
        throw new ResourceAccessException("I/O error on " + method.name() + " request for \"" + url + "\":" + ex.getMessage(), ex);
    }
}
Also used : AsyncClientHttpRequest(org.springframework.http.client.AsyncClientHttpRequest) IOException(java.io.IOException) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse)

Example 2 with ClientHttpResponse

use of org.springframework.http.client.ClientHttpResponse in project spring-framework by spring-projects.

the class AbstractRequestExpectationManager method validateRequest.

@Override
public ClientHttpResponse validateRequest(ClientHttpRequest request) throws IOException {
    synchronized (this.lock) {
        if (getRequests().isEmpty()) {
            afterExpectationsDeclared();
        }
        ClientHttpResponse response = validateRequestInternal(request);
        getRequests().add(request);
        return response;
    }
}
Also used : ClientHttpResponse(org.springframework.http.client.ClientHttpResponse)

Example 3 with ClientHttpResponse

use of org.springframework.http.client.ClientHttpResponse in project spring-framework by spring-projects.

the class SimpleRequestExpectationManager method validateRequestInternal.

@Override
public ClientHttpResponse validateRequestInternal(ClientHttpRequest request) throws IOException {
    RequestExpectation expectation;
    try {
        expectation = next(request);
        expectation.match(request);
    } catch (AssertionError error) {
        expectation = this.repeatExpectations.findExpectation(request);
        if (expectation == null) {
            throw error;
        }
    }
    ClientHttpResponse response = expectation.createResponse(request);
    this.repeatExpectations.update(expectation);
    return response;
}
Also used : ClientHttpResponse(org.springframework.http.client.ClientHttpResponse)

Example 4 with ClientHttpResponse

use of org.springframework.http.client.ClientHttpResponse in project spring-boot by spring-projects.

the class AbstractServletWebServerFactoryTests method restartWithKeepAlive.

@Test
public void restartWithKeepAlive() throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    this.webServer = factory.getWebServer(exampleServletRegistration());
    this.webServer.start();
    HttpComponentsAsyncClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsAsyncClientHttpRequestFactory();
    ListenableFuture<ClientHttpResponse> response1 = clientHttpRequestFactory.createAsyncRequest(new URI(getLocalUrl("/hello")), HttpMethod.GET).executeAsync();
    assertThat(response1.get(10, TimeUnit.SECONDS).getRawStatusCode()).isEqualTo(200);
    this.webServer.stop();
    this.webServer = factory.getWebServer(exampleServletRegistration());
    this.webServer.start();
    ListenableFuture<ClientHttpResponse> response2 = clientHttpRequestFactory.createAsyncRequest(new URI(getLocalUrl("/hello")), HttpMethod.GET).executeAsync();
    assertThat(response2.get(10, TimeUnit.SECONDS).getRawStatusCode()).isEqualTo(200);
    clientHttpRequestFactory.destroy();
}
Also used : URI(java.net.URI) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) HttpComponentsAsyncClientHttpRequestFactory(org.springframework.http.client.HttpComponentsAsyncClientHttpRequestFactory) Test(org.junit.Test)

Example 5 with ClientHttpResponse

use of org.springframework.http.client.ClientHttpResponse in project spring-boot by spring-projects.

the class EndpointWebMvcAutoConfigurationTests method assertContent.

private void assertContent(String scheme, String url, int port, Object expected) throws Exception {
    SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build());
    HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
    ClientHttpRequest request = requestFactory.createRequest(new URI(scheme + "://localhost:" + port + url), HttpMethod.GET);
    try {
        ClientHttpResponse response = request.execute();
        if (HttpStatus.NOT_FOUND.equals(response.getStatusCode())) {
            throw new FileNotFoundException();
        }
        try {
            String actual = StreamUtils.copyToString(response.getBody(), Charset.forName("UTF-8"));
            if (expected instanceof Matcher) {
                assertThat(actual).is(Matched.by((Matcher<?>) expected));
            } else {
                assertThat(actual).isEqualTo(expected);
            }
        } finally {
            response.close();
        }
    } catch (Exception ex) {
        if (expected == null) {
            if (SocketException.class.isInstance(ex) || FileNotFoundException.class.isInstance(ex)) {
                return;
            }
        }
        throw ex;
    }
}
Also used : Matcher(org.hamcrest.Matcher) HttpClient(org.apache.http.client.HttpClient) FileNotFoundException(java.io.FileNotFoundException) HttpComponentsClientHttpRequestFactory(org.springframework.http.client.HttpComponentsClientHttpRequestFactory) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) URI(java.net.URI) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy) FileNotFoundException(java.io.FileNotFoundException) WebServerException(org.springframework.boot.web.server.WebServerException) SocketException(java.net.SocketException) ExpectedException(org.junit.rules.ExpectedException)

Aggregations

ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)132 IOException (java.io.IOException)38 Test (org.junit.Test)36 HttpHeaders (org.springframework.http.HttpHeaders)34 ResponseErrorHandler (org.springframework.web.client.ResponseErrorHandler)23 ByteArrayInputStream (java.io.ByteArrayInputStream)22 URI (java.net.URI)22 Test (org.junit.jupiter.api.Test)21 ClientHttpRequest (org.springframework.http.client.ClientHttpRequest)20 RestTemplate (org.springframework.web.client.RestTemplate)18 DefaultResponseErrorHandler (org.springframework.web.client.DefaultResponseErrorHandler)16 HttpComponentsClientHttpRequestFactory (org.springframework.http.client.HttpComponentsClientHttpRequestFactory)14 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 MockClientHttpResponse (org.springframework.mock.http.client.MockClientHttpResponse)9 ResponseExtractor (org.springframework.web.client.ResponseExtractor)9 Tag (io.micrometer.core.instrument.Tag)8 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)6 ServiceError (com.kixeye.chassis.transport.dto.ServiceError)5