Search in sources :

Example 1 with EmptyContent

use of com.google.api.client.http.EmptyContent in project google-api-java-client by google.

the class AbstractGoogleClientRequestTest method subtestBuildHttpRequest_emptyContent.

private void subtestBuildHttpRequest_emptyContent(String method, boolean expectEmptyContent) throws Exception {
    HttpTransport transport = new MockHttpTransport();
    MockGoogleClient client = new MockGoogleClient.Builder(transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null).setApplicationName("Test Application").build();
    MockGoogleClientRequest<String> request = new MockGoogleClientRequest<String>(client, method, URI_TEMPLATE, null, String.class);
    HttpRequest httpRequest = request.buildHttpRequest();
    if (expectEmptyContent) {
        assertTrue(httpRequest.getContent() instanceof EmptyContent);
    } else {
        assertNull(httpRequest.getContent());
    }
}
Also used : LowLevelHttpRequest(com.google.api.client.http.LowLevelHttpRequest) HttpRequest(com.google.api.client.http.HttpRequest) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) HttpTransport(com.google.api.client.http.HttpTransport) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockGoogleClient(com.google.api.client.googleapis.testing.services.MockGoogleClient) MockGoogleClientRequest(com.google.api.client.googleapis.testing.services.MockGoogleClientRequest) EmptyContent(com.google.api.client.http.EmptyContent)

Example 2 with EmptyContent

use of com.google.api.client.http.EmptyContent in project google-api-java-client by google.

the class MediaHttpUploader method serverErrorCallback.

/**
 * {@link Beta} <br/>
 * The call back method that will be invoked on a server error or an I/O exception during
 * resumable upload inside {@link #upload}.
 *
 * <p>
 * This method changes the current request to query the current status of the upload to find how
 * many bytes were successfully uploaded before the server error occurred.
 * </p>
 */
@Beta
void serverErrorCallback() throws IOException {
    Preconditions.checkNotNull(currentRequest, "The current request should not be null");
    // Query the current status of the upload by issuing an empty PUT request on the upload URI.
    currentRequest.setContent(new EmptyContent());
    currentRequest.getHeaders().setContentRange("bytes */" + mediaContentLengthStr);
}
Also used : EmptyContent(com.google.api.client.http.EmptyContent) Beta(com.google.api.client.util.Beta)

Example 3 with EmptyContent

use of com.google.api.client.http.EmptyContent in project google-api-java-client by google.

the class AbstractGoogleClientRequest method buildHttpRequest.

/**
 * Create a request suitable for use against this service.
 */
private HttpRequest buildHttpRequest(boolean usingHead) throws IOException {
    Preconditions.checkArgument(uploader == null);
    Preconditions.checkArgument(!usingHead || requestMethod.equals(HttpMethods.GET));
    String requestMethodToUse = usingHead ? HttpMethods.HEAD : requestMethod;
    final HttpRequest httpRequest = getAbstractGoogleClient().getRequestFactory().buildRequest(requestMethodToUse, buildHttpRequestUrl(), httpContent);
    new MethodOverride().intercept(httpRequest);
    httpRequest.setParser(getAbstractGoogleClient().getObjectParser());
    // custom methods may use POST with no content but require a Content-Length header
    if (httpContent == null && (requestMethod.equals(HttpMethods.POST) || requestMethod.equals(HttpMethods.PUT) || requestMethod.equals(HttpMethods.PATCH))) {
        httpRequest.setContent(new EmptyContent());
    }
    httpRequest.getHeaders().putAll(requestHeaders);
    if (!disableGZipContent) {
        httpRequest.setEncoding(new GZipEncoding());
    }
    final HttpResponseInterceptor responseInterceptor = httpRequest.getResponseInterceptor();
    httpRequest.setResponseInterceptor(new HttpResponseInterceptor() {

        public void interceptResponse(HttpResponse response) throws IOException {
            if (responseInterceptor != null) {
                responseInterceptor.interceptResponse(response);
            }
            if (!response.isSuccessStatusCode() && httpRequest.getThrowExceptionOnExecuteError()) {
                throw newExceptionOnError(response);
            }
        }
    });
    return httpRequest;
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) GZipEncoding(com.google.api.client.http.GZipEncoding) HttpResponseInterceptor(com.google.api.client.http.HttpResponseInterceptor) HttpResponse(com.google.api.client.http.HttpResponse) IOException(java.io.IOException) MethodOverride(com.google.api.client.googleapis.MethodOverride) EmptyContent(com.google.api.client.http.EmptyContent)

