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

The following examples show how to use com.google.appengine.api.urlfetch.HTTPRequest. 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: URLFetchUtilsTest.java    From appengine-gcs-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testDescribeRequestAndResponseF() throws Exception {
  HTTPRequest request = new HTTPRequest(new URL("http://ping/pong"));
  request.setPayload("hello".getBytes());
  request.addHeader(new HTTPHeader("k1", "v1"));
  request.addHeader(new HTTPHeader("k2", "v2"));
  HTTPResponse response = mock(HTTPResponse.class);
  when(response.getHeadersUncombined()).thenReturn(ImmutableList.of(new HTTPHeader("k3", "v3")));
  when(response.getResponseCode()).thenReturn(500);
  when(response.getContent()).thenReturn("bla".getBytes());
  String expected = "Request: GET http://ping/pong\nk1: v1\nk2: v2\n\n"
      + "5 bytes of content\n\nResponse: 500 with 3 bytes of content\nk3: v3\nbla\n";
  String result =
      URLFetchUtils.describeRequestAndResponse(new HTTPRequestInfo(request), response);
  assertEquals(expected, result);
}
 
Example #2
Source File: NordnUploadActionTest.java    From nomulus with Apache License 2.0 6 votes vote down vote up
@Before
public void before() throws Exception {
  inject.setStaticField(Ofy.class, "clock", clock);
  when(fetchService.fetch(any(HTTPRequest.class))).thenReturn(httpResponse);
  when(httpResponse.getContent()).thenReturn("Success".getBytes(US_ASCII));
  when(httpResponse.getResponseCode()).thenReturn(SC_ACCEPTED);
  when(httpResponse.getHeadersUncombined())
      .thenReturn(ImmutableList.of(new HTTPHeader(LOCATION, "http://trololol")));
  persistResource(loadRegistrar("TheRegistrar").asBuilder().setIanaIdentifier(99999L).build());
  createTld("tld");
  persistResource(Registry.get("tld").asBuilder().setLordnUsername("lolcat").build());
  action.clock = clock;
  action.fetchService = fetchService;
  action.lordnRequestInitializer = lordnRequestInitializer;
  action.phase = "claims";
  action.taskQueueUtils = new TaskQueueUtils(new Retrier(new FakeSleeper(clock), 3));
  action.tld = "tld";
  action.tmchMarksdbUrl = "http://127.0.0.1";
  action.random = new SecureRandom();
  action.retrier = new Retrier(new FakeSleeper(clock), 3);
}
 
Example #3
Source File: URLFetchTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testAsyncOps() throws Exception {
    URLFetchService service = URLFetchServiceFactory.getURLFetchService();

    URL adminConsole = findAvailableUrl(URLS);
    Future<HTTPResponse> response = service.fetchAsync(adminConsole);
    printResponse(response.get(5, TimeUnit.SECONDS));

    response = service.fetchAsync(new HTTPRequest(adminConsole));
    printResponse(response.get(5, TimeUnit.SECONDS));

    URL jbossOrg = new URL("http://www.jboss.org");
    if (available(jbossOrg)) {
        response = service.fetchAsync(jbossOrg);
        printResponse(response.get(30, TimeUnit.SECONDS));
    }

    sync(5000L); // wait a bit for async to finish
}
 
