@Override public String toString() { return partialUriTemplateComponents.toString(); }
@Override public URI toUri() { PartialUriTemplate partialUriTemplate = new PartialUriTemplate(partialUriTemplateComponents.toString()); final String actionLink = partialUriTemplate.stripOptionalVariables(actionDescriptors) .toString(); if (actionLink == null || actionLink.contains("{")) { throw new IllegalStateException("cannot convert template to URI"); } return UriComponentsBuilder.fromUriString(actionLink) .build() .toUri(); }
@Override public URI toUri() { PartialUriTemplate partialUriTemplate = new PartialUriTemplate(partialUriTemplateComponents.toString()); final String actionLink = partialUriTemplate.stripOptionalVariables(actionDescriptors) .toString(); if (actionLink == null || actionLink.contains("{")) { throw new IllegalStateException("cannot convert template to URI"); } return UriComponentsBuilder.fromUriString(actionLink) .build() .toUri(); }
/** * Expands template variables as far as possible, unsatisfied variables will remain variables. This is primarily for * manually created affordances. If the Affordance has been created with linkTo-methodOn, it should not be necessary * to expand the affordance again. * * @param arguments for expansion, in the order they appear in the template * @return partially expanded affordance */ public Affordance expandPartially(Object... arguments) { return new Affordance(partialUriTemplate.expand(arguments) .toString(), linkParams, actionDescriptors); }
/** * Expands template variables as far as possible, unsatisfied variables will remain variables. This is primarily for * manually created affordances. If the Affordance has been created with linkTo-methodOn, it should not be necessary * to expand the affordance again. * * @param arguments for expansion * @return partially expanded affordance */ public Affordance expandPartially(Map<String, ? extends Object> arguments) { return new Affordance(partialUriTemplate.expand((Map<String, Object>) arguments) .toString(), linkParams, actionDescriptors); }
/** * Expands template variables as far as possible, unsatisfied variables will remain variables. This is primarily for * manually created affordances. If the Affordance has been created with linkTo-methodOn, it should not be necessary * to expand the affordance again. * * @param arguments for expansion, in the order they appear in the template * @return partially expanded affordance */ public Affordance expandPartially(Object... arguments) { return new Affordance(partialUriTemplate.expand(arguments) .toString(), linkParams, actionDescriptors); }
@Test public void testExpandAllComponentsButFragmentIdentifier() throws Exception { final PartialUriTemplate template = new PartialUriTemplate("http://example.com/events{/city}{?eventName," + "location}{#section}"); Map<String, Object> val = new HashMap<String, Object>(); val.put("city", "Wiesbaden"); val.put("eventName", "Revo Tour"); val.put("location", "Schlachthof"); final PartialUriTemplateComponents expanded = template.expand(val); Assert.assertEquals("http://example.com/events/Wiesbaden?eventName=Revo+Tour&location=Schlachthof{#section}", expanded.toString()); }
@Test public void testExpandSegmentVariable() throws Exception { final PartialUriTemplate template = new PartialUriTemplate("http://example" + ".com/events/{city}/concerts{?eventName,location}"); Map<String, Object> val = new HashMap<String, Object>(); val.put("city", "Wiesbaden"); val.put("location", "Schlachthof"); final PartialUriTemplateComponents expanded = template.expand(val); Assert.assertEquals("http://example.com/events/Wiesbaden/concerts?location=Schlachthof{&eventName}", expanded .toString()); }
@Test public void testExpandQueryContinuationTemplatesAfterFixedQueryContinuation() throws Exception { final PartialUriTemplate template = new PartialUriTemplate("http://example" + ".com/events{/city}/concerts?eventName=Revo+Tour&foo=bar{&location,baz}"); Map<String, Object> val = new HashMap<String, Object>(); val.put("baz", "Gnarf"); val.put("location", "Schlachthof"); final PartialUriTemplateComponents expanded = template.expand(val); Assert.assertEquals("http://example" + ".com/events{/city}/concerts?eventName=Revo+Tour&foo=bar&location=Schlachthof&baz=Gnarf", expanded .toString()); }
@Test public void testToStringWithQueryVariables() throws Exception { PartialUriTemplateComponents partialUriTemplateComponents = new PartialUriTemplateComponents("http://example" + ".com", "", "baz,gnarf", "#fragmentIdentifier", Arrays.asList("baz", "gnarf")); Assert.assertEquals("http://example.com{?baz,gnarf}#fragmentIdentifier", partialUriTemplateComponents.toString()); } }
@Test public void testToStringWithQueryContinuation() throws Exception { PartialUriTemplateComponents partialUriTemplateComponents = new PartialUriTemplateComponents("http://example" + ".com", "?foo=bar", "baz,gnarf", "#fragmentIdentifier", Arrays.asList("baz", "gnarf")); Assert.assertEquals("http://example.com?foo=bar{&baz,gnarf}#fragmentIdentifier", partialUriTemplateComponents .toString()); }
@Test public void testExpandLevelOnePathSegment() throws Exception { final PartialUriTemplate template = new PartialUriTemplate("http://example.com/events/{city}"); Map<String, Object> val = new HashMap<String, Object>(); val.put("city", "Wiesbaden"); final PartialUriTemplateComponents expanded = template.expand(val); Assert.assertEquals("http://example.com/events/Wiesbaden", expanded.toString()); }
@Test public void testExpandLevelOnePathSegmentWithRegex() throws Exception { final PartialUriTemplate template = new PartialUriTemplate("http://example.com/events/{city:+}"); Map<String, Object> val = new HashMap<String, Object>(); val.put("city", "Wiesbaden"); final PartialUriTemplateComponents expanded = template.expand(val); Assert.assertEquals("http://example.com/events/Wiesbaden", expanded.toString()); }
@Test public void testExpandOneOfTwoQueryVariables() throws Exception { final PartialUriTemplate template = new PartialUriTemplate("http://example" + ".com/events{/city}/concerts{?eventName,location}"); Map<String, Object> val = new HashMap<String, Object>(); val.put("location", "Schlachthof"); final PartialUriTemplateComponents expanded = template.expand(val); Assert.assertEquals("http://example.com/events{/city}/concerts?location=Schlachthof{&eventName}", expanded .toString()); }
@Test public void testExpandLevelOnePathSegmentWithPrefix() throws Exception { final PartialUriTemplate template = new PartialUriTemplate("http://example.com/events/v{version}/Wiesbaden"); Map<String, Object> val = new HashMap<String, Object>(); val.put("version", "1.2.0"); final PartialUriTemplateComponents expanded = template.expand(val); Assert.assertEquals("http://example.com/events/v1.2.0/Wiesbaden", expanded.toString()); }
@Test public void testExpandQueryContinuationTemplate() throws Exception { final PartialUriTemplate template = new PartialUriTemplate("http://example" + ".com/events{/city}/concerts?eventName=Revo+Tour{&location}"); Map<String, Object> val = new HashMap<String, Object>(); val.put("location", "Schlachthof"); final PartialUriTemplateComponents expanded = template.expand(val); Assert.assertEquals("http://example.com/events{/city}/concerts?eventName=Revo+Tour&location=Schlachthof", expanded.toString()); }
@Test public void testExpandQueryContinuationTemplateAfterFixedQueryContinuation() throws Exception { final PartialUriTemplate template = new PartialUriTemplate("http://example" + ".com/events{/city}/concerts?eventName=Revo+Tour&foo=bar{&location}"); Map<String, Object> val = new HashMap<String, Object>(); val.put("location", "Schlachthof"); final PartialUriTemplateComponents expanded = template.expand(val); Assert.assertEquals("http://example" + ".com/events{/city}/concerts?eventName=Revo+Tour&foo=bar&location=Schlachthof", expanded.toString()); }
@Test public void testExpandSimpleStringVariablesWithUrl() { final PartialUriTemplate template = new PartialUriTemplate ("http://localhost/things/{id}/widgets?type={widgetType}&redirect={url}"); Map<String, Object> arguments = new HashMap<String, Object>(); arguments.put("id", 101); assertEquals("http://localhost/things/22/widgets?type=ACME+lawn+mower&redirect=http%3A%2F%2Fexample" + ".com%2FLM%2B6000%3Fpreorder%3Dtrue%23%2Forder%2Facme-lm-6000", template.expand("22", "ACME lawn mower", "http://example.com/LM+6000?preorder=true#/order/acme-lm-6000") .toString()); }
@Test public void testContextAbsolutePath() { final PartialUriTemplate template = new PartialUriTemplate("/protected/res/documents/index" + ".html?focus={contractId}&caller=BLUE&referrer=/protected/res/my_contracts/index" + ".html&fragment=/contractDetails/{ref}"); assertEquals("/protected/res/documents/index" + ".html?focus={contractId}&caller=BLUE&referrer=/protected/res/my_contracts/index" + ".html&fragment=/contractDetails/{ref}", template.expand() .toString()); } }
@Test public void testPreservesUnexpandedSimpleStringVariables() { final PartialUriTemplate template = new PartialUriTemplate (LAWNMOWER_TEMPLATE_STRING); assertEquals("http://localhost/things/22/widgets?type={widgetType}&redirect=http://example" + ".com/{widgetName}?preorder=true#/order/{widgetId}", template.expand("22") .toString()); }