Example 4 with EmptyContent

use of com.google.api.client.http.EmptyContent in project google-api-java-client by google.

the class MethodOverride method intercept.

public void intercept(HttpRequest request) throws IOException {
    if (overrideThisMethod(request)) {
        String requestMethod = request.getRequestMethod();
        request.setRequestMethod(HttpMethods.POST);
        request.getHeaders().set(HEADER, requestMethod);
        if (requestMethod.equals(HttpMethods.GET)) {
            // take the URI query part and put it into the HTTP body
            request.setContent(new UrlEncodedContent(request.getUrl().clone()));
            // remove query parameters from URI
            request.getUrl().clear();
        } else if (request.getContent() == null) {
            // Google servers will fail to process a POST unless the Content-Length header is specified
            request.setContent(new EmptyContent());
        }
    }
}
Also used : UrlEncodedContent(com.google.api.client.http.UrlEncodedContent) EmptyContent(com.google.api.client.http.EmptyContent)

Example 5 with EmptyContent

use of com.google.api.client.http.EmptyContent in project google-api-java-client by google.

the class MediaHttpUploader method executeUploadInitiation.

/**
 * This method sends a POST request with empty content to get the unique upload URL.
 *
 * @param initiationRequestUrl The request URL where the initiation request will be sent
 */
private HttpResponse executeUploadInitiation(GenericUrl initiationRequestUrl) throws IOException {
    updateStateAndNotifyListener(UploadState.INITIATION_STARTED);
    initiationRequestUrl.put("uploadType", "resumable");
    HttpContent content = metadata == null ? new EmptyContent() : metadata;
    HttpRequest request = requestFactory.buildRequest(initiationRequestMethod, initiationRequestUrl, content);
    initiationHeaders.set(CONTENT_TYPE_HEADER, mediaContent.getType());
    if (isMediaLengthKnown()) {
        initiationHeaders.set(CONTENT_LENGTH_HEADER, getMediaContentLength());
    }
    request.getHeaders().putAll(initiationHeaders);
    HttpResponse response = executeCurrentRequest(request);
    boolean notificationCompleted = false;
    try {
        updateStateAndNotifyListener(UploadState.INITIATION_COMPLETE);
        notificationCompleted = true;
    } finally {
        if (!notificationCompleted) {
            response.disconnect();
        }
    }
    return response;
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpResponse(com.google.api.client.http.HttpResponse) HttpContent(com.google.api.client.http.HttpContent) EmptyContent(com.google.api.client.http.EmptyContent)

Aggregations

EmptyContent (com.google.api.client.http.EmptyContent)5 HttpRequest (com.google.api.client.http.HttpRequest)3 HttpResponse (com.google.api.client.http.HttpResponse)2 MethodOverride (com.google.api.client.googleapis.MethodOverride)1 MockGoogleClient (com.google.api.client.googleapis.testing.services.MockGoogleClient)1 MockGoogleClientRequest (com.google.api.client.googleapis.testing.services.MockGoogleClientRequest)1 GZipEncoding (com.google.api.client.http.GZipEncoding)1 HttpContent (com.google.api.client.http.HttpContent)1 HttpResponseInterceptor (com.google.api.client.http.HttpResponseInterceptor)1 HttpTransport (com.google.api.client.http.HttpTransport)1 LowLevelHttpRequest (com.google.api.client.http.LowLevelHttpRequest)1 UrlEncodedContent (com.google.api.client.http.UrlEncodedContent)1 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)1 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)1 Beta (com.google.api.client.util.Beta)1 IOException (java.io.IOException)1