10

Is there a Java standalone implementation to extract values ​​of parameters in an URI as defined by an URI-Template (RFC 6570)?

The best implementation I've found is a ruby implementation ( https://github.com/sporkmonger/addressable )

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

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.

Implentations of the JAX-RS (or Restlet) have this feature internally. But none seems to have isolated this feature module which could used independently.

Does anyone have another idea?


Here a example to Use 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);
}
JYC
  • 343
  • 3
  • 8
  • Have you ever found a solution? I am looking for exactly the same but it looks like I have to bring my own implementation... – Dominik Obermaier Jan 28 '13 at 13:53
  • You could possibly look at the Google http client, which has similar functionality. See https://code.google.com/p/google-http-java-client/ – sappenin Dec 22 '13 at 15:03

2 Answers2

1

The damnhandy uri template library has an open issue for exactly this feature. I've already gotten the PR for the feature merged and it should be out in version 2.2! Head over there and let the maintainers know you're interested.

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

kag0
  • 5,624
  • 7
  • 34
  • 67
-5

java.net.URI

Can't set the parameters after it's instantiated, but it has a nice set of getters and you can contruct a new one to alter it.

squishy
  • 61
  • 5
  • This has nothing to do with URI Templates as defined by https://tools.ietf.org/html/rfc6570 – deamon Sep 03 '15 at 18:09