Java Code Examples for com.google.appengine.api.urlfetch.HTTPMethod

The following examples show how to use com.google.appengine.api.urlfetch.HTTPMethod. These examples are extracted from open source projects. 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
public static String getResponseDELETE(String url, Map<String, String> params, Map<String, String> headers) {
	int retry = 0;
	while (retry < 3) {
		long start = System.currentTimeMillis();
		try {
			URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService();
			String urlStr = ToolString.toUrl(url.trim(), params);
			logger.debug("DELETE : " + urlStr);
			HTTPRequest httpRequest = new HTTPRequest(new URL(urlStr), HTTPMethod.DELETE, FetchOptions.Builder.withDeadline(deadline));
			HTTPResponse response = fetcher.fetch(httpRequest);
			return processResponse(response);
		} catch (Throwable e) {
			retry++;
			if (e instanceof RuntimeException) {
				throw (RuntimeException) e;
			} else if (retry < 3) {
				logger.warn("retrying after " + (System.currentTimeMillis() - start) + " and " + retry + " retries\n" + e.getClass() + e.getMessage());
			} else {
				logger.error(e.getClass() + "\n" + ToolString.stack2string(e));
			}
		}
	}
	return null;
}
 
Example 2
public static String getResponseProxyPOST(URL url) throws MalformedURLException, UnsupportedEncodingException, IOException {
	URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService();
	String base64payload = "base64url=" + Base64UrlSafe.encodeServer(url.toString());
	String urlStr = url.toString();
	if (urlStr.contains("?")) {
		base64payload = "base64url=" + Base64UrlSafe.encodeServer(urlStr.substring(0, urlStr.indexOf("?")));
		base64payload += "&base64content=" + Base64UrlSafe.encodeServer(urlStr.substring(urlStr.indexOf("?") + 1));
	} else {
		base64payload = "base64url=" + Base64UrlSafe.encodeServer(urlStr);
	}
	HTTPRequest httpRequest = new HTTPRequest(new URL(HttpClientGAE.POSTPROXY_PHP), HTTPMethod.POST, FetchOptions.Builder.withDeadline(30d).doNotValidateCertificate());
	httpRequest.setPayload(base64payload.getBytes(UTF8));
	HTTPResponse response = fetcher.fetch(httpRequest);
	String processResponse = HttpClientGAE.processResponse(response);
	logger.info("proxying " + url + "\nprocessResponse:" + processResponse);
	return processResponse;
}
 
Example 3
/**
 * Creates a new instance.
 *
 * @param instanceName the name of the instance to create.
 * @param bootDiskName the name of the disk to create the instance with.
 * @param projectApiKey the project API key.
 * @param accessToken the access token.
 * @param configProperties the configuration properties.
 * @throws MalformedURLException
 * @throws IOException
 * @throws OrchestratorException if the REST API call failed to create instance.
 */
private void createInstance(String instanceName, String bootDiskName, String projectApiKey,
    String accessToken, Map<String, String> configProperties)
    throws MalformedURLException, IOException, OrchestratorException {
  String url = GceApiUtils.composeInstanceApiUrl(
      ConfigProperties.urlPrefixWithProjectAndZone, projectApiKey);
  String payload = createPayload_instance(instanceName, bootDiskName, configProperties);
  logger.info(
      "Calling " + url + " to create instance " + instanceName + "with payload " + payload);
  HTTPResponse httpResponse =
      GceApiUtils.makeHttpRequest(accessToken, url, payload, HTTPMethod.POST);
  int responseCode = httpResponse.getResponseCode();
  if (!(responseCode == 200 || responseCode == 204)) {
    throw new OrchestratorException("Failed to create GCE instance. " + instanceName
        + ". Response code " + responseCode + " Reason: "
        + new String(httpResponse.getContent()));
  }
}
 
Example 4
/**
 * Checks whether the disk or instance is available.
 *
 * @param accessToken the access token.
 * @param url the URL to check whether the disk/instance has been created.
 * @return true if the disk/instance is available, false otherwise.
 * @throws MalformedURLException
 * @throws IOException
 */