Example #4
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 #5
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 #6
Source File: GceApiUtils.java    From solutions-google-compute-engine-orchestrator with Apache License 2.0 6 votes vote down vote up
/**
 * 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 #7
Source File: AppEngineHttpFetcher.java    From openid4java with Apache License 2.0 6 votes vote down vote up
private static void addHeaders(HTTPRequest httpRequest,
    HttpRequestOptions requestOptions) {

  String contentType = requestOptions.getContentType();

  if (contentType != null) {
    httpRequest.addHeader(new HTTPHeader("Content-Type", contentType));
  }

  Map<String, String> headers = getRequestHeaders(requestOptions);

  if (headers != null) {
    for (Map.Entry<String, String> header : headers.entrySet()) {
      httpRequest.addHeader(new HTTPHeader(header.getKey(), header.getValue()));
    }
  }
}
 
Example #8
Source File: CurlServlet.java    From appengine-java-vm-runtime with Apache License 2.0 6 votes vote down vote up
public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws IOException, ServletException {
  String url = req.getParameter("url");
  String deadlineSecs = req.getParameter("deadline");
  URLFetchService service = URLFetchServiceFactory.getURLFetchService();
  HTTPRequest fetchReq = new HTTPRequest(new URL(url));
  if (deadlineSecs != null) {
    fetchReq.getFetchOptions().setDeadline(Double.valueOf(deadlineSecs));
  }
  HTTPResponse fetchRes = service.fetch(fetchReq);
  for (HTTPHeader header : fetchRes.getHeaders()) {
    res.addHeader(header.getName(), header.getValue());
  }
  if (fetchRes.getResponseCode() == 200) {
    res.getOutputStream().write(fetchRes.getContent());
  } else {
    res.sendError(fetchRes.getResponseCode(), "Error while fetching");
  }
}
 
Example #9
Source File: GaePendingResult.java    From google-maps-services-java with Apache License 2.0 6 votes vote down vote up
/**
 * @param request HTTP request to execute.
 * @param client The client used to execute the request.
 * @param responseClass Model class to unmarshal JSON body content.
 * @param fieldNamingPolicy FieldNamingPolicy for unmarshaling JSON.
 * @param errorTimeOut Number of milliseconds to re-send erroring requests.
 * @param maxRetries Number of times allowed to re-send erroring requests.
 */
public GaePendingResult(
    HTTPRequest request,
    URLFetchService client,
    Class<R> responseClass,
    FieldNamingPolicy fieldNamingPolicy,
    long errorTimeOut,
    Integer maxRetries,
    ExceptionsAllowedToRetry exceptionsAllowedToRetry,
    RequestMetrics metrics) {
  this.request = request;
  this.client = client;
  this.responseClass = responseClass;
  this.fieldNamingPolicy = fieldNamingPolicy;
  this.errorTimeOut = errorTimeOut;
  this.maxRetries = maxRetries;
  this.exceptionsAllowedToRetry = exceptionsAllowedToRetry;
  this.metrics = metrics;

  metrics.startNetwork();
  this.call = client.fetchAsync(request);
}
 
Example #10
Source File: OauthRawGcsService.java    From appengine-gcs-client with Apache License 2.0 6 votes vote down vote up
@Override
public void copyObject(GcsFilename source, GcsFilename dest, GcsFileOptions fileOptions,
    long timeoutMillis) throws IOException {
  HTTPRequest req = makeRequest(dest, null, PUT, timeoutMillis);
  req.setHeader(new HTTPHeader(X_GOOG_COPY_SOURCE, makePath(source)));
  if (fileOptions != null) {
    req.setHeader(REPLACE_METADATA_HEADER);
    addOptionsHeaders(req, fileOptions);
  }
  HTTPResponse resp;
  try {
    resp = urlfetch.fetch(req);
  } catch (IOException e) {
    throw createIOException(new HTTPRequestInfo(req), e);
  }
  if (resp.getResponseCode() != 200) {
    throw HttpErrorHandler.error(new HTTPRequestInfo(req), resp);
  }
}
 
Example #11
Source File: OauthRawGcsService.java    From appengine-gcs-client with Apache License 2.0 6 votes vote down vote up
@Override
public void composeObject(Iterable<String> source, GcsFilename dest, long timeoutMillis)
    throws IOException {
  StringBuilder xmlContent = new StringBuilder(Iterables.size(source) * 50);
  Escaper escaper = XmlEscapers.xmlContentEscaper();
  xmlContent.append("<ComposeRequest>");
  for (String srcFileName : source) {
    xmlContent.append("<Component><Name>")
        .append(escaper.escape(srcFileName))
        .append("</Name></Component>");
  }
  xmlContent.append("</ComposeRequest>");
  HTTPRequest req = makeRequest(
      dest, COMPOSE_QUERY_STRINGS, PUT, timeoutMillis, xmlContent.toString().getBytes(UTF_8));
  HTTPResponse resp;
  try {
    resp = urlfetch.fetch(req);
  } catch (IOException e) {
    throw createIOException(new HTTPRequestInfo(req), e);
  }
  if (resp.getResponseCode() != 200) {
    throw HttpErrorHandler.error(new HTTPRequestInfo(req), resp);
  }
}
 
