Example usage for org.springframework.web.client RequestCallback doWithRequest

List of usage examples for org.springframework.web.client RequestCallback doWithRequest

Introduction

In this page you can find the example usage for org.springframework.web.client RequestCallback doWithRequest.

Prototype

void doWithRequest(ClientHttpRequest request) throws IOException;

Source Link

Document

Gets called by RestTemplate#execute with an opened ClientHttpRequest .

Usage

From source file:com.alexshabanov.springrestapi.restapitest.RestOperationsTestClient.java

@Override
protected <T> T doExecute(URI url, HttpMethod method, RequestCallback requestCallback,
        ResponseExtractor<T> responseExtractor) throws RestClientException {
    try {//from   ww w .ja  v  a  2 s  . co  m
        final ClientHttpRequest clientHttpRequest = createRequest(url, method);

        if (requestCallback != null) {
            requestCallback.doWithRequest(clientHttpRequest);
        }

        final MockHttpServletResponse mockHttpServletResponse = testSupport
                .handle(toMockHttpServletRequest(url, method, clientHttpRequest));

        final ClientHttpResponse clientHttpResponse = new ClientHttpResponseAdapter(mockHttpServletResponse);

        // translate error statuses
        if (getErrorHandler().hasError(clientHttpResponse)) {
            getErrorHandler().handleError(clientHttpResponse);
        }

        return responseExtractor == null ? null : responseExtractor.extractData(clientHttpResponse);
    } catch (IOException e) {
        throw new ResourceAccessException("Can't access the resource provided: " + url, e);
    }
}

From source file:org.springframework.web.client.RestTemplate.java

/**
 * Execute the given method on the provided URI.
 * <p>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})
 * @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}
 *//*from w  w w  . j a  v a 2  s .c om*/
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 on " + method.name() + " request for \"" + url + "\": " + ex.getMessage(), ex);
    } finally {
        if (response != null) {
            response.close();
        }
    }
}

From source file:software.coolstuff.springframework.owncloud.service.impl.rest.AbstractPipedStreamRestSynchronizerImpl.java

private void wrapRequestCallback(ClientHttpRequest clientHttpRequest, RequestCallback requestCallback)
        throws IOException {
    log.debug("Execute {} on {}", clientHttpRequest.getMethod(), clientHttpRequest.getURI());
    OwncloudRestUtils.addAuthorizationHeader(clientHttpRequest.getHeaders(), getAuthentication());
    addKeepAliveConnectionHeader(clientHttpRequest);
    if (requestCallback != null) {
        requestCallback.doWithRequest(clientHttpRequest);
    }//from   ww w .j  ava  2  s  . c  om
}