Search in sources :

Example 6 with ClientHttpResponse

use of org.springframework.http.client.ClientHttpResponse 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 7 with ClientHttpResponse

use of org.springframework.http.client.ClientHttpResponse 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 8 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 9 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 10 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)70 Test (org.junit.Test)35 IOException (java.io.IOException)25 ResponseErrorHandler (org.springframework.web.client.ResponseErrorHandler)21 URI (java.net.URI)17 HttpHeaders (org.springframework.http.HttpHeaders)17 DefaultResponseErrorHandler (org.springframework.web.client.DefaultResponseErrorHandler)15 ClientHttpRequest (org.springframework.http.client.ClientHttpRequest)14 HttpComponentsClientHttpRequestFactory (org.springframework.http.client.HttpComponentsClientHttpRequestFactory)11 RestTemplate (org.springframework.web.client.RestTemplate)11 ResponseExtractor (org.springframework.web.client.ResponseExtractor)10 ByteArrayInputStream (java.io.ByteArrayInputStream)7 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)7 ServiceError (com.kixeye.chassis.transport.dto.ServiceError)6 SerDeHttpMessageConverter (com.kixeye.chassis.transport.http.SerDeHttpMessageConverter)6 MessageSerDe (com.kixeye.chassis.transport.serde.MessageSerDe)6 JsonJacksonMessageSerDe (com.kixeye.chassis.transport.serde.converter.JsonJacksonMessageSerDe)6 ProtobufMessageSerDe (com.kixeye.chassis.transport.serde.converter.ProtobufMessageSerDe)6 XmlMessageSerDe (com.kixeye.chassis.transport.serde.converter.XmlMessageSerDe)6 YamlJacksonMessageSerDe (com.kixeye.chassis.transport.serde.converter.YamlJacksonMessageSerDe)6