Java Code Examples for com.google.api.client.http.GenericUrl#put()

The following examples show how to use com.google.api.client.http.GenericUrl#put() . 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: UploadIdResponseInterceptorTest.java    From beam with Apache License 2.0 6 votes vote down vote up
/**
 * Builds an HttpResponse with the given string response.
 *
 * @param header header value to provide or null if none.
 * @param uploadId upload id to provide in the url upload id param or null if none.
 * @param uploadType upload type to provide in url upload type param or null if none.
 * @return HttpResponse with the given parameters
 * @throws IOException
 */
private HttpResponse buildHttpResponse(String header, String uploadId, String uploadType)
    throws IOException {
  MockHttpTransport.Builder builder = new MockHttpTransport.Builder();
  MockLowLevelHttpResponse resp = new MockLowLevelHttpResponse();
  builder.setLowLevelHttpResponse(resp);
  resp.setStatusCode(200);
  GenericUrl url = new GenericUrl(HttpTesting.SIMPLE_URL);
  if (header != null) {
    resp.addHeader("X-GUploader-UploadID", header);
  }
  if (uploadId != null) {
    url.put("upload_id", uploadId);
  }
  if (uploadType != null) {
    url.put("uploadType", uploadType);
  }
  return builder.build().createRequestFactory().buildGetRequest(url).execute();
}
 
Example 2
Source File: MediaHttpUploader.java    From google-api-java-client with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 3
Source File: UploadIdResponseInterceptorTest.java    From beam with Apache License 2.0 5 votes vote down vote up
/** Check that a response logs with the correct log. */
@Test
public void testResponseLogs() throws IOException {
  new UploadIdResponseInterceptor().interceptResponse(buildHttpResponse("abc", null, "type"));
  GenericUrl url = new GenericUrl(HttpTesting.SIMPLE_URL);
  url.put("uploadType", "type");
  String worker = System.getProperty("worker_id");
  expectedLogs.verifyDebug("Upload ID for url " + url + " on worker " + worker + " is abc");
}
 
Example 4
Source File: MediaHttpUploader.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * Direct Uploads the media.
 *
 * @param initiationRequestUrl The request URL where the initiation request will be sent
 * @return HTTP response
 */
private HttpResponse directUpload(GenericUrl initiationRequestUrl) throws IOException {
  updateStateAndNotifyListener(UploadState.MEDIA_IN_PROGRESS);

  HttpContent content = mediaContent;
  if (metadata != null) {
    content = new MultipartContent().setContentParts(Arrays.asList(metadata, mediaContent));
    initiationRequestUrl.put("uploadType", "multipart");
  } else {
    initiationRequestUrl.put("uploadType", "media");
  }
  HttpRequest request =
      requestFactory.buildRequest(initiationRequestMethod, initiationRequestUrl, content);
  request.getHeaders().putAll(initiationHeaders);
  // We do not have to do anything special here if media content length is unspecified because
  // direct media upload works even when the media content length == -1.
  HttpResponse response = executeCurrentRequest(request);
  boolean responseProcessed = false;
  try {
    if (isMediaLengthKnown()) {
      totalBytesServerReceived = getMediaContentLength();
    }
    updateStateAndNotifyListener(UploadState.MEDIA_COMPLETE);
    responseProcessed = true;
  } finally {
    if (!responseProcessed) {
      response.disconnect();
    }
  }
  return response;
}
 
