users@jersey.java.net

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

From: geoffrey hendrey <geoff_at_nextdb.net>
Date: Tue, 9 Feb 2010 20:14:35 -0800

Hi Paul,

Thanks for your response. I am presently using a regex to identify the
portions of the URL I want to replace. If I don't find the match already in
the URL then I create the path segment and append it to the URL. The regex
needs to be a bit pickier than the one you provided, but thanks for
providing one anyway. The regex I am using is ("/page/([^/#;\\?]+) because
I do not want to blow away matrix parmeters or query parameters. I just
replace the content of the capturing group when I find a match.

"replace", for my needs, means replaceAll, though I think ideally one would
have the option to replace the first, last, or all.

I am logging the issue. Thanks again for your response, and for all you are
doing for Jersey. It's really an amazing product.

-geoff



>>>
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";
        System.out.println(s);

        s = x(s, "/color/[^/]+", "/color/BLUE");
        System.out.println(s);

        s = x(s, "/height/[^/]+", "/height/2");
        System.out.println(s);
    }

    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 building.

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.

Paul.

[1] https://jsr311.dev.java.net/servlets/ProjectIssues



-- 
http://nextdb.net - RESTful Relational Database
http://www.nextdb.net/wiki/en/REST