You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by Vidar Ramdal <vi...@idium.no> on 2008/04/01 17:55:54 UTC

Servlet registration acrobatics

Hi;

I want to register a servlet that should only be invoked on a given
resourceType, and when the request has no extension.

Given a JCR node /content, with sling:resourceType=ourResourceType

The servlet should handle this request: GET /content
... but not this: GET /content.html

>From http://incubator.apache.org/sling/site/servlet-resolution.html
(albeit outdated) I read that "[...] a Resource provider is registered
for the Servlet for each permutation resource types, selectors,
extensions and methods".
What I want, is not all possible permutations, but rather the
intersection of extension and resourceType.
Is this possible?

-- 
Vidar S. Ramdal <vi...@idium.no> - http://www.idium.no
Akersgata 16, N-0158 Oslo, Norway

Re: Servlet registration acrobatics

Posted by Felix Meschberger <fm...@gmail.com>.
Hi Betrand

Am Mittwoch, den 02.04.2008, 09:47 +0200 schrieb Bertrand Delacretaz:
> On Tue, Apr 1, 2008 at 9:23 PM, Felix Meschberger <fm...@gmail.com> wrote:
> 
> > ...maybe an API
> >  call such as
> >
> >    boolean canHandle(SlingHttpServletRequest)
> >
> >  in an interface, which a servlet may implement. If a servlet would be
> >  selected which implements the method and returns false, the servlet
> >  would be ignored for the resolution and the resolution process would
> >  continue as if the servlet would not exist.....
> 
> Maybe providing extension points for the ServletResolver would be
> cleaner, and allow for easier control of the priority of servlet
> resolution?
> 
> We could use additional ServletResolver services, that the main
> ServletResolver would call in (configurable) priority order, and stop
> as soon as one returns a Servlet.
> 
> A class can be both a Servlet and ServletResolver service, so this
> could be used in a similar way than what you suggest, but in addition
> allow more sophisticated resolving algorithms.
> 
> WDYT?

I am not sure whether we really need this sofistication. And we can
still add this later in a way which is compatible with Vidar's current
proposal.

Regards
Felix


Re: Servlet registration acrobatics

Posted by Vidar Ramdal <vi...@idium.no>.
On Wed, Apr 2, 2008 at 9:47 AM, Bertrand Delacretaz
<bd...@apache.org> wrote:
> On Tue, Apr 1, 2008 at 9:23 PM, Felix Meschberger <fm...@gmail.com> wrote:
>
>  > ...maybe an API
>
> >  call such as
>  >
>  >    boolean canHandle(SlingHttpServletRequest)
>  >
>  >  in an interface, which a servlet may implement. If a servlet would be
>  >  selected which implements the method and returns false, the servlet
>  >  would be ignored for the resolution and the resolution process would
>  >  continue as if the servlet would not exist.....


