A standalone Java implementation for retrieving values โ€‹โ€‹in a URI pattern (RFC 6570)?

Is there a standalone Java implementation for retrieving parameter values โ€‹โ€‹in a URI as defined by a URI template (RFC 6570)?

The best implementation I've found is the ruby โ€‹โ€‹implementation ( https://github.com/sporkmonger/addressable )

Through http://code.google.com/p/uri-templates/wiki/Implementations I found a Java implementation: Handy-URI-Templates

It supports resolving a URI template with parameter values โ€‹โ€‹up to the final URI. Unfortunately, he cannot do the opposite: retrieving parameter values โ€‹โ€‹in a URI according to the URI-Template.

Implentations JAX-RS (or Restlet) has this feature inside. But not one of them isolated this functional module, which could be used independently.

Does anyone have any other idea?


Here is an example using spring-web:

import org.springframework.web.util.UriTemplate; public class UriParserSpringImpl implements UriParser { private final UriTemplate uriTemplate; private final String uriTemplateStr; public UriParserSpringImpl(final String template) { this.uriTemplateStr = template; this.uriTemplate = new UriTemplate(template); } @Override public Map<String, String> parse(final String uri) { final boolean match = this.uriTemplate.matches(uri); if (!match) { return null; } return uriUtils.decodeParams(this.uriTemplate.match(uri)); } @Override public Set<String> getVariables() { return Collections.unmodifiableSet(new LinkedHashSet<String>(this.uriTemplate.getVariableNames())); } } 

Another for Jersey (JAX-RS implementation):

 import com.sun.jersey.api.uri.UriTemplate; public class UriParserJerseyImpl implements UriParser { private final UriTemplate uriTemplate; private final Map<String, String> valuesMaps; public UriParserJerseyImpl(final String template) { this.uriTemplate = new UriTemplate(template); final Map<String, String> valuesMaps = new HashMap<String, String>(); for (final String prop : this.uriTemplate.getTemplateVariables()) { valuesMaps.put(prop, null); } this.valuesMaps = Collections.unmodifiableMap(valuesMaps); } @Override public Map<String, String> parse(final String uri) { final Map<String, String> values = new HashMap<String, String>(this.valuesMaps); final boolean match = this.uriTemplate.match(uri, values); if (!match) { return null; } return values; } @Override public Set<String> getVariables() { return this.valuesMaps.keySet(); } } 

With interface:

 public interface UriParser { public Set<String> getVariables(); public Map<String, String> parse(final String uri); } 
+7
source share
2 answers

The black uri template library has an open problem for this feature. I already got a PR for this feature, and it should be in version 2.2! Head there and let the parties know what interests you.

Also, if you cannot wait, you can see how I did it here and use it for yourself.

0
source

java.net.URI

It is not possible to set parameters after creating it, but it has a good set of getters, and you can create a new one to change it.

-5
source

All Articles