Search in sources :

Example 1 with TransportInterceptor

use of org.elasticsearch.transport.TransportInterceptor in project elasticsearch by elastic.

the class NetworkModuleTests method testRegisterInterceptor.

public void testRegisterInterceptor() {
    Settings settings = Settings.builder().put(NetworkModule.HTTP_ENABLED.getKey(), false).put(NetworkModule.TRANSPORT_TYPE_KEY, "local").build();
    AtomicInteger called = new AtomicInteger(0);
    TransportInterceptor interceptor = new TransportInterceptor() {

        @Override
        public <T extends TransportRequest> TransportRequestHandler<T> interceptHandler(String action, String executor, boolean forceExecution, TransportRequestHandler<T> actualHandler) {
            called.incrementAndGet();
            if ("foo/bar/boom".equals(action)) {
                assertTrue(forceExecution);
            } else {
                assertFalse(forceExecution);
            }
            return actualHandler;
        }
    };
    NetworkModule module = newNetworkModule(settings, false, new NetworkPlugin() {

        @Override
        public List<TransportInterceptor> getTransportInterceptors(NamedWriteableRegistry namedWriteableRegistry, ThreadContext threadContext) {
            assertNotNull(threadContext);
            return Collections.singletonList(interceptor);
        }
    });
    TransportInterceptor transportInterceptor = module.getTransportInterceptor();
    assertEquals(0, called.get());
    transportInterceptor.interceptHandler("foo/bar/boom", null, true, null);
    assertEquals(1, called.get());
    transportInterceptor.interceptHandler("foo/baz/boom", null, false, null);
    assertEquals(2, called.get());
    assertTrue(transportInterceptor instanceof NetworkModule.CompositeTransportInterceptor);
    assertEquals(((NetworkModule.CompositeTransportInterceptor) transportInterceptor).transportInterceptors.size(), 1);
    assertSame(((NetworkModule.CompositeTransportInterceptor) transportInterceptor).transportInterceptors.get(0), interceptor);
    NullPointerException nullPointerException = expectThrows(NullPointerException.class, () -> {
        newNetworkModule(settings, false, new NetworkPlugin() {

            @Override
            public List<TransportInterceptor> getTransportInterceptors(NamedWriteableRegistry namedWriteableRegistry, ThreadContext threadContext) {
                assertNotNull(threadContext);
                return Collections.singletonList(null);
            }
        });
    });
    assertEquals("interceptor must not be null", nullPointerException.getMessage());
}
Also used : NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) NetworkPlugin(org.elasticsearch.plugins.NetworkPlugin) TransportInterceptor(org.elasticsearch.transport.TransportInterceptor) TransportRequest(org.elasticsearch.transport.TransportRequest) TransportRequestHandler(org.elasticsearch.transport.TransportRequestHandler) ThreadContext(org.elasticsearch.common.util.concurrent.ThreadContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) List(java.util.List) Settings(org.elasticsearch.common.settings.Settings)

Aggregations

List (java.util.List)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 NamedWriteableRegistry (org.elasticsearch.common.io.stream.NamedWriteableRegistry)1 Settings (org.elasticsearch.common.settings.Settings)1 ThreadContext (org.elasticsearch.common.util.concurrent.ThreadContext)1 NetworkPlugin (org.elasticsearch.plugins.NetworkPlugin)1 TransportInterceptor (org.elasticsearch.transport.TransportInterceptor)1 TransportRequest (org.elasticsearch.transport.TransportRequest)1 TransportRequestHandler (org.elasticsearch.transport.TransportRequestHandler)1