private boolean checkDiskOrInstance(String accessToken, String url)
    throws MalformedURLException, IOException {
  HTTPResponse httpResponse = GceApiUtils.makeHttpRequest(accessToken, url, "", HTTPMethod.GET);
  int responseCode = httpResponse.getResponseCode();
  if (!(responseCode == 200 || responseCode == 204)) {
    logger.fine("Disk/instance not ready. Response code " + responseCode + " Reason: "
        + new String(httpResponse.getContent()));
    return false;
  }
  // Check if the disk/instance is in status "READY".
  String contentStr = new String(httpResponse.getContent());
  JsonParser parser = new JsonParser();
  JsonObject o = (JsonObject) parser.parse(contentStr);
  String status = o.get("status").getAsString();
  if (!status.equals("READY") && !status.equals("RUNNING")) {
    return false;
  }
  return true;
}
 
Example 5
/**
 * Creates an HTTPRequest with the information passed in.
 *
 * @param accessToken the access token necessary to authorize the request.
 * @param url the url to query.
 * @param payload the payload for the request.
 * @return the created HTTP request.
 * @throws IOException
 */
public static HTTPResponse makeHttpRequest(
    String accessToken, final String url, String payload, HTTPMethod method) throws IOException {

  // Create HTTPRequest and set headers
  HTTPRequest httpRequest = new HTTPRequest(new URL(url.toString()), method);
  httpRequest.addHeader(new HTTPHeader("Authorization", "OAuth " + accessToken));
  httpRequest.addHeader(new HTTPHeader("Host", "www.googleapis.com"));
  httpRequest.addHeader(new HTTPHeader("Content-Length", Integer.toString(payload.length())));
  httpRequest.addHeader(new HTTPHeader("Content-Type", "application/json"));
  httpRequest.addHeader(new HTTPHeader("User-Agent", "google-api-java-client/1.0"));
  httpRequest.setPayload(payload.getBytes());

  URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService();
  HTTPResponse httpResponse = fetcher.fetch(httpRequest);
  return httpResponse;
}
 
Example 6
/**
 * Deletes an instance.
 *
 * @param name the name of the instance to delete.
 * @throws OrchestratorException if delete failed.
 */
private void deleteInstance(String name, String accessToken, String url)
    throws OrchestratorException {
  logger.info("Shutting down instance: " + name);
  HTTPResponse httpResponse;
  try {
    httpResponse = GceApiUtils.makeHttpRequest(accessToken, url, "", HTTPMethod.DELETE);
    int responseCode = httpResponse.getResponseCode();
    if (!(responseCode == 200 || responseCode == 204)) {
      throw new OrchestratorException("Delete Instance failed. Response code " + responseCode
          + " Reason: " + new String(httpResponse.getContent()));
    }
  } catch (IOException e) {
    throw new OrchestratorException(e);
  }
}
 
Example 7
Source Project: openid4java   Source File: AppEngineHttpFetcher.java    License: Apache License 2.0 5 votes vote down vote up
private HttpResponse fetch(String url, HttpRequestOptions requestOptions,
    HTTPMethod method, String content) throws IOException {

  final FetchOptions options = getFetchOptions(requestOptions);

  String currentUrl = url;

  for (int i = 0; i <= requestOptions.getMaxRedirects(); i++) {

    HTTPRequest httpRequest = new HTTPRequest(new URL(currentUrl),
        method, options);

    addHeaders(httpRequest, requestOptions);

    if (method == HTTPMethod.POST && content != null) {
      httpRequest.setPayload(content.getBytes());
    }

    HTTPResponse httpResponse;
    try {
      httpResponse = fetchService.fetch(httpRequest);
    } catch (ResponseTooLargeException e) {
      return new TooLargeResponse(currentUrl);
    }

    if (!isRedirect(httpResponse.getResponseCode())) {
      boolean isResponseTooLarge =
        (getContentLength(httpResponse) > requestOptions.getMaxBodySize());
      return new AppEngineFetchResponse(httpResponse,
          isResponseTooLarge, currentUrl);
    } else {
      currentUrl = getResponseHeader(httpResponse, "Location").getValue();
    }
  }
  throw new IOException("exceeded maximum number of redirects");
}
 
