Search in sources :

Example 16 with HttpHeaders

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

the class OAuth2Utils method runningOnComputeEngine.

static boolean runningOnComputeEngine(HttpTransport transport, SystemEnvironmentProvider environment) {
    // If the environment has requested that we do no GCE checks, return immediately.
    if (Boolean.parseBoolean(environment.getEnv("NO_GCE_CHECK"))) {
        return false;
    }
    GenericUrl tokenUrl = new GenericUrl(getMetadataServerUrl(environment));
    for (int i = 1; i <= MAX_COMPUTE_PING_TRIES; ++i) {
        try {
            HttpRequest request = transport.createRequestFactory().buildGetRequest(tokenUrl);
            request.setConnectTimeout(COMPUTE_PING_CONNECTION_TIMEOUT_MS);
            HttpResponse response = request.execute();
            try {
                HttpHeaders headers = response.getHeaders();
                return headersContainValue(headers, "Metadata-Flavor", "Google");
            } finally {
                response.disconnect();
            }
        } catch (SocketTimeoutException expected) {
        // Ignore logging timeouts which is the expected failure mode in non GCE environments.
        } catch (IOException e) {
            LOGGER.log(Level.WARNING, "Failed to detect whether we are running on Google Compute Engine.", e);
        }
    }
    return false;
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpHeaders(com.google.api.client.http.HttpHeaders) SocketTimeoutException(java.net.SocketTimeoutException) HttpResponse(com.google.api.client.http.HttpResponse) GenericUrl(com.google.api.client.http.GenericUrl) IOException(java.io.IOException)

Example 17 with HttpHeaders

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

the class BatchUnparsedResponse method parseAndCallback.

/**
 * Parse an object into a new instance of the data class using
 * {@link HttpResponse#parseAs(java.lang.reflect.Type)}.
 */
private <T, E> void parseAndCallback(RequestInfo<T, E> requestInfo, int statusCode, HttpResponse response) throws IOException {
    BatchCallback<T, E> callback = requestInfo.callback;
    HttpHeaders responseHeaders = response.getHeaders();
    HttpUnsuccessfulResponseHandler unsuccessfulResponseHandler = requestInfo.request.getUnsuccessfulResponseHandler();
    BackOffPolicy backOffPolicy = requestInfo.request.getBackOffPolicy();
    // Reset backOff flag.
    backOffRequired = false;
    if (HttpStatusCodes.isSuccess(statusCode)) {
        if (callback == null) {
            // No point in parsing if there is no callback.
            return;
        }
        T parsed = getParsedDataClass(requestInfo.dataClass, response, requestInfo);
        callback.onSuccess(parsed, responseHeaders);
    } else {
        HttpContent content = requestInfo.request.getContent();
        boolean retrySupported = retryAllowed && (content == null || content.retrySupported());
        boolean errorHandled = false;
        boolean redirectRequest = false;
        if (unsuccessfulResponseHandler != null) {
            errorHandled = unsuccessfulResponseHandler.handleResponse(requestInfo.request, response, retrySupported);
        }
        if (!errorHandled) {
            if (requestInfo.request.handleRedirect(response.getStatusCode(), response.getHeaders())) {
                redirectRequest = true;
            } else if (retrySupported && backOffPolicy != null && backOffPolicy.isBackOffRequired(response.getStatusCode())) {
                backOffRequired = true;
            }
        }
        if (retrySupported && (errorHandled || backOffRequired || redirectRequest)) {
            unsuccessfulRequestInfos.add(requestInfo);
        } else {
            if (callback == null) {
                // No point in parsing if there is no callback.
                return;
            }
            E parsed = getParsedDataClass(requestInfo.errorClass, response, requestInfo);
            callback.onFailure(parsed, responseHeaders);
        }
    }
}
Also used : HttpHeaders(com.google.api.client.http.HttpHeaders) BackOffPolicy(com.google.api.client.http.BackOffPolicy) HttpUnsuccessfulResponseHandler(com.google.api.client.http.HttpUnsuccessfulResponseHandler) HttpContent(com.google.api.client.http.HttpContent)

Example 18 with HttpHeaders

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

the class FhirIOTestUtil method tearDownTempBucket.

public static void tearDownTempBucket() throws IOException {
    GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
    HttpRequestInitializer requestInitializer = request -> {
        HttpHeaders requestHeaders = request.getHeaders();
        if (!credentials.hasRequestMetadata()) {
            return;
        }
        URI uri = null;
        if (request.getUrl() != null) {
            uri = request.getUrl().toURI();
        }
        Map<String, List<String>> credentialHeaders = credentials.getRequestMetadata(uri);
        if (credentialHeaders == null) {
            return;
        }
        for (Map.Entry<String, List<String>> entry : credentialHeaders.entrySet()) {
            String headerName = entry.getKey();
            List<String> requestValues = new ArrayList<>(entry.getValue());
            requestHeaders.put(headerName, requestValues);
        }
        // 1 minute connect timeout
        request.setConnectTimeout(60000);
        // 1 minute read timeout
        request.setReadTimeout(60000);
    };
    Storage storage = new Storage.Builder(new NetHttpTransport(), new JacksonFactory(), requestInitializer).build();
    List<StorageObject> blobs = storage.objects().list(DEFAULT_TEMP_BUCKET).execute().getItems();
    if (blobs != null) {
        for (StorageObject blob : blobs) {
            storage.objects().delete(DEFAULT_TEMP_BUCKET, blob.getId());
        }
    }
}
Also used : JsonObject(com.google.gson.JsonObject) Storage(com.google.api.services.storage.Storage) Arrays(java.util.Arrays) HashMap(java.util.HashMap) JsonParser(com.google.gson.JsonParser) ArrayList(java.util.ArrayList) JsonElement(com.google.gson.JsonElement) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) Map(java.util.Map) URI(java.net.URI) Path(java.nio.file.Path) StorageObject(com.google.api.services.storage.model.StorageObject) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) HttpHeaders(com.google.api.client.http.HttpHeaders) Files(java.nio.file.Files) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) List(java.util.List) Stream(java.util.stream.Stream) Paths(java.nio.file.Paths) HttpBody(com.google.api.services.healthcare.v1.model.HttpBody) HealthcareHttpException(org.apache.beam.sdk.io.gcp.healthcare.HttpHealthcareApiClient.HealthcareHttpException) Collections(java.util.Collections) HttpHeaders(com.google.api.client.http.HttpHeaders) StorageObject(com.google.api.services.storage.model.StorageObject) URI(java.net.URI) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) Storage(com.google.api.services.storage.Storage) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) ArrayList(java.util.ArrayList) List(java.util.List) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) HashMap(java.util.HashMap) Map(java.util.Map)

