in nifi-toolkit/nifi-toolkit-cli/src/main/java/org/apache/nifi/toolkit/cli/impl/client/NiFiRegistryClientFactory.java [53:177]
public NiFiRegistryClient createClient(final Properties properties) throws MissingOptionException {
final String url = properties.getProperty(CommandOption.URL.getLongName());
if (StringUtils.isBlank(url)) {
throw new MissingOptionException("Missing required option '" + CommandOption.URL.getLongName() + "'");
}
final String connectionTimeout = properties.getProperty(CommandOption.CONNECTION_TIMEOUT.getLongName());
final String readTimeout = properties.getProperty(CommandOption.READ_TIMEOUT.getLongName());
final String keystore = properties.getProperty(CommandOption.KEYSTORE.getLongName());
final String keystoreType = properties.getProperty(CommandOption.KEYSTORE_TYPE.getLongName());
final String keystorePasswd = properties.getProperty(CommandOption.KEYSTORE_PASSWORD.getLongName());
final String keyPasswd = properties.getProperty(CommandOption.KEY_PASSWORD.getLongName());
final String truststore = properties.getProperty(CommandOption.TRUSTSTORE.getLongName());
final String truststoreType = properties.getProperty(CommandOption.TRUSTSTORE_TYPE.getLongName());
final String truststorePasswd = properties.getProperty(CommandOption.TRUSTSTORE_PASSWORD.getLongName());
final String proxiedEntity = properties.getProperty(CommandOption.PROXIED_ENTITY.getLongName());
final String protocol = properties.getProperty(CommandOption.PROTOCOL.getLongName());
final String basicAuthUsername = properties.getProperty(CommandOption.BASIC_AUTH_USER.getLongName());
final String basicAuthPassword = properties.getProperty(CommandOption.BASIC_AUTH_PASSWORD.getLongName());
final String bearerToken = properties.getProperty(CommandOption.BEARER_TOKEN.getLongName());
final boolean secureUrl = url.startsWith("https");
if (secureUrl && (StringUtils.isBlank(truststore)
|| StringUtils.isBlank(truststoreType)
|| StringUtils.isBlank(truststorePasswd))
) {
throw new MissingOptionException(CommandOption.TRUSTSTORE.getLongName() + ", " + CommandOption.TRUSTSTORE_TYPE.getLongName()
+ ", and " + CommandOption.TRUSTSTORE_PASSWORD.getLongName() + " are required when using an https url");
}
if (!StringUtils.isBlank(proxiedEntity) && (!StringUtils.isBlank(basicAuthUsername) || !StringUtils.isBlank(basicAuthPassword))) {
throw new IllegalStateException(CommandOption.PROXIED_ENTITY.getLongName() + " and basic authentication can not be used together");
}
if (!StringUtils.isBlank(proxiedEntity) && !StringUtils.isBlank(bearerToken)) {
throw new IllegalStateException(CommandOption.PROXIED_ENTITY.getLongName() + " and "
+ CommandOption.BEARER_TOKEN.getLongName() + " can not be used together");
}
if (!StringUtils.isBlank(bearerToken) && (!StringUtils.isBlank(basicAuthUsername) || !StringUtils.isBlank(basicAuthPassword))) {
throw new IllegalStateException(CommandOption.BEARER_TOKEN.getLongName() + " and basic authentication can not be used together");
}
if (!StringUtils.isBlank(basicAuthUsername) && StringUtils.isBlank(basicAuthPassword)) {
throw new MissingOptionException(CommandOption.BASIC_AUTH_PASSWORD.getLongName()
+ " is required when specifying " + CommandOption.BASIC_AUTH_USER.getLongName());
}
if (!StringUtils.isBlank(basicAuthPassword) && StringUtils.isBlank(basicAuthUsername)) {
throw new MissingOptionException(CommandOption.BASIC_AUTH_USER.getLongName()
+ " is required when specifying " + CommandOption.BASIC_AUTH_PASSWORD.getLongName());
}
final NiFiRegistryClientConfig.Builder clientConfigBuilder = new NiFiRegistryClientConfig.Builder()
.baseUrl(url);
if (secureUrl) {
if (!StringUtils.isBlank(keystore)) {
clientConfigBuilder.keystoreFilename(keystore);
}
if (!StringUtils.isBlank(keystoreType)) {
clientConfigBuilder.keystoreType(KeystoreType.valueOf(keystoreType.toUpperCase()));
}
if (!StringUtils.isBlank(keystorePasswd)) {
clientConfigBuilder.keystorePassword(keystorePasswd);
}
if (!StringUtils.isBlank(keyPasswd)) {
clientConfigBuilder.keyPassword(keyPasswd);
}
if (!StringUtils.isBlank(truststore)) {
clientConfigBuilder.truststoreFilename(truststore);
}
if (!StringUtils.isBlank(truststoreType)) {
clientConfigBuilder.truststoreType(KeystoreType.valueOf(truststoreType.toUpperCase()));
}
if (!StringUtils.isBlank(truststorePasswd)) {
clientConfigBuilder.truststorePassword(truststorePasswd);
}
if (!StringUtils.isBlank(protocol)) {
clientConfigBuilder.protocol(protocol);
}
}
if (!StringUtils.isBlank(connectionTimeout)) {
try {
Integer timeout = Integer.valueOf(connectionTimeout);
clientConfigBuilder.connectTimeout(timeout);
} catch (Exception e) {
throw new MissingOptionException("connectionTimeout has to be an integer");
}
}
if (!StringUtils.isBlank(readTimeout)) {
try {
Integer timeout = Integer.valueOf(readTimeout);
clientConfigBuilder.readTimeout(timeout);
} catch (Exception e) {
throw new MissingOptionException("readTimeout has to be an integer");
}
}
final NiFiRegistryClientConfig clientConfig = clientConfigBuilder.build();
final NiFiRegistryClient client = new JerseyNiFiRegistryClient.Builder().config(clientConfig).build();
// return a wrapped client based which arguments were provided, otherwise return the regular client
if (!StringUtils.isBlank(proxiedEntity)) {
final RequestConfig proxiedEntityConfig = new ProxiedEntityRequestConfig(proxiedEntity);
return new NiFiRegistryClientWithRequestConfig(client, proxiedEntityConfig);
} else if (!StringUtils.isBlank(bearerToken)) {
final RequestConfig bearerTokenConfig = new BearerTokenRequestConfig(bearerToken);
return new NiFiRegistryClientWithRequestConfig(client, bearerTokenConfig);
} else if (!StringUtils.isBlank(basicAuthUsername) && !StringUtils.isBlank(basicAuthPassword)) {
final RequestConfig basicAuthConfig = new BasicAuthRequestConfig(basicAuthUsername, basicAuthPassword);
return new NiFiRegistryClientWithRequestConfig(client, basicAuthConfig);
} else {
return client;
}
}