use of org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClient in project nifi by apache.
the class CLIMain method createContext.
private static Context createContext(final PrintStream output, final boolean isInteractive) {
Session session;
final String userHomeValue = System.getProperty("user.home");
final File userHome = Paths.get(userHomeValue).toFile();
if (!userHome.exists() || !userHome.canRead() || !userHome.canWrite()) {
session = new InMemorySession();
if (isInteractive) {
output.println();
output.println("Unable to create session from " + userHomeValue + ", falling back to in-memory session");
output.println();
}
} else {
final InMemorySession inMemorySession = new InMemorySession();
final File sessionState = new File(userHome.getAbsolutePath(), SESSION_PERSISTENCE_FILE);
try {
if (!sessionState.exists()) {
sessionState.createNewFile();
}
final PersistentSession persistentSession = new PersistentSession(sessionState, inMemorySession);
persistentSession.loadSession();
session = persistentSession;
if (isInteractive) {
output.println();
output.println("Session loaded from " + sessionState.getAbsolutePath());
output.println();
}
} catch (Exception e) {
session = inMemorySession;
if (isInteractive) {
output.println();
output.println("Unable to load session from " + sessionState.getAbsolutePath() + ", falling back to in-memory session");
output.println();
}
}
}
final ClientFactory<NiFiClient> niFiClientFactory = new NiFiClientFactory();
final ClientFactory<NiFiRegistryClient> nifiRegClientFactory = new NiFiRegistryClientFactory();
return new StandardContext.Builder().output(output).session(session).nifiClientFactory(niFiClientFactory).nifiRegistryClientFactory(nifiRegClientFactory).interactive(isInteractive).build();
}
use of org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClient in project nifi by apache.
the class NiFiClientFactory method createClient.
@Override
public NiFiClient 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 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 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");
}
final NiFiClientConfig.Builder clientConfigBuilder = new NiFiClientConfig.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);
}
}
final NiFiClient client = new JerseyNiFiClient.Builder().config(clientConfigBuilder.build()).build();
// if a proxied entity was specified then return a wrapped client, otherwise return the regular client
if (!StringUtils.isBlank(proxiedEntity)) {
return new NiFiClientFactory.ProxiedNiFiClient(client, proxiedEntity);
} else {
return client;
}
}
use of org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClient in project nifi by apache.
the class AbstractCompositeCommand method execute.
@Override
public final R execute(final CommandLine cli) throws CommandException {
try {
final Properties nifiProperties = createProperties(cli, CommandOption.NIFI_PROPS, SessionVariable.NIFI_CLIENT_PROPS);
if (nifiProperties == null) {
throw new CommandException("Unable to find NiFi config, must specify --" + CommandOption.NIFI_PROPS.getLongName() + ", or setup session config");
}
final ClientFactory<NiFiClient> nifiClientFactory = getContext().getNiFiClientFactory();
final NiFiClient nifiClient = nifiClientFactory.createClient(nifiProperties);
final Properties registryProperties = createProperties(cli, CommandOption.NIFI_REG_PROPS, SessionVariable.NIFI_REGISTRY_CLIENT_PROPS);
if (registryProperties == null) {
throw new CommandException("Unable to find NiFi Registry config, must specify --" + CommandOption.NIFI_REG_PROPS.getLongName() + ", or setup session config");
}
final ClientFactory<NiFiRegistryClient> registryClientFactory = getContext().getNiFiRegistryClientFactory();
final NiFiRegistryClient registryClient = registryClientFactory.createClient(registryProperties);
return doExecute(cli, nifiClient, nifiProperties, registryClient, registryProperties);
} catch (CommandException ce) {
throw ce;
} catch (Exception e) {
throw new CommandException("Error executing command '" + getName() + "' : " + e.getMessage(), e);
}
}
use of org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClient in project nifi by apache.
the class PGList method doExecute.
@Override
public ProcessGroupsResult doExecute(final NiFiClient client, final Properties properties) throws NiFiClientException, IOException {
final FlowClient flowClient = client.getFlowClient();
// get the optional id of the parent PG, otherwise fallback to the root group
String parentPgId = getArg(properties, CommandOption.PG_ID);
if (StringUtils.isBlank(parentPgId)) {
parentPgId = flowClient.getRootGroupId();
}
final ProcessGroupFlowEntity processGroupFlowEntity = flowClient.getProcessGroup(parentPgId);
final ProcessGroupFlowDTO processGroupFlowDTO = processGroupFlowEntity.getProcessGroupFlow();
final FlowDTO flowDTO = processGroupFlowDTO.getFlow();
final List<ProcessGroupDTO> processGroups = new ArrayList<>();
if (flowDTO.getProcessGroups() != null) {
flowDTO.getProcessGroups().stream().map(pge -> pge.getComponent()).forEach(dto -> processGroups.add(dto));
}
return new ProcessGroupsResult(getResultType(properties), processGroups);
}
use of org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClient in project nifi by apache.
the class NiFiCLIMainRunner method main.
public static void main(String[] args) {
final String[] cmdArgs = ("registry list-buckets help " + "").split("[ ]");
final Session session = new InMemorySession();
final ClientFactory<NiFiClient> niFiClientFactory = new NiFiClientFactory();
final ClientFactory<NiFiRegistryClient> nifiRegClientFactory = new NiFiRegistryClientFactory();
final Context context = new StandardContext.Builder().output(System.out).session(session).nifiClientFactory(niFiClientFactory).nifiRegistryClientFactory(nifiRegClientFactory).build();
final Map<String, Command> commands = CommandFactory.createTopLevelCommands(context);
final Map<String, CommandGroup> commandGroups = CommandFactory.createCommandGroups(context);
final CommandProcessor processor = new CommandProcessor(commands, commandGroups, context);
processor.process(cmdArgs);
}
Aggregations