Specifying a servlet filter name

158 views
Skip to first unread message

Cafesolo

unread,
Aug 21, 2009, 1:05:37 AM8/21/09
to google-guice
Hi everyone!

I'm trying to configure a servlet filter using a ServletModule instead
of the web.xml file, and I need to specify a name for it. This is my
current XML configuration:

<filter>
<filter-name>wicketFilter</filter-name>
<filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-
class>
<init-param>
<param-name>applicationClassName</param-name>
<param-value>com.example.MyApplication</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>wicketFilter</filter-name>
<url-pattern>/app/*</url-pattern>
</filter-mapping>

And this is as far as I got with the ServletModule class:

class MyServletModule extends com.google.inject.servlet.ServletModule
{

@Override protected void configureServlets() {
bind(WicketFilter.class).in(Scopes.SINGLETON);
filter("/app/*").through(WicketFilter.class);
}

}

As you can see, I'm not specifying a name for this filter (i.e.
"wicketFilter"), and I can't find a way to do it. The WicketFilter
class needs a name in order to work.

Is it possible to specify a name for a filter using the ServletModule
class?

Regards,
-- Cafesolo

Dhanji R. Prasanna

unread,
Aug 21, 2009, 1:48:54 AM8/21/09
to google...@googlegroups.com
Why does it need a name in order to work?

By default we just provide Key.toString() as the name of the filter...

Cafesolo

unread,
Aug 21, 2009, 9:52:01 PM8/21/09
to google-guice
Hi Dhanji,

I don't know why it needs a name. It's a Wicket thing. I'm sure there
must be a good reason for this.

I tried to override the Key.toString() method but it's declared as
final.

Any ideas?

Regards,
-- Cafesolo

Max Bowsher

unread,
Aug 22, 2009, 8:15:35 AM8/22/09
to google...@googlegroups.com
Cafesolo wrote:
> Hi Dhanji,
>
> I don't know why it needs a name. It's a Wicket thing. I'm sure there
> must be a good reason for this.

Are you sure it really does? I use Wicket, though not (yet) with Guice,
and I can't think of any particular reason.

Max.

signature.asc

Cafesolo

unread,
Aug 22, 2009, 11:35:53 AM8/22/09
to google-guice
Hi Max,

In Wicket 1.4, the WicketFilter throws an exception if I configure it
using Guice instead of web.xml.

I think they made WicketFilter throw an exception if you don't specify
a name because you need a <filter-mapping> in your web.xml file, which
needs a <filter-name>...</filter-name> element. Correct me if I'm
wrong, but a filter without a mapping is useless, so I think they
wanted to throw a descriptive exception to make newbies lifes easier.

Regards,
-- Cafesolo
>  signature.asc
> < 1KViewDownload

Brian Pontarelli

unread,
Aug 22, 2009, 11:46:58 AM8/22/09
to google...@googlegroups.com

Send the exception. I doubt you need a name because that is primarily
used to map a filter to a filter-mapping in the web.xml and serves
little purpose beyond that. I'd be really surprised if Wicket was
using this information.

-bp

Cafesolo

unread,
Aug 22, 2009, 1:06:30 PM8/22/09
to google-guice
This is the exception's stack trace:

java.lang.IllegalArgumentException: Error initializing WicketFilter -
you have no <filter-mapping> element with a url-pattern that uses
filter: Key[type=org.apache.wicket.protocol.http.WicketFilter,
annotation=[none]]
at org.apache.wicket.protocol.http.WicketFilter.getFilterPath
(WicketFilter.java:907)
at org.apache.wicket.protocol.http.WicketFilter.init
(WicketFilter.java:654)
at com.google.inject.servlet.FilterDefinition.init
(FilterDefinition.java:81)
at com.google.inject.servlet.ManagedFilterPipeline.initPipeline
(ManagedFilterPipeline.java:102)
at com.google.inject.servlet.GuiceFilter.init(GuiceFilter.java:168)
at org.mortbay.jetty.servlet.FilterHolder.doStart(FilterHolder.java:
99)
at org.mortbay.component.AbstractLifeCycle.start
(AbstractLifeCycle.java:40)
at org.mortbay.jetty.servlet.ServletHandler.initialize
(ServletHandler.java:589)
at org.mortbay.jetty.servlet.Context.startContext(Context.java:139)
at org.mortbay.jetty.webapp.WebAppContext.startContext
(WebAppContext.java:1218)
at org.mortbay.jetty.handler.ContextHandler.doStart
(ContextHandler.java:500)
at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:
448)
at org.mortbay.component.AbstractLifeCycle.start
(AbstractLifeCycle.java:40)
at org.mortbay.jetty.handler.HandlerWrapper.doStart
(HandlerWrapper.java:117)
at org.mortbay.component.AbstractLifeCycle.start
(AbstractLifeCycle.java:40)
at org.mortbay.jetty.handler.HandlerWrapper.doStart
(HandlerWrapper.java:117)
at org.mortbay.jetty.Server.doStart(Server.java:217)
at org.mortbay.component.AbstractLifeCycle.start
(AbstractLifeCycle.java:40)
...
...

Regards,
-- Cafesolo

Dhanji R. Prasanna

unread,
Aug 22, 2009, 8:37:55 PM8/22/09
to google...@googlegroups.com
Yea, wicket does all this extra checking to baby sit your config. I'll talk to Eelco and see what the best way to make it work with Guice is.

Dhanji.

Brian Pontarelli

unread,
Aug 22, 2009, 10:15:02 PM8/22/09
to google...@googlegroups.com
Interesting approach. Is this useful?

Sent from my iPhone

Max Bowsher

unread,
Aug 23, 2009, 6:55:56 AM8/23/09
to google...@googlegroups.com
Cafesolo wrote:
> This is the exception's stack trace:
>
> java.lang.IllegalArgumentException: Error initializing WicketFilter -
> you have no <filter-mapping> element with a url-pattern that uses
> filter: Key[type=org.apache.wicket.protocol.http.WicketFilter,
> annotation=[none]]
> at org.apache.wicket.protocol.http.WicketFilter.getFilterPath
> (WicketFilter.java:907)
> at org.apache.wicket.protocol.http.WicketFilter.init
> (WicketFilter.java:654)

Ah, thought it might be that - so, no, setting the name is not the
important thing here at all.

If you have a look at WicketFilter's sourcecode you can see that it's
trying to figure out what the root path assigned to WicketFilter within
your webapp is - and that the Wicket developers have provided a way for
you to pass that via a filter configuration parameter if your setup is
such that Wicket can't fish it out of web.xml itself.

There does appear to be one small bug in WicketFilter though - the part
where it tries to trap exceptions from determining the url-pattern
doesn't catch IllegalArgumentException.

Max.

signature.asc

gagarin61

unread,
Aug 24, 2009, 4:22:34 AM8/24/09
to google-guice
I faced the same problem with Tapestry. Tapestry filter uses its name
to define application id, which makes configuring Tapestry entirely
with Guice impossible.
I gave up relatively fast when found that guice-servlet does not
provide filter naming out-of-the-box, so now I go with classic web.xml
configuration. Of course, on the one hand it's not very fancy way to
configure app, but on the other - guice-servlet just does not fully
cover web.xml configuration topics.

Dhanji R. Prasanna

unread,
Aug 24, 2009, 5:02:49 AM8/24/09
to google...@googlegroups.com
Also note that you don't have to choose either or, you can configure WicketFilter with web.xml and everything else with Guice Servlet, while we sort this issue out. It's pretty ridiculous that the name of the filter is significant to web frameworks, however.

Dhanji.

gagarin61

unread,
Aug 24, 2009, 7:53:45 AM8/24/09
to google-guice
I find this like just another approach of meta-oriented programming/
configuration that become more popular novadays. It might be
inappropriate way for someone, but I personally find more
inappropriate to hide possibilities of configuration of standard
properties like <filter-name/> from developer. See FilterConfig
interface and its implementation in guice-servlet, where getFilterName
() just returns key.toString().

On Aug 24, 12:02 pm, "Dhanji R. Prasanna" <dha...@gmail.com> wrote:
> Also note that you don't have to choose either or, you can configure
> WicketFilter with web.xml and everything else with Guice Servlet, while we
> sort this issue out. It's pretty ridiculous that the name of the filter is
> significant to web frameworks, however.
> Dhanji.
>

Max Bowsher

unread,
Aug 24, 2009, 8:25:34 AM8/24/09
to google...@googlegroups.com
gagarin61 wrote:
> I find this like just another approach of meta-oriented programming/
> configuration that become more popular novadays. It might be
> inappropriate way for someone, but I personally find more
> inappropriate to hide possibilities of configuration of standard
> properties like <filter-name/> from developer. See FilterConfig
> interface and its implementation in guice-servlet, where getFilterName
> () just returns key.toString().

I'd imagine it's a consequence of no-one thinking anyone would ever care
about names of servlets/filters, so why bother exposing a useless API.


> On Aug 24, 12:02 pm, "Dhanji R. Prasanna" <dha...@gmail.com> wrote:
>> Also note that you don't have to choose either or, you can configure
>> WicketFilter with web.xml and everything else with Guice Servlet, while we
>> sort this issue out. It's pretty ridiculous that the name of the filter is
>> significant to web frameworks, however.


I can't speak for Tapestry, but as far as I can see from the code,
Wicket only cares to the extent of wanting to discover the base URL
within your webapp which is assigned to Wicket, and it provides a filter
init-param for you to override the default logic which attempts to parse
web.xml.


Max.

signature.asc

Cafesolo

unread,
Aug 24, 2009, 8:47:09 PM8/24/09
to google-guice
Max,

> I can't speak for Tapestry, but as far as I can see from the code,
> Wicket only cares to the extent of wanting to discover the base URL
> within your webapp which is assigned to Wicket, and it provides a filter
> init-param for you to override the default logic which attempts to parse
> web.xml.

You were right. WicketFilter provides a filter init-param called
"filterMappingUrlPattern" that overrides the default logic. The
WicketFilter works perfectly with Guice Servlet if you specify the url
pattern using this parameter.

For anyone interested, this is how my ServletModule looks right now:

public class ServletModule extends
com.google.inject.servlet.ServletModule {

@Override protected void configureServlets() {
serve("/myapp/foo").with(FooServlet.class);
serve("/myapp/bar").with(BarServlet.class);

Map<String, String> params = new HashMap<String, String>();
params.put("applicationClassName", "com.myapp.MyWicketApplication");
params.put("filterMappingUrlPattern", "/myapp/*");
filter("/myapp/*").through(WicketFilter.class, params);
bind(WicketFilter.class).in(Scopes.SINGLETON);
}

}

Thanks guys!

Regards,
-- Cafesolo
>  signature.asc
> < 1KViewDownload

Dhanji R. Prasanna

unread,
Aug 24, 2009, 9:30:55 PM8/24/09
to google...@googlegroups.com
Perfect, I was going to suggest something like this to the wicket team. We should add this to the FAQ.

Dhanji.

hbf

unread,
Aug 26, 2009, 5:47:36 AM8/26/09
to google-guice


On Aug 25, 3:30 am, "Dhanji R. Prasanna" <dha...@gmail.com> wrote:
> Perfect, I was going to suggest something like this to the wicket team. We
> should add this to the FAQ.
> Dhanji.

That would indeed be nice. Also, if I understand correctly, warp-
servlet is not needed anymore (Guice 2.0 "replaces" it). Could this be
mentioned? Best at both places, guice and warp-servlet.

Kaspar

Dhanji R. Prasanna

unread,
Aug 26, 2009, 7:41:31 AM8/26/09
to google...@googlegroups.com
Guice Servlet 2 is basically warp-servlet.
Reply all
Reply to author
Forward
0 new messages