Search in sources :

Example 1 with CommandException

use of org.apache.nifi.toolkit.cli.api.CommandException in project nifi by apache.

the class AbstractPropertyCommand method execute.

@Override
public final R execute(final CommandLine commandLine) throws CommandException {
    try {
        final Properties properties = new Properties();
        // start by loading the properties file if it was specified
        if (commandLine.hasOption(CommandOption.PROPERTIES.getLongName())) {
            final String propertiesFile = commandLine.getOptionValue(CommandOption.PROPERTIES.getLongName());
            if (!StringUtils.isBlank(propertiesFile)) {
                try (final InputStream in = new FileInputStream(propertiesFile)) {
                    properties.load(in);
                }
            }
        } else {
            // no properties file was specified so see if there is anything in the session
            final SessionVariable sessionVariable = getPropertiesSessionVariable();
            if (sessionVariable != null) {
                final Session session = getContext().getSession();
                final String sessionPropsFiles = session.get(sessionVariable.getVariableName());
                if (!StringUtils.isBlank(sessionPropsFiles)) {
                    try (final InputStream in = new FileInputStream(sessionPropsFiles)) {
                        properties.load(in);
                    }
                }
            }
        }
        // add in anything specified on command line, and override anything that was already there
        for (final Option option : commandLine.getOptions()) {
            final String optValue = option.getValue() == null ? "" : option.getValue();
            properties.setProperty(option.getLongOpt(), optValue);
        }
        // delegate to sub-classes
        return doExecute(properties);
    } catch (CommandException ce) {
        throw ce;
    } catch (Exception e) {
        throw new CommandException("Error executing command '" + getName() + "' : " + e.getMessage(), e);
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Option(org.apache.commons.cli.Option) SessionVariable(org.apache.nifi.toolkit.cli.impl.session.SessionVariable) CommandException(org.apache.nifi.toolkit.cli.api.CommandException) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) CommandException(org.apache.nifi.toolkit.cli.api.CommandException) Session(org.apache.nifi.toolkit.cli.api.Session)

Example 2 with CommandException

use of org.apache.nifi.toolkit.cli.api.CommandException 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);
    }
}
Also used : NiFiClient(org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClient) NiFiRegistryClient(org.apache.nifi.registry.client.NiFiRegistryClient) CommandException(org.apache.nifi.toolkit.cli.api.CommandException) Properties(java.util.Properties) NiFiClientException(org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClientException) IOException(java.io.IOException) CommandException(org.apache.nifi.toolkit.cli.api.CommandException) NiFiRegistryException(org.apache.nifi.registry.client.NiFiRegistryException) ParseException(org.apache.commons.cli.ParseException) SessionException(org.apache.nifi.toolkit.cli.api.SessionException)

Example 3 with CommandException

use of org.apache.nifi.toolkit.cli.api.CommandException in project nifi by apache.

the class GetVariable method execute.

@Override
public StringResult execute(final CommandLine commandLine) throws CommandException {
    final String[] args = commandLine.getArgs();
    if (args == null || args.length != 1 || StringUtils.isBlank(args[0])) {
        throw new CommandException("Incorrect number of arguments, should be: <var>");
    }
    final Session session = getContext().getSession();
    try {
        final String value = session.get(args[0]);
        if (value == null) {
            return new StringResult("", getContext().isInteractive());
        } else {
            return new StringResult(value, getContext().isInteractive());
        }
    } catch (SessionException se) {
        throw new CommandException(se.getMessage(), se);
    }
}
Also used : SessionException(org.apache.nifi.toolkit.cli.api.SessionException) CommandException(org.apache.nifi.toolkit.cli.api.CommandException) StringResult(org.apache.nifi.toolkit.cli.impl.result.StringResult) Session(org.apache.nifi.toolkit.cli.api.Session)

Example 4 with CommandException

use of org.apache.nifi.toolkit.cli.api.CommandException in project nifi by apache.

the class ShowSession method execute.

@Override
public VoidResult execute(final CommandLine cli) throws CommandException {
    try {
        final Session session = getContext().getSession();
        final PrintStream printStream = getContext().getOutput();
        session.printVariables(printStream);
        return VoidResult.getInstance();
    } catch (SessionException se) {
        throw new CommandException(se.getMessage(), se);
    }
}
Also used : PrintStream(java.io.PrintStream) SessionException(org.apache.nifi.toolkit.cli.api.SessionException) CommandException(org.apache.nifi.toolkit.cli.api.CommandException) Session(org.apache.nifi.toolkit.cli.api.Session)

