org.springframework.http.client.AsyncClientHttpRequest Java Examples

The following examples show how to use org.springframework.http.client.AsyncClientHttpRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AsyncRestTemplate.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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<T>(method, url, responseFuture, responseExtractor);
	}
	catch (IOException ex) {
		throw new ResourceAccessException("I/O error on " + method.name() +
				" request for \"" + url + "\":" + ex.getMessage(), ex);
	}
}
 
Example #2
Source File: SpringHttpClientTagAdapterTest.java    From wingtips with Apache License 2.0 6 votes vote down vote up
@DataProvider(value = {
    "true   |   spring.asyncresttemplate",
    "false  |   spring.resttemplate"
}, splitBy = "\\|")
@Test
public void getSpanHandlerTagValue_works_as_expected_when_request_is_an_HttpRequestWrapper(
    boolean wrappedRequestIsAsyncClientHttpRequest, String expectedResult
) {
    // given
    HttpRequestWrapper request = mock(HttpRequestWrapper.class);
    HttpRequest wrappedRequest = (wrappedRequestIsAsyncClientHttpRequest)
                                 ? mock(AsyncClientHttpRequest.class)
                                 : mock(HttpRequest.class);

    doReturn(wrappedRequest).when(request).getRequest();

    // when
    String result = implSpy.getSpanHandlerTagValue(request, responseMock);

    // then
    assertThat(result).isEqualTo(expectedResult);
}
 
Example #3
Source File: SpringHttpClientTagAdapterTest.java    From wingtips with Apache License 2.0 6 votes vote down vote up
@DataProvider(value = {
    "true   |   spring.asyncresttemplate",
    "false  |   spring.resttemplate"
}, splitBy = "\\|")
@Test
public void getSpanHandlerTagValue_works_as_expected_when_request_is_not_an_HttpRequestWrapper(
    boolean requestIsAsyncClientHttpRequest, String expectedResult
) {
    // given
    HttpRequest request = (requestIsAsyncClientHttpRequest)
                          ? mock(AsyncClientHttpRequest.class)
                          : mock(HttpRequest.class);

    // when
    String result = implSpy.getSpanHandlerTagValue(request, responseMock);

    // then
    assertThat(result).isEqualTo(expectedResult);
}
 
Example #4
Source File: AsyncRestTemplate.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * 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<T>(method, url, responseFuture, responseExtractor);
	}
	catch (IOException ex) {
		throw new ResourceAccessException("I/O error on " + method.name() +
				" request for \"" + url + "\":" + ex.getMessage(), ex);
	}
}
 
Example #5
Source File: RestRequestInterceptor.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
    Object ret) throws Throwable {
    AsyncClientHttpRequest clientHttpRequest = (AsyncClientHttpRequest) ret;
    if (ret != null) {
        Object[] cacheValues = (Object[]) objInst.getSkyWalkingDynamicField();
        ContextCarrier contextCarrier = (ContextCarrier) cacheValues[1];
        CarrierItem next = contextCarrier.items();
        while (next.hasNext()) {
            next = next.next();
            clientHttpRequest.getHeaders().set(next.getHeadKey(), next.getHeadValue());
        }
    }
    return ret;
}
 
Example #6
Source File: NonBlockingIO.java    From riptide with MIT License 5 votes vote down vote up
@Override
public CompletableFuture<ClientHttpResponse> execute(final RequestArguments arguments) throws IOException {
    final URI uri = arguments.getRequestUri();
    final HttpMethod method = arguments.getMethod();

    final AsyncClientHttpRequest request = requestFactory.createAsyncRequest(uri, method);

    copyTo(arguments.getHeaders(), request.getHeaders());
    arguments.getEntity().writeTo(request);

    return toCompletable(request.executeAsync());
}
 
Example #7
Source File: SpringHttpClientTagAdapter.java    From wingtips with Apache License 2.0 5 votes vote down vote up
@Override
public @Nullable String getSpanHandlerTagValue(@Nullable HttpRequest request, @Nullable ClientHttpResponse response) {
    if (request instanceof HttpRequestWrapper) {
        request = ((HttpRequestWrapper) request).getRequest();
    }

    if (request instanceof AsyncClientHttpRequest) {
        return "spring.asyncresttemplate";
    }

    return "spring.resttemplate";
}
 
Example #8
Source File: AsyncHttpAccessor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new {@link AsyncClientHttpRequest} via this template's {@link
 * AsyncClientHttpRequestFactory}.
 * @param url the URL to connect to
 * @param method the HTTP method to execute (GET, POST, etc.)
 * @return the created request
 * @throws IOException in case of I/O errors
 */
