use of com.google.api.client.http.HttpTransport in project google-cloud-intellij by GoogleCloudPlatform.
the class CloudDebuggerClient method getClient.
/**
* Returns a cloud debugger connection given a user email to indicate the credentials to use. The
* function may return null if the user is not logged in.
*/
@Nullable
private static Debugger getClient(@Nullable final String userEmail, final int timeout) {
if (Strings.isNullOrEmpty(userEmail)) {
LOG.warn("unexpected null email in controller initialize.");
return null;
}
final String hashkey = userEmail + timeout;
Debugger cloudDebuggerClient = debuggerClientsFromUserEmail.get(hashkey);
if (cloudDebuggerClient == null) {
try {
final CredentialedUser user = Services.getLoginService().getAllUsers().get(userEmail);
final Credential credential = (user != null ? user.getCredential() : null);
if (credential != null) {
user.getGoogleLoginState().addLoginListener(new LoginListener() {
@Override
public void statusChanged(boolean login) {
if (!login) {
// aggressively remove the cached item on any status change.
debuggerClientsFromUserEmail.remove(hashkey);
} else {
// NOPMD
// user logged in, should we do something?
}
}
});
HttpRequestInitializer initializer = new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest httpRequest) throws IOException {
HttpHeaders headers = new HttpHeaders();
httpRequest.setConnectTimeout(timeout);
httpRequest.setReadTimeout(timeout);
httpRequest.setHeaders(headers);
credential.initialize(httpRequest);
}
};
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
String userAgent = ServiceManager.getService(PluginInfoService.class).getUserAgent();
cloudDebuggerClient = new Builder(httpTransport, JSON_FACTORY, initializer).setRootUrl(ROOT_URL).setApplicationName(userAgent).build().debugger();
}
} catch (IOException ex) {
LOG.warn("Error connecting to Cloud Debugger API", ex);
} catch (GeneralSecurityException ex) {
LOG.warn("Error connecting to Cloud Debugger API", ex);
}
if (cloudDebuggerClient != null) {
debuggerClientsFromUserEmail.put(hashkey, cloudDebuggerClient);
}
}
return cloudDebuggerClient;
}
use of com.google.api.client.http.HttpTransport in project google-api-java-client by google.
the class AppIdentityCredentialTest method testAppEngineCredentialWrapperGetAccessToken.
public void testAppEngineCredentialWrapperGetAccessToken() throws IOException {
final String expectedAccessToken = "ExpectedAccessToken";
HttpTransport transport = new MockHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
MockAppIdentityService appIdentity = new MockAppIdentityService();
appIdentity.setAccessTokenText(expectedAccessToken);
AppIdentityCredential.Builder builder = new AppIdentityCredential.Builder(SCOPES);
builder.setAppIdentityService(appIdentity);
AppIdentityCredential appCredential = builder.build();
GoogleCredential wrapper = new AppIdentityCredential.AppEngineCredentialWrapper(appCredential, transport, jsonFactory);
assertTrue(wrapper.refreshToken());
assertEquals(expectedAccessToken, wrapper.getAccessToken());
}
use of com.google.api.client.http.HttpTransport in project google-api-java-client by google.
the class AppIdentityCredentialTest method testAppEngineCredentialWrapperNullJsonFactoryThrows.
public void testAppEngineCredentialWrapperNullJsonFactoryThrows() throws IOException {
HttpTransport transport = new MockHttpTransport();
try {
new AppIdentityCredential.AppEngineCredentialWrapper(transport, null);
fail();
} catch (NullPointerException expected) {
}
}
use of com.google.api.client.http.HttpTransport in project google-api-java-client by google.
the class AppIdentityCredentialTest method testAppEngineCredentialWrapper.
public void testAppEngineCredentialWrapper() throws IOException {
final String expectedAccessToken = "ExpectedAccessToken";
final Collection<String> emptyScopes = Collections.emptyList();
HttpTransport transport = new MockHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
MockAppIdentityService appIdentity = new MockAppIdentityService();
appIdentity.setAccessTokenText(expectedAccessToken);
AppIdentityCredential.Builder builder = new AppIdentityCredential.Builder(emptyScopes);
builder.setAppIdentityService(appIdentity);
AppIdentityCredential appCredential = builder.build();
GoogleCredential wrapper = new AppIdentityCredential.AppEngineCredentialWrapper(appCredential, transport, jsonFactory);
HttpRequest request = transport.createRequestFactory().buildRequest("get", null, null);
assertTrue(wrapper.createScopedRequired());
try {
wrapper.intercept(request);
fail("Should not be able to use credential without scopes.");
} catch (Exception expected) {
}
assertEquals(1, appIdentity.getGetAccessTokenCallCount());
GoogleCredential scopedWrapper = wrapper.createScoped(SCOPES);
assertNotSame(wrapper, scopedWrapper);
scopedWrapper.intercept(request);
assertEquals(2, appIdentity.getGetAccessTokenCallCount());
HttpHeaders headers = request.getHeaders();
String authHeader = headers.getAuthorization();
assertTrue(authHeader.contains(expectedAccessToken));
}
use of com.google.api.client.http.HttpTransport in project google-api-java-client by google.
the class DefaultCredentialProviderTest method testGetDefaultCredentials_cloudshell.
public void testGetDefaultCredentials_cloudshell() throws IOException {
HttpTransport transport = new MockHttpTransport();
TestDefaultCredentialProvider testProvider = new TestDefaultCredentialProvider();
testProvider.setEnv(DefaultCredentialProvider.CLOUD_SHELL_ENV_VAR, "4");
GoogleCredential defaultCredential = testProvider.getDefaultCredential(transport, JSON_FACTORY);
assertTrue(defaultCredential instanceof CloudShellCredential);
assertEquals(((CloudShellCredential) defaultCredential).getAuthPort(), 4);
}
Aggregations