users@grizzly.java.net

Re: Adding a filter at runtime

From: Oleksiy Stashok <oleksiy.stashok_at_oracle.com>
Date: Tue, 27 Jan 2015 23:45:43 -0800

Hi Tiran,

unfortunately in Grizzly 2.3.x FilterChain modification at runtime is
tricky (it's fixed on 3.0 branch).
We suggest to install all the Filters in advance and use a switch to
turn the filter logic on and off, like here:
https://github.com/AsyncHttpClient/async-http-client/blob/1.9.x/src/main/java/com/ning/http/client/providers/grizzly/GrizzlyAsyncHttpProvider.java#L2446

Specifically in your case the problem could be related to
FilterChainBuilder instance, which underneath updates existing
FilterChain instead of creating a new FilterChain instance.
You can try to create a separate FilterChainBuilder and initiate it with
all the Filters you need.

IMO FilterChainBuilder logic (updating an existing FilterChain) is wrong
and it has to be fixed. It has to create new FilterChain instance every
time build() is invoked. Will appreciate if you can bug an issue for this.

Thank you.

WBR,
Alexey.




On 27.01.15 09:40, Meltser Tiran wrote:
>
> Hi all,
>
> I’m trying to add a filter (SSLFilter) at runtime to support a
> STARTTLS IMAP command.
>
> *I tried the following code:*
>
> Pair<NIOTransport,FilterChainBuilder> transportData =
> mTransportsMap.get(aServiceGate.getSecurityLevel());
>
> if (transportData != null)
>
> {
>
> FilterChainBuilder filterChainBuilder = transportData.getSecond();
>
> if (filterChainBuilder != null)
>
> {
>
> // We'll place the SSLFilter right after the TransportFilter
>
> int indexOfTransportFilter =
> filterChainBuilder.indexOfType(TransportFilter.class);
>
> // Initialize and add SSLFilter
>
> filterChainBuilder.add(indexOfTransportFilter + 1,obtainSSLFilter());
>
> // Secure the entry point connection (build a proper filter chain for
> this connection)
>
> aEntryPointConnection.setProcessor(filterChainBuilder.build());
>
> }
>
> }
>
> *But got the following Exception:*
>
> *java.lang.ArrayIndexOutOfBoundsException: 6*
>
> at
> org.glassfish.grizzly.filterchain.DefaultFilterChain$FiltersState.peekUnparsedIdx(DefaultFilterChain.java:619)
>
> at
> org.glassfish.grizzly.filterchain.DefaultFilterChain.prepareRemainder(DefaultFilterChain.java:301)
>
> at
> org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:154)
>
> at
> org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
>
> at
> org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
>
> at
> org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
>
> at
> org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
>
> at
> org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
>
> at
> org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
>
> at
> org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
>
> at
> org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
>
> at
> org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
>
> at java.lang.Thread.run(Thread.java:745)
>
> I came across the following item:
> https://java.net/jira/browse/GRIZZLY-1560
> <https://java.net/jira/browse/GRIZZLY-1560>, which seems to be my
> problem. I saw that the issue was solved:
>
> 1.In which version was it solved (I’m using 2.3.18)?
>
> 2.Can someone share a pseudo code (suitable to the 2.3.18 line) of the
> recommended way to perform a dynamic adding of a filter at runtime?
>
> Thanks,
>
> *Tiran****Meltser***
>
> ------------------------------------------------------------------------
> “This e-mail message may contain confidential, commercial or
> privileged information that constitutes proprietary information of
> Comverse Inc. or its subsidiaries. If you are not the intended
> recipient of this message, you are hereby notified that any review,
> use or distribution of this information is absolutely prohibited and
> we request that you delete all copies and contact us by e-mailing to:
> security_at_comverse.com. Thank You.”