Search in sources :

Example 36 with HttpHeaders

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

the class BatchRequest method execute.

/**
 * Executes all queued HTTP requests in a single call, parses the responses and invokes callbacks.
 *
 * <p>
 * Calling {@link #execute()} executes and clears the queued requests. This means that the
 * {@link BatchRequest} object can be reused to {@link #queue} and {@link #execute()} requests
 * again.
 * </p>
 */
public void execute() throws IOException {
    boolean retryAllowed;
    Preconditions.checkState(!requestInfos.isEmpty());
    HttpRequest batchRequest = requestFactory.buildPostRequest(this.batchUrl, null);
    // NOTE: batch does not support gzip encoding
    HttpExecuteInterceptor originalInterceptor = batchRequest.getInterceptor();
    batchRequest.setInterceptor(new BatchInterceptor(originalInterceptor));
    int retriesRemaining = batchRequest.getNumberOfRetries();
    BackOffPolicy backOffPolicy = batchRequest.getBackOffPolicy();
    if (backOffPolicy != null) {
        // Reset the BackOffPolicy at the start of each execute.
        backOffPolicy.reset();
    }
    do {
        retryAllowed = retriesRemaining > 0;
        MultipartContent batchContent = new MultipartContent();
        batchContent.getMediaType().setSubType("mixed");
        int contentId = 1;
        for (RequestInfo<?, ?> requestInfo : requestInfos) {
            batchContent.addPart(new MultipartContent.Part(new HttpHeaders().setAcceptEncoding(null).set("Content-ID", contentId++), new HttpRequestContent(requestInfo.request)));
        }
        batchRequest.setContent(batchContent);
        HttpResponse response = batchRequest.execute();
        BatchUnparsedResponse batchResponse;
        try {
            // Find the boundary from the Content-Type header.
            String boundary = "--" + response.getMediaType().getParameter("boundary");
            // Parse the content stream.
            InputStream contentStream = response.getContent();
            batchResponse = new BatchUnparsedResponse(contentStream, boundary, requestInfos, retryAllowed);
            while (batchResponse.hasNext) {
                batchResponse.parseNextResponse();
            }
        } finally {
            response.disconnect();
        }
        List<RequestInfo<?, ?>> unsuccessfulRequestInfos = batchResponse.unsuccessfulRequestInfos;
        if (!unsuccessfulRequestInfos.isEmpty()) {
            requestInfos = unsuccessfulRequestInfos;
            // backOff if required.
            if (batchResponse.backOffRequired && backOffPolicy != null) {
                long backOffTime = backOffPolicy.getNextBackOffMillis();
                if (backOffTime != BackOffPolicy.STOP) {
                    try {
                        sleeper.sleep(backOffTime);
                    } catch (InterruptedException exception) {
                    // ignore
                    }
                }
            }
        } else {
            break;
        }
        retriesRemaining--;
    } while (retryAllowed);
    requestInfos.clear();
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpHeaders(com.google.api.client.http.HttpHeaders) BackOffPolicy(com.google.api.client.http.BackOffPolicy) InputStream(java.io.InputStream) HttpResponse(com.google.api.client.http.HttpResponse) MultipartContent(com.google.api.client.http.MultipartContent) HttpExecuteInterceptor(com.google.api.client.http.HttpExecuteInterceptor)

Example 37 with HttpHeaders

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

the class SmugMugInterface method postRequest.

<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);
    // TODO(olsona): sign request
    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) 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)

Example 38 with HttpHeaders

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

the class GerritApiTransportImpl method getHttpRequestFactory.

/**
 * TODO(malcon): Consolidate GitHub and this one in one class
 */
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)

Example 39 with HttpHeaders

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

the class GoogleBaseImageValidator method validate.

