ContextHandlerCollection as a child handler of ContextHandler

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

ContextHandlerCollection as a child handler of ContextHandler

Neil Williams
Looking at having the following:

public class ContextPlay {
  public static void main(String[] args) throws Exception {
    Server server = new Server(8000);

    ContextHandlerCollection chc = new ContextHandlerCollection();
    chc.setHandlers(new Handler[] {new PrintHandler("/a"), new PrintHandler("/b"), new PrintHandler("/c")});

    ContextHandler ch = new ContextHandler("/base")
    ch.setHandler(chc);

    server.setHandler(ch);

    server.start();
    server.join();
  }

  public static class PrintHandler extends ContextHandler {
    public PrintHandler(String context) {
      super(context);
    }

    @Override
    public void doHandle(String target, Request baseRequest, HttpServletRequest request,
        HttpServletResponse response) throws IOException, ServletException {
      response.getWriter().print(getContextPath());
      baseRequest.setHandled(true);
    }
  }
}

So a base ContextHandler with the ContextHandlerCollection as the ContextHandler's child handler.

A request to http://localhost:8000/base/c will 404, a request to http://localhost:8000/base/a will succeed. Is this sort of chaining meant to be supported by Jetty? It would certainly be useful.

Thanks, Neil

_______________________________________________
jetty-users mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/jetty-users
Reply | Threaded
Open this post in threaded view
|

Re: ContextHandlerCollection as a child handler of ContextHandler

Jan Bartel-3
Neil,

I'm not really sure why you want to do that?

It is the ContextHandlerCollection that has the smarts to choose
amongst the best context paths that match the request. As the request
is eg  /base/a/xxxx then none of your PrintHandlers match that.

This will certainly work:
chc.setHandlers(new Handler[] {new PrintHandler("/base/a"), new
PrintHandler("/base/b"), new PrintHandler("/base/c")});

cheers
Jan

On 9 September 2014 21:46, Neil Williams <[hidden email]> wrote:

> Looking at having the following:
>
> public class ContextPlay {
>   public static void main(String[] args) throws Exception {
>     Server server = new Server(8000);
>
>     ContextHandlerCollection chc = new ContextHandlerCollection();
>     chc.setHandlers(new Handler[] {new PrintHandler("/a"), new
> PrintHandler("/b"), new PrintHandler("/c")});
>
>     ContextHandler ch = new ContextHandler("/base")
>     ch.setHandler(chc);
>
>     server.setHandler(ch);
>
>     server.start();
>     server.join();
>   }
>
>   public static class PrintHandler extends ContextHandler {
>     public PrintHandler(String context) {
>       super(context);
>     }
>
>     @Override
>     public void doHandle(String target, Request baseRequest,
> HttpServletRequest request,
>         HttpServletResponse response) throws IOException, ServletException {
>       response.getWriter().print(getContextPath());
>       baseRequest.setHandled(true);
>     }
>   }
> }
>
> So a base ContextHandler with the ContextHandlerCollection as the
> ContextHandler's child handler.
>
> A request to http://localhost:8000/base/c will 404, a request to
> http://localhost:8000/base/a will succeed. Is this sort of chaining meant to
> be supported by Jetty? It would certainly be useful.
>
> Thanks, Neil
>
> _______________________________________________
> jetty-users mailing list
> [hidden email]
> To change your delivery options, retrieve your password, or unsubscribe from
> this list, visit
> https://dev.eclipse.org/mailman/listinfo/jetty-users



--
Jan Bartel <[hidden email]>
www.webtide.com
'Expert Jetty/CometD developer,production,operations advice'
_______________________________________________
jetty-users mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/jetty-users
Reply | Threaded
Open this post in threaded view
|

Re: ContextHandlerCollection as a child handler of ContextHandler

Neil Williams
Hi Jan, thanks for the response, 

Would be useful in my particular use case to have all requests for a certain base path to have to pass through a handler, so:

    ContextHandlerCollection chc = new ContextHandlerCollection();
    chc.setHandlers(new Handler[] {new PrintHandler("/a"), new PrintHandler("/b"), new PrintHandler("/c")});

    HandlerWrapper wrapper = new HandlerWrapper();
    wrapper.setHandler(chc);

    ContextHandler ch = new ContextHandler("/base")
    ch.setHandler(wrapper);

Or perhaps, have a AbstractHandler instead of the wrapper that could reject requests before passing it onto the ContextHandlerCollection() if the request is missing required data common to many context handlers.