Example 5 with CommandException

use of org.apache.nifi.toolkit.cli.api.CommandException in project nifi by apache.

the class GetRegistryClientId method doExecute.

@Override
public RegistryClientIDResult doExecute(final NiFiClient client, final Properties properties) throws NiFiClientException, IOException, CommandException {
    final String regClientName = getArg(properties, CommandOption.REGISTRY_CLIENT_NAME);
    final String regClientUrl = getArg(properties, CommandOption.REGISTRY_CLIENT_URL);
    if (!StringUtils.isBlank(regClientName) && !StringUtils.isBlank(regClientUrl)) {
        throw new CommandException("Name and URL cannot be specified at the same time");
    }
    if (StringUtils.isBlank(regClientName) && StringUtils.isBlank(regClientUrl)) {
        throw new CommandException("Name or URL must be specified");
    }
    final RegistryClientsEntity registries = client.getControllerClient().getRegistryClients();
    RegistryDTO registry;
    if (!StringUtils.isBlank(regClientName)) {
        registry = registries.getRegistries().stream().map(r -> r.getComponent()).filter(r -> r.getName().equalsIgnoreCase(regClientName.trim())).findFirst().orElse(null);
    } else {
        registry = registries.getRegistries().stream().map(r -> r.getComponent()).filter(r -> r.getUri().equalsIgnoreCase(regClientUrl.trim())).findFirst().orElse(null);
    }
    if (registry == null) {
        throw new NiFiClientException("No registry client exists with the name '" + regClientName + "'");
    } else {
        return new RegistryClientIDResult(getResultType(properties), registry);
    }
}
Also used : NiFiClient(org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClient) Properties(java.util.Properties) RegistryClientsEntity(org.apache.nifi.web.api.entity.RegistryClientsEntity) NiFiClientException(org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClientException) RegistryClientIDResult(org.apache.nifi.toolkit.cli.impl.result.RegistryClientIDResult) CommandOption(org.apache.nifi.toolkit.cli.impl.command.CommandOption) AbstractNiFiCommand(org.apache.nifi.toolkit.cli.impl.command.nifi.AbstractNiFiCommand) IOException(java.io.IOException) CommandException(org.apache.nifi.toolkit.cli.api.CommandException) StringUtils(org.apache.commons.lang3.StringUtils) Context(org.apache.nifi.toolkit.cli.api.Context) RegistryDTO(org.apache.nifi.web.api.dto.RegistryDTO) NiFiClientException(org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClientException) RegistryClientsEntity(org.apache.nifi.web.api.entity.RegistryClientsEntity) RegistryClientIDResult(org.apache.nifi.toolkit.cli.impl.result.RegistryClientIDResult) CommandException(org.apache.nifi.toolkit.cli.api.CommandException) RegistryDTO(org.apache.nifi.web.api.dto.RegistryDTO)

Aggregations

CommandException (org.apache.nifi.toolkit.cli.api.CommandException)7 Properties (java.util.Properties)3 Session (org.apache.nifi.toolkit.cli.api.Session)3 SessionException (org.apache.nifi.toolkit.cli.api.SessionException)3 IOException (java.io.IOException)2 ParseException (org.apache.commons.cli.ParseException)2 NiFiClient (org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClient)2 NiFiClientException (org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClientException)2 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 PrintStream (java.io.PrintStream)1 CommandLine (org.apache.commons.cli.CommandLine)1 Option (org.apache.commons.cli.Option)1 StringUtils (org.apache.commons.lang3.StringUtils)1 NiFiRegistryClient (org.apache.nifi.registry.client.NiFiRegistryClient)1 NiFiRegistryException (org.apache.nifi.registry.client.NiFiRegistryException)1 Context (org.apache.nifi.toolkit.cli.api.Context)1 ReferenceResolver (org.apache.nifi.toolkit.cli.api.ReferenceResolver)1 Referenceable (org.apache.nifi.toolkit.cli.api.Referenceable)1 Result (org.apache.nifi.toolkit.cli.api.Result)1