Re: [Jersey] Re:Re: [Jersey] can we get a UriBuilder.addOrReplaceSegment method? Tuesday, February 2, 2010 11:51 PM

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 08 Feb 2010 12:17:42 +0100

Hi Geoff,

One approach is to use a regex to replace the bits you want. You can
substitute {foo} for the regular expression [^/]+, for example:

     public static void main(String[] args) throws Exception {
         String s = "/color/red/width/10/height/5";

         s = x(s, "/color/[^/]+", "/color/BLUE");

         s = x(s, "/height/[^/]+", "/height/2");

     public static String x(String s, String regex, String value) {
         if (s.matches(".*" + regex + ".*")) {
             s = s.replaceFirst(regex, value);
         } else {
             s = s + value;
         return s;

For your case i am not actually sure UriBuilder is the right
abstraction and what you require is a higher-level abstraction that
captures the particulars of the URI structure you require. That higher-
level abstraction could well be built on top of UriBuilder.

Further more the concept of replace or add in URI building is
problematic. There is no concept path segment positioning when URI

What does "replace" mean? replace the first, last or all matches?

What does "add" mean, add at the end, in the middle or at some
particular location?

Could you please log an issue against JSR 311 [1] so we can take this
into account for any JAX-RS 2.0 effort.



On Feb 5, 2010, at 3:45 AM, geoffrey hendrey wrote:

> Hi Craig,
> I don't want a new view of the same resource. For example, consider
> a URI like "/page/5". The resource for page 5 wants to be able to
> create links to /page/4 and /page/6, it is extremely useful to be
> able to say addOrReplaceSegment("/page/{page}", "6"). I can even
> envision a syntax like: addOrReplaceSegment("/page/{page}", "${page}
> +1") but that's above and beyond what I need.
> I hope I won't see a response saying "page numbers should be query
> parameters" since at that point the issue drifts toward the
> religious rather than the practical. There are many reasons, such as
> availability of matrix parameters to the path segments, or behaviors
> of web caches, that make path parameters preferable in some
> situations. I just want an easy way to take a resource's URI and
> mutate its path parameters so that I can easily create links to
> similar resource. So far, virtually everything else is a dream with
> Jersey and JSR-311, but this is one place where I think it could be
> improved.
> Cheers,
> geoff
> --
> - RESTful Relational Database