The reason for this is that we have a few context handlers that provide some read only internal information about the server, such as ThreadPool stats, a jetty StatisticsHandler, we don't want these to be publicly viewable but want them all to exist on a base context, something like /internal/httpstats, /internal/dbstats.  That way we can stick an authentication check on the handler after /internal/ ContextHandler but before it hits the ContextHandlerCollection. 

Cheers

On Wed, Sep 10, 2014 at 10:58 PM, Jan Bartel <[hidden email]> wrote:
Neil,

I'm not really sure why you want to do that?

It is the ContextHandlerCollection that has the smarts to choose
amongst the best context paths that match the request. As the request
is eg  /base/a/xxxx then none of your PrintHandlers match that.

This will certainly work:
chc.setHandlers(new Handler[] {new PrintHandler("/base/a"), new
PrintHandler("/base/b"), new PrintHandler("/base/c")});

cheers
Jan

On 9 September 2014 21:46, Neil Williams <[hidden email]> wrote:
> Looking at having the following:
>
> public class ContextPlay {
>   public static void main(String[] args) throws Exception {
>     Server server = new Server(8000);
>
>     ContextHandlerCollection chc = new ContextHandlerCollection();
>     chc.setHandlers(new Handler[] {new PrintHandler("/a"), new
> PrintHandler("/b"), new PrintHandler("/c")});
>
>     ContextHandler ch = new ContextHandler("/base")
>     ch.setHandler(chc);
>
>     server.setHandler(ch);
>
>     server.start();
>     server.join();
>   }
>
>   public static class PrintHandler extends ContextHandler {
>     public PrintHandler(String context) {
>       super(context);
>     }
>
>     @Override
>     public void doHandle(String target, Request baseRequest,
> HttpServletRequest request,
>         HttpServletResponse response) throws IOException, ServletException {
>       response.getWriter().print(getContextPath());
>       baseRequest.setHandled(true);
>     }
>   }
> }
>
> So a base ContextHandler with the ContextHandlerCollection as the
> ContextHandler's child handler.
>
> A request to http://localhost:8000/base/c will 404, a request to
> http://localhost:8000/base/a will succeed. Is this sort of chaining meant to
> be supported by Jetty? It would certainly be useful.
>
> Thanks, Neil
>
> _______________________________________________
> jetty-users mailing list
> [hidden email]
> To change your delivery options, retrieve your password, or unsubscribe from
> this list, visit
> https://dev.eclipse.org/mailman/listinfo/jetty-users



--
Jan Bartel <[hidden email]>
www.webtide.com
'Expert Jetty/CometD developer,production,operations advice'
_______________________________________________
jetty-users mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/jetty-users


_______________________________________________
jetty-users mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/jetty-users
Reply | Threaded
Open this post in threaded view
|

Re: ContextHandlerCollection as a child handler of ContextHandler

John English
On 12/09/2014 12:06, Neil Williams wrote:
> The reason for this is that we have a few context handlers that provide some
> read only internal information about the server, such as ThreadPool stats, a
> jetty StatisticsHandler, we don't want these to be publicly viewable but want
> them all to exist on a base context, something like /internal/httpstats,
> /internal/dbstats.  That way we can stick an authentication check on the handler
> after /internal/ ContextHandler but before it hits the ContextHandlerCollection.

Couldn't you use a filter to do this?
--
John English
_______________________________________________
jetty-users mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/jetty-users
Reply | Threaded
Open this post in threaded view
|

Re: ContextHandlerCollection as a child handler of ContextHandler

Neil Williams
Hi John, 
I don't believe Handlers have Filters like Servlets, but I suppose a basic implementation would be to put an AbstractHandler somewhere in the handler chaining and have it check the request path, then have the ContextHandlerCollection have each ContextHandler with its full path including the '/internal' bit. 

On Fri, Sep 12, 2014 at 12:10 PM, John English <[hidden email]> wrote:
On 12/09/2014 12:06, Neil Williams wrote:
The reason for this is that we have a few context handlers that provide some
read only internal information about the server, such as ThreadPool stats, a
jetty StatisticsHandler, we don't want these to be publicly viewable but want
them all to exist on a base context, something like /internal/httpstats,
/internal/dbstats.  That way we can stick an authentication check on the handler
after /internal/ ContextHandler but before it hits the ContextHandlerCollection.

Couldn't you use a filter to do this?
--
John English

_______________________________________________
jetty-users mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/jetty-users


_______________________________________________
jetty-users mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/jetty-users