jakartaee / rest

Jakarta RESTful Web Services
Other
361 stars 117 forks source link

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

Open glassfishrobot opened 11 years ago

glassfishrobot commented 11 years ago

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.

Affected Versions

[2.1]

glassfishrobot commented 6 years ago
glassfishrobot commented 11 years ago

@glassfishrobot Commented Reported by gdavison

glassfishrobot commented 11 years ago

@glassfishrobot Commented gdavison said: 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.

glassfishrobot commented 11 years ago

@glassfishrobot Commented @mpotociar said: Not sure what the method above is supposed to do. Can you please elaborate?

glassfishrobot commented 11 years ago

@glassfishrobot Commented gdavison said: 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.

glassfishrobot commented 11 years ago

@glassfishrobot Commented @mpotociar said: 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.

glassfishrobot commented 11 years ago

@glassfishrobot Commented gdavison said: 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.

glassfishrobot commented 11 years ago

@glassfishrobot Commented @mpotociar said: 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:

glassfishrobot commented 11 years ago

@glassfishrobot Commented @mpotociar said: 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.

glassfishrobot commented 7 years ago

@glassfishrobot Commented This issue was imported from java.net JIRA JAX_RS_SPEC-359