Search in sources :

Example 1 with ConnectionConfig

use of io.cdap.cdap.client.config.ConnectionConfig in project cdap by caskdata.

the class InstanceURIParser method parseInstanceURI.

public CLIConnectionConfig parseInstanceURI(String uriString, String namespaceString) {
    uriString = addScheme(uriString);
    // Having '/' at the end of the path helps java.net.URI to recognise this as a valid URI path
    if (uriString.length() > 0 && !uriString.endsWith("/")) {
        uriString = String.format("%s/", uriString);
    }
    URI uri = URI.create(uriString);
    NamespaceId namespace = (namespaceString == null || namespaceString.isEmpty()) ? NamespaceId.DEFAULT : new NamespaceId(namespaceString);
    String apiPath = uri.getPath();
    if (apiPath != null && apiPath.startsWith("/")) {
        apiPath = apiPath.substring(1);
    }
    ConnectionConfig config = ConnectionConfig.builder().setHostname(uri.getHost()).setPort(uri.getPort() == -1 ? null : uri.getPort()).setSSLEnabled("https".equals(uri.getScheme())).setApiPath(apiPath).build();
    return new CLIConnectionConfig(config, namespace, null);
}
Also used : CLIConnectionConfig(io.cdap.cdap.cli.CLIConnectionConfig) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) URI(java.net.URI) ConnectionConfig(io.cdap.cdap.client.config.ConnectionConfig) CLIConnectionConfig(io.cdap.cdap.cli.CLIConnectionConfig)

Example 2 with ConnectionConfig

use of io.cdap.cdap.client.config.ConnectionConfig in project cdap by caskdata.

the class CLIMainLinkTest method createCLIConfigWithURIPrefix.

public static CLIConfig createCLIConfigWithURIPrefix(URI standaloneUri) throws Exception {
    ConnectionConfig connectionConfig = InstanceURIParser.DEFAULT.parseInstanceURI(standaloneUri.toString(), null);
    ClientConfig clientConfig = new ClientConfig.Builder().setConnectionConfig(connectionConfig).build();
    clientConfig.setAllTimeouts(60000);
    return new CLIConfig(clientConfig, System.out, new CsvTableRenderer());
}
Also used : CsvTableRenderer(io.cdap.cdap.cli.util.table.CsvTableRenderer) ClientConfig(io.cdap.cdap.client.config.ClientConfig) ConnectionConfig(io.cdap.cdap.client.config.ConnectionConfig)

Example 3 with ConnectionConfig

use of io.cdap.cdap.client.config.ConnectionConfig in project cdap by caskdata.

the class AppFabricTestBase method getClientConfig.

private static ClientConfig getClientConfig(DiscoveryServiceClient discoveryClient, String service) {
    EndpointStrategy endpointStrategy = new RandomEndpointStrategy(() -> discoveryClient.discover(service));
    Discoverable discoverable = endpointStrategy.pick(1, TimeUnit.SECONDS);
    Assert.assertNotNull(discoverable);
    ConnectionConfig connectionConfig = ConnectionConfig.builder().setHostname(discoverable.getSocketAddress().getHostName()).setPort(discoverable.getSocketAddress().getPort()).setSSLEnabled(URIScheme.HTTPS.isMatch(discoverable)).build();
    return ClientConfig.builder().setVerifySSLCert(false).setConnectionConfig(connectionConfig).build();
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy) EndpointStrategy(io.cdap.cdap.common.discovery.EndpointStrategy) ConnectionConfig(io.cdap.cdap.client.config.ConnectionConfig) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy)

Example 4 with ConnectionConfig

use of io.cdap.cdap.client.config.ConnectionConfig in project cdap by caskdata.

the class UsageHandlerTestRun method doGet.

