use of com.google.api.client.http.HttpTransport in project google-api-java-client by google.
the class ComputeCredentialTest method testExecuteRefreshToken.
public void testExecuteRefreshToken() throws Exception {
HttpTransport transport = new MockMetadataServerTransport(ACCESS_TOKEN);
ComputeCredential credential = new ComputeCredential(transport, new JacksonFactory());
assertTrue(credential.refreshToken());
assertEquals(ACCESS_TOKEN, credential.getAccessToken());
}
use of com.google.api.client.http.HttpTransport in project google-api-java-client by google.
the class AbstractGoogleClientRequestTest method testExecuteUnparsed_error.
public void testExecuteUnparsed_error() throws Exception {
HttpTransport transport = new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(final String method, final String url) {
return new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() {
assertEquals("GET", method);
assertEquals("https://www.googleapis.com/test/path/v1/tests/foo", url);
MockLowLevelHttpResponse result = new MockLowLevelHttpResponse();
result.setStatusCode(HttpStatusCodes.STATUS_CODE_UNAUTHORIZED);
result.setContentType(Json.MEDIA_TYPE);
result.setContent(ERROR_CONTENT);
return result;
}
};
}
};
MockGoogleClient client = new MockGoogleClient.Builder(transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null).setApplicationName("Test Application").build();
MockGoogleClientRequest<String> request = new MockGoogleClientRequest<String>(client, HttpMethods.GET, URI_TEMPLATE, null, String.class);
try {
request.put("testId", "foo");
request.executeUnparsed();
fail("expected " + HttpResponseException.class);
} catch (HttpResponseException e) {
// expected
assertEquals("401" + StringUtils.LINE_SEPARATOR + ERROR_CONTENT, e.getMessage());
}
}
use of com.google.api.client.http.HttpTransport in project google-api-java-client by google.
the class AbstractGoogleClientRequestTest method subtestBuildHttpRequest_emptyContent.
private void subtestBuildHttpRequest_emptyContent(String method, boolean expectEmptyContent) throws Exception {
HttpTransport transport = new MockHttpTransport();
MockGoogleClient client = new MockGoogleClient.Builder(transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null).setApplicationName("Test Application").build();
MockGoogleClientRequest<String> request = new MockGoogleClientRequest<String>(client, method, URI_TEMPLATE, null, String.class);
HttpRequest httpRequest = request.buildHttpRequest();
if (expectEmptyContent) {
assertTrue(httpRequest.getContent() instanceof EmptyContent);
} else {
assertNull(httpRequest.getContent());
}
}
use of com.google.api.client.http.HttpTransport in project cloudbreak by hortonworks.
the class TagsUtil method checkTagsGcp.
protected static void checkTagsGcp(ApplicationContext applicationContext, String applicationName, String projectId, String serviceAccountId, String p12File, String availabilityZone, Iterable<String> instanceIdList, Map<String, String> tagsToCheckMap) throws Exception {
String serviceAccountPrivateKey = ResourceUtil.readBase64EncodedContentFromResource(applicationContext, p12File);
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
PrivateKey privateKey = SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.getPkcs12KeyStore(), new ByteArrayInputStream(Base64.decodeBase64(serviceAccountPrivateKey)), "notasecret", "privatekey", "notasecret");
JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance();
GoogleCredential googleCredential = new GoogleCredential.Builder().setTransport(httpTransport).setJsonFactory(jsonFactory).setServiceAccountId(serviceAccountId).setServiceAccountScopes(Collections.singletonList(ComputeScopes.COMPUTE)).setServiceAccountPrivateKey(privateKey).build();
Compute compute = new Builder(httpTransport, jsonFactory, null).setApplicationName(applicationName).setHttpRequestInitializer(googleCredential).build();
Instances instances = compute.instances();
for (String id : instanceIdList) {
Get response = instances.get(projectId, availabilityZone, id);
com.google.api.services.compute.model.Instance instance = response.execute();
Tags gcpTags = instance.getTags();
Map<String, String> extractedTags = new HashMap<>();
List<String> tagList = gcpTags.getItems();
for (String i : tagList) {
String[] tmpTagList = i.split("-");
if (tmpTagList.length > 1) {
extractedTags.put(tmpTagList[0], tmpTagList[1]);
}
}
checkTags(tagsToCheckMap, extractedTags);
extractedTags.clear();
}
}
use of com.google.api.client.http.HttpTransport in project cloudbreak by hortonworks.
the class FilesystemUtil method deleteGcsObjects.
private static void deleteGcsObjects(ApplicationContext applicationContext, Map<String, String> cloudProviderParams, String bucketName, String folderPrefix) throws IOException, GeneralSecurityException {
String serviceAccountPrivateKey = ResourceUtil.readBase64EncodedContentFromResource(applicationContext, cloudProviderParams.get("p12File"));
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
PrivateKey privateKey = SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.getPkcs12KeyStore(), new ByteArrayInputStream(Base64.decodeBase64(serviceAccountPrivateKey)), "notasecret", "privatekey", "notasecret");
JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance();
GoogleCredential googleCredential = new GoogleCredential.Builder().setTransport(httpTransport).setJsonFactory(jsonFactory).setServiceAccountId(cloudProviderParams.get("serviceAccountId")).setServiceAccountScopes(Collections.singletonList(STORAGE_SCOPE)).setServiceAccountPrivateKey(privateKey).build();
Storage storage = new Builder(httpTransport, jsonFactory, googleCredential).setApplicationName("Google-BucketsInsertExample/1.0").build();
List listObjects = storage.objects().list(bucketName).setPrefix(folderPrefix);
Objects objects = listObjects.execute();
Assert.assertNotNull(objects.getItems(), "Not found any objects with " + folderPrefix + " prefix.");
Iterable<StorageObject> storageObjects = new ArrayList<>(objects.getItems());
for (StorageObject storageObject : storageObjects) {
storage.objects().delete("hm-bucket", storageObject.getName()).execute();
}
}
Aggregations