Java Code Examples for org.apache.nifi.util.StringUtils#isBlank()

The following examples show how to use org.apache.nifi.util.StringUtils#isBlank() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PutElasticsearchRecord.java    From nifi with Apache License 2.0 6 votes vote down vote up
private void removeEmpty(Map<String, Object> input) {
    Map<String, Object> copy = new HashMap<>(input);

    for (Map.Entry<String, Object> entry : input.entrySet()) {
        if (entry.getValue() == null) {
           copy.remove(entry.getKey());
        } else {
            if (StringUtils.isBlank(entry.getValue().toString())) {
                copy.remove(entry.getKey());
            } else if (entry.getValue() instanceof Map) {
                removeEmpty((Map<String, Object>) entry.getValue());
            } else if (entry.getValue() instanceof List) {
                for (Object value : (List)entry.getValue()) {
                    if (value instanceof Map) {
                        removeEmpty((Map<String, Object>) value);
                    }
                }
            }
        }
    }

    input.clear();
    input.putAll(copy);
}
 
Example 2
Source File: AbstractKiteProcessor.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
public ValidationResult validate(String subject, String uri, ValidationContext context) {
    Configuration conf = getConfiguration(context.getProperty(CONF_XML_FILES).evaluateAttributeExpressions().getValue());
    String error = null;

    if(StringUtils.isBlank(uri)) {
        return new ValidationResult.Builder().subject(subject).input(uri).explanation("Schema cannot be null.").valid(false).build();
    }

    final boolean elPresent = context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(uri);
    if (!elPresent) {
        try {
            getSchema(uri, conf);
        } catch (SchemaNotFoundException e) {
            error = e.getMessage();
        }
    }
    return new ValidationResult.Builder()
            .subject(subject)
            .input(uri)
            .explanation(error)
            .valid(error == null)
            .build();
}
 
Example 3
Source File: AbstractBigQueryProcessor.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
protected BigQueryOptions getServiceOptions(ProcessContext context, GoogleCredentials credentials) {
    final String projectId = context.getProperty(PROJECT_ID).evaluateAttributeExpressions().getValue();
    final Integer retryCount = Integer.valueOf(context.getProperty(RETRY_COUNT).getValue());

    final BigQueryOptions.Builder builder = BigQueryOptions.newBuilder();

    if (!StringUtils.isBlank(projectId)) {
        builder.setProjectId(projectId);
    }

    return builder.setCredentials(credentials)
            .setRetrySettings(RetrySettings.newBuilder().setMaxAttempts(retryCount).build())
            .setTransportOptions(getTransportOptions(context))
            .build();
}
 
Example 4
Source File: SystemBundle.java    From nifi-minifi with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a bundle representing the system class loader.
 *
 * @param niFiProperties a NiFiProperties instance which will be used to obtain the default NAR library path,
 *                       which will become the working directory of the returned bundle
 * @return a bundle for the system class loader
 */
public static Bundle create(final NiFiProperties niFiProperties) {
    final ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();

    final String narLibraryDirectory = niFiProperties.getProperty(NiFiProperties.NAR_LIBRARY_DIRECTORY);
    if (StringUtils.isBlank(narLibraryDirectory)) {
        throw new IllegalStateException("Unable to create system bundle because " + NiFiProperties.NAR_LIBRARY_DIRECTORY + " was null or empty");
    }

    final BundleDetails systemBundleDetails = new BundleDetails.Builder()
            .workingDir(new File(narLibraryDirectory))
            .coordinate(SYSTEM_BUNDLE_COORDINATE)
            .build();

    return new Bundle(systemBundleDetails, systemClassLoader);
}
 
Example 5
Source File: KerberosPrincipalParser.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * <p>Determines the realm specified in the given kerberos principal.
 *
 * <p>The content of the given {@code principal} after the occurrence
 * of the last non-escaped realm delimiter ("@") will be considered
 * the realm of the principal.
 *
 * <p>The validity of the given {@code principal} and the determined realm
 * is not be verified by this method.
 *
 * @param principal the principal for which the realm will be determined
 * @return the realm of the given principal
 */
