public void setNamespace(Id.Namespace namespace) { ConnectionConfig connectionConfig = ConnectionConfig.builder(clientConfig.getConnectionConfig()) .setNamespace(namespace) .build(); this.setConnectionConfig(connectionConfig); }
public void tryConnect(ConnectionConfig connectionConfig, PrintStream output, boolean debug) throws Exception { try { AccessToken accessToken = acquireAccessToken(clientConfig, connectionConfig, output, debug); checkConnection(clientConfig, connectionConfig, accessToken); setConnectionConfig(connectionConfig); output.printf( "Successfully connected CDAP instance at %s", connectionConfig.getURI().toString()); output.println(); } catch (IOException e) { throw new IOException( String.format( "CDAP instance at '%s' could not be reached: %s", connectionConfig.getURI().toString(), e.getMessage()), e); } }
private AccessToken getNewAccessToken( ConnectionConfig connectionInfo, PrintStream output, boolean debug) throws IOException { AuthenticationClient authenticationClient = getAuthenticationClient(connectionInfo); Properties properties = new Properties(); properties.put( BasicAuthenticationClient.VERIFY_SSL_CERT_PROP_NAME, String.valueOf(clientConfig.isVerifySSLCert())); // obtain new access token via manual user input output.printf( "Authentication is enabled in the CDAP instance: %s.\n", connectionInfo.getHostname()); ConsoleReader reader = new ConsoleReader(); for (Credential credential : authenticationClient.getRequiredCredentials()) { String prompt = "Please, specify " + credential.getDescription() + "> "; String credentialValue; if (credential.isSecret()) { credentialValue = reader.readLine(prompt, '*'); } else { credentialValue = reader.readLine(prompt); } properties.put(credential.getName(), credentialValue); } authenticationClient.configure(properties); AccessToken accessToken = authenticationClient.getAccessToken(); if (accessToken != null) { if (saveAccessToken(accessToken, connectionInfo.getHostname()) && debug) { output.printf( "Saved access token to %s\n", getAccessTokenFile(connectionInfo.getHostname()).getAbsolutePath()); } } return accessToken; }
private AccessToken acquireAccessToken( ClientConfig clientConfig, ConnectionConfig connectionInfo, PrintStream output, boolean debug) throws IOException { if (!isAuthenticationEnabled(connectionInfo)) { return null; } try { AccessToken savedAccessToken = getSavedAccessToken(connectionInfo.getHostname()); checkConnection(clientConfig, connectionInfo, savedAccessToken); return savedAccessToken; } catch (UnauthorizedException ignored) { // access token invalid - fall through to try acquiring token manually } return getNewAccessToken(connectionInfo, output, debug); }
private AuthenticationClient getAuthenticationClient(ConnectionConfig connectionInfo) { AuthenticationClient authenticationClient = new BasicAuthenticationClient(); authenticationClient.setConnectionInfo( connectionInfo.getHostname(), connectionInfo.getPort(), connectionInfo.isSSLEnabled()); return authenticationClient; }