protected AsyncClientHttpRequest createAsyncRequest(URI url, HttpMethod method)
		throws IOException {
	AsyncClientHttpRequest request = getAsyncRequestFactory().createAsyncRequest(url, method);
	if (logger.isDebugEnabled()) {
		logger.debug("Created asynchronous " + method.name() + " request for \"" + url + "\"");
	}
	return request;
}
 
Example #9
Source File: AsyncRestTemplate.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void doWithRequest(final AsyncClientHttpRequest request) throws IOException {
	if (this.adaptee != null) {
		this.adaptee.doWithRequest(new ClientHttpRequest() {
			@Override
			public ClientHttpResponse execute() throws IOException {
				throw new UnsupportedOperationException("execute not supported");
			}
			@Override
			public OutputStream getBody() throws IOException {
				return request.getBody();
			}
			@Override
			public HttpMethod getMethod() {
				return request.getMethod();
			}
			@Override
			public URI getURI() {
				return request.getURI();
			}
			@Override
			public HttpHeaders getHeaders() {
				return request.getHeaders();
			}
		});
	}
}
 
Example #10
Source File: AsyncRestTemplate.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void doWithRequest(final AsyncClientHttpRequest request) throws IOException {
	if (this.adaptee != null) {
		this.adaptee.doWithRequest(new ClientHttpRequest() {
			@Override
			public ClientHttpResponse execute() throws IOException {
				throw new UnsupportedOperationException("execute not supported");
			}
			@Override
			public OutputStream getBody() throws IOException {
				return request.getBody();
			}
			@Override
			public HttpMethod getMethod() {
				return request.getMethod();
			}
			@Override
			public URI getURI() {
				return request.getURI();
			}
			@Override
			public HttpHeaders getHeaders() {
				return request.getHeaders();
			}
		});
	}
}
 
Example #11
Source File: MockRestServiceServer.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException {
	return createRequestInternal(uri, httpMethod);
}
 
Example #12
Source File: RibbonAsyncClientHttpRequestFactory.java    From myfeed with Apache License 2.0 4 votes vote down vote up
@Override
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException {
	return super.createAsyncRequest(expand(uri), httpMethod);
}
 
Example #13
Source File: MultipleAsyncRestTemplateTests.java    From spring-cloud-sleuth with Apache License 2.0 4 votes vote down vote up
@Override
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod)
		throws IOException {
	return this.factory.createAsyncRequest(uri, httpMethod);
}
 
Example #14
Source File: EurekaRestTemplateBuilder.java    From oneplatform with Apache License 2.0 4 votes vote down vote up
@Override
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException {
	uri = convertToRealUri(uri);
	return super.createAsyncRequest(uri, httpMethod);
}
 
Example #15
Source File: AsyncHttpAccessor.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Create a new {@link AsyncClientHttpRequest} via this template's
 * {@link AsyncClientHttpRequestFactory}.
 * @param url the URL to connect to
 * @param method the HTTP method to execute (GET, POST, etc.)
 * @return the created request
 * @throws IOException in case of I/O errors
 */
protected AsyncClientHttpRequest createAsyncRequest(URI url, HttpMethod method) throws IOException {
	AsyncClientHttpRequest request = getAsyncRequestFactory().createAsyncRequest(url, method);
	if (logger.isDebugEnabled()) {
		logger.debug("Created asynchronous " + method.name() + " request for \"" + url + "\"");
	}
	return request;
}
 
Example #16
Source File: AsyncRequestCallback.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Gets called by {@link AsyncRestTemplate#execute} with an opened {@code ClientHttpRequest}.
 * Does not need to care about closing the request or about handling errors:
 * this will all be handled by the {@code RestTemplate}.
 * @param request the active HTTP request
 * @throws java.io.IOException in case of I/O errors
 */
void doWithRequest(AsyncClientHttpRequest request) throws IOException;
 
Example #17
Source File: AsyncRequestCallback.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Gets called by {@link AsyncRestTemplate#execute} with an opened {@code ClientHttpRequest}.
 * Does not need to care about closing the request or about handling errors:
 * this will all be handled by the {@code RestTemplate}.
 * @param request the active HTTP request
 * @throws java.io.IOException in case of I/O errors
 */
void doWithRequest(AsyncClientHttpRequest request) throws IOException;