public static String getRealm(String principal) {
    if (StringUtils.isBlank(principal)) {
        throw new IllegalArgumentException("principal can not be null or empty");
    }

    char previousChar = 0;
    int realmDelimiterIndex = -1;
    char currentChar;
    boolean realmDelimiterFound = false;
    int principalLength = principal.length();

    // find the last non-escaped occurrence of the realm delimiter
    for (int i = 0; i < principalLength; ++i) {
        currentChar = principal.charAt(i);
        if (currentChar == '@' && previousChar != '\\' ) {
            realmDelimiterIndex = i;
            realmDelimiterFound = true;
        }
        previousChar = currentChar;
    }

    String principalAfterLastRealmDelimiter = principal.substring(realmDelimiterIndex + 1);
    return realmDelimiterFound && realmDelimiterIndex + 1 < principalLength ? principalAfterLastRealmDelimiter : null;
}
 
Example 6
Source File: CompositeConfigurableUserGroupProvider.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void onConfigured(AuthorizerConfigurationContext configurationContext) throws AuthorizerCreationException {
    final PropertyValue configurableUserGroupProviderKey = configurationContext.getProperty(PROP_CONFIGURABLE_USER_GROUP_PROVIDER);
    if (!configurableUserGroupProviderKey.isSet()) {
        throw new AuthorizerCreationException("The Configurable User Group Provider must be set.");
    }

    final UserGroupProvider userGroupProvider = userGroupProviderLookup.getUserGroupProvider(configurableUserGroupProviderKey.getValue());

    if (userGroupProvider == null) {
        throw new AuthorizerCreationException(String.format("Unable to locate the Configurable User Group Provider: %s", configurableUserGroupProviderKey));
    }

    if (!(userGroupProvider instanceof ConfigurableUserGroupProvider)) {
        throw new AuthorizerCreationException(String.format("The Configurable User Group Provider is not configurable: %s", configurableUserGroupProviderKey));
    }

    // Ensure that the ConfigurableUserGroupProvider is not also listed as one of the providers for the CompositeUserGroupProvider
    for (Map.Entry<String, String> entry : configurationContext.getProperties().entrySet()) {
        Matcher matcher = USER_GROUP_PROVIDER_PATTERN.matcher(entry.getKey());
        if (matcher.matches() && !StringUtils.isBlank(entry.getValue())) {
            final String userGroupProviderKey = entry.getValue();
            if (userGroupProviderKey.equals(configurableUserGroupProviderKey.getValue())) {
                throw new AuthorizerCreationException(String.format("Duplicate provider in Composite Configurable User Group Provider configuration: %s", userGroupProviderKey));
            }
        }
    }

    configurableUserGroupProvider = (ConfigurableUserGroupProvider) userGroupProvider;

    // configure the CompositeUserGroupProvider
    super.onConfigured(configurationContext);
}
 
Example 7
Source File: PutHBaseCell.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
protected PutFlowFile createPut(final ProcessSession session, final ProcessContext context, final FlowFile flowFile) {
    final String tableName = context.getProperty(TABLE_NAME).evaluateAttributeExpressions(flowFile).getValue();
    final String row = context.getProperty(ROW_ID).evaluateAttributeExpressions(flowFile).getValue();
    final String columnFamily = context.getProperty(COLUMN_FAMILY).evaluateAttributeExpressions(flowFile).getValue();
    final String columnQualifier = context.getProperty(COLUMN_QUALIFIER).evaluateAttributeExpressions(flowFile).getValue();
    final String timestampValue = context.getProperty(TIMESTAMP).evaluateAttributeExpressions(flowFile).getValue();

    final String visibilityStringToUse = pickVisibilityString(columnFamily, columnQualifier, flowFile, context);

    final Long timestamp;
    if (!StringUtils.isBlank(timestampValue)) {
        try {
            timestamp = Long.valueOf(timestampValue);
        } catch (Exception e) {
            getLogger().error("Invalid timestamp value: " + timestampValue, e);
            return null;
        }
    } else {
        timestamp = null;
    }


    final byte[] buffer = new byte[(int) flowFile.getSize()];
    session.read(flowFile, in -> StreamUtils.fillBuffer(in, buffer));

    PutColumn column = StringUtils.isEmpty(visibilityStringToUse)
            ? new PutColumn(columnFamily.getBytes(StandardCharsets.UTF_8),
            columnQualifier.getBytes(StandardCharsets.UTF_8), buffer, timestamp)
            : new PutColumn(columnFamily.getBytes(StandardCharsets.UTF_8),
            columnQualifier.getBytes(StandardCharsets.UTF_8), buffer, timestamp, visibilityStringToUse);

    final Collection<PutColumn> columns = Collections.singletonList(column);
    byte[] rowKeyBytes = getRow(row,context.getProperty(ROW_ID_ENCODING_STRATEGY).getValue());


    return new PutFlowFile(tableName,rowKeyBytes , columns, flowFile);
}
 
