Search in sources :

Example 1 with HttpHeaders

use of com.google.api.client.http.HttpHeaders in project google-cloud-java by GoogleCloudPlatform.

the class HttpStorageRpc method open.

@Override
public String open(StorageObject object, Map<Option, ?> options) {
    try {
        Insert req = storage.objects().insert(object.getBucket(), object);
        GenericUrl url = req.buildHttpRequest().getUrl();
        String scheme = url.getScheme();
        String host = url.getHost();
        String path = "/upload" + url.getRawPath();
        url = new GenericUrl(scheme + "://" + host + path);
        url.set("uploadType", "resumable");
        url.set("name", object.getName());
        for (Option option : options.keySet()) {
            Object content = option.get(options);
            if (content != null) {
                url.set(option.value(), content.toString());
            }
        }
        JsonFactory jsonFactory = storage.getJsonFactory();
        HttpRequestFactory requestFactory = storage.getRequestFactory();
        HttpRequest httpRequest = requestFactory.buildPostRequest(url, new JsonHttpContent(jsonFactory, object));
        HttpHeaders requestHeaders = httpRequest.getHeaders();
        requestHeaders.set("X-Upload-Content-Type", firstNonNull(object.getContentType(), "application/octet-stream"));
        String key = Option.CUSTOMER_SUPPLIED_KEY.getString(options);
        if (key != null) {
            BaseEncoding base64 = BaseEncoding.base64();
            HashFunction hashFunction = Hashing.sha256();
            requestHeaders.set("x-goog-encryption-algorithm", "AES256");
            requestHeaders.set("x-goog-encryption-key", key);
            requestHeaders.set("x-goog-encryption-key-sha256", base64.encode(hashFunction.hashBytes(base64.decode(key)).asBytes()));
        }
        HttpResponse response = httpRequest.execute();
        if (response.getStatusCode() != 200) {
            GoogleJsonError error = new GoogleJsonError();
            error.setCode(response.getStatusCode());
            error.setMessage(response.getStatusMessage());
            throw translate(error);
        }
        return response.getHeaders().getLocation();
    } catch (IOException ex) {
        throw translate(ex);
    }
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpHeaders(com.google.api.client.http.HttpHeaders) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) JsonFactory(com.google.api.client.json.JsonFactory) HttpResponse(com.google.api.client.http.HttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) GenericUrl(com.google.api.client.http.GenericUrl) JsonHttpContent(com.google.api.client.http.json.JsonHttpContent) IOException(java.io.IOException) Insert(com.google.api.services.storage.Storage.Objects.Insert) BaseEncoding(com.google.common.io.BaseEncoding) HashFunction(com.google.common.hash.HashFunction) StorageObject(com.google.api.services.storage.model.StorageObject) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError)

Example 2 with HttpHeaders

use of com.google.api.client.http.HttpHeaders in project google-cloud-java by GoogleCloudPlatform.

the class HttpStorageRpc method read.

