com.google.appengine.api.urlfetch.FetchOptions Java Examples

The following examples show how to use com.google.appengine.api.urlfetch.FetchOptions. 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: HttpClientGAE.java    From flickr-uploader with GNU General Public License v2.0 6 votes vote down vote up
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 #2
Source File: HttpClientGAE.java    From flickr-uploader with GNU General Public License v2.0 6 votes vote down vote up
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 #3
Source File: HttpRequestTest.java    From appengine-tck with 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 #4
Source File: HttpClientGAE.java    From flickr-uploader with GNU General Public License v2.0 5 votes vote down vote up
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 #5
Source File: HttpClientGAE.java    From flickr-uploader with GNU General Public License v2.0 5 votes vote down vote up
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 #6
Source File: GoogleAppEngineRequestor.java    From dropbox-sdk-java with MIT License 5 votes vote down vote up
/**
 * Returns a new instance of the default {@link FetchOptions} used by this requestor. This
 * method exists primarily since {@link FetchOptions} provides no copy methods.
 *
 * @return new instance of default fetch options used by this requestor.
 */
public static FetchOptions newDefaultOptions() {
    return FetchOptions.Builder.withDefaults()
        .validateCertificate()
        .doNotFollowRedirects()
        .disallowTruncate()
        // seconds
        .setDeadline((DEFAULT_CONNECT_TIMEOUT_MILLIS + DEFAULT_READ_TIMEOUT_MILLIS)/1000.0);
}
 
Example #7
Source File: FetchOptionsBuilderTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testDoNotFollowRedirects() throws Exception {
    final URL redirect = getUrl("redirect");
    FetchOptions options = FetchOptions.Builder.doNotFollowRedirects();
    testOptions(redirect, options, new ResponseHandler() {
        public void handle(HTTPResponse response) throws Exception {
            Assert.assertEquals(302, response.getResponseCode());
        }
    });
}
 
Example #8
Source File: FetchOptionsBuilderTest.java    From appengine-tck with 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 #9
Source File: FetchOptionsBuilderTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testFollowRedirects() throws Exception {
    final URL redirect = getUrl("redirect");
    FetchOptions options = FetchOptions.Builder.followRedirects();
    testOptions(redirect, options, new ResponseHandler() {
        public void handle(HTTPResponse response) throws Exception {
            URL finalURL = response.getFinalUrl();
            Assert.assertEquals(getUrl(""), finalURL);
        }
    });
}
 
Example #10
Source File: AppEngineHttpFetcher.java    From openid4java with 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 #11
Source File: URLFetchServiceTest.java    From appengine-tck with 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 File: UrlFetchTransport.java    From google-http-java-client with 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 #13
Source File: OauthRawGcsService.java    From appengine-gcs-client with 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 #14
Source File: FetchOptionsTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testDoNotFollowRedirects() throws Exception {
    final URL redirect = getUrl("redirect");
    FetchOptions options = buildFetchOptions();
    options.doNotFollowRedirects();
    testOptions(redirect, options, new ResponseHandler() {
        public void handle(HTTPResponse response) throws Exception {
            Assert.assertEquals(302, response.getResponseCode());
        }
    });
}
 
Example #15
Source File: FetchOptionsTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testFollowRedirects() throws Exception {
    final URL redirect = getUrl("redirect");
    FetchOptions options = buildFetchOptions();
    options.followRedirects();
    testOptions(redirect, options, new ResponseHandler() {
        public void handle(HTTPResponse response) throws Exception {
            URL finalURL = response.getFinalUrl();
            Assert.assertEquals(getUrl(""), finalURL);
        }
    });
}
 
Example #16
Source File: FetchOptionsBuilderTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
@Test
public void testAllowTruncate() throws Exception {
    FetchOptions options = FetchOptions.Builder.allowTruncate();
    testOptions(options);
}
 
Example #17
Source File: AppEngineHttpFetcher.java    From openid4java with Apache License 2.0 4 votes vote down vote up
private FetchOptions getFetchOptions(HttpRequestOptions requestOptions) {
  return FetchOptions.Builder.disallowTruncate()
      .doNotFollowRedirects()
      .setDeadline(requestOptions.getConnTimeout() / 1000.0);
}
 
Example #18
Source File: UrlFetchRequest.java    From google-http-java-client with 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 #19
Source File: FetchOptionsTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
protected FetchOptions buildFetchOptions() {
    return FetchOptions.Builder.withDefaults();
}
 
Example #20
Source File: GoogleAppEngineRequestor.java    From dropbox-sdk-java with MIT License 4 votes vote down vote up
public FetchOptions getOptions() {
    return options;
}
 
Example #21
Source File: GoogleAppEngineRequestor.java    From dropbox-sdk-java with MIT License 4 votes vote down vote up
public GoogleAppEngineRequestor(FetchOptions options, URLFetchService service) {
    if (options == null) throw new NullPointerException("options");
    if (service == null) throw new NullPointerException("service");
    this.options = options;
    this.service = service;
}
 
Example #22
Source File: GoogleAppEngineRequestor.java    From dropbox-sdk-java with MIT License 4 votes vote down vote up
public GoogleAppEngineRequestor(FetchOptions options) {
    this(options, URLFetchServiceFactory.getURLFetchService());
}
 
Example #23
Source File: FetchOptionsBuilderTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
@Test
public void testWithDeadline() throws Exception {
    FetchOptions options = FetchOptions.Builder.withDeadline(10 * 1000.0);
    testOptions(options);
}
 
Example #24
Source File: FetchOptionsBuilderTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
@Test
public void testDoNotValidateCertificate() throws Exception {
    FetchOptions options = FetchOptions.Builder.doNotValidateCertificate();
    testOptions(options);
}
 
Example #25
Source File: FetchOptionsBuilderTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
@Test
public void testValidateCertificate() throws Exception {
    FetchOptions options = FetchOptions.Builder.validateCertificate();
    testOptions(options);
}
 
Example #26
Source File: FetchOptionsBuilderTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
@Test
public void testDisallowTruncate() throws Exception {
    FetchOptions options = FetchOptions.Builder.disallowTruncate();
    testOptions(options);
}
 
Example #27
Source File: FetchOptionsTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
@Test
public void testDoNotValidateCertificate() throws Exception {
    FetchOptions options = buildFetchOptions();
    options.doNotValidateCertificate();
    testOptions(options);
}
 
Example #28
Source File: FetchOptionsTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
@Test
public void testAllowTruncate() throws Exception {
    FetchOptions options = buildFetchOptions();
    options.allowTruncate();
    testOptions(options);
}
 
Example #29
Source File: FetchOptionsBuilderTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
@Test
public void testWithDefaults() throws Exception {
    FetchOptions options = FetchOptions.Builder.withDefaults();
    testOptions(options);
}
 
Example #30
Source File: FetchOptionsTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
@Test
public void testDisallowTruncate() throws Exception {
    FetchOptions options = buildFetchOptions();
    options.disallowTruncate();
    testOptions(options);
}