Example 8
Source File: SystemBundle.java    From nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a bundle representing the system class loader.
 *
 * @param niFiProperties a NiFiProperties instance which will be used to obtain the default NAR library path,
 *                       which will become the working directory of the returned bundle
 * @return a bundle for the system class loader
 */
public static Bundle create(final NiFiProperties niFiProperties, final ClassLoader systemClassLoader) {
    final String narLibraryDirectory = niFiProperties.getProperty(NiFiProperties.NAR_LIBRARY_DIRECTORY);
    if (StringUtils.isBlank(narLibraryDirectory)) {
        throw new IllegalStateException("Unable to create system bundle because " + NiFiProperties.NAR_LIBRARY_DIRECTORY + " was null or empty");
    }

    final BundleDetails systemBundleDetails = new BundleDetails.Builder()
            .workingDir(new File(narLibraryDirectory))
            .coordinate(SYSTEM_BUNDLE_COORDINATE)
            .build();

    return new Bundle(systemBundleDetails, systemClassLoader);
}
 
Example 9
Source File: Syslog5424RecordReader.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public Record nextRecord(boolean coerceTypes, boolean dropUnknownFields) throws IOException, MalformedRecordException {
    String line = reader.readLine();

    if ( line == null ) {
        // a null return from readLine() signals the end of the stream
        return null;
    }

    if (StringUtils.isBlank(line)) {
        // while an empty string is an error
        throw new MalformedRecordException("Encountered a blank message!");
    }


    final MalformedRecordException malformedRecordException;
    Syslog5424Event event = parser.parseEvent(ByteBuffer.wrap(line.getBytes(parser.getCharsetName())));

    if (!event.isValid()) {
        if (event.getException() != null) {
            malformedRecordException = new MalformedRecordException(
                    String.format("Failed to parse %s as a Syslog message: it does not conform to any of the RFC "+
                            "formats supported", line), event.getException());
        } else {
            malformedRecordException = new MalformedRecordException(
                    String.format("Failed to parse %s as a Syslog message: it does not conform to any of the RFC" +
                            " formats supported", line));
        }
        throw malformedRecordException;
    }

    Map<String,Object> modifiedMap = new HashMap<>(event.getFieldMap());
    modifiedMap.put(SyslogAttributes.TIMESTAMP.key(),convertTimeStamp((String)event.getFieldMap().get(SyslogAttributes.TIMESTAMP.key())));

    if(includeRaw) {
        modifiedMap.put(Syslog5424Reader.RAW_MESSAGE_NAME, line);
    }

    return new MapRecord(schema,modifiedMap);
}
 
Example 10
Source File: AbstractCRUDJerseyClient.java    From nifi with Apache License 2.0 5 votes vote down vote up
protected <T> T get(
    String id,
    Class<T> entityType,
    String entityTypeName,
    String entityPath
) throws NiFiRegistryException, IOException {
    if (StringUtils.isBlank(id)) {
        throw new IllegalArgumentException(entityTypeName + " id cannot be blank");
    }

    return executeAction("Error retrieving " + entityTypeName.toLowerCase(), () -> {
        final WebTarget target = baseTarget.path(entityPath).path(id);
        return getRequestBuilder(target).get(entityType);
    });
}
 
Example 11
Source File: JerseyTenantsClient.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public UserGroupEntity getUserGroup(final String id) throws NiFiClientException, IOException {
    if (StringUtils.isBlank(id)) {
        throw new IllegalArgumentException("User group id cannot be null");
    }

    return executeAction("Error retrieving user group", () -> {
        final WebTarget target = tenantsTarget
                .path("user-groups/{id}")
                .resolveTemplate("id", id);
        return getRequestBuilder(target).get(UserGroupEntity.class);
    });
}
 
