Patan
Patan

Reputation: 17873

Is it possible to get the absolute request url without path parameters

I am trying to get the request url without values of path parameters into it.

Consider my complete url is

URl: http://localhost:8080/aaa/mock/abcd/1234/true
Path parameters: abcd, true
Output needed: /aaa/mock/abcd

My web service method looks like this.

@Path(value = "/aaa/mock")
@Component
public class MockService
{
    private static Log log = LogFactory.getLog(MockService.class);


    //address
    @GET
    @Path(value = "/{mockrequest}/{status}")
    @Produces(MediaType.JSON)
    public String mockEngagement(@Context ContainerRequestContext request,@PathParam("mockrequest") String mockrequest,@PathParam("status") String status )
    {
        log.info("The mock url is"+request.getUriInfo().getRequestUri());  
        log.info("The mock url is"+request.getUriInfo().getAbsolutePath()); 
        log.info("The mock url is"+request.getUriInfo().getBaseUri()); 
        log.info("The mock url is"+request.getUriInfo().getMatchedURIs()); 
        **//Out put needed /aaa/mock/abcd**
        return "ajaja";
    }


}

None of the above calls return the required info.

I am thinking if there is a generic process to get the desired output irrespective of number of path parameters.

Any such methods.

Upvotes: 5

Views: 10795

Answers (3)

Ajay
Ajay

Reputation: 1

Try this:

request.getUriInfo().getPathSegments().get(0).getPath()

Upvotes: 0

Bacem W.
Bacem W.

Reputation: 102

public void filter(ContainerRequestContext context) throws IOException {


    Message message = PhaseInterceptorChain.getCurrentMessage();

    Set<Map.Entry<String, Object>> o = (Set<Map.Entry<String, Object>>)message.entrySet(); 

    for (Map.Entry<String, Object> oo : o) { 
        String key = oo.getKey(); 
        Object val = oo.getValue();

        // Thises two properties gives the path of web service 
        //path_to_match_slash
        //org.apache.cxf.request.uri
        if(key.equals("path_to_match_slash"))
        {   String v = (String)val;
            System.out.println (key);
            System.out.println (v);

        }
        if(key.equals("org.apache.cxf.request.uri"))
        {   String v = (String)val;
            System.out.println (key);
            System.out.println (v);
        }

    }
}

this code could work only for apache cxf rest we can found path_to_match_slash , org.apache.cxf.request.uri properties in the ContainerRequestContext

Upvotes: -3

Eric Stein
Eric Stein

Reputation: 13672

Try UriInfo#getPath(), UriInfo#getPath(boolean), or UriInfo#getPathSegments(). The boolean argument is whether the path should be encoded or not.

https://jersey.java.net/apidocs/2.3.1/jersey/index.html

You could also get the absolute path and the base path and then use URI#relativize(URI).

Upvotes: 4

Related Questions