serveRegex("^((?!^/index\\.html$|^/test\\.html$).)*$").with(MyServlet.class)
It will pass every URI to your servlet, except /index.html and
/test.html which will be served by servlet container's default
servlet. Maybe this useful trick should be documented in ServletModule
documentation? It could help a lot when defining request authorization
with filterRegex, which I consider guice-servlets's unique feature.
> --
> You received this message because you are subscribed to the Google Groups "google-guice" group.
> To post to this group, send email to google...@googlegroups.com.
> To unsubscribe from this group, send email to google-guice...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.
>
>
--
"Meaning is differential not referential"
kazik 'morisil' pogoda
http://www.xemantic.com/ http://blog.xemantic.com/
You could do the opposite and first list the urls or folders that are served by a StaticServlet that simply forwards to the resource, and fall back on /* for everything else.
Moandji
Sent from my Android
I would be surprised if you couldn't do the same thing with a regular
Jetty or Tomcat server. But maybe there is a reason why you would like
these files to go through a servlet?
Cheers,
Philippe
In my case, I'm running the GWT Jetty server locally, but it's going
to be deployed to a different container (not sure which one yet). So
putting the configuration in guice-servlet would keep things cleaner.
> I would be surprised if you couldn't do the same thing with a regular
> Jetty or Tomcat server. But maybe there is a reason why you would like
> these files to go through a servlet?
No, not really - quite the opposite; I'd like it to just get served
as-is. If running through a servlet is the best way to do that, fine.
I'd rather configure in guice than in some config file somewhere, and
this seems like something guice could do.
No worries - everything works fine with the url prefix, I'm just
speculating about a possible better way to do it.
> You might want to use [.] instead of \\. for the literal period,
I am not a regex expert - Is there any practical difference, or just a
matter of notation?
> or
> even
>
> /(?!index|test)[.]html$
There is a reason I put each URI separately, in code it looks like:
serveRegex("^((?!" +
"^/index\\.html$|" +
"^/test\\.html$" +
").)*$") ...
, this way it is easy to add/remove new URIs. It could be convenient
to write some regex string producer which would take varargs of URIs,
escape all special characters, and join them all using guava's Joiner.
More practical example, serve everything with some dispatcher servlet,
except /images/* and /resources/*
serveRegex("^((?!" +
"^/images/.+|" +
"^/resources/.+" +
").)*$") ...
But returning to your proposal, see code below:
Pattern p = Pattern.compile("^((?!^/index\\.html$|^/test\\.html$).)*$");
out.println(p.matcher("/").matches());
out.println(p.matcher("/foo.html").matches());
out.println(p.matcher("/index.html_foo").matches());
out.println(p.matcher("/index").matches());
out.println(p.matcher("/index.html").matches());
out.println(p.matcher("/test.html").matches());
It returns:
true
true
true
true
false
false
However after replacing regex with:
/(?!index|test)[.]html$
It returns 6 x false which is not desired behavior. :(
--
"Meaning is differential not referential"
kazik 'morisil' pogoda
http://www.xemantic.com/ http://blog.xemantic.com/
AFAIR the servlet request handling sequence works the way, that after
all the filters, if no servlet mapping is resolved, the request goes
to so called default servlet (mapped to /*). Every servlet container
has own implementation of this. Here is one for jetty:
It would be possibe to do something like:
bind(DefaultServlet.class).in(Singleton.class);
serve("/index.html").with(DefaultServlet.class);
,however AFAIR there are some drawbacks of mapping DefaultServlet to
something other than /*, at least on some servlet containers - the
path before * could be excluded from webapp context relative file
path. In some servlet containers the default servlet is declared with
name 'default' and you can use own mapping to this servlet in web.xml,
however it does not seem to be portable. The solution I proposed looks
ugly - I know it, however it seems to be portable, as long as we can
count on different default servlet implementations. :)
--
"Meaning is differential not referential"
kazik 'morisil' pogoda
http://www.xemantic.com/ http://blog.xemantic.com/