Example #12
Source File: OauthRawGcsService.java    From appengine-gcs-client with Apache License 2.0 6 votes vote down vote up
@Override
public GcsFileMetadata getObjectMetadata(GcsFilename filename, long timeoutMillis)
    throws IOException {
  HTTPRequest req = makeRequest(filename, null, HEAD, timeoutMillis);
  HTTPResponse resp;
  try {
    resp = urlfetch.fetch(req);
  } catch (IOException e) {
    throw createIOException(new HTTPRequestInfo(req), e);
  }
  int responseCode = resp.getResponseCode();
  if (responseCode == 404) {
    return null;
  }
  if (responseCode != 200) {
    throw HttpErrorHandler.error(new HTTPRequestInfo(req), resp);
  }
  return getMetadataFromResponse(
      filename, resp, getLengthFromHeader(resp, X_GOOG_CONTENT_LENGTH));
}
 
Example #13
Source File: OauthRawGcsService.java    From appengine-gcs-client with Apache License 2.0 6 votes vote down vote up
/** True if deleted, false if not found. */
@Override
public boolean deleteObject(GcsFilename filename, long timeoutMillis) throws IOException {
  HTTPRequest req = makeRequest(filename, null, DELETE, timeoutMillis);
  HTTPResponse resp;
  try {
    resp = urlfetch.fetch(req);
  } catch (IOException e) {
    throw createIOException(new HTTPRequestInfo(req), e);
  }
  switch (resp.getResponseCode()) {
    case 204:
      return true;
    case 404:
      return false;
    default:
      throw HttpErrorHandler.error(new HTTPRequestInfo(req), resp);
  }
}
 
Example #14
Source File: OauthRawGcsService.java    From appengine-gcs-client with Apache License 2.0 6 votes vote down vote up
/**
 * Same as {@link #put} but is runs asynchronously and returns a future. In the event of an error
 * the exception out of the future will be an ExecutionException with the cause set to the same
 * exception that would have been thrown by put.
 */
private Future<RawGcsCreationToken> putAsync(final GcsRestCreationToken token,
    ByteBuffer chunk, final boolean isFinalChunk, long timeoutMillis) {
  final int length = chunk.remaining();
  HTTPRequest request = createPutRequest(token, chunk, isFinalChunk, timeoutMillis, length);
  final HTTPRequestInfo info = new HTTPRequestInfo(request);
  return new FutureWrapper<HTTPResponse, RawGcsCreationToken>(urlfetch.fetchAsync(request)) {
    @Override
    protected Throwable convertException(Throwable e) {
      return OauthRawGcsService.convertException(info, e);
    }

    @Override
    protected GcsRestCreationToken wrap(HTTPResponse resp) throws Exception {
      return handlePutResponse(token, isFinalChunk, length, info, resp);
    }
  };
}
 
Example #15
Source File: OauthRawGcsService.java    From appengine-gcs-client with Apache License 2.0 6 votes vote down vote up
@Override
public RawGcsCreationToken beginObjectCreation(
    GcsFilename filename, GcsFileOptions options, long timeoutMillis) throws IOException {
  HTTPRequest req = makeRequest(filename, null, POST, timeoutMillis);
  req.setHeader(RESUMABLE_HEADER);
  addOptionsHeaders(req, options);
  HTTPResponse resp;
  try {
    resp = urlfetch.fetch(req);
  } catch (IOException e) {
    throw createIOException(new HTTPRequestInfo(req), e);
  }
  if (resp.getResponseCode() == 201) {
    String location = URLFetchUtils.getSingleHeader(resp, LOCATION);
    String queryString = new URL(location).getQuery();
    Preconditions.checkState(
        queryString != null, LOCATION + " header," + location + ", witout a query string");
    Map<String, String> params = Splitter.on('&').withKeyValueSeparator('=').split(queryString);
    Preconditions.checkState(params.containsKey(UPLOAD_ID),
        LOCATION + " header," + location + ", has a query string without " + UPLOAD_ID);
    return new GcsRestCreationToken(filename, params.get(UPLOAD_ID), 0);
  } else {
    throw HttpErrorHandler.error(new HTTPRequestInfo(req), resp);
  }
}
 
