title: Standalone Java Implementation for extracting values in URI Template (RFC 6570)?
body: <p>Is there a Java standalone implementation to extract values ​​of parameters in an URI as defined by an URI-Template (RFC 6570)?</p>
  
  <p>The best implementation I've found is a ruby implementation ( <a href="https://github.com/sporkmonger/addressable" rel="noreferrer">https://github.com/sporkmonger/addressable</a> )</p>
  
  <p>Via <a href="http://code.google.com/p/uri-templates/wiki/Implementations" rel="noreferrer">http://code.google.com/p/uri-templates/wiki/Implementations</a> I found a Java implementation: Handy-URI-Templates</p>
  
  <p>It supports the resolution of an URI-Template with parameter values to a final URI. Unfortunately, it can not do the reverse: extraction of parameter values ​​in the URI according URI-Template.</p>
  
  <p>Implentations of the JAX-RS (or Restlet) have this feature internally.
  But none seems to have isolated this feature module which could used independently.</p>
  
  <p>Does anyone have another idea?</p>
  
  <hr>
  
  <p>Here a example to Use spring-Web :</p>
  
  
  
  <pre class="lang-java prettyprint-override"><code>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&lt;String, String&gt; 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&lt;String&gt; getVariables() {
      return Collections.unmodifiableSet(new LinkedHashSet&lt;String&gt;(this.uriTemplate.getVariableNames()));
    }
  }
  </code></pre>
  
  <p>Another for Jersey (JAX-RS implementation) :</p>
  
  <pre class="lang-java prettyprint-override"><code>import com.sun.jersey.api.uri.UriTemplate;
  public class UriParserJerseyImpl implements UriParser {
  
    private final UriTemplate uriTemplate;
  
    private final Map&lt;String, String&gt; valuesMaps;
  
    public UriParserJerseyImpl(final String template) {
      this.uriTemplate = new UriTemplate(template);
      final Map&lt;String, String&gt; valuesMaps = new HashMap&lt;String, String&gt;();
      for (final String prop : this.uriTemplate.getTemplateVariables()) {
        valuesMaps.put(prop, null);
      }
      this.valuesMaps = Collections.unmodifiableMap(valuesMaps);
    }
  
    @Override
    public Map&lt;String, String&gt; parse(final String uri) {
      final Map&lt;String, String&gt; values = new HashMap&lt;String, String&gt;(this.valuesMaps);
      final boolean match = this.uriTemplate.match(uri, values);
      if (!match) {
        return null;
      }
      return values;
    }
  
    @Override
    public Set&lt;String&gt; getVariables() {
      return this.valuesMaps.keySet();
    }
  }
  </code></pre>
  
  <p>With interface :</p>
  
  <pre class="lang-java prettyprint-override"><code>public interface UriParser {
    public Set&lt;String&gt; getVariables();
    public Map&lt;String, String&gt; parse(final String uri);
  }
  </code></pre>