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();
}
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);
}
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));
});
}
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() + ".");
}
}
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);
}
Aggregations