@Test public void testCustomAuthenticator() throws Exception { final String uniqueInstanceKey = String.valueOf(instanceCount.getAndIncrement()); final String[] customAuthArgs = new String[]{ uniqueInstanceKey, "arg1", "arg2" }; final String serializedArgList = StringUtils.join(customAuthArgs,','); final HttpClientConfigCallback hccc = authTestBase( ImmutableMap.<String, String>builder(). put("index." + INDEX_NAME + ".elasticsearch.interface", "REST_CLIENT"). put("index." + INDEX_NAME + ".elasticsearch.http.auth.type", HttpAuthTypes.CUSTOM.toString()). put("index." + INDEX_NAME + ".elasticsearch.http.auth.custom.authenticator-class", TestCustomAuthenticator.class.getName()). put("index." + INDEX_NAME + ".elasticsearch.http.auth.custom.authenticator-args", serializedArgList). build() ); verify(restClientSetup).getCustomAuthenticator( eq(TestCustomAuthenticator.class.getName()), eq(customAuthArgs)); TestCustomAuthenticator customAuth = TestCustomAuthenticator.instanceMap.get(uniqueInstanceKey); assertNotNull(customAuth); assertEquals(1, customAuth.numInitCalls); final HttpAsyncClientBuilder hacb = mock(HttpAsyncClientBuilder.class); hccc.customizeHttpClient(hacb); assertEquals(1, customAuth.customizeHttpClientHistory.size()); assertSame(hacb, customAuth.customizeHttpClientHistory.get(0)); assertArrayEquals(customAuthArgs, customAuth.args); }
protected RestClientAuthenticator getCustomAuthenticator(String authClassName, String[] authClassConstructorArgList) { Preconditions.checkArgument(StringUtils.isNotEmpty(authClassName), "Custom authenticator class name cannot be empty"); Preconditions.checkNotNull(authClassConstructorArgList, "Custom authenticator class constructor argument list cannot be null"); final RestClientAuthenticator authenticator; try { final Class<?> c = Class.forName(authClassName); Preconditions.checkArgument(RestClientAuthenticator.class.isAssignableFrom(c), "Authenticator class " + authClassName + " must be a subclass of " + RestClientAuthenticator.class.getName()); @SuppressWarnings("unchecked") final Constructor<RestClientAuthenticator> ctr = ((Class<RestClientAuthenticator>)c).getConstructor(String[].class); authenticator = ctr.newInstance((Object)authClassConstructorArgList); } catch (Exception e) { log.error("Unable to instantiate the custom authenticator {} with constructor arguments \"{}\"", authClassName, authClassConstructorArgList, e); throw new RuntimeException("Unable to instantiate the custom authenticator", e); } try { authenticator.init(); } catch (IOException e) { log.error("Unable to initialize the custom authenticator {} with constructor arguments \"{}\"", authClassName, authClassConstructorArgList, e); throw new RuntimeException("Unable to initialize the custom authenticator", e); } return authenticator; }
RestClientSetup { protected RestClientAuthenticator getCustomAuthenticator(String authClassName, String[] authClassConstructorArgList) { Preconditions.checkArgument(StringUtils.isNotEmpty(authClassName), "Custom authenticator class name cannot be empty"); Preconditions.checkNotNull(authClassConstructorArgList, "Custom authenticator class constructor argument list cannot be null"); final RestClientAuthenticator authenticator; try { final Class<?> c = Class.forName(authClassName); Preconditions.checkArgument(RestClientAuthenticator.class.isAssignableFrom(c), "Authenticator class " + authClassName + " must be a subclass of " + RestClientAuthenticator.class.getName()); @SuppressWarnings("unchecked") final Constructor<RestClientAuthenticator> ctr = ((Class<RestClientAuthenticator>)c).getConstructor(String[].class); authenticator = ctr.newInstance((Object)authClassConstructorArgList); } catch (Exception e) { log.error("Unable to instantiate the custom authenticator {} with constructor arguments \"{}\"", authClassName, authClassConstructorArgList, e); throw new RuntimeException("Unable to instantiate the custom authenticator", e); } try { authenticator.init(); } catch (IOException e) { log.error("Unable to initialize the custom authenticator {} with constructor arguments \"{}\"", authClassName, authClassConstructorArgList, e); throw new RuntimeException("Unable to initialize the custom authenticator", e); } return authenticator; } }
RestClientSetup { protected RestClientAuthenticator getCustomAuthenticator(String authClassName, String[] authClassConstructorArgList) { Preconditions.checkArgument(StringUtils.isNotEmpty(authClassName), "Custom authenticator class name cannot be empty"); Preconditions.checkNotNull(authClassConstructorArgList, "Custom authenticator class constructor argument list cannot be null"); final RestClientAuthenticator authenticator; try { final Class<?> c = Class.forName(authClassName); Preconditions.checkArgument(RestClientAuthenticator.class.isAssignableFrom(c), "Authenticator class " + authClassName + " must be a subclass of " + RestClientAuthenticator.class.getName()); @SuppressWarnings("unchecked") final Constructor<RestClientAuthenticator> ctr = ((Class<RestClientAuthenticator>)c).getConstructor(String[].class); authenticator = ctr.newInstance((Object)authClassConstructorArgList); } catch (Exception e) { log.error("Unable to instantiate the custom authenticator {} with constructor arguments \"{}\"", authClassName, authClassConstructorArgList, e); throw new RuntimeException("Unable to instantiate the custom authenticator", e); } try { authenticator.init(); } catch (IOException e) { log.error("Unable to initialize the custom authenticator {} with constructor arguments \"{}\"", authClassName, authClassConstructorArgList, e); throw new RuntimeException("Unable to initialize the custom authenticator", e); } return authenticator; }  }
RestClientSetup { protected RestClientAuthenticator getCustomAuthenticator(String authClassName, String[] authClassConstructorArgList) { Preconditions.checkArgument(StringUtils.isNotEmpty(authClassName), "Custom authenticator class name cannot be empty"); Preconditions.checkNotNull(authClassConstructorArgList, "Custom authenticator class constructor argument list cannot be null"); final RestClientAuthenticator authenticator; try { final Class<?> c = Class.forName(authClassName); Preconditions.checkArgument(RestClientAuthenticator.class.isAssignableFrom(c), "Authenticator class " + authClassName + " must be a subclass of " + RestClientAuthenticator.class.getName()); @SuppressWarnings("unchecked") final Constructor<RestClientAuthenticator> ctr = ((Class<RestClientAuthenticator>)c).getConstructor(String[].class); authenticator = ctr.newInstance((Object)authClassConstructorArgList); } catch (Exception e) { log.error("Unable to instantiate the custom authenticator {} with constructor arguments \"{}\"", authClassName, authClassConstructorArgList, e); throw new RuntimeException("Unable to instantiate the custom authenticator", e); } try { authenticator.init(); } catch (IOException e) { log.error("Unable to initialize the custom authenticator {} with constructor arguments \"{}\"", authClassName, authClassConstructorArgList, e); throw new RuntimeException("Unable to initialize the custom authenticator", e); } return authenticator; }  ElasticSearchClient connect(Configuration config); }
RestClientSetup { protected RestClientAuthenticator getCustomAuthenticator(String authClassName, String[] authClassConstructorArgList) { Preconditions.checkArgument(StringUtils.isNotEmpty(authClassName), "Custom authenticator class name cannot be empty"); Preconditions.checkNotNull(authClassConstructorArgList, "Custom authenticator class constructor argument list cannot be null"); final RestClientAuthenticator authenticator; try { final Class<?> c = Class.forName(authClassName); Preconditions.checkArgument(RestClientAuthenticator.class.isAssignableFrom(c), "Authenticator class " + authClassName + " must be a subclass of " + RestClientAuthenticator.class.getName()); @SuppressWarnings("unchecked") final Constructor<RestClientAuthenticator> ctr = ((Class<RestClientAuthenticator>)c).getConstructor(String[].class); authenticator = ctr.newInstance((Object)authClassConstructorArgList); } catch (Exception e) { log.error("Unable to instantiate the custom authenticator {} with constructor arguments \"{}\"", authClassName, authClassConstructorArgList, e); throw new RuntimeException("Unable to instantiate the custom authenticator", e); } try { authenticator.init(); } catch (IOException e) { log.error("Unable to initialize the custom authenticator {} with constructor arguments \"{}\"", authClassName, authClassConstructorArgList, e); throw new RuntimeException("Unable to initialize the custom authenticator", e); } return authenticator; }  ElasticSearchClient connect(Configuration config);  }