Search in sources :

Example 1 with ClientHttpRequest

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

the class RootUriRequestExpectationManagerTests method validateRequestWhenUriDoesNotStartWithRootUriShouldDelegateToExpectationManager.

@Test
public void validateRequestWhenUriDoesNotStartWithRootUriShouldDelegateToExpectationManager() throws Exception {
    ClientHttpRequest request = mock(ClientHttpRequest.class);
    given(request.getURI()).willReturn(new URI("http://spring.io/test"));
    this.manager.validateRequest(request);
    verify(this.delegate).validateRequest(request);
}
Also used : ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) URI(java.net.URI) Test(org.junit.Test)

Example 2 with ClientHttpRequest

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

the class ServletWebServerMvcIntegrationTests method doTest.

private void doTest(AnnotationConfigServletWebServerApplicationContext context, String resourcePath) throws Exception {
    SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
    ClientHttpRequest request = clientHttpRequestFactory.createRequest(new URI("http://localhost:" + context.getWebServer().getPort() + resourcePath), HttpMethod.GET);
    ClientHttpResponse response = request.execute();
    try {
        String actual = StreamUtils.copyToString(response.getBody(), Charset.forName("UTF-8"));
        assertThat(actual).isEqualTo("Hello World");
    } finally {
        response.close();
    }
}
Also used : SimpleClientHttpRequestFactory(org.springframework.http.client.SimpleClientHttpRequestFactory) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) URI(java.net.URI) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse)

Example 3 with ClientHttpRequest

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

the class ClassPathChangeUploader method performUpload.

private void performUpload(ClassLoaderFiles classLoaderFiles, byte[] bytes) throws IOException {
    try {
        while (true) {
            try {
                ClientHttpRequest request = this.requestFactory.createRequest(this.uri, HttpMethod.POST);
                HttpHeaders headers = request.getHeaders();
                headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
                headers.setContentLength(bytes.length);
                FileCopyUtils.copy(bytes, request.getBody());
                ClientHttpResponse response = request.execute();
                Assert.state(response.getStatusCode() == HttpStatus.OK, "Unexpected " + response.getStatusCode() + " response uploading class files");
                logUpload(classLoaderFiles);
                return;
            } catch (ConnectException ex) {
                logger.warn("Failed to connect when uploading to " + this.uri + ". Upload will be retried in 2 seconds");
                Thread.sleep(2000);
            }
        }
    } catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
        throw new IllegalStateException(ex);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) ConnectException(java.net.ConnectException)

Example 4 with ClientHttpRequest

use of org.springframework.http.client.ClientHttpRequest in project spring-data-document-examples by spring-projects.

the class RestTemplate method doExecute.

/**
	 * Execute the given method on the provided URI. The {@link 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</code>)
	 * @param responseExtractor object that extracts the return value from the response (can be <code>null</code>)
	 * @return an arbitrary object, as returned by the {@link ResponseExtractor}
	 */
protected <T> T doExecute(URI url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor) throws RestClientException {
    Assert.notNull(url, "'url' must not be null");
    Assert.notNull(method, "'method' must not be null");
    ClientHttpResponse response = null;
    try {
        ClientHttpRequest request = createRequest(url, method);
        if (requestCallback != null) {
            requestCallback.doWithRequest(request);
        }
        response = request.execute();
        if (!getErrorHandler().hasError(response)) {
            logResponseStatus(method, url, response);
        } else {
            handleResponseError(method, url, response);
        }
        if (responseExtractor != null) {
            return responseExtractor.extractData(response);
        } else {
            return null;
        }
    } catch (IOException ex) {
        throw new ResourceAccessException("I/O error: " + ex.getMessage(), ex);
    } finally {
        if (response != null) {
            response.close();
        }
    }
}
Also used : IOException(java.io.IOException) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse)

Example 5 with ClientHttpRequest

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

the class AbstractRequestExpectationManager method getRequestDetails.

/**
	 * Return details of executed requests.
	 */
protected String getRequestDetails() {
    StringBuilder sb = new StringBuilder();
    sb.append(getRequests().size()).append(" request(s) executed");
    if (!getRequests().isEmpty()) {
        sb.append(":\n");
        for (ClientHttpRequest request : getRequests()) {
            sb.append(request.toString()).append("\n");
        }
    } else {
        sb.append(".\n");
    }
    return sb.toString();
}
Also used : ClientHttpRequest(org.springframework.http.client.ClientHttpRequest)

Aggregations

ClientHttpRequest (org.springframework.http.client.ClientHttpRequest)36 URI (java.net.URI)19 Test (org.junit.Test)13 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)13 IOException (java.io.IOException)11 HttpMethod (org.springframework.http.HttpMethod)9 ClientHttpRequestFactory (org.springframework.http.client.ClientHttpRequestFactory)9 HttpHeaders (org.springframework.http.HttpHeaders)6 MockClientHttpRequest (org.springframework.mock.http.client.MockClientHttpRequest)6 RequestMatcher (org.springframework.test.web.client.RequestMatcher)6 SimpleClientHttpRequestFactory (org.springframework.http.client.SimpleClientHttpRequestFactory)4 AccessTokenRequest (org.springframework.security.oauth2.client.token.AccessTokenRequest)4 DefaultAccessTokenRequest (org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest)4 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ArrayList (java.util.ArrayList)2 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)2 MediaType (org.springframework.http.MediaType)2 ClientHttpRequestInterceptor (org.springframework.http.client.ClientHttpRequestInterceptor)2 HttpComponentsClientHttpRequestFactory (org.springframework.http.client.HttpComponentsClientHttpRequestFactory)2