Skip to content

Commit 83743d8

Browse files
committed
#137 - Initial support for link templates.
Added LinkTemplate domain type that can deal with URI templates as defined in http://tools.ietf.org/html/rfc6570. We currently support /, ?, & and # variables up to level 3 of the spec (multiple parameter definitions). The templates can be expanded to a Link instance. Added the necessary HAL mixins to render the instances as specified.
1 parent 653f287 commit 83743d8

File tree

8 files changed

+644
-7
lines changed

8 files changed

+644
-7
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.hateoas;
17+
18+
import java.util.List;
19+
import java.util.Map;
20+
21+
/**
22+
* A link template.
23+
*
24+
* @author Oliver Gierke
25+
*/
26+
public class LinkTemplate extends Link {
27+
28+
private static final long serialVersionUID = 770560851448247262L;
29+
30+
private UriTemplate uriTemplate;
31+
32+
/**
33+
* Creates a new {@link LinkTemplate} for the given template string and relation type.
34+
*
35+
* @param template must not be {@literal null} or empty.
36+
* @param rel must not be {@literal null} or empty.
37+
*/
38+
public LinkTemplate(String template, String rel) {
39+
40+
super(template, rel);
41+
42+
this.uriTemplate = new UriTemplate(template);
43+
}
44+
45+
/**
46+
* Default constructor for the marshalling frameworks.
47+
*/
48+
protected LinkTemplate() {}
49+
50+
/**
51+
* Returns the variable names contained in the template.
52+
*
53+
* @return
54+
*/
55+
public List<String> getVariableNames() {
56+
return uriTemplate.getVariableNames();
57+
}
58+
59+
/**
60+
* Returns whether the link is templated.
61+
*
62+
* @return
63+
*/
64+
public boolean isTemplate() {
65+
return true;
66+
}
67+
68+
/**
69+
* Turns the current template into a {@link Link} by expanding it using the given parameters.
70+
*
71+
* @param arguments
72+
* @return
73+
*/
74+
public Link toLink(Object... arguments) {
75+
return new Link(uriTemplate.expand(arguments).toString(), getRel());
76+
}
77+
78+
/**
79+
* Turns the current template into a {@link Link} by expanding it using the given parameters.
80+
*
81+
* @param arguments
82+
* @return
83+
*/
84+
public Link toLink(Map<String, ? extends Object> arguments) {
85+
return new Link(uriTemplate.expand(arguments).toString(), getRel());
86+
}
87+
}

0 commit comments

Comments
 (0)