private <T> T doGet(String path, Type responseType) throws IOException {
    ConnectionConfig connectionConfig = getClientConfig().getConnectionConfig();
    URL url = new URL(String.format("%s://%s:%d%s", connectionConfig.isSSLEnabled() ? "https" : "http", connectionConfig.getHostname(), connectionConfig.getPort(), path));
    HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
    try {
        Assert.assertEquals(200, urlConn.getResponseCode());
        try (Reader reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), Charsets.UTF_8))) {
            return GSON.fromJson(reader, responseType);
        }
    } finally {
        urlConn.disconnect();
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) ConnectionConfig(io.cdap.cdap.client.config.ConnectionConfig) URL(java.net.URL)

Example 5 with ConnectionConfig

use of io.cdap.cdap.client.config.ConnectionConfig in project cdap by caskdata.

the class UpgradeTool method getClientConfig.

private static ClientConfig getClientConfig(CommandLine commandLine) throws IOException {
    String uriStr = commandLine.hasOption("u") ? commandLine.getOptionValue("u") : "localhost:11015";
    if (!uriStr.contains("://")) {
        uriStr = "http://" + uriStr;
    }
    URI uri = URI.create(uriStr);
    String hostname = uri.getHost();
    int port = uri.getPort();
    boolean sslEnabled = "https".equals(uri.getScheme());
    ConnectionConfig connectionConfig = ConnectionConfig.builder().setHostname(hostname).setPort(port).setSSLEnabled(sslEnabled).build();
    int readTimeout = commandLine.hasOption("t") ? Integer.parseInt(commandLine.getOptionValue("t")) : DEFAULT_READ_TIMEOUT_MILLIS;
    ClientConfig.Builder clientConfigBuilder = ClientConfig.builder().setDefaultReadTimeout(readTimeout).setConnectionConfig(connectionConfig);
    if (commandLine.hasOption("a")) {
        String tokenFilePath = commandLine.getOptionValue("a");
        File tokenFile = new File(tokenFilePath);
        if (!tokenFile.exists()) {
            throw new IllegalArgumentException("Access token file " + tokenFilePath + " does not exist.");
        }
        if (!tokenFile.isFile()) {
            throw new IllegalArgumentException("Access token file " + tokenFilePath + " is not a file.");
        }
        String tokenValue = new String(Files.readAllBytes(tokenFile.toPath()), StandardCharsets.UTF_8).trim();
        AccessToken accessToken = new AccessToken(tokenValue, 82000L, "Bearer");
        clientConfigBuilder.setAccessToken(accessToken);
    }
    return clientConfigBuilder.build();
}
Also used : AccessToken(io.cdap.cdap.security.authentication.client.AccessToken) ClientConfig(io.cdap.cdap.client.config.ClientConfig) URI(java.net.URI) File(java.io.File) ConnectionConfig(io.cdap.cdap.client.config.ConnectionConfig)

Aggregations

ConnectionConfig (io.cdap.cdap.client.config.ConnectionConfig)18 ClientConfig (io.cdap.cdap.client.config.ClientConfig)7 RandomEndpointStrategy (io.cdap.cdap.common.discovery.RandomEndpointStrategy)5 Discoverable (org.apache.twill.discovery.Discoverable)5 IOException (java.io.IOException)4 NotFoundException (io.cdap.cdap.common.NotFoundException)3 EndpointStrategy (io.cdap.cdap.common.discovery.EndpointStrategy)3 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)3 InetSocketAddress (java.net.InetSocketAddress)3 DiscoveryServiceClient (org.apache.twill.discovery.DiscoveryServiceClient)3 Injector (com.google.inject.Injector)2 DatasetProperties (io.cdap.cdap.api.dataset.DatasetProperties)2 CLIConnectionConfig (io.cdap.cdap.cli.CLIConnectionConfig)2 CsvTableRenderer (io.cdap.cdap.cli.util.table.CsvTableRenderer)2 CConfiguration (io.cdap.cdap.common.conf.CConfiguration)2 URIScheme (io.cdap.cdap.common.discovery.URIScheme)2 RetryStrategy (io.cdap.cdap.common.service.RetryStrategy)2 AccessToken (io.cdap.cdap.security.authentication.client.AccessToken)2 URI (java.net.URI)2 Properties (java.util.Properties)2