Example #16
Source File: OauthRawGcsService.java    From appengine-gcs-client with Apache License 2.0 6 votes vote down vote up
private void addOptionsHeaders(HTTPRequest req, GcsFileOptions options) {
  if (options == null) {
    return;
  }
  if (options.getMimeType() != null) {
    req.setHeader(new HTTPHeader(CONTENT_TYPE, options.getMimeType()));
  }
  if (options.getAcl() != null) {
    req.setHeader(new HTTPHeader(ACL, options.getAcl()));
  }
  if (options.getCacheControl() != null) {
    req.setHeader(new HTTPHeader(CACHE_CONTROL, options.getCacheControl()));
  }
  if (options.getContentDisposition() != null) {
    req.setHeader(new HTTPHeader(CONTENT_DISPOSITION, options.getContentDisposition()));
  }
  if (options.getContentEncoding() != null) {
    req.setHeader(new HTTPHeader(CONTENT_ENCODING, options.getContentEncoding()));
  }
  for (Entry<String, String> entry : options.getUserMetadata().entrySet()) {
    req.setHeader(new HTTPHeader(X_GOOG_META + entry.getKey(), entry.getValue()));
  }
}
 
Example #17
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 #18
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 #19
Source File: OauthRawGcsServiceTest.java    From appengine-gcs-client with Apache License 2.0 5 votes vote down vote up
@Test
public void makeRequestShouldHandleAnAbsentPayload() throws MalformedURLException {
  URL url = new URL("https://storage.googleapis.com/sample-bucket/sample-object");
  HTTPRequest expected = new HTTPRequest(url, PUT, FETCH_OPTIONS);
  expected.addHeader(new HTTPHeader("Content-Length", "0"));
  expected.addHeader(new HTTPHeader("User-Agent", OauthRawGcsService.USER_AGENT_PRODUCT));
  assertHttpRequestEquals(expected, service.makeRequest(GCS_FILENAME, null, PUT, 30000));
}
 
Example #20
Source File: OauthRawGcsServiceTest.java    From appengine-gcs-client with Apache License 2.0 5 votes vote down vote up
@Test
public void makeRequestShouldHandleAnEmptyPayload() throws MalformedURLException {
  URL url = new URL("https://storage.googleapis.com/sample-bucket/sample-object");
  HTTPRequest expected = new HTTPRequest(url, PUT, FETCH_OPTIONS);
  expected.addHeader(new HTTPHeader("Content-Length", "0"));
  expected.addHeader(new HTTPHeader("User-Agent", OauthRawGcsService.USER_AGENT_PRODUCT));
  assertHttpRequestEquals(
      expected, service.makeRequest(GCS_FILENAME, null, PUT, 30000, new byte[0]));
}
 
Example #21
Source File: OauthRawGcsServiceTest.java    From appengine-gcs-client with Apache License 2.0 5 votes vote down vote up
@Test
public void makeRequestShouldHandleAPresentPayload() throws MalformedURLException {
  URL url = new URL("https://storage.googleapis.com/sample-bucket/sample-object");
  byte[] payload = "hello".getBytes(UTF_8);
  HTTPRequest expected = new HTTPRequest(url, PUT, FETCH_OPTIONS);
  expected.addHeader(new HTTPHeader("Content-Length", "5"));
  expected.addHeader(new HTTPHeader("Content-MD5", "XUFAKrxLKna5cZ2REBfFkg=="));
  expected.addHeader(new HTTPHeader("User-Agent", OauthRawGcsService.USER_AGENT_PRODUCT));
  expected.setPayload(payload);
  assertHttpRequestEquals(expected, service.makeRequest(GCS_FILENAME, null, PUT, 30000, payload));
}
 