Example 19 with HttpHeaders

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

the class GceMetadataService method metadata.

public String metadata(String metadataPath) throws IOException, URISyntaxException {
    // Forcing Google Token API URL as set in GCE SDK to
    //      http://metadata/computeMetadata/v1/instance/service-accounts/default/token
    // See https://developers.google.com/compute/docs/metadata#metadataserver
    final URI urlMetadataNetwork = new URI(GCE_HOST.get(settings)).resolve("/computeMetadata/v1/instance/").resolve(metadataPath);
    logger.debug("get metadata from [{}]", urlMetadataNetwork);
    HttpHeaders headers;
    try {
        // hack around code messiness in GCE code
        // TODO: get this fixed
        headers = Access.doPrivileged(HttpHeaders::new);
        GenericUrl genericUrl = Access.doPrivileged(() -> new GenericUrl(urlMetadataNetwork));
        // This is needed to query meta data: https://cloud.google.com/compute/docs/metadata
        headers.put("Metadata-Flavor", "Google");
        HttpResponse response = Access.doPrivilegedIOException(() -> getGceHttpTransport().createRequestFactory().buildGetRequest(genericUrl).setHeaders(headers).execute());
        String metadata = response.parseAsString();
        logger.debug("metadata found [{}]", metadata);
        return metadata;
    } catch (Exception e) {
        throw new IOException("failed to fetch metadata from [" + urlMetadataNetwork + "]", e);
    }
}
Also used : HttpHeaders(com.google.api.client.http.HttpHeaders) HttpResponse(com.google.api.client.http.HttpResponse) GenericUrl(com.google.api.client.http.GenericUrl) IOException(java.io.IOException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) GeneralSecurityException(java.security.GeneralSecurityException)

Example 20 with HttpHeaders

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

the class GoogleCloudStorageBlobStore method deleteBlobs.

/**
     * Deletes multiple blobs in the given bucket (uses a batch request to perform this)
     *
     * @param blobNames names of the bucket to delete
     */
void deleteBlobs(Collection<String> blobNames) throws IOException {
    if (blobNames == null || blobNames.isEmpty()) {
        return;
    }
    if (blobNames.size() == 1) {
        deleteBlob(blobNames.iterator().next());
        return;
    }
    final List<Storage.Objects.Delete> deletions = new ArrayList<>();
    final Iterator<String> blobs = blobNames.iterator();
    SocketAccess.doPrivilegedVoidIOException(() -> {
        while (blobs.hasNext()) {
            // Create a delete request for each blob to delete
            deletions.add(client.objects().delete(bucket, blobs.next()));
            if (blobs.hasNext() == false || deletions.size() == MAX_BATCHING_REQUESTS) {
                try {
                    // Deletions are executed using a batch request
                    BatchRequest batch = client.batch();
                    // Used to track successful deletions
                    CountDown countDown = new CountDown(deletions.size());
                    for (Storage.Objects.Delete delete : deletions) {
                        // Queue the delete request in batch
                        delete.queue(batch, new JsonBatchCallback<Void>() {

                            @Override
                            public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) throws IOException {
                                logger.error("failed to delete blob [{}] in bucket [{}]: {}", delete.getObject(), delete.getBucket(), e.getMessage());
                            }

                            @Override
                            public void onSuccess(Void aVoid, HttpHeaders responseHeaders) throws IOException {
                                countDown.countDown();
                            }
                        });
                    }
                    batch.execute();
                    if (countDown.isCountedDown() == false) {
                        throw new IOException("Failed to delete all [" + deletions.size() + "] blobs");
                    }
                } finally {
                    deletions.clear();
                }
            }
        }
    });
}
Also used : BatchRequest(com.google.api.client.googleapis.batch.BatchRequest) HttpHeaders(com.google.api.client.http.HttpHeaders) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CountDown(org.elasticsearch.common.util.concurrent.CountDown) Storage(com.google.api.services.storage.Storage) Objects(com.google.api.services.storage.model.Objects) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError)

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