Java Code Examples for com.google.appengine.api.urlfetch.HTTPRequest#setHeader()

The following examples show how to use com.google.appengine.api.urlfetch.HTTPRequest#setHeader() . 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: 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 2
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 3
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 4
Source File: AbstractOAuthURLFetchService.java    From appengine-gcs-client with Apache License 2.0 5 votes vote down vote up
private HTTPRequest createAuthorizeRequest(final HTTPRequest req) throws RetryHelperException {
  String token = RetryHelper.runWithRetries(new Callable<String>() {
    @Override
    public String call() {
      return getToken();
    }
  }, RETRY_PARAMS, EXCEPTION_HANDLER);
  HTTPRequest request = URLFetchUtils.copyRequest(req);
  request.setHeader(new HTTPHeader("Authorization", "Bearer " + token));
  return request;
}
 
Example 5
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 6
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 7
Source File: URLFetchUtilsTest.java    From appengine-gcs-client with 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 8
Source File: URLFetchTest.java    From appengine-tck with 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 9
Source File: OauthRawGcsService.java    From appengine-gcs-client with Apache License 2.0 4 votes vote down vote up
/**
 * Might not fill all of dst.
 */
@Override
public Future<GcsFileMetadata> readObjectAsync(final ByteBuffer dst, final GcsFilename filename,
    long startOffsetBytes, long timeoutMillis) {
  Preconditions.checkArgument(startOffsetBytes >= 0, "%s: offset must be non-negative: %s", this,
      startOffsetBytes);
  final int n = dst.remaining();
  Preconditions.checkArgument(n > 0, "%s: dst full: %s", this, dst);
  final int want = Math.min(READ_LIMIT_BYTES, n);

  final HTTPRequest req = makeRequest(filename, null, GET, timeoutMillis);
  req.setHeader(
      new HTTPHeader(RANGE, "bytes=" + startOffsetBytes + "-" + (startOffsetBytes + want - 1)));
  final HTTPRequestInfo info = new HTTPRequestInfo(req);
  return new FutureWrapper<HTTPResponse, GcsFileMetadata>(urlfetch.fetchAsync(req)) {
    @Override
    protected GcsFileMetadata wrap(HTTPResponse resp) throws IOException {
      long totalLength;
      switch (resp.getResponseCode()) {
        case 200:
          totalLength = getLengthFromHeader(resp, X_GOOG_CONTENT_LENGTH);
          break;
        case 206:
          totalLength = getLengthFromContentRange(resp);
          break;
        case 404:
          throw new FileNotFoundException("Could not find: " + filename);
        case 416:
          throw new BadRangeException("Requested Range not satisfiable; perhaps read past EOF? "
              + URLFetchUtils.describeRequestAndResponse(info, resp));
        default:
          throw HttpErrorHandler.error(info, resp);
      }
      byte[] content = resp.getContent();
      Preconditions.checkState(content.length <= want, "%s: got %s > wanted %s", this,
          content.length, want);
      dst.put(content);
      return getMetadataFromResponse(filename, resp, totalLength);
    }

    @Override
    protected Throwable convertException(Throwable e) {
      return OauthRawGcsService.convertException(info, e);
    }
  };
}