Search in sources :

Example 11 with ClientHttpResponse

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

the class EndpointWebMvcAutoConfigurationTests method hasHeader.

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);
}
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 12 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 13 with ClientHttpResponse

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

the class RestTemplateXhrTransportTests method connectReceiveAndCloseWithPrelude.

@Test
public void connectReceiveAndCloseWithPrelude() throws Exception {
    StringBuilder sb = new StringBuilder(2048);
    for (int i = 0; i < 2048; i++) {
        sb.append('h');
    }
    String body = sb.toString() + "\n" + "o\n" + "a[\"foo\"]\n" + "c[3000,\"Go away!\"]";
    ClientHttpResponse response = response(HttpStatus.OK, body);
    connect(response);
    verify(this.webSocketHandler).afterConnectionEstablished(any());
    verify(this.webSocketHandler).handleMessage(any(), eq(new TextMessage("foo")));
    verify(this.webSocketHandler).afterConnectionClosed(any(), eq(new CloseStatus(3000, "Go away!")));
    verifyNoMoreInteractions(this.webSocketHandler);
}
Also used : CloseStatus(org.springframework.web.socket.CloseStatus) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Example 14 with ClientHttpResponse

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

the class RestTemplateXhrTransportTests method connectReceiveAndClose.

@Test
public void connectReceiveAndClose() throws Exception {
    String body = "o\n" + "a[\"foo\"]\n" + "c[3000,\"Go away!\"]";
    ClientHttpResponse response = response(HttpStatus.OK, body);
    connect(response);
    verify(this.webSocketHandler).afterConnectionEstablished(any());
    verify(this.webSocketHandler).handleMessage(any(), eq(new TextMessage("foo")));
    verify(this.webSocketHandler).afterConnectionClosed(any(), eq(new CloseStatus(3000, "Go away!")));
    verifyNoMoreInteractions(this.webSocketHandler);
}
Also used : CloseStatus(org.springframework.web.socket.CloseStatus) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Example 15 with ClientHttpResponse

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

the class RestTemplateXhrTransportTests method responseClosedAfterDisconnected.

@Test
public void responseClosedAfterDisconnected() throws Exception {
    String body = "o\n" + "c[3000,\"Go away!\"]\n" + "a[\"foo\"]\n";
    ClientHttpResponse response = response(HttpStatus.OK, body);
    connect(response);
    verify(this.webSocketHandler).afterConnectionEstablished(any());
    verify(this.webSocketHandler).afterConnectionClosed(any(), any());
    verifyNoMoreInteractions(this.webSocketHandler);
    verify(response).close();
}
Also used : ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) Test(org.junit.Test)

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