Example 12
Source File: PutMongo.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    final FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }

    final ComponentLog logger = getLogger();

    final Charset charset = Charset.forName(context.getProperty(CHARACTER_SET).getValue());
    final String mode = context.getProperty(MODE).getValue();
    final String updateMode = context.getProperty(UPDATE_MODE).getValue();
    final WriteConcern writeConcern = getWriteConcern(context);

    try {
        final MongoCollection<Document> collection = getCollection(context, flowFile).withWriteConcern(writeConcern);
        // Read the contents of the FlowFile into a byte array
        final byte[] content = new byte[(int) flowFile.getSize()];
        session.read(flowFile, in -> StreamUtils.fillBuffer(in, content, true));

        // parse
        final Object doc = (mode.equals(MODE_INSERT) || (mode.equals(MODE_UPDATE) && updateMode.equals(UPDATE_WITH_DOC.getValue())))
                ? Document.parse(new String(content, charset)) : JSON.parse(new String(content, charset));

        if (MODE_INSERT.equalsIgnoreCase(mode)) {
            collection.insertOne((Document)doc);
            logger.info("inserted {} into MongoDB", new Object[] { flowFile });
        } else {
            // update
            final boolean upsert = context.getProperty(UPSERT).asBoolean();
            final String updateKey = context.getProperty(UPDATE_QUERY_KEY).evaluateAttributeExpressions(flowFile).getValue();
            final String filterQuery = context.getProperty(UPDATE_QUERY).evaluateAttributeExpressions(flowFile).getValue();
            final Document query;

            if (!StringUtils.isBlank(updateKey)) {
                query = parseUpdateKey(updateKey, (Map)doc);
                removeUpdateKeys(updateKey, (Map)doc);
            } else {
                query = Document.parse(filterQuery);
            }

            if (updateMode.equals(UPDATE_WITH_DOC.getValue())) {
                collection.replaceOne(query, (Document)doc, new UpdateOptions().upsert(upsert));
            } else {
                BasicDBObject update = (BasicDBObject)doc;
                update.remove(updateKey);
                collection.updateOne(query, update, new UpdateOptions().upsert(upsert));
            }
            logger.info("updated {} into MongoDB", new Object[] { flowFile });
        }

        session.getProvenanceReporter().send(flowFile, getURI(context));
        session.transfer(flowFile, REL_SUCCESS);
    } catch (Exception e) {
        logger.error("Failed to insert {} into MongoDB due to {}", new Object[] {flowFile, e}, e);
        session.transfer(flowFile, REL_FAILURE);
        context.yield();
    }
}
 