Example 5
Source File: FreebaseUtil.java    From xcurator with Apache License 2.0 5 votes vote down vote up
public static JSONArray search(String query, String mql_query_file, int limit) {
        try {
            properties.load(new FileInputStream(FREEBASE_PROPERTIES_LOC));
            HttpTransport httpTransport = new NetHttpTransport();
            HttpRequestFactory requestFactory = httpTransport.createRequestFactory();

            GenericUrl url = new GenericUrl("https://www.googleapis.com/freebase/v1/search");
            url.put("query", query);
//            url.put("filter", "(all type:/music/artist created:\"The Lady Killer\")");
            url.put("limit", limit);
            String mql_query = IOUtils.toString(new FileInputStream(mql_query_file));

            url.put("mql_output", mql_query);
            url.put("key", properties.get("API_KEY"));
            HttpRequest request = requestFactory.buildGetRequest(url);
            HttpResponse httpResponse = null;
            try {
                httpResponse = request.execute();
            } catch (Exception e) {

            }
            if (httpResponse == null) {
                return null;
            }
            JSON obj = JSONSerializer.toJSON(httpResponse.parseAsString());
            if (obj.isEmpty()) {
                return null;
            }
            JSONObject jsonObject = (JSONObject) obj;
            JSONArray results = jsonObject.getJSONArray("result");
            LogUtils.info(FreebaseUtil.class, results.toString());
            return results;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
 
Example 6
Source File: FreebaseUtil.java    From xcurator with Apache License 2.0 5 votes vote down vote up
public static JSONArray fetch(String query_template_file, Map<String, String> params) {
    try {
        properties.load(new FileInputStream(FREEBASE_PROPERTIES_LOC));
        HttpTransport httpTransport = new NetHttpTransport();
        HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
        String query = IOUtils.toString(new FileInputStream(query_template_file));
        query = manipulateQuery(query, params);
        GenericUrl url = new GenericUrl("https://www.googleapis.com/freebase/v1/mqlread");
        url.put("query", query);
        url.put("key", properties.get("API_KEY"));
        System.out.println("URL:" + url);

        HttpRequest request = requestFactory.buildGetRequest(url);
        HttpResponse httpResponse = request.execute();
        JSON obj = JSONSerializer.toJSON(httpResponse.parseAsString());
        if (obj.isEmpty()) {
            return null;
        }
        JSONObject jsonObject = (JSONObject) obj;
        JSONArray results = jsonObject.getJSONArray("result");
        System.out.println(results.toString());
        return results;
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return null;
}
 
Example 7
Source File: OAuthSigner.java    From Woocommerce-Android-Client with MIT License 4 votes vote down vote up
public LinkedHashMap<String, String> getSignature(Map<String, String> options, RequestMethod requestMethod, String endpoint) {
    LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();

    OAuthParameters parameters = new OAuthParameters();
    parameters.computeTimestamp();
    parameters.computeNonce();
    parameters.version = "1.0";
    parameters.consumerKey = wooCommerce.getWc_key();
    GenericUrl genericUrl = new GenericUrl();
    genericUrl.setScheme(wooCommerce.isHttps() ? "https" : "http");
    genericUrl.setHost(wooCommerce.getBaseUrl());
    genericUrl.appendRawPath("/wc-api");
    genericUrl.appendRawPath("/v3");
    /*
     *    The endpoint to be called is specified next
     *    */

    genericUrl.appendRawPath(endpoint);

    for (Map.Entry<String, String> entry : options.entrySet())
    {
        System.out.println(entry.getKey() + "/" + entry.getValue());
        genericUrl.appendRawPath("/"+entry.getValue());
    }

    OAuthHmacSigner oAuthHmacSigner = new OAuthHmacSigner();
    oAuthHmacSigner.clientSharedSecret = wooCommerce.getWc_secret();


    parameters.signer = oAuthHmacSigner;
    parameters.signatureMethod = wooCommerce.getSigning_method().getVal();
    try {
        parameters.computeSignature(requestMethod.getVal(), genericUrl);
    } catch (GeneralSecurityException e) {
        e.printStackTrace();
    }

    map.put("oauth_consumer_key", parameters.consumerKey);
    map.put("oauth_signature_method", parameters.signatureMethod);
    map.put("oauth_timestamp", parameters.timestamp);
    map.put("oauth_nonce", parameters.nonce);
    map.put("oauth_version", parameters.version);
    map.put("oauth_signature", parameters.signature);

    genericUrl.put("oauth_consumer_key", parameters.consumerKey);
    genericUrl.put("oauth_signature_method", parameters.signatureMethod);
    genericUrl.put("oauth_timestamp", parameters.timestamp);
    genericUrl.put("oauth_nonce", parameters.nonce);
    genericUrl.put("oauth_version", parameters.version);
    genericUrl.put("oauth_signature", parameters.signature);

    Log.i(TAG,genericUrl.build());


    return map;
}