Java Code Examples for com.google.api.client.http.HttpRequest#getContent()

The following examples show how to use com.google.api.client.http.HttpRequest#getContent() . 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: MethodOverride.java    From google-api-java-client with Apache License 2.0 6 votes vote down vote up
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());
    }
  }
}
 
Example 2
Source File: GoogleHttpClientEdgeGridRequestSigner.java    From AkamaiOPEN-edgegrid-java with Apache License 2.0 6 votes vote down vote up
private byte[] serializeContent(HttpRequest request) {

        byte[] contentBytes;
        try {
            HttpContent content = request.getContent();
            if (content == null) {
                return new byte[]{};
            }
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            content.writeTo(bos);
            contentBytes = bos.toByteArray();
            // for non-retryable content, reset the content for downstream handlers
            if (!content.retrySupported()) {
                HttpContent newContent = new ByteArrayContent(content.getType(), contentBytes);
                request.setContent(newContent);
            }
            return contentBytes;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
 
Example 3
Source File: GoogleApiClientSigner.java    From oauth1-signer-java with MIT License 5 votes vote down vote up
public void sign(HttpRequest request) throws IOException {
    URI uri = request.getUrl().toURI();
    String method = request.getRequestMethod();
    String payload = null;

    HttpContent content = request.getContent();
    if (null != content && content.getLength() > 0) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        content.writeTo(outputStream);
        payload = outputStream.toString(charset.name());
    }

    String authorizationHeader = OAuth.getAuthorizationHeader(uri, method, payload, charset, consumerKey, signingKey);
    request.getHeaders().setAuthorization(authorizationHeader);
}
 
Example 4
Source File: DatastoreAdminTest.java    From nomulus with Apache License 2.0 5 votes vote down vote up
private static Optional<String> getRequestContent(HttpRequest httpRequest) throws IOException {
  if (httpRequest.getContent() == null) {
    return Optional.empty();
  }
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  httpRequest.getContent().writeTo(outputStream);
  outputStream.close();
  return Optional.of(outputStream.toString(StandardCharsets.UTF_8.name()));
}
 
Example 5
Source File: GoogleCloudStorageIntegrationHelper.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
/** Convert request to string representation that could be used for assertions in tests */
public static String requestToString(HttpRequest request) {
  String method = request.getRequestMethod();
  String url = request.getUrl().toString();
  String requestString = method + ":" + url;
  if ("POST".equals(method) && url.contains("uploadType=multipart")) {
    MultipartContent content = (MultipartContent) request.getContent();
    JsonHttpContent jsonRequest =
        (JsonHttpContent) Iterables.get(content.getParts(), 0).getContent();
    String objectName = ((StorageObject) jsonRequest.getData()).getName();
    requestString += ":" + objectName;
  }
  return requestString;
}
 
Example 6
Source File: MediaHttpUploader.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * Executes the current request with some common code that includes exponential backoff and GZip
 * encoding.
 *
 * @param request current request
 * @return HTTP response
 */
private HttpResponse executeCurrentRequest(HttpRequest request) throws IOException {
  // enable GZip encoding if necessary
  if (!disableGZipContent && !(request.getContent() instanceof EmptyContent)) {
    request.setEncoding(new GZipEncoding());
  }
  // execute request
  HttpResponse response = executeCurrentRequestWithoutGZip(request);
  return response;
}
 
Example 7
Source File: MethodOverrideTest.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
public void testInterceptMaxLength() throws IOException {
  HttpTransport transport = new MockHttpTransport();
  GenericUrl url = new GenericUrl(HttpTesting.SIMPLE_URL);
  url.set("a", "foo");
  HttpRequest request =
      transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
  new MethodOverride().intercept(request);
  assertEquals(HttpMethods.GET, request.getRequestMethod());
  assertNull(request.getHeaders().get(MethodOverride.HEADER));
  assertNull(request.getContent());
  char[] arr = new char[MethodOverride.MAX_URL_LENGTH];
  Arrays.fill(arr, 'x');
  url.set("a", new String(arr));
  request.setUrl(url);
  new MethodOverride().intercept(request);
  assertEquals(HttpMethods.POST, request.getRequestMethod());
  assertEquals(HttpMethods.GET, request.getHeaders().get(MethodOverride.HEADER));
  assertEquals(HttpTesting.SIMPLE_GENERIC_URL, request.getUrl());
  char[] arr2 = new char[arr.length + 2];
  Arrays.fill(arr2, 'x');
  arr2[0] = 'a';
  arr2[1] = '=';
  UrlEncodedContent content = (UrlEncodedContent) request.getContent();
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  content.writeTo(out);
  assertEquals(new String(arr2), out.toString());
}
 
Example 8
Source File: ClientParametersAuthenticationTest.java    From google-oauth-java-client with Apache License 2.0 5 votes vote down vote up
public void test() throws Exception {
  HttpRequest request = new MockHttpTransport().createRequestFactory()
      .buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
  ClientParametersAuthentication auth =
      new ClientParametersAuthentication(CLIENT_ID, CLIENT_SECRET);
  assertEquals(CLIENT_ID, auth.getClientId());
  assertEquals(CLIENT_SECRET, auth.getClientSecret());
  auth.intercept(request);
  UrlEncodedContent content = (UrlEncodedContent) request.getContent();
  @SuppressWarnings("unchecked")
  Map<String, ?> data = (Map<String, ?>) content.getData();
  assertEquals(CLIENT_ID, data.get("client_id"));
  assertEquals(CLIENT_SECRET, data.get("client_secret"));
}
 
Example 9
Source File: ClientParametersAuthenticationTest.java    From google-oauth-java-client with Apache License 2.0 5 votes vote down vote up
public void test_noSecret() throws Exception {
  HttpRequest request = new MockHttpTransport().createRequestFactory()
      .buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
  ClientParametersAuthentication auth =
      new ClientParametersAuthentication(CLIENT_ID, null);
  assertEquals(CLIENT_ID, auth.getClientId());
  assertNull(auth.getClientSecret());
  auth.intercept(request);
  UrlEncodedContent content = (UrlEncodedContent) request.getContent();
  @SuppressWarnings("unchecked")
  Map<String, ?> data = (Map<String, ?>) content.getData();
  assertEquals(CLIENT_ID, data.get("client_id"));
  assertNull(data.get("client_secret"));
}
 
Example 10
Source File: URLFetchUtils.java    From appengine-gcs-client with Apache License 2.0 5 votes vote down vote up
HTTPRequestInfo(HttpRequest req) {
  method = req.getRequestMethod();
  url = req.getUrl().toURL();
  long myLength;
  HttpContent content = req.getContent();
  try {
    myLength = content == null ? -1 : content.getLength();
  } catch (IOException e) {
    myLength = -1;
  }
  length = myLength;
  h1 = req.getHeaders();
  h2 = null;
}
 
Example 11
Source File: HttpClient.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public RequestCallable(HttpRequest request, @Nullable TypeToken<T> returnType, HttpOption...options) {
    this.request = checkNotNull(request, "http request");
    this.content = (Content) request.getContent();
    this.returnType = returnType;
    this.callSite = new Exception().getStackTrace();
}