Example #22
Source File: OauthRawGcsServiceTest.java    From appengine-gcs-client with Apache License 2.0 5 votes vote down vote up
private void assertHttpRequestEquals(HTTPRequest req1, HTTPRequest req2) {
  Map<String, String> req1Headers = toMap(req1.getHeaders());
  Map<String, String> req2Headers = toMap(req2.getHeaders());

  assertArrayEquals(req1.getPayload(), req2.getPayload());
  assertEquals(req1.getURL().toString(), req2.getURL().toString());
  assertEquals(req1.getMethod(), req2.getMethod());
  assertEquals(req1Headers.size(), req2Headers.size());
  for (Map.Entry<String, String> entry : req1Headers.entrySet()) {
    assertEquals(entry.getValue(), req2Headers.get(entry.getKey()));
  }
}
 
Example #23
Source File: AppIdenityOauthTest.java    From appengine-gcs-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testAuthIsRetried() throws IOException, InterruptedException, ExecutionException {
  URLFetchService urlFetchService = mock(URLFetchService.class, RETURNS_MOCKS);
  FailingFetchService failingFetchService = new FailingFetchService(urlFetchService, 1);
  failingFetchService.fetch(mock(HTTPRequest.class));

  failingFetchService = new FailingFetchService(urlFetchService, 1);
  failingFetchService.fetchAsync(mock(HTTPRequest.class)).get();
}
 
Example #24
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 #25
Source File: GoogleAppEngineRequestor.java    From dropbox-sdk-java with 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 #26
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 #27
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 #28
Source File: OauthRawGcsService.java    From appengine-gcs-client with Apache License 2.0 5 votes vote down vote up
/**
 * Write the provided chunk at the offset specified in the token. If finalChunk is set, the file
 * will be closed.
 */
private RawGcsCreationToken put(final GcsRestCreationToken token, ByteBuffer chunk,
    final boolean isFinalChunk, long timeoutMillis) throws IOException {
  final int length = chunk.remaining();
  HTTPRequest req = createPutRequest(token, chunk, isFinalChunk, timeoutMillis, length);
  HTTPRequestInfo info = new HTTPRequestInfo(req);
  HTTPResponse response;
  try {
    response = urlfetch.fetch(req);
  } catch (IOException e) {
    throw createIOException(info, e);
  }
  return handlePutResponse(token, isFinalChunk, length, info, response);
}
 
Example #29
Source File: OauthRawGcsService.java    From appengine-gcs-client with Apache License 2.0 5 votes vote down vote up
HTTPRequest createPutRequest(final GcsRestCreationToken token, final ByteBuffer chunk,
    final boolean isFinalChunk, long timeoutMillis, final int length) {
  long offset = token.offset;
  Preconditions.checkArgument(offset % CHUNK_ALIGNMENT_BYTES == 0,
      "%s: Offset not aligned; offset=%s, length=%s, token=%s",
      this, offset, length, token);
  Preconditions.checkArgument(isFinalChunk || length % CHUNK_ALIGNMENT_BYTES == 0,
      "%s: Chunk not final and not aligned: offset=%s, length=%s, token=%s",
      this, offset, length, token);
  Preconditions.checkArgument(isFinalChunk || length > 0,
      "%s: Chunk empty and not final: offset=%s, length=%s, token=%s",
      this, offset, length, token);
  if (log.isLoggable(Level.FINEST)) {
    log.finest(this + ": About to write to " + token + " " + String.format("0x%x", length)
        + " bytes at offset " + String.format("0x%x", offset)
        + "; isFinalChunk: " + isFinalChunk + ")");
  }
  long limit = offset + length;
  Map<String, String> queryStrings = Collections.singletonMap(UPLOAD_ID, token.uploadId);
  final HTTPRequest req =
      makeRequest(token.filename, queryStrings, PUT, timeoutMillis, chunk);
  req.setHeader(
      new HTTPHeader(CONTENT_RANGE,
          "bytes " + (length == 0 ? "*" : offset + "-" + (limit - 1))
          + (isFinalChunk ? "/" + limit : "/*")));
  return req;
}
 
Example #30
Source File: URLFetchUtils.java    From appengine-gcs-client with Apache License 2.0 5 votes vote down vote up
HTTPRequestInfo(HTTPRequest req) {
  method = req.getMethod().toString();
  byte[] payload = req.getPayload();
  length = payload == null ? -1 : payload.length;
  url = req.getURL();
  h1 = null;
  h2 = req.getHeaders();
}