Search in sources :

Example 1 with Builder

use of com.google.api.services.clouddebugger.v2.Clouddebugger.Builder 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;
}
Also used : Debugger(com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger) HttpRequest(com.google.api.client.http.HttpRequest) HttpHeaders(com.google.api.client.http.HttpHeaders) Credential(com.google.api.client.auth.oauth2.Credential) Builder(com.google.api.services.clouddebugger.v2.Clouddebugger.Builder) GeneralSecurityException(java.security.GeneralSecurityException) PluginInfoService(com.google.cloud.tools.intellij.service.PluginInfoService) IOException(java.io.IOException) HttpTransport(com.google.api.client.http.HttpTransport) GoogleNetHttpTransport(com.google.api.client.googleapis.javanet.GoogleNetHttpTransport) LoginListener(com.google.gdt.eclipse.login.common.LoginListener) CredentialedUser(com.google.cloud.tools.intellij.login.CredentialedUser) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Credential (com.google.api.client.auth.oauth2.Credential)1 GoogleNetHttpTransport (com.google.api.client.googleapis.javanet.GoogleNetHttpTransport)1 HttpHeaders (com.google.api.client.http.HttpHeaders)1 HttpRequest (com.google.api.client.http.HttpRequest)1 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)1 HttpTransport (com.google.api.client.http.HttpTransport)1 Builder (com.google.api.services.clouddebugger.v2.Clouddebugger.Builder)1 Debugger (com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger)1 CredentialedUser (com.google.cloud.tools.intellij.login.CredentialedUser)1 PluginInfoService (com.google.cloud.tools.intellij.service.PluginInfoService)1 LoginListener (com.google.gdt.eclipse.login.common.LoginListener)1 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 Nullable (org.jetbrains.annotations.Nullable)1