jax-rs-spec
  1. jax-rs-spec
  2. JAX_RS_SPEC-359

It would be useful to have a UriTemplateParser as part of the spec

    Details

    • Type: Improvement Improvement
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 2.1
    • Fix Version/s: ice box
    • Component/s: None
    • Labels:
      None

      Description

      UriTemplateParser is useful when generating client boilerplate as it allows a client to extra parameters from a URI that the user of the API has provided.

      At the moment I have to rely on an internal implementation that is part of Jersey making the JAX-RS 2.0 client not cross platform.

        Activity

        Hide
        gdavison added a comment -

        I wonder if this could be as simple as adding a method to UriBuilder:

        public boolean match(URI uri, Map<String,String> parameters);

        This would greatly reduce the API impact of this request.

        Show
        gdavison added a comment - I wonder if this could be as simple as adding a method to UriBuilder: public boolean match(URI uri, Map<String,String> parameters); This would greatly reduce the API impact of this request.
        Hide
        Marek Potociar added a comment -

        Not sure what the method above is supposed to do. Can you please elaborate?

        Show
        Marek Potociar added a comment - Not sure what the method above is supposed to do. Can you please elaborate?
        Hide
        gdavison added a comment -

        Sure, sorry the original bug was poorly written.

        So the problem I am trying to deal with when generating a JAX-RS 2.0 client is that the user would like the client to be able to take in a URI:

        http://example.com/name/Bob/age/41

        And to understand and extract the parameters so they can be reset, so assuming the template looks like this:

        http://example.com/name/

        {name}/age/{age}

        So ideally the code would look like this:

        String template = "http://example.com/name/{name}

        /age/

        {age}

        ";
        UriBuilder builder = UriBuilder.fromPath(template);
        Map<String,String> parameters = new HashMap<>();
        builder.match(uri, parameters);

        Then you can change a parameter:

        parameters.put("name", "Arnold");
        builder.build(parameters);

        Which would give the URI:

        http://example.com/name/Arnold/age/41

        The match method returns a boolean so it would return false if the URI didn't match what was being provided. I hope this is a lot clearer as to my intent.

        Show
        gdavison added a comment - Sure, sorry the original bug was poorly written. So the problem I am trying to deal with when generating a JAX-RS 2.0 client is that the user would like the client to be able to take in a URI: http://example.com/name/Bob/age/41 And to understand and extract the parameters so they can be reset, so assuming the template looks like this: http://example.com/name/ {name}/age/{age} So ideally the code would look like this: String template = "http://example.com/name/{name} /age/ {age} "; UriBuilder builder = UriBuilder.fromPath(template); Map<String,String> parameters = new HashMap<>(); builder.match(uri, parameters); Then you can change a parameter: parameters.put("name", "Arnold"); builder.build(parameters); Which would give the URI: http://example.com/name/Arnold/age/41 The match method returns a boolean so it would return false if the URI didn't match what was being provided. I hope this is a lot clearer as to my intent.
        Hide
        Marek Potociar added a comment -

        Ok, so what you are looking for is perhaps a method like:

        Map<String, String> params = builder.parametersOf(uri);
        

        Correct? Or do you prefer your version where you have to create the Map instance? (If yes, then why?)

        FWIW, I'm releasing PFD today, but let me bring this up to EG - maybe we can add this before final release if EG agrees.

        Show
        Marek Potociar added a comment - Ok, so what you are looking for is perhaps a method like: Map< String , String > params = builder.parametersOf(uri); Correct? Or do you prefer your version where you have to create the Map instance? (If yes, then why?) FWIW, I'm releasing PFD today, but let me bring this up to EG - maybe we can add this before final release if EG agrees.
        Hide
        gdavison added a comment -

        I think the reason behind having to pass the parameters in is that it allow the method to still return a boolean parameter which tells you if the URI matches at all. I guess the method could throw an exception in this case instead - don't have a suggestion of a suitable one though.

        Also the conservative part of me always like to be able to pass in a Map just in case I want to be able to reuse an instance of an object. (I spend many a happy hour dealing with the fact that early versions of AWT would always create a Rectangle to get hold of size rather than allows you to pass one in as that did for Graphics2D)

        It would be useful to have this as the JAX-RS 2.0 client generated for the moment is depending on the jersey UriTemplate class and I would like to have it removed of course.

        Show
        gdavison added a comment - I think the reason behind having to pass the parameters in is that it allow the method to still return a boolean parameter which tells you if the URI matches at all. I guess the method could throw an exception in this case instead - don't have a suggestion of a suitable one though. Also the conservative part of me always like to be able to pass in a Map just in case I want to be able to reuse an instance of an object. (I spend many a happy hour dealing with the fact that early versions of AWT would always create a Rectangle to get hold of size rather than allows you to pass one in as that did for Graphics2D) It would be useful to have this as the JAX-RS 2.0 client generated for the moment is depending on the jersey UriTemplate class and I would like to have it removed of course.
        Hide
        Marek Potociar added a comment -

        Thinking more about it, I'd really like to have a solution that does not mixes up a builder components with URI template matching ones...

        So the proposed solution may look like:

        • introduce UriTemplate to represent, well, URI templates...
        • add ability to build UriTemplate instances from UriBuilder
        • expose the boolean UriTemplate.match(URI, Map<String, String>) method as part of the UriTemplate API.
        Show
        Marek Potociar added a comment - Thinking more about it, I'd really like to have a solution that does not mixes up a builder components with URI template matching ones... So the proposed solution may look like: introduce UriTemplate to represent, well, URI templates... add ability to build UriTemplate instances from UriBuilder expose the boolean UriTemplate.match(URI, Map<String, String>) method as part of the UriTemplate API.
        Hide
        Marek Potociar added a comment -

        The proposal was discussed in EG and EG decided that it's too late to include this in 2.0 release unfortunately. We need to defer the issue for now.

        Show
        Marek Potociar added a comment - The proposal was discussed in EG and EG decided that it's too late to include this in 2.0 release unfortunately. We need to defer the issue for now.

          People

          • Assignee:
            Unassigned
            Reporter:
            gdavison
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated: