Discussion:
Catch all requests under a specific path
Erik Holstad
2014-03-20 23:34:14 UTC
Permalink
Hey!

I wounder if there is a way to route all calls to any sub paths to a given
parent path.

Let's pretend we have 2 endpoints

GET /foo and
GET /foo/bar

I would like to be able to catch them both in one method and then branch
afterwards.

I know I can do this using the regular HttpServletRequest but I just want
this functionality for one specific path so don't want to change the
structure of the other servlets.
--
Regards Erik
Ted M. Young [@jitterted]
2014-03-21 03:44:49 UTC
Permalink
Hi Erik,

What would the benefit of this be? Why not just create two endpoints that
then call the same method internally? e.g.:

@Path("/foo")
public class StocksResource {
@GET // just matches /foo
@Produces(MediaType.APPLICATION_JSON)
public Response getFoo() {
return foo(false);
}

@GET
@Path("/bar") // matches /foo/bar
@Produces(MediaType.APPLICATION_JSON)
public Response getFooBar() {
return foo(true);
}

private foo(boolean hasBar) {
// do stuff here
}
}

;ted
Post by Erik Holstad
Hey!
I wounder if there is a way to route all calls to any sub paths to a given
parent path.
Let's pretend we have 2 endpoints
GET /foo and
GET /foo/bar
I would like to be able to catch them both in one method and then branch
afterwards.
I know I can do this using the regular HttpServletRequest but I just want
this functionality for one specific path so don't want to change the
structure of the other servlets.
--
Regards Erik
Erik Holstad
2014-03-24 21:02:11 UTC
Permalink
I'm planning to use this for testing purposes, so would be nice to capture
all requests instead of having to add a new endpoint every time I add a new
one to that actual code.
Post by Ted M. Young [@jitterted]
Hi Erik,
What would the benefit of this be? Why not just create two endpoints that
@Path("/foo")
public class StocksResource {
@GET // just matches /foo
@Produces(MediaType.APPLICATION_JSON)
public Response getFoo() {
return foo(false);
}
@GET
@Path("/bar") // matches /foo/bar
@Produces(MediaType.APPLICATION_JSON)
public Response getFooBar() {
return foo(true);
}
private foo(boolean hasBar) {
// do stuff here
}
}
;ted
Post by Erik Holstad
Hey!
I wounder if there is a way to route all calls to any sub paths to a
given parent path.
Let's pretend we have 2 endpoints
GET /foo and
GET /foo/bar
I would like to be able to catch them both in one method and then branch
afterwards.
I know I can do this using the regular HttpServletRequest but I just want
this functionality for one specific path so don't want to change the
structure of the other servlets.
--
Regards Erik
--
Regards Erik
Ted M. Young [@jitterted]
2014-03-24 21:28:18 UTC
Permalink
Ah. Well then I'm not sure what to suggest, other than perhaps consuming
the logging output as that will have all of the requests in it.

;ted
Post by Erik Holstad
I'm planning to use this for testing purposes, so would be nice to capture
all requests instead of having to add a new endpoint every time I add a new
one to that actual code.
Post by Ted M. Young [@jitterted]
Hi Erik,
What would the benefit of this be? Why not just create two endpoints that
@Path("/foo")
public class StocksResource {
@GET // just matches /foo
@Produces(MediaType.APPLICATION_JSON)
public Response getFoo() {
return foo(false);
}
@GET
@Path("/bar") // matches /foo/bar
@Produces(MediaType.APPLICATION_JSON)
public Response getFooBar() {
return foo(true);
}
private foo(boolean hasBar) {
// do stuff here
}
}
;ted
Post by Erik Holstad
Hey!
I wounder if there is a way to route all calls to any sub paths to a
given parent path.
Let's pretend we have 2 endpoints
GET /foo and
GET /foo/bar
I would like to be able to catch them both in one method and then branch
afterwards.
I know I can do this using the regular HttpServletRequest but I just
want this functionality for one specific path so don't want to change the
structure of the other servlets.
--
Regards Erik
--
Regards Erik
Robert DiFalco
2014-03-24 21:34:16 UTC
Permalink
Erik,

I'm not super clear on what you are trying to do here but would a
ContainerRequestFilter work for you?
Post by Ted M. Young [@jitterted]
Ah. Well then I'm not sure what to suggest, other than perhaps consuming
the logging output as that will have all of the requests in it.
;ted
Post by Erik Holstad
I'm planning to use this for testing purposes, so would be nice to
capture all requests instead of having to add a new endpoint every time I
add a new one to that actual code.
Post by Ted M. Young [@jitterted]
Hi Erik,
What would the benefit of this be? Why not just create two endpoints
@Path("/foo")
public class StocksResource {
@GET // just matches /foo
@Produces(MediaType.APPLICATION_JSON)
public Response getFoo() {
return foo(false);
}
@GET
@Path("/bar") // matches /foo/bar
@Produces(MediaType.APPLICATION_JSON)
public Response getFooBar() {
return foo(true);
}
private foo(boolean hasBar) {
// do stuff here
}
}
;ted
Post by Erik Holstad
Hey!
I wounder if there is a way to route all calls to any sub paths to a
given parent path.
Let's pretend we have 2 endpoints
GET /foo and
GET /foo/bar
I would like to be able to catch them both in one method and then
branch afterwards.
I know I can do this using the regular HttpServletRequest but I just
want this functionality for one specific path so don't want to change the
structure of the other servlets.
--
Regards Erik
--
Regards Erik
Erik Holstad
2014-03-24 21:45:06 UTC
Permalink
Basically I want to run integration tests on the "deployed" code and not
just a unit test where I don't write to the correct table in the db, but a
special test table.

