users@jersey.java.net

how to generate URLs

From: Geoff hendrey <geoff_hendrey_at_yahoo.com>
Date: Thu, 24 Sep 2009 19:39:33 -0700 (PDT)

Let's say I have a resource at relative path /book/1/chapter/2, or more generally, /book/{bookNo}/chapter/{chapterNo}.

I've been looking for a generic way to generate a list of links. I've made up a fake method called "replaceOrAppendPathParameter" to try to convey what I want to do. The URIInfo passed into the createLinks method might be for any of the following:

/
/book
/book/9
/book/2/chapter/3

I want to be able to pass the UriInfo for any of the path's above into the method below to generate a list of links that includes both the bookNo and the chapterNo path parameters.

List<URI> createLinks(UriInfo uriInfo){
    List<URI> links = new ArrayList<URI>();
    for(int i=0;i<NUM_BOOKS;i++){
       for(int j=0;j<NUM_CHAPTERS;j++){
            URI link = uriInfo.getAbsolutePathBuilder().replaceOrAppendPathParameter("bookNo", i).replaceOrAppendPathParameter("chapterNo", j).build();
            links.add(link);
        }
    }
    return links;
}

Can anyone help me to accomplish this? I don't want to manually feed "/book/{bookNo}/chapter/{chapterNo}" into the UriBuilder as that defeats the purpose of having a single generalized method capable of generating simple numbered links.

 -geoff