@Override
public void validate(ConfigProblemSetBuilder p, GoogleBaseImage n) {
    String sourceImage = n.getVirtualizationSettings().getSourceImage();
    String sourceImageFamily = n.getVirtualizationSettings().getSourceImageFamily();
    if (StringUtils.isEmpty(sourceImage) && StringUtils.isEmpty(sourceImageFamily)) {
        p.addProblem(Problem.Severity.ERROR, "Either source image or source image family must be specified for " + n.getBaseImage().getId() + ".");
    }
    if (!StringUtils.isEmpty(sourceImage)) {
        int i = 0;
        boolean[] foundSourceImageHolder = new boolean[1];
        while (!foundSourceImageHolder[0] && i < credentialsList.size()) {
            GoogleNamedAccountCredentials credentials = credentialsList.get(i);
            List<String> imageProjects = Lists.newArrayList(credentials.getProject());
            imageProjects.addAll(credentials.getImageProjects());
            imageProjects.addAll(baseImageProjects);
            Compute compute = credentials.getCompute();
            BatchRequest imageListBatch = buildBatchRequest(compute);
            JsonBatchCallback<ImageList> imageListCallback = new JsonBatchCallback<ImageList>() {

                @Override
                public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) throws IOException {
                    p.addProblem(Problem.Severity.ERROR, "Error locating " + sourceImage + " in these projects: " + imageProjects + ": " + e.getMessage() + ".");
                }

                @Override
                public void onSuccess(ImageList imageList, HttpHeaders responseHeaders) throws IOException {
                    // No need to look through these images if the requested image was already found.
                    if (!foundSourceImageHolder[0]) {
                        if (imageList.getItems() != null) {
                            foundSourceImageHolder[0] = imageList.getItems().stream().filter(image -> image.getName().equals(sourceImage)).findFirst().isPresent();
                        }
                    }
                }
            };
            try {
                for (String imageProject : imageProjects) {
                    compute.images().list(imageProject).queue(imageListBatch, imageListCallback);
                }
                imageListBatch.execute();
            } catch (IOException e) {
                p.addProblem(Problem.Severity.ERROR, "Error locating " + sourceImage + " in these projects: " + imageProjects + ": " + e.getMessage() + ".");
            }
            i++;
        }
        if (!foundSourceImageHolder[0]) {
            p.addProblem(Problem.Severity.ERROR, "Image " + sourceImage + " not found via any configured google account.");
        }
    }
    if (!StringUtils.isEmpty(sourceImageFamily)) {
        int i = 0;
        boolean[] foundSourceImageFamilyHolder = new boolean[1];
        while (!foundSourceImageFamilyHolder[0] && i < credentialsList.size()) {
            GoogleNamedAccountCredentials credentials = credentialsList.get(i);
            List<String> imageProjects = Lists.newArrayList(credentials.getProject());
            imageProjects.addAll(credentials.getImageProjects());
            imageProjects.addAll(baseImageProjects);
            Compute compute = credentials.getCompute();
            BatchRequest imageListBatch = buildBatchRequest(compute);
            JsonBatchCallback<ImageList> imageListCallback = new JsonBatchCallback<ImageList>() {

                @Override
                public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) throws IOException {
                    p.addProblem(Problem.Severity.ERROR, "Error locating " + sourceImageFamily + " in these projects: " + imageProjects + ": " + e.getMessage() + ".");
                }

                @Override
                public void onSuccess(ImageList imageList, HttpHeaders responseHeaders) throws IOException {
                    // No need to look through these images if the requested image family was already found.
                    if (!foundSourceImageFamilyHolder[0]) {
                        if (imageList.getItems() != null) {
                            foundSourceImageFamilyHolder[0] = imageList.getItems().stream().filter(image -> sourceImageFamily.equals(image.getFamily())).findFirst().isPresent();
                        }
                    }
                }
            };
            try {
                for (String imageProject : imageProjects) {
                    compute.images().list(imageProject).queue(imageListBatch, imageListCallback);
                }
                imageListBatch.execute();
            } catch (IOException e) {
                p.addProblem(Problem.Severity.ERROR, "Error locating " + sourceImageFamily + " in these projects: " + imageProjects + ": " + e.getMessage() + ".");
            }
            i++;
        }
        if (!foundSourceImageFamilyHolder[0]) {
            p.addProblem(Problem.Severity.ERROR, "Image family " + sourceImageFamily + " not found via any configured google account.");
        }
    }
    if (StringUtils.isEmpty(n.getBaseImage().getPackageType())) {
        p.addProblem(Problem.Severity.ERROR, "Package type must be specified for " + n.getBaseImage().getId() + ".");
    }
}
Also used : GoogleNamedAccountCredentials(com.netflix.spinnaker.clouddriver.google.security.GoogleNamedAccountCredentials) HttpHeaders(com.google.api.client.http.HttpHeaders) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError) GoogleBaseImage(com.netflix.spinnaker.halyard.config.model.v1.providers.google.GoogleBaseImage) IOException(java.io.IOException) HttpRequest(com.google.api.client.http.HttpRequest) ImageList(com.google.api.services.compute.model.ImageList) EqualsAndHashCode(lombok.EqualsAndHashCode) ConfigProblemSetBuilder(com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemSetBuilder) List(java.util.List) Lists(com.google.common.collect.Lists) Validator(com.netflix.spinnaker.halyard.config.model.v1.node.Validator) Data(lombok.Data) Problem(com.netflix.spinnaker.halyard.core.problem.v1.Problem) JsonBatchCallback(com.google.api.client.googleapis.batch.json.JsonBatchCallback) BatchRequest(com.google.api.client.googleapis.batch.BatchRequest) Compute(com.google.api.services.compute.Compute) StringUtils(org.springframework.util.StringUtils) BatchRequest(com.google.api.client.googleapis.batch.BatchRequest) HttpHeaders(com.google.api.client.http.HttpHeaders) JsonBatchCallback(com.google.api.client.googleapis.batch.json.JsonBatchCallback) IOException(java.io.IOException) GoogleNamedAccountCredentials(com.netflix.spinnaker.clouddriver.google.security.GoogleNamedAccountCredentials) ImageList(com.google.api.services.compute.model.ImageList) Compute(com.google.api.services.compute.Compute) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError)

Example 40 with HttpHeaders

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

the class HttpCredentialsAdapterTest method initialize_populatesOAuth2Credentials.

@Test
public void initialize_populatesOAuth2Credentials() throws IOException {
    final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2";
    final String expectedAuthorization = InternalAuthHttpConstants.BEARER_PREFIX + accessToken;
    MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
    transportFactory.transport.addClient(CLIENT_ID, CLIENT_SECRET);
    transportFactory.transport.addRefreshToken(REFRESH_TOKEN, accessToken);
    OAuth2Credentials credentials = UserCredentials.newBuilder().setClientId(CLIENT_ID).setClientSecret(CLIENT_SECRET).setRefreshToken(REFRESH_TOKEN).setHttpTransportFactory(transportFactory).build();
    HttpCredentialsAdapter adapter = new HttpCredentialsAdapter(credentials);
    HttpRequestFactory requestFactory = transportFactory.transport.createRequestFactory();
    HttpRequest request = requestFactory.buildGetRequest(new GenericUrl("http://foo"));
    adapter.initialize(request);
    HttpHeaders requestHeaders = request.getHeaders();
    String authorizationHeader = requestHeaders.getAuthorization();
    assertEquals(authorizationHeader, expectedAuthorization);
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpHeaders(com.google.api.client.http.HttpHeaders) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) MockTokenServerTransportFactory(com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory) OAuth2Credentials(com.google.auth.oauth2.OAuth2Credentials) GenericUrl(com.google.api.client.http.GenericUrl) Test(org.junit.Test)

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