Example 8
Source Project: google-http-java-client   Source File: UrlFetchTransport.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected UrlFetchRequest buildRequest(String method, String url) throws IOException {
  Preconditions.checkArgument(supportsMethod(method), "HTTP method %s not supported", method);
  HTTPMethod httpMethod;
  if (method.equals(HttpMethods.DELETE)) {
    httpMethod = HTTPMethod.DELETE;
  } else if (method.equals(HttpMethods.GET)) {
    httpMethod = HTTPMethod.GET;
  } else if (method.equals(HttpMethods.HEAD)) {
    httpMethod = HTTPMethod.HEAD;
  } else if (method.equals(HttpMethods.POST)) {
    httpMethod = HTTPMethod.POST;
  } else if (method.equals(HttpMethods.PATCH)) {
    httpMethod = HTTPMethod.PATCH;
  } else {
    httpMethod = HTTPMethod.PUT;
  }
  // fetch options
  FetchOptions fetchOptions =
      FetchOptions.Builder.doNotFollowRedirects().disallowTruncate().validateCertificate();
  switch (certificateValidationBehavior) {
    case VALIDATE:
      fetchOptions.validateCertificate();
      break;
    case DO_NOT_VALIDATE:
      fetchOptions.doNotValidateCertificate();
      break;
    default:
      break;
  }
  return new UrlFetchRequest(fetchOptions, httpMethod, url);
}
 
Example 9
Source Project: appengine-gcs-client   Source File: OauthRawGcsService.java    License: Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
HTTPRequest makeRequest(GcsFilename filename, @Nullable Map<String, String> queryStrings,
    HTTPMethod method, long timeoutMillis, byte[] payload) {
  HTTPRequest request = new HTTPRequest(makeUrl(filename, queryStrings), method,
      FetchOptions.Builder.disallowTruncate()
          .doNotFollowRedirects()
          .validateCertificate()
          .setDeadline(timeoutMillis / 1000.0));
  for (HTTPHeader header : headers) {
    request.addHeader(header);
  }
  request.addHeader(USER_AGENT);
  if (payload != null && payload.length > 0) {
    request.setHeader(new HTTPHeader(CONTENT_LENGTH, String.valueOf(payload.length)));
    try {
      request.setHeader(new HTTPHeader(CONTENT_MD5,
          BaseEncoding.base64().encode(MessageDigest.getInstance("MD5").digest(payload))));
    } catch (NoSuchAlgorithmException e) {
      log.severe(
          "Unable to get a MessageDigest instance, no Content-MD5 header sent.\n" + e.toString());
    }
    request.setPayload(payload);
  } else {
    request.setHeader(ZERO_CONTENT_LENGTH);
  }
  return request;
}
 
Example 10
Source Project: appengine-gcs-client   Source File: URLFetchUtilsTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCopyRequest() throws Exception {
  HTTPRequest request = new HTTPRequest(new URL("http://h1/v1"), HTTPMethod.HEAD);
  request.setHeader(new HTTPHeader("k3", "v3"));
  HTTPRequest copy = URLFetchUtils.copyRequest(request);
  assertEquals("http://h1/v1", copy.getURL().toString());
  assertEquals(HTTPMethod.HEAD, copy.getMethod());
  assertEquals(1, copy.getHeaders().size());
  assertEquals("k3", copy.getHeaders().get(0).getName());
  assertEquals("v3", copy.getHeaders().get(0).getValue());
}
 