Example 13
Source File: TestStandardReportingContext.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Before
public void setup() {
    flowFileEventRepo = Mockito.mock(FlowFileEventRepository.class);
    auditService = Mockito.mock(AuditService.class);
    final Map<String, String> otherProps = new HashMap<>();
    otherProps.put(NiFiProperties.PROVENANCE_REPO_IMPLEMENTATION_CLASS, MockProvenanceRepository.class.getName());
    otherProps.put("nifi.remote.input.socket.port", "");
    otherProps.put("nifi.remote.input.secure", "");
    nifiProperties = NiFiProperties.createBasicNiFiProperties(propsFile, otherProps);
    final String algorithm = nifiProperties.getProperty(NiFiProperties.SENSITIVE_PROPS_ALGORITHM);
    final String provider = nifiProperties.getProperty(NiFiProperties.SENSITIVE_PROPS_PROVIDER);
    String password = nifiProperties.getProperty(NiFiProperties.SENSITIVE_PROPS_KEY);
    if (StringUtils.isBlank(password)) {
        password = DEFAULT_SENSITIVE_PROPS_KEY;
    }
    encryptor = StringEncryptor.createEncryptor(algorithm, provider, password);

    // use the system bundle
    systemBundle = SystemBundle.create(nifiProperties);
    extensionManager = new StandardExtensionDiscoveringManager();
    extensionManager.discoverExtensions(systemBundle, Collections.emptySet());

    User user1 = new User.Builder().identifier("user-id-1").identity("user-1").build();
    User user2 = new User.Builder().identifier("user-id-2").identity("user-2").build();

    Group group1 = new Group.Builder().identifier("group-id-1").name("group-1").addUser(user1.getIdentifier()).build();
    Group group2 = new Group.Builder().identifier("group-id-2").name("group-2").build();

    AccessPolicy policy1 = new AccessPolicy.Builder()
            .identifier("policy-id-1")
            .resource("resource1")
            .action(RequestAction.READ)
            .addUser(user1.getIdentifier())
            .addUser(user2.getIdentifier())
            .build();

    AccessPolicy policy2 = new AccessPolicy.Builder()
            .identifier("policy-id-2")
            .resource("resource2")
            .action(RequestAction.READ)
            .addGroup(group1.getIdentifier())
            .addGroup(group2.getIdentifier())
            .addUser(user1.getIdentifier())
            .addUser(user2.getIdentifier())
            .build();

    Set<Group> groups1 = new LinkedHashSet<>();
    groups1.add(group1);
    groups1.add(group2);

    Set<User> users1 = new LinkedHashSet<>();
    users1.add(user1);
    users1.add(user2);

    Set<AccessPolicy> policies1 = new LinkedHashSet<>();
    policies1.add(policy1);
    policies1.add(policy2);

    authorizer = new MockPolicyBasedAuthorizer(groups1, users1, policies1);
    variableRegistry = new FileBasedVariableRegistry(nifiProperties.getVariableRegistryPropertiesPaths());
    flowRegistry = Mockito.mock(FlowRegistryClient.class);

    bulletinRepo = Mockito.mock(BulletinRepository.class);
    controller = FlowController.createStandaloneInstance(flowFileEventRepo, nifiProperties, authorizer, auditService, encryptor,
            bulletinRepo, variableRegistry, flowRegistry, extensionManager);
}
 
Example 14
Source File: SslContextFactory.java    From nifi with Apache License 2.0 4 votes vote down vote up
public static boolean isValidClientAuthType(String type) {
    if (StringUtils.isBlank(type)) {
        return false;
    }
    return (Arrays.stream(values()).map(ca -> ca.getType().toLowerCase()).collect(Collectors.toList()).contains(type.toLowerCase()));
}
 
Example 15
Source File: ScriptingComponentHelper.java    From nifi-script-tester with Apache License 2.0 4 votes vote down vote up
/**
 * Configures the specified script engine. First, the engine is loaded and instantiated using the JSR-223
 * javax.script APIs. Then, if any script configurators have been defined for this engine, their init() method is
 * called, and the configurator is saved for future calls.
 *
 * @param numberOfScriptEngines number of engines to setup
 * @see nifi.script.ScriptEngineConfigurator
 */
protected void setupEngines(int numberOfScriptEngines, ComponentLog log) {
    engineQ = new LinkedBlockingQueue<>(numberOfScriptEngines);
    ClassLoader originalContextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        if (StringUtils.isBlank(scriptEngineName)) {
            throw new IllegalArgumentException("The script engine name cannot be null");
        }

        ScriptEngineConfigurator configurator = scriptEngineConfiguratorMap.get(scriptEngineName.toLowerCase());

        // Get a list of URLs from the configurator (if present), or just convert modules from Strings to URLs
        URL[] additionalClasspathURLs = null;
        if (configurator != null) {
            additionalClasspathURLs = configurator.getModuleURLsForClasspath(modules, log);
        } else {
            if (modules != null) {
                List<URL> urls = new LinkedList<>();
                for (String modulePathString : modules) {
                    try {
                        urls.add(new File(modulePathString).toURI().toURL());
                    } catch (MalformedURLException mue) {
                        log.error("{} is not a valid file, ignoring", new Object[]{modulePathString}, mue);
                    }
                }
                additionalClasspathURLs = urls.toArray(new URL[urls.size()]);
            }
        }

        // Need the right classloader when the engine is created. This ensures the NAR's execution class loader
        // (plus the module path) becomes the parent for the script engine
        ClassLoader scriptEngineModuleClassLoader = additionalClasspathURLs != null
                ? new URLClassLoader(additionalClasspathURLs, originalContextClassLoader)
                : originalContextClassLoader;
        if (scriptEngineModuleClassLoader != null) {
            Thread.currentThread().setContextClassLoader(scriptEngineModuleClassLoader);
        }

        for (int i = 0; i < numberOfScriptEngines; i++) {
            ScriptEngine scriptEngine = createScriptEngine();
            try {
                if (configurator != null) {
                    configurator.init(scriptEngine, modules);
                }
                if (!engineQ.offer(scriptEngine)) {
                    log.error("Error adding script engine {}", new Object[]{scriptEngine.getFactory().getEngineName()});
                }

            } catch (ScriptException se) {
                log.error("Error initializing script engine configurator {}", new Object[]{scriptEngineName});
                if (log.isDebugEnabled()) {
                    log.error("Error initializing script engine configurator", se);
                }
            }
        }
    } finally {
        // Restore original context class loader
        Thread.currentThread().setContextClassLoader(originalContextClassLoader);
    }
}
 
