users@jersey.java.net

[Jersey] Re: How to get matched path in jersey 1.0

From: Magesh STM <magesh_stm_at_yahoo.com>
Date: Thu, 14 Aug 2014 11:49:17 -0700

Thanks very much Mira.

I could make this work with ContainerResponseFilter as follows:
public class TestContainerResponseFilter implements ContainerResponseFilter {
    @Context UriInfo uriInfo;

    @Override
    public ContainerResponse filter(ContainerRequest containerRequest, ContainerResponse containerResponse) {
        System.out.println("uriInfo.getMatchedURIs():" + uriInfo.getMatchedURIs());
        System.out.println("uriInfo.getPathParameters():" + uriInfo.getPathParameters());
        
        return containerResponse;
    }
}

(In ContainerRequestFilter, with the same kind of injection, uriInfo.getMatchedURIs() is empty.)

I have a related question. Please help.
Below is a corner case scenario, but if there is a solution my code will be clean.

Consider:
@Path("hello")
public class SayHello {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    @Path("/from/{fromName}/to/{toName}")
    public String sayPlainTextHello(@PathParam("fromName") String fromName, @PathParam("toName") String toName) {
      return "Hello " + toName + " - " + fromName;
    }
}

When I access "/hello/from/John/to/John", below outputs get printed:
uriInfo.getMatchedURIs():[hello/from/John/to/John, hello]
uriInfo.getPathParameters():{toName=[John], fromName=[John]}

Now my question is:
How can I determine if the url is "/hello/from/{fromName}/to/{toName} or /hello/from/{toName}/to/{fromName}" ?
Can I rely on the order of uriInfo.getPathParameters() ?

Thanks,
Magesh


On Thursday, 14 August 2014 3:01 PM, Miroslav Fuksa <miroslav.fuksa_at_oracle.com> wrote:
 


Hi,

I wrote an answer for Jersey 2.x and then I notices you have in the email subject “jersey 1.0”. So, this is the answer for Jersey 2.x:

for such a purpose like a logging I would use Monitoring support in Jersey [1]. You can register the ApplicationEventListener which would return an implementation of RequestEventListener from onRequest() method. Your RequestEventListener implementation will log on event type RESOURCE_METHOD_START. You can get the UriInfo by RequestEvent.getUriInfo(). UriInfo contains information about path parameters.

Another option is to use ContainerRequestFilter (but it must NOT be @Prematching filter). There you can access UriInfo too (ContainerRequestContext.getUriInfo()). If you need to log only this case then the filter is fine. If you decide to log more events (like exception mappers, responses) the monitoring support described above is more suitable I would say (you can hook to many places of request processing)

For Jersey 1.x you can use the solution with ContainerRequestFilter.

I hope this helps.
Mira


[1]: jersey monitoring documentation https://jersey.java.net/documentation/latest/monitoring_tracing.html#monitoring



On Aug 14, 2014, at 7:38 AM, Magesh STM <magesh_stm_at_yahoo.com> wrote:

I have this following question. Please help.
>
>
>Please refer following code. /hello/from/JohnDoe will hit the method sayPlainTextHello.
>
>
>When "/hello/from/JohnDoe" is accessed, I want to store the matched path which is /hello/from/{name} in a log. Please note that I can't modify below code but can add filter, etc. to the app. How to get the matched path "/hello/from/{name}" ?
>
>
>    @Path("hello")
>    public class SayHello {
>
>
>        @GET
>        @Produces(MediaType.TEXT_PLAIN)
>        @Path("/from/{name}")
>        public String sayPlainTextHello(@PathParam("name") String fromName) {
>            return "Hello Jersey - " + fromName;
>        }
>    }