Example 11
Source Project: appengine-tck   Source File: URLFetchServiceTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test(expected = IOException.class)
public void fetchNonExistentLocalAppThrowsException() throws Exception {
    URL selfURL = new URL("BOGUS-" + appUrlBase + "/");
    FetchOptions fetchOptions = FetchOptions.Builder.withDefaults()
        .doNotFollowRedirects()
        .setDeadline(10.0);
    HTTPRequest httpRequest = new HTTPRequest(selfURL, HTTPMethod.GET, fetchOptions);
    URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService();
    HTTPResponse httpResponse = urlFetchService.fetch(httpRequest);
    fail("expected exception, got " + httpResponse.getResponseCode());
}
 
Example 12
Source Project: appengine-tck   Source File: HttpRequestTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testGetters() throws Exception {
    HTTPRequest request = new HTTPRequest(getFetchUrl(), HTTPMethod.PATCH, FetchOptions.Builder.withDefaults());
    request.addHeader(new HTTPHeader("foo", "bar"));
    request.setPayload("qwerty".getBytes());

    Assert.assertEquals(getFetchUrl(), request.getURL());
    Assert.assertEquals(HTTPMethod.PATCH, request.getMethod());
    Assert.assertNotNull(request.getFetchOptions());
    Assert.assertNotNull(request.getHeaders());
    Assert.assertEquals(1, request.getHeaders().size());
    assertEquals(new HTTPHeader("foo", "bar"), request.getHeaders().get(0));
    Assert.assertArrayEquals("qwerty".getBytes(), request.getPayload());
}
 
Example 13
Source Project: appengine-tck   Source File: URLFetchTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testPayload() throws Exception {
    URLFetchService service = URLFetchServiceFactory.getURLFetchService();

    URL url = getFetchUrl();

    HTTPRequest req = new HTTPRequest(url, HTTPMethod.POST);
    req.setHeader(new HTTPHeader("Content-Type", "application/octet-stream"));
    req.setPayload("Tralala".getBytes(UTF_8));

    HTTPResponse response = service.fetch(req);
    String content = new String(response.getContent());
    Assert.assertEquals("Hopsasa", content);
}
 
Example 14
Source Project: appengine-tck   Source File: FetchOptionsBuilderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testFollowRedirectsExternal() throws Exception {
    final URL redirectUrl = new URL("http://google.com/");
    final String expectedDestinationURLPrefix = "http://www.google.";

    FetchOptions options = FetchOptions.Builder.followRedirects();

    HTTPRequest request = new HTTPRequest(redirectUrl, HTTPMethod.GET, options);
    URLFetchService service = URLFetchServiceFactory.getURLFetchService();
    HTTPResponse response = service.fetch(request);
    String destinationUrl = response.getFinalUrl().toString();
    assertTrue("Did not get redirected.", destinationUrl.startsWith(expectedDestinationURLPrefix));
}
 
Example 15
Source Project: dropbox-sdk-java   Source File: GoogleAppEngineRequestor.java    License: MIT License 5 votes vote down vote up
private HTTPRequest newRequest(String url, HTTPMethod method, Iterable<Header> headers) throws IOException {
    HTTPRequest request = new HTTPRequest(new URL(url), method, options);
    for (Header header : headers) {
        request.addHeader(new HTTPHeader(header.getKey(), header.getValue()));
    }
    return request;
}
 
Example 16
public static String getResponsePUT(String url, Map<String, String> params, String json, Map<String, String> headers) {
	int retry = 0;
	while (retry < 3) {
		long start = System.currentTimeMillis();
		try {
			URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService();
			String urlStr = ToolString.toUrl(url.trim(), params);
			logger.debug("PUT : " + urlStr);
			HTTPRequest httpRequest = new HTTPRequest(new URL(urlStr), HTTPMethod.PUT, FetchOptions.Builder.withDeadline(deadline));
			if (headers != null) {
				for (String header : headers.keySet()) {
					httpRequest.addHeader(new HTTPHeader(header, headers.get(header)));
				}
			}
			httpRequest.setPayload(json.getBytes());
			HTTPResponse response = fetcher.fetch(httpRequest);
			return processResponse(response);
		} catch (Throwable e) {
			retry++;
			if (e instanceof RuntimeException) {
				throw (RuntimeException) e;
			} else if (retry < 3) {
				logger.warn("retrying after " + (System.currentTimeMillis() - start) + " and " + retry + " retries\n" + e.getClass() + e.getMessage());
			} else {
				logger.error(e.getClass() + "\n" + ToolString.stack2string(e));
			}
		}
	}
	return null;
}
 