@Override
public Tuple<String, byte[]> read(StorageObject from, Map<Option, ?> options, long position, int bytes) {
    try {
        Get req = storage.objects().get(from.getBucket(), from.getName()).setGeneration(from.getGeneration()).setIfMetagenerationMatch(Option.IF_METAGENERATION_MATCH.getLong(options)).setIfMetagenerationNotMatch(Option.IF_METAGENERATION_NOT_MATCH.getLong(options)).setIfGenerationMatch(Option.IF_GENERATION_MATCH.getLong(options)).setIfGenerationNotMatch(Option.IF_GENERATION_NOT_MATCH.getLong(options));
        checkArgument(position >= 0, "Position should be non-negative, is %d", position);
        StringBuilder range = new StringBuilder();
        range.append("bytes=").append(position).append("-").append(position + bytes - 1);
        HttpHeaders requestHeaders = req.getRequestHeaders();
        requestHeaders.setRange(range.toString());
        setEncryptionHeaders(requestHeaders, ENCRYPTION_KEY_PREFIX, options);
        ByteArrayOutputStream output = new ByteArrayOutputStream(bytes);
        HttpResponse httpResponse = req.executeMedia();
        // todo(mziccard) remove when
        // https://github.com/GoogleCloudPlatform/google-cloud-java/issues/982 is fixed
        String contentEncoding = httpResponse.getContentEncoding();
        if (contentEncoding != null && contentEncoding.contains("gzip")) {
            try {
                Field responseField = httpResponse.getClass().getDeclaredField("response");
                responseField.setAccessible(true);
                LowLevelHttpResponse lowLevelHttpResponse = (LowLevelHttpResponse) responseField.get(httpResponse);
                IOUtils.copy(lowLevelHttpResponse.getContent(), output);
            } catch (IllegalAccessException | NoSuchFieldException ex) {
                throw new StorageException(BaseServiceException.UNKNOWN_CODE, "Error parsing gzip response", ex);
            }
        } else {
            httpResponse.download(output);
        }
        String etag = req.getLastResponseHeaders().getETag();
        return Tuple.of(etag, output.toByteArray());
    } catch (IOException ex) {
        StorageException serviceException = translate(ex);
        if (serviceException.getCode() == HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE) {
            return Tuple.of(null, new byte[0]);
        }
        throw serviceException;
    }
}
Also used : HttpHeaders(com.google.api.client.http.HttpHeaders) HttpResponse(com.google.api.client.http.HttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Field(java.lang.reflect.Field) Get(com.google.api.services.storage.Storage.Objects.Get) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) StorageException(com.google.cloud.storage.StorageException)

Example 3 with HttpHeaders

use of com.google.api.client.http.HttpHeaders in project google-cloud-java by GoogleCloudPlatform.

the class HttpStorageRpc method rewrite.

private RewriteResponse rewrite(RewriteRequest req, String token) {
    try {
        Long maxBytesRewrittenPerCall = req.megabytesRewrittenPerCall != null ? req.megabytesRewrittenPerCall * MEGABYTE : null;
        Storage.Objects.Rewrite rewrite = storage.objects().rewrite(req.source.getBucket(), req.source.getName(), req.target.getBucket(), req.target.getName(), req.overrideInfo ? req.target : null).setSourceGeneration(req.source.getGeneration()).setRewriteToken(token).setMaxBytesRewrittenPerCall(maxBytesRewrittenPerCall).setProjection(DEFAULT_PROJECTION).setIfSourceMetagenerationMatch(Option.IF_SOURCE_METAGENERATION_MATCH.getLong(req.sourceOptions)).setIfSourceMetagenerationNotMatch(Option.IF_SOURCE_METAGENERATION_NOT_MATCH.getLong(req.sourceOptions)).setIfSourceGenerationMatch(Option.IF_SOURCE_GENERATION_MATCH.getLong(req.sourceOptions)).setIfSourceGenerationNotMatch(Option.IF_SOURCE_GENERATION_NOT_MATCH.getLong(req.sourceOptions)).setIfMetagenerationMatch(Option.IF_METAGENERATION_MATCH.getLong(req.targetOptions)).setIfMetagenerationNotMatch(Option.IF_METAGENERATION_NOT_MATCH.getLong(req.targetOptions)).setIfGenerationMatch(Option.IF_GENERATION_MATCH.getLong(req.targetOptions)).setIfGenerationNotMatch(Option.IF_GENERATION_NOT_MATCH.getLong(req.targetOptions));
        HttpHeaders requestHeaders = rewrite.getRequestHeaders();
        setEncryptionHeaders(requestHeaders, SOURCE_ENCRYPTION_KEY_PREFIX, req.sourceOptions);
        setEncryptionHeaders(requestHeaders, ENCRYPTION_KEY_PREFIX, req.targetOptions);
        com.google.api.services.storage.model.RewriteResponse rewriteResponse = rewrite.execute();
        return new RewriteResponse(req, rewriteResponse.getResource(), rewriteResponse.getObjectSize().longValue(), rewriteResponse.getDone(), rewriteResponse.getRewriteToken(), rewriteResponse.getTotalBytesRewritten().longValue());
    } catch (IOException ex) {
        throw translate(ex);
    }
}
Also used : HttpHeaders(com.google.api.client.http.HttpHeaders) Objects(com.google.api.services.storage.model.Objects) IOException(java.io.IOException)