The way I see it I have at least 2 options and I'm having issues with both:


1. Running regular integration tests and sending all requests to some
/test/ endpoint which forwards them to the real endpoints but also sets a
testing flag to be used only for internal use.

2. Use JerseyTest which works fine for the above use case, but is giving me
issues with bean/parameter validation(Question about this in separate email)
Post by Robert DiFalco
Erik,
I'm not super clear on what you are trying to do here but would a
ContainerRequestFilter work for you?
Post by Ted M. Young [@jitterted]
Ah. Well then I'm not sure what to suggest, other than perhaps consuming
the logging output as that will have all of the requests in it.
;ted
Post by Erik Holstad
I'm planning to use this for testing purposes, so would be nice to
capture all requests instead of having to add a new endpoint every time I
add a new one to that actual code.
Post by Ted M. Young [@jitterted]
Hi Erik,
What would the benefit of this be? Why not just create two endpoints
@Path("/foo")
public class StocksResource {
@GET // just matches /foo
@Produces(MediaType.APPLICATION_JSON)
public Response getFoo() {
return foo(false);
}
@GET
@Path("/bar") // matches /foo/bar
@Produces(MediaType.APPLICATION_JSON)
public Response getFooBar() {
return foo(true);
}
private foo(boolean hasBar) {
// do stuff here
}
}
;ted
Post by Erik Holstad
Hey!
I wounder if there is a way to route all calls to any sub paths to a
given parent path.
Let's pretend we have 2 endpoints
GET /foo and
GET /foo/bar
I would like to be able to catch them both in one method and then
branch afterwards.
I know I can do this using the regular HttpServletRequest but I just
want this functionality for one specific path so don't want to change the
structure of the other servlets.
--
Regards Erik
--
Regards Erik
--
Regards Erik
Simon Roberts
2014-03-25 03:58:47 UTC
Permalink
I'm not totally sure I know what you want, but this does what I think
you're asking for:

@Path("someting/{seg: .*}")

It will match _anything_ starting with something, no matter how many more
elements come below it (because the wildcard matches / too)

You would inject this into your method arguments:

@Context UriInfo uriInfo,

and you can extract the individual segments like this:

List<PathSegment> lps = uriInfo.getPathSegments();

HTH,
Simon
Subject: [Jersey] Re: Catch all requests under a specific path
I'm planning to use this for testing purposes, so would be nice to capture
all requests instead of having to add a new endpoint every time I add a new
one to that actual code.
--
Simon Roberts
Certified Professional Photographer
http://dancingcloudphotography.com
(303) 249 3613
Erik Holstad
2014-03-26 00:41:46 UTC
Permalink
Thanks!


On Mon, Mar 24, 2014 at 8:58 PM, Simon Roberts <
Post by Simon Roberts
I'm not totally sure I know what you want, but this does what I think
@Path("someting/{seg: .*}")
It will match _anything_ starting with something, no matter how many more
elements come below it (because the wildcard matches / too)
@Context UriInfo uriInfo,
List<PathSegment> lps = uriInfo.getPathSegments();
HTH,
Simon
Subject: [Jersey] Re: Catch all requests under a specific path
I'm planning to use this for testing purposes, so would be nice to
capture all requests instead of having to add a new endpoint every time I
add a new one to that actual code.
--
Simon Roberts
Certified Professional Photographer
http://dancingcloudphotography.com
(303) 249 3613
--
Regards Erik
Marek Potociar
2014-03-26 11:17:39 UTC
Permalink
Post by Simon Roberts
@Path("someting/{seg: .*}")
It will match _anything_ starting with something, no matter how many more elements come below it (because the wildcard matches / too)
@Context UriInfo uriInfo,
List<PathSegment> lps = uriInfo.getPathSegments();
With Jersey you can also directly inject the list of path segments:

myResourceMethod(@PathParam("seg") List<PathSegment> segments) { ... }

Marek
Post by Simon Roberts
HTH,
Simon
Subject: [Jersey] Re: Catch all requests under a specific path
I'm planning to use this for testing purposes, so would be nice to capture all requests instead of having to add a new endpoint every time I add a new one to that actual code.
--
Simon Roberts
Certified Professional Photographer
http://dancingcloudphotography.com
(303) 249 3613
Loading...