Example 17
public static String getResponseGET(String url, Map<String, String> params, Map<String, String> headers) {
	int retry = 0;
	while (retry < 3) {
		long start = System.currentTimeMillis();
		try {
			URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService();
			String urlStr = ToolString.toUrl(url.trim(), params);
			HTTPRequest httpRequest = new HTTPRequest(new URL(urlStr), HTTPMethod.GET, FetchOptions.Builder.withDeadline(deadline));
			if (headers != null) {
				for (String name : headers.keySet()) {
					httpRequest.addHeader(new HTTPHeader(name, headers.get(name)));
				}
			}
			return processResponse(fetcher.fetch(httpRequest));
		} catch (Throwable e) {
			retry++;
			if (e instanceof RuntimeException) {
				throw (RuntimeException) e;
			} else if (retry < 3) {
				logger.warn("retrying after " + (System.currentTimeMillis() - start) + " and " + retry + " retries\n" + e.getClass() + e.getMessage());
			} else {
				logger.error(e.getClass() + "\n" + ToolString.stack2string(e));
			}
		}
	}
	return null;
}
 
Example 18
/**
 * Creates a new disk.
 *
 * @param diskName the name of the disk to create.
 * @param accessToken the access token.
 * @param configProperties the configuration properties.
 * @throws MalformedURLException
 * @throws IOException
 * @throws OrchestratorException if the REST API call failed to create disk.
 */
private void createDisk(String diskName, String accessToken, String projectApiKey,
    Map<String, String> configProperties)
    throws MalformedURLException, IOException, OrchestratorException {
  String url =
      GceApiUtils.composeDiskApiUrl(ConfigProperties.urlPrefixWithProjectAndZone, projectApiKey);
  String payload = createPayload_disk(diskName, configProperties);
  HTTPResponse httpResponse =
      GceApiUtils.makeHttpRequest(accessToken, url, payload, HTTPMethod.POST);
  int responseCode = httpResponse.getResponseCode();
  if (!(responseCode == 200 || responseCode == 204)) {
    throw new OrchestratorException("Failed to create Disk " + diskName + ". Response code "
        + responseCode + " Reason: " + new String(httpResponse.getContent()));
  }
}
 
Example 19
/**
 * Deletes a disk.
 *
 * @param diskName the name of the disk to delete.
 * @throws IOException thrown by makeHttpRequest if the Compute Engine API could not be contacted.
 * @throws OrchestratorException if the REST API call failed to delete the disk.
 */
public static void deleteDisk(String diskName, String accessToken, String url)
    throws IOException, OrchestratorException {
  HTTPResponse httpResponse =
      GceApiUtils.makeHttpRequest(accessToken, url, "", HTTPMethod.DELETE);
  int responseCode = httpResponse.getResponseCode();
  if (!(responseCode == 200 || responseCode == 204)) {
    throw new OrchestratorException("Delete Disk failed. Response code " + responseCode
        + " Reason: " + new String(httpResponse.getContent()));
  }
}
 
Example 20
/**
 * Checks whether the instance is deleted.
 *
 * @param accessToken the access token.
 * @param url the URL to check whether the instance is still up.
 * @return true if the instance is not found, false otherwise.
 * @throws IOException thrown by makeHttpRequest if the Compute Engine API could not be contacted.
 * @throws OrchestratorException
 */
