Use PathMappings to route servlet requests

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

Use PathMappings to route servlet requests

Robert Stepanek
Since this is my first post: many thanks to all the Jetty developers!

Now to my question: I'm reusing the PathMappings and UriTemplatePathSpec classes of the jetty-http pathmap package for URL routing within my servlets [1]. I don't need a full-blown REST framework, and these classes cover all my requirements. I've written small glue code that inspects the pathInfo of the servlet request and uses a PathMappings class to route the request to the respective handler method within my servlet, passing the UriTemplatePathSpec variables as arguments.

I wonder if there is a more canonical way planned to do this? E.g. is there a plan for Jetty to use the PathMappings/PathSpec classes for registering handlers or servlets/filter? I don't see any indication of this on the jetty-10.0.x branch, nor do I see that the Servlet 4.0 specification has made path specs any smarter. I did however notice that the PathMap class will be removed in Jetty 10 in favour of PathMappings, so at least it seems to safe to build on the new PathMappings classes?

Thanks,
Cheers,
Robert

[1] https://github.com/eclipse/jetty.project/tree/jetty-9.4.x/jetty-http/src/main/java/org/eclipse/jetty/http/pathmap
_______________________________________________
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: Use PathMappings to route servlet requests

Joakim Erdfelt-8
The org.eclipse.jetty.http.pathmap  package was originally created to support the JSR356 (javax.websocket) server behaviors for the URI Template requirement.
But over time, it was moved out of the org.eclipse.jetty.websocket package space and into org.eclipse.jetty.http to enable similar support in the default Servlet mappings.

Only the ServletPathSpec is supported if you have a WebAppContext and a traditional deployment.
There are no plans to expose this via the existing APIs on WebAppContext and ServletContextHandler for .addServlet() and .addFilter().
The path mappings in ServletHandler are not currently expose for embedded jetty users.
If you want this exposed, open an issue at https://github.com/eclipse/jetty.project/issues asking for it.

Note: There is some debate internally within Jetty developers that the search/lookup order of the PathMappings matching logic hasn't been rigorously tested enough (its a touchy area of the Servlet spec).
If you suspect any such behavioral concerns in your usage, let us know!

In the JSR356 (javax.websocket) implementation we use the UriTemplatePathSpec like this ...
  1. The WebSocketUpgradeFilter is servlet mapped to "/*"
  2. The WebSocketUpgradeFilter performs its own PathMappings lookup based on the request pathInfo and prior mapped WebSocket endpoints.
  3. The PathSpec that matched is added to the request.setAttribute(PathSpec.class.getName(), pathSpecMatched) so that other deeper request processing can use it, if needed.
  4. The JsrCreator pulls the PathSpec request attribute always.
  5. If the PathSpec is UriTemplatePathSpec, then it will grab the path parameters out via Map<String, String> pathMap = pathSpec.getPathParams(requestPath);



Joakim Erdfelt / [hidden email]

On Tue, Feb 6, 2018 at 3:27 AM, Robert Stepanek <[hidden email]> wrote:
Since this is my first post: many thanks to all the Jetty developers!

Now to my question: I'm reusing the PathMappings and UriTemplatePathSpec classes of the jetty-http pathmap package for URL routing within my servlets [1]. I don't need a full-blown REST framework, and these classes cover all my requirements. I've written small glue code that inspects the pathInfo of the servlet request and uses a PathMappings class to route the request to the respective handler method within my servlet, passing the UriTemplatePathSpec variables as arguments.

I wonder if there is a more canonical way planned to do this? E.g. is there a plan for Jetty to use the PathMappings/PathSpec classes for registering handlers or servlets/filter? I don't see any indication of this on the jetty-10.0.x branch, nor do I see that the Servlet 4.0 specification has made path specs any smarter. I did however notice that the PathMap class will be removed in Jetty 10 in favour of PathMappings, so at least it seems to safe to build on the new PathMappings classes?

Thanks,
Cheers,
Robert

[1] https://github.com/eclipse/jetty.project/tree/jetty-9.4.x/jetty-http/src/main/java/org/eclipse/jetty/http/pathmap
_______________________________________________
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: Use PathMappings to route servlet requests

Robert Stepanek
Thanks for the thorough answer, Joakim.

On Tue, Feb 6, 2018, at 16:06, Joakim Erdfelt wrote:
The path mappings in ServletHandler are not currently expose for embedded jetty users.
If you want this exposed, open an issue at https://github.com/eclipse/jetty.project/issues asking for it.
Note: There is some debate internally within Jetty developers that the search/lookup order of the PathMappings matching logic hasn't been rigorously tested enough (its a touchy area of the Servlet spec).
If you suspect any such behavioral concerns in your usage, let us know!

I won't open an issue, I can imagine the path spec matching to be tricky enough with the Servlet spec as-is. Should I encounter a problem while using the pathmap package, I'll surely will send an issue or pull request upstream.

In the JSR356 (javax.websocket) implementation we use the UriTemplatePathSpec like this ...
  1. The WebSocketUpgradeFilter is servlet mapped to "/*
[...]
  1. If the PathSpec is UriTemplatePathSpec, then it will grab the path parameters out via Map<String, String> pathMap = pathSpec.getPathParams(requestPath);

That pretty much sums up also what I'm doing internally in my servlets with the UriTemplatePathSpec.

Thanks again,
Robert

_______________________________________________
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