Example 4 with HttpHeaders

use of com.google.api.client.http.HttpHeaders in project data-transfer-project by google.

the class SmugMugPhotoService method postRequest.

private <T> T postRequest(String url, HttpContent content, Map<String, String> headers, TypeReference<T> typeReference) throws IOException {
    HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
    String fullUrl = url;
    if (!fullUrl.contains("://")) {
        fullUrl = BASE_URL + url;
    }
    HttpRequest postRequest = requestFactory.buildPostRequest(new GenericUrl(fullUrl), content);
    HttpHeaders httpHeaders = new HttpHeaders().setAccept("application/json").setContentType("application/json");
    for (Entry<String, String> entry : headers.entrySet()) {
        httpHeaders.put(entry.getKey(), entry.getValue());
    }
    postRequest.setHeaders(httpHeaders);
    try {
        postRequest = (HttpRequest) this.authConsumer.sign(postRequest).unwrap();
    } catch (OAuthMessageSignerException | OAuthExpectationFailedException | OAuthCommunicationException e) {
        throw new IOException("Couldn't create post request", e);
    }
    HttpResponse response;
    try {
        response = postRequest.execute();
    } catch (HttpResponseException e) {
        throw new IOException("Problem making request: " + postRequest.getUrl(), e);
    }
    int statusCode = response.getStatusCode();
    if (statusCode < 200 || statusCode >= 300) {
        throw new IOException("Bad status code: " + statusCode + " error: " + response.getStatusMessage());
    }
    String result = CharStreams.toString(new InputStreamReader(response.getContent(), Charsets.UTF_8));
    return MAPPER.readValue(result, typeReference);
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpHeaders(com.google.api.client.http.HttpHeaders) InputStreamReader(java.io.InputStreamReader) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) OAuthMessageSignerException(oauth.signpost.exception.OAuthMessageSignerException) OAuthExpectationFailedException(oauth.signpost.exception.OAuthExpectationFailedException) HttpResponse(com.google.api.client.http.HttpResponse) HttpResponseException(com.google.api.client.http.HttpResponseException) GenericUrl(com.google.api.client.http.GenericUrl) IOException(java.io.IOException) OAuthCommunicationException(oauth.signpost.exception.OAuthCommunicationException)

Example 5 with HttpHeaders

use of com.google.api.client.http.HttpHeaders in project copybara by google.

the class GitHubApiTransportImpl method getHttpRequestFactory.

private HttpRequestFactory getHttpRequestFactory(@Nullable UserPassword userPassword) throws RepoException, ValidationException {
    return httpTransport.createRequestFactory(request -> {
        request.setConnectTimeout((int) Duration.ofMinutes(1).toMillis());
        request.setReadTimeout((int) Duration.ofMinutes(1).toMillis());
        HttpHeaders httpHeaders = new HttpHeaders();
        if (userPassword != null) {
            httpHeaders.setBasicAuthentication(userPassword.getUsername(), userPassword.getPassword_BeCareful());
        }
        request.setHeaders(httpHeaders);
        request.setParser(new JsonObjectParser(JSON_FACTORY));
    });
}
Also used : HttpHeaders(com.google.api.client.http.HttpHeaders) JsonObjectParser(com.google.api.client.json.JsonObjectParser)

Aggregations

HttpHeaders (com.google.api.client.http.HttpHeaders)46 HttpRequest (com.google.api.client.http.HttpRequest)22 IOException (java.io.IOException)21 GenericUrl (com.google.api.client.http.GenericUrl)13 HttpResponse (com.google.api.client.http.HttpResponse)13 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)10 GoogleJsonError (com.google.api.client.googleapis.json.GoogleJsonError)7 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)6 Test (org.junit.Test)6 HttpContent (com.google.api.client.http.HttpContent)5 Objects (com.google.api.services.storage.model.Objects)5 HttpResponseException (com.google.api.client.http.HttpResponseException)4 HttpTransport (com.google.api.client.http.HttpTransport)4 JsonObjectParser (com.google.api.client.json.JsonObjectParser)4 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)4 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)4 SocketTimeoutException (java.net.SocketTimeoutException)4 Map (java.util.Map)4 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)3 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)3