private boolean isInstanceDeleted(String accessToken, String url)
    throws IOException, OrchestratorException {
  HTTPResponse httpResponse = GceApiUtils.makeHttpRequest(accessToken, url, "", HTTPMethod.GET);
  int responseCode = httpResponse.getResponseCode();
  if (responseCode == 404) { // Not Found.
    return true;
  } else if (responseCode == 200 || responseCode == 204) {
    return false;
  } else {
    throw new OrchestratorException("Failed to check if instance is deleted. Response code "
        + responseCode + " Reason: " + new String(httpResponse.getContent()));
  }
}
 
Example 21
Source Project: openid4java   Source File: AppEngineHttpFetcher.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public HttpResponse get(String url, HttpRequestOptions requestOptions)
    throws IOException {
  return fetch(url, requestOptions, HTTPMethod.GET, null);
}
 
Example 22
Source Project: openid4java   Source File: AppEngineHttpFetcher.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public HttpResponse head(String url, HttpRequestOptions requestOptions)
    throws IOException {
  return fetch(url, requestOptions, HTTPMethod.HEAD, null);
}
 
Example 23
Source Project: openid4java   Source File: AppEngineHttpFetcher.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public HttpResponse post(String url, Map<String, String> parameters,
    HttpRequestOptions requestOptions) throws IOException {
  return fetch(url, requestOptions, HTTPMethod.POST, encodeParameters(parameters ));
}
 
Example 24
Source Project: google-http-java-client   Source File: UrlFetchRequest.java    License: Apache License 2.0 4 votes vote down vote up
UrlFetchRequest(FetchOptions fetchOptions, HTTPMethod method, String url) throws IOException {
  request = new HTTPRequest(new URL(url), method, fetchOptions);
}
 
Example 25
Source Project: appengine-gcs-client   Source File: OauthRawGcsService.java    License: Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
HTTPRequest makeRequest(GcsFilename filename, @Nullable Map<String, String> queryStrings,
    HTTPMethod method, long timeoutMillis) {
  return makeRequest(filename, queryStrings, method, timeoutMillis, EMPTY_PAYLOAD);
}
 
Example 26
Source Project: appengine-gcs-client   Source File: OauthRawGcsService.java    License: Apache License 2.0 4 votes vote down vote up
private HTTPRequest makeRequest(GcsFilename filename, @Nullable Map<String, String> queryStrings,
    HTTPMethod method, long timeoutMillis, ByteBuffer payload) {
  return makeRequest(filename, queryStrings, method, timeoutMillis, peekBytes(payload));
}
 
Example 27
Source Project: appengine-tck   Source File: URLFetchTestBase.java    License: Apache License 2.0 4 votes vote down vote up
protected void testOptions(URL url, FetchOptions options, ResponseHandler handler) throws Exception {
    testOptions(url, HTTPMethod.GET, options, handler);
}
 
Example 28
Source Project: appengine-tck   Source File: URLFetchTestBase.java    License: Apache License 2.0 4 votes vote down vote up
protected void testOptions(URL url, HTTPMethod method, FetchOptions options, ResponseHandler handler) throws Exception {
    HTTPRequest request = new HTTPRequest(url, method, options);
    URLFetchService service = URLFetchServiceFactory.getURLFetchService();
    HTTPResponse response = service.fetch(request);
    handler.handle(response);
}
 
Example 29
Source Project: dropbox-sdk-java   Source File: GoogleAppEngineRequestor.java    License: MIT License 4 votes vote down vote up
@Override
public Response doGet(String url, Iterable<Header> headers) throws IOException {
    HTTPRequest request = newRequest(url, HTTPMethod.GET, headers);
    HTTPResponse response = service.fetch(request);
    return toRequestorResponse(response);
}
 
Example 30
Source Project: dropbox-sdk-java   Source File: GoogleAppEngineRequestor.java    License: MIT License 4 votes vote down vote up
@Override
public Uploader startPost(String url, Iterable<Header> headers) throws IOException {
    HTTPRequest request = newRequest(url, HTTPMethod.POST, headers);
    return new FetchServiceUploader(service, request, new ByteArrayOutputStream());
}