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());
}
}
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);
}
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;
}
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());
}
}
}
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;
}
Aggregations