Example 16
Source File: ScanAccumulo.java    From nifi with Apache License 2.0 4 votes vote down vote up
private Authorizations stringToAuth(final String authorizations){
    if (!StringUtils.isBlank(authorizations))
        return  new Authorizations(authorizations.split(","));
    else
        return new Authorizations();
}
 
Example 17
Source File: StandardSSLContextService.java    From nifi with Apache License 2.0 4 votes vote down vote up
private static boolean truststorePropertiesEmpty(Map<PropertyDescriptor, String> properties) {
    return StringUtils.isBlank(properties.get(TRUSTSTORE)) && StringUtils.isBlank(properties.get(TRUSTSTORE_PASSWORD)) && StringUtils.isBlank(properties.get(TRUSTSTORE_TYPE));
}
 
Example 18
Source File: AbstractElasticsearchTransportClientProcessor.java    From nifi with Apache License 2.0 4 votes vote down vote up
protected TransportClient getTransportClient(Settings.Builder settingsBuilder, String shieldUrl,
                                             String username, String password)
        throws MalformedURLException {

    // Create new transport client using the Builder pattern
    TransportClient.Builder builder = TransportClient.builder();

    // See if the Elasticsearch Shield JAR location was specified, and add the plugin if so. Also create the
    // authorization token if username and password are supplied.
    final ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
    if (!StringUtils.isBlank(shieldUrl)) {
        ClassLoader shieldClassLoader =
                new URLClassLoader(new URL[]{new File(shieldUrl).toURI().toURL()}, this.getClass().getClassLoader());
        Thread.currentThread().setContextClassLoader(shieldClassLoader);

        try {
            Class shieldPluginClass = Class.forName("org.elasticsearch.shield.ShieldPlugin", true, shieldClassLoader);
            builder = builder.addPlugin(shieldPluginClass);

            if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {

                // Need a couple of classes from the Shield plugin to build the token
                Class usernamePasswordTokenClass =
                        Class.forName("org.elasticsearch.shield.authc.support.UsernamePasswordToken", true, shieldClassLoader);

                Class securedStringClass =
                        Class.forName("org.elasticsearch.shield.authc.support.SecuredString", true, shieldClassLoader);

                Constructor<?> securedStringCtor = securedStringClass.getConstructor(char[].class);
                Object securePasswordString = securedStringCtor.newInstance(password.toCharArray());

                Method basicAuthHeaderValue = usernamePasswordTokenClass.getMethod("basicAuthHeaderValue", String.class, securedStringClass);
                authToken = (String) basicAuthHeaderValue.invoke(null, username, securePasswordString);
            }
        } catch (ClassNotFoundException
                | NoSuchMethodException
                | InstantiationException
                | IllegalAccessException
                | InvocationTargetException shieldLoadException) {
            getLogger().debug("Did not detect Elasticsearch Shield plugin, secure connections and/or authorization will not be available");
        }
    } else {
        getLogger().debug("No Shield plugin location specified, secure connections and/or authorization will not be available");
    }
    TransportClient transportClient = builder.settings(settingsBuilder.build()).build();
    Thread.currentThread().setContextClassLoader(originalClassLoader);
    return transportClient;
}
 
