Search in sources :

Example 1 with HTTPRequestInfo

use of com.google.appengine.tools.cloudstorage.oauth.URLFetchUtils.HTTPRequestInfo in project appengine-gcs-client by GoogleCloudPlatform.

the class OauthRawGcsService method readObjectAsync.

/**
 * 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);
        }
    };
}
Also used : HTTPRequest(com.google.appengine.api.urlfetch.HTTPRequest) HTTPResponse(com.google.appengine.api.urlfetch.HTTPResponse) FutureWrapper(com.google.appengine.api.utils.FutureWrapper) FileNotFoundException(java.io.FileNotFoundException) HTTPRequestInfo(com.google.appengine.tools.cloudstorage.oauth.URLFetchUtils.HTTPRequestInfo) BadRangeException(com.google.appengine.tools.cloudstorage.BadRangeException) HTTPHeader(com.google.appengine.api.urlfetch.HTTPHeader)

Example 2 with HTTPRequestInfo

use of com.google.appengine.tools.cloudstorage.oauth.URLFetchUtils.HTTPRequestInfo in project appengine-gcs-client by GoogleCloudPlatform.

the class OauthRawGcsService method beginObjectCreation.

@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);
    }
}
Also used : HTTPRequest(com.google.appengine.api.urlfetch.HTTPRequest) HTTPResponse(com.google.appengine.api.urlfetch.HTTPResponse) HTTPRequestInfo(com.google.appengine.tools.cloudstorage.oauth.URLFetchUtils.HTTPRequestInfo) IOException(java.io.IOException) URL(java.net.URL)

Example 3 with HTTPRequestInfo

use of com.google.appengine.tools.cloudstorage.oauth.URLFetchUtils.HTTPRequestInfo in project appengine-gcs-client by GoogleCloudPlatform.

the class OauthRawGcsService method copyObject.

@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);
    }
}
Also used : HTTPRequest(com.google.appengine.api.urlfetch.HTTPRequest) HTTPResponse(com.google.appengine.api.urlfetch.HTTPResponse) HTTPRequestInfo(com.google.appengine.tools.cloudstorage.oauth.URLFetchUtils.HTTPRequestInfo) IOException(java.io.IOException) HTTPHeader(com.google.appengine.api.urlfetch.HTTPHeader)

Example 4 with HTTPRequestInfo

use of com.google.appengine.tools.cloudstorage.oauth.URLFetchUtils.HTTPRequestInfo in project appengine-gcs-client by GoogleCloudPlatform.

the class OauthRawGcsService method putAsync.

/**
 * 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);
        }
    };
}
Also used : HTTPRequest(com.google.appengine.api.urlfetch.HTTPRequest) HTTPResponse(com.google.appengine.api.urlfetch.HTTPResponse) FutureWrapper(com.google.appengine.api.utils.FutureWrapper) HTTPRequestInfo(com.google.appengine.tools.cloudstorage.oauth.URLFetchUtils.HTTPRequestInfo)

Example 5 with HTTPRequestInfo

use of com.google.appengine.tools.cloudstorage.oauth.URLFetchUtils.HTTPRequestInfo in project appengine-gcs-client by GoogleCloudPlatform.

the class OauthRawGcsService method put.

/**
 * 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);
}
Also used : HTTPRequest(com.google.appengine.api.urlfetch.HTTPRequest) HTTPResponse(com.google.appengine.api.urlfetch.HTTPResponse) HTTPRequestInfo(com.google.appengine.tools.cloudstorage.oauth.URLFetchUtils.HTTPRequestInfo) IOException(java.io.IOException)

Aggregations

HTTPRequestInfo (com.google.appengine.tools.cloudstorage.oauth.URLFetchUtils.HTTPRequestInfo)12 HTTPRequest (com.google.appengine.api.urlfetch.HTTPRequest)11 HTTPResponse (com.google.appengine.api.urlfetch.HTTPResponse)11 IOException (java.io.IOException)8 HTTPHeader (com.google.appengine.api.urlfetch.HTTPHeader)3 FutureWrapper (com.google.appengine.api.utils.FutureWrapper)2 URL (java.net.URL)2 Test (org.junit.Test)2 HttpRequest (com.google.api.client.http.HttpRequest)1 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)1 HttpResponseException (com.google.api.client.http.HttpResponseException)1 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)1 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)1 Storage (com.google.api.services.storage.Storage)1 BadRangeException (com.google.appengine.tools.cloudstorage.BadRangeException)1 GcsFilename (com.google.appengine.tools.cloudstorage.GcsFilename)1 ListItem (com.google.appengine.tools.cloudstorage.ListItem)1 Escaper (com.google.common.escape.Escaper)1 FileNotFoundException (java.io.FileNotFoundException)1 ArrayList (java.util.ArrayList)1