Java Code Examples for org.elasticsearch.client.transport.TransportClient#builder()

The following examples show how to use org.elasticsearch.client.transport.TransportClient#builder() . 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: AbstractElasticsearchTransportClientProcessor.java    From localization_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 2
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;
}