Example 19
Source File: DeleteByQueryElasticsearch.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
    FlowFile input = null;
    if (context.hasIncomingConnection()) {
        input = session.get();

        if (input == null && context.hasNonLoopConnection()) {
            return;
        }
    }

    try {
        final String query = getQuery(input, context, session);
        final String index = context.getProperty(INDEX).evaluateAttributeExpressions(input).getValue();
        final String type  = context.getProperty(TYPE).isSet()
                ? context.getProperty(TYPE).evaluateAttributeExpressions(input).getValue()
                : null;
        final String queryAttr = context.getProperty(QUERY_ATTRIBUTE).isSet()
                ? context.getProperty(QUERY_ATTRIBUTE).evaluateAttributeExpressions(input).getValue()
                : null;
        DeleteOperationResponse dor = clientService.deleteByQuery(query, index, type);

        if (input == null) {
            input = session.create();
        }

        Map<String, String> attrs = new HashMap<>();
        attrs.put(TOOK_ATTRIBUTE, String.valueOf(dor.getTook()));
        if (!StringUtils.isBlank(queryAttr)) {
            attrs.put(queryAttr, query);
        }

        input = session.putAllAttributes(input, attrs);

        session.transfer(input, REL_SUCCESS);
    } catch (Exception e) {
        if (input != null) {
            input = session.putAttribute(input, ERROR_ATTRIBUTE, e.getMessage());
            session.transfer(input, REL_FAILURE);
        }
        getLogger().error("Error running delete by query: ", e);
        context.yield();
    }
}
 
Example 20
Source File: ScriptingComponentHelper.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
/**
 * Configures the specified script engine. First, the engine is loaded and instantiated using the JSR-223
 * javax.script APIs. Then, if any script configurators have been defined for this engine, their init() method is
 * called, and the configurator is saved for future calls.
 *
 * @param numberOfScriptEngines number of engines to setup
 * @see org.apache.nifi.processors.script.ScriptEngineConfigurator
 */
protected void setupEngines(int numberOfScriptEngines, ComponentLog log) {
    engineQ = new LinkedBlockingQueue<>(numberOfScriptEngines);
    ClassLoader originalContextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        if (StringUtils.isBlank(scriptEngineName)) {
            throw new IllegalArgumentException("The script engine name cannot be null");
        }

        ScriptEngineConfigurator configurator = scriptEngineConfiguratorMap.get(scriptEngineName.toLowerCase());

        // Get a list of URLs from the configurator (if present), or just convert modules from Strings to URLs
        URL[] additionalClasspathURLs = null;
        if (configurator != null) {
            additionalClasspathURLs = configurator.getModuleURLsForClasspath(modules, log);
        } else {
            if (modules != null) {
                List<URL> urls = new LinkedList<>();
                for (String modulePathString : modules) {
                    try {
                        urls.add(new File(modulePathString).toURI().toURL());
                    } catch (MalformedURLException mue) {
                        log.error("{} is not a valid file, ignoring", new Object[]{modulePathString}, mue);
                    }
                }
                additionalClasspathURLs = urls.toArray(new URL[urls.size()]);
            }
        }

        // Need the right classloader when the engine is created. This ensures the NAR's execution class loader
        // (plus the module path) becomes the parent for the script engine
        ClassLoader scriptEngineModuleClassLoader = additionalClasspathURLs != null
                ? new URLClassLoader(additionalClasspathURLs, originalContextClassLoader)
                : originalContextClassLoader;
        if (scriptEngineModuleClassLoader != null) {
            Thread.currentThread().setContextClassLoader(scriptEngineModuleClassLoader);
        }

        for (int i = 0; i < numberOfScriptEngines; i++) {
            ScriptEngine scriptEngine = createScriptEngine();
            try {
                if (configurator != null) {
                    configurator.init(scriptEngine, modules);
                }
                if (!engineQ.offer(scriptEngine)) {
                    log.error("Error adding script engine {}", new Object[]{scriptEngine.getFactory().getEngineName()});
                }

            } catch (ScriptException se) {
                log.error("Error initializing script engine configurator {}", new Object[]{scriptEngineName});
                if (log.isDebugEnabled()) {
                    log.error("Error initializing script engine configurator", se);
                }
            }
        }
    } finally {
        // Restore original context class loader
        Thread.currentThread().setContextClassLoader(originalContextClassLoader);
    }
}