Standalone Java Implementation for extracting values in URI Template (RFC 6570)?
Asked Answered
G

2

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);
}
Guru answered 18/11, 2012 at 23:48 Comment(2)
Have you ever found a solution? I am looking for exactly the same but it looks like I have to bring my own implementation...Regicide
You could possibly look at the Google http client, which has similar functionality. See code.google.com/p/google-http-java-clientInelegancy
I
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.

Ingrown answered 17/12, 2016 at 20:23 Comment(0)
M
-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.

Mu answered 3/4, 2015 at 15:5 Comment(1)
This has nothing to do with URI Templates as defined by tools.ietf.org/html/rfc6570Attar

© 2022 - 2024 — McMap. All rights reserved.