I was thinking about introducing a SlingRequestHandlerServlet
interface, with a method accepts(HtttpServletRequest), and then
modifying SlingServletResolver along these lines:
     private Servlet getServlet(ResourceResolver resolver,
                               Iterator<String> paths, String
baseName, HttpServletRequest request) {
         while (paths.hasNext()) {
             String location = paths.next();
             try {
                 Servlet result = getServletAt(resolver, location, baseName);
+                 if (result != null) {
+                    boolean servletAcceptsRequest = !(result
instanceof SlingRequestHandlerServlet)
+                            || ((SlingRequestHandlerServlet)
result).accepts(request);
+                    if (servletAcceptsRequest) {
                    return result;
+                }

>  Maybe providing extension points for the ServletResolver would be
>  cleaner, and allow for easier control of the priority of servlet
>  resolution?
>
>  We could use additional ServletResolver services, that the main
>  ServletResolver would call in (configurable) priority order, and stop
>  as soon as one returns a Servlet.
>
>  A class can be both a Servlet and ServletResolver service, so this
>  could be used in a similar way than what you suggest, but in addition
>  allow more sophisticated resolving algorithms.
>
>  WDYT?

Your solution sounds a lot more flexible, though, and we could
definitely need some kind of servlet priority logic. However, I'm not
sure I'm up to the task of coding it, not knowing the details of
Sling's request handling logic. But I can create a JIRA issue for it,
if that's any help :)

-- 
Vidar S. Ramdal <vi...@idium.no> - http://www.idium.no
Akersgata 16, N-0158 Oslo, Norway

Re: Servlet registration acrobatics

Posted by Bertrand Delacretaz <bd...@apache.org>.
On Tue, Apr 1, 2008 at 9:23 PM, Felix Meschberger <fm...@gmail.com> wrote:

> ...maybe an API
>  call such as
>
>    boolean canHandle(SlingHttpServletRequest)
>
>  in an interface, which a servlet may implement. If a servlet would be
>  selected which implements the method and returns false, the servlet
>  would be ignored for the resolution and the resolution process would
>  continue as if the servlet would not exist.....

Maybe providing extension points for the ServletResolver would be
cleaner, and allow for easier control of the priority of servlet
resolution?

We could use additional ServletResolver services, that the main
ServletResolver would call in (configurable) priority order, and stop
as soon as one returns a Servlet.

A class can be both a Servlet and ServletResolver service, so this
could be used in a similar way than what you suggest, but in addition
allow more sophisticated resolving algorithms.

WDYT?

-Bertrand

Re: Servlet registration acrobatics

Posted by Felix Meschberger <fm...@gmail.com>.
Hi Vidal,

Am Freitag, den 04.04.2008, 11:26 +0200 schrieb Vidar Ramdal:
> On Wed, Apr 2, 2008 at 4:57 PM, Vidar Ramdal <vi...@idium.no> wrote:
> 
> >  Created https://issues.apache.org/jira/browse/SLING-352
> 
> Any possibility for incorporating this in 1.0?
> 

Sorry for not answering earlier, I was distracted by other work ...

Anyway, I quickly looked at your patch, which seems ok for checkin.

I just assume silent consent and apply your patch.

Of course the patch adds more complications to the resolution process,
so we should probably also add some request log information about the
situation that a servlet refuses to handle the request. I will do that.

Regards
Felix


Re: Servlet registration acrobatics

Posted by Vidar Ramdal <vi...@idium.no>.
On Wed, Apr 2, 2008 at 4:57 PM, Vidar Ramdal <vi...@idium.no> wrote:

>  Created https://issues.apache.org/jira/browse/SLING-352

Any possibility for incorporating this in 1.0?

-- 
Vidar S. Ramdal <vi...@idium.no> - http://www.idium.no
Akersgata 16, N-0158 Oslo, Norway

Re: Servlet registration acrobatics

Posted by Vidar Ramdal <vi...@idium.no>.
On Wed, Apr 2, 2008 at 9:01 AM, Vidar Ramdal <vi...@idium.no> wrote:

> On Tue, Apr 1, 2008 at 9:23 PM, Felix Meschberger <fm...@gmail.com> wrote:
>  >  If you can come up with a good solution to this problem (maybe an API
>  >  call such as
>  >
>  >    boolean canHandle(SlingHttpServletRequest)
>  >
>  >  in an interface, which a servlet may implement. If a servlet would be
>  >  selected which implements the method and returns false, the servlet
>  >  would be ignored for the resolution and the resolution process would
>  >  continue as if the servlet would not exist.
>  >
>  >  (yes, we discussed this already, but never got around to doing it. You
>  >  are of course welcome to provide something like that...)
>
>  :) OK, I'll see what I can come up with.

Created https://issues.apache.org/jira/browse/SLING-352 for this, and
created a patch (hey, I even wrote a testcase:). Please take a look at
it and see if it's usable.

This, of course, would be obsolete if Bertrand's solution would
somehow appear in the codebase.

-- 
Vidar S. Ramdal <vi...@idium.no> - http://www.idium.no
Akersgata 16, N-0158 Oslo, Norway

Re: Servlet registration acrobatics

Posted by Vidar Ramdal <vi...@idium.no>.
On Tue, Apr 1, 2008 at 9:23 PM, Felix Meschberger <fm...@gmail.com> wrote:
> Hi Vidar,
>
>  It is currently not possible to register a servlet/script for "no
>  extension".
>
>  What you could do as a workaround (not complete, though) is to register
>  the servlet just for the GET method without an extension and send 404
>  (or so) on requests with an extension.

Well, yes, but a 404 would only return the error to the browser. I
really need to send the request down the Sling "request handler
chain", because other servlets might want to handle it.

>  If you can come up with a good solution to this problem (maybe an API
>  call such as
>
>    boolean canHandle(SlingHttpServletRequest)
>
>  in an interface, which a servlet may implement. If a servlet would be
>  selected which implements the method and returns false, the servlet
>  would be ignored for the resolution and the resolution process would
>  continue as if the servlet would not exist.
>
>  (yes, we discussed this already, but never got around to doing it. You
>  are of course welcome to provide something like that...)

:) OK, I'll see what I can come up with.

-- 
Vidar S. Ramdal <vi...@idium.no> - http://www.idium.no
Akersgata 16, N-0158 Oslo, Norway

Re: Servlet registration acrobatics

Posted by Felix Meschberger <fm...@gmail.com>.
Hi Vidar,

It is currently not possible to register a servlet/script for "no
extension".

What you could do as a workaround (not complete, though) is to register
the servlet just for the GET method without an extension and send 404
(or so) on requests with an extension.

If you can come up with a good solution to this problem (maybe an API
call such as

   boolean canHandle(SlingHttpServletRequest)

in an interface, which a servlet may implement. If a servlet would be
selected which implements the method and returns false, the servlet
would be ignored for the resolution and the resolution process would
continue as if the servlet would not exist.

(yes, we discussed this already, but never got around to doing it. You
are of course welcome to provide something like that...)

Regards
Felix

Am Dienstag, den 01.04.2008, 17:55 +0200 schrieb Vidar Ramdal:
> Hi;
> 
> I want to register a servlet that should only be invoked on a given
> resourceType, and when the request has no extension.
> 
> Given a JCR node /content, with sling:resourceType=ourResourceType
> 
> The servlet should handle this request: GET /content
> ... but not this: GET /content.html
> 
> >From http://incubator.apache.org/sling/site/servlet-resolution.html
> (albeit outdated) I read that "[...] a Resource provider is registered
> for the Servlet for each permutation resource types, selectors,
> extensions and methods".
> What I want, is not all possible permutations, but rather the
> intersection of extension and resourceType.
> Is this possible?
>