Skip to content

Commit 891fd90

Browse files
gregturnodrotbohm
authored andcommitted
#567 - Add extra attributes to Link and use for HAL mediatype.
Adds many additional attributes defined in RFC5988 and verifies they work properly in the neutral representation of Link while also being rendered properly in the HAL module. Related tickets: #100, #417, #235 Previous pull requests: #240, #238, #223, #79
1 parent af3ec1a commit 891fd90

File tree

8 files changed

+214
-82
lines changed

8 files changed

+214
-82
lines changed

src/main/java/org/springframework/hateoas/Link.java

Lines changed: 63 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,6 +15,13 @@
1515
*/
1616
package org.springframework.hateoas;
1717

18+
import lombok.AccessLevel;
19+
import lombok.AllArgsConstructor;
20+
import lombok.EqualsAndHashCode;
21+
import lombok.Getter;
22+
import lombok.NoArgsConstructor;
23+
import lombok.experimental.Wither;
24+
1825
import java.io.Serializable;
1926
import java.util.Collections;
2027
import java.util.HashMap;
@@ -37,9 +44,14 @@
3744
* Value object for links.
3845
*
3946
* @author Oliver Gierke
47+
* @author Greg Turnquist
4048
*/
4149
@XmlType(name = "link", namespace = Link.ATOM_NAMESPACE)
4250
@JsonIgnoreProperties("templated")
51+
@NoArgsConstructor(access = AccessLevel.PROTECTED)
52+
@AllArgsConstructor(access = AccessLevel.PACKAGE)
53+
@Getter
54+
@EqualsAndHashCode(of = {"rel", "href", "hreflang", "media", "title", "deprecation"})
4355
public class Link implements Serializable {
4456

4557
private static final long serialVersionUID = -9037755944661782121L;
@@ -53,8 +65,13 @@ public class Link implements Serializable {
5365
public static final String REL_NEXT = "next";
5466
public static final String REL_LAST = "last";
5567

56-
@XmlAttribute private String rel;
57-
@XmlAttribute private String href;
68+
@XmlAttribute @Wither private String rel;
69+
@XmlAttribute @Wither private String href;
70+
@XmlAttribute @Wither private String hreflang;
71+
@XmlAttribute @Wither private String media;
72+
@XmlAttribute @Wither private String title;
73+
@XmlAttribute @Wither private String type;
74+
@XmlAttribute @Wither private String deprecation;
5875
@XmlTransient @JsonIgnore private UriTemplate template;
5976

6077
/**
@@ -93,41 +110,6 @@ public Link(UriTemplate template, String rel) {
93110
this.rel = rel;
94111
}
95112

96-
/**
97-
* Empty constructor required by the marshalling framework.
98-
*/
99-
protected Link() {
100-
101-
}
102-
103-
/**
104-
* Returns the actual URI the link is pointing to.
105-
*
106-
* @return
107-
*/
108-
public String getHref() {
109-
return href;
110-
}
111-
112-
/**
113-
* Returns the rel of the link.
114-
*
115-
* @return
116-
*/
117-
public String getRel() {
118-
return rel;
119-
}
120-
121-
/**
122-
* Returns a {@link Link} pointing to the same URI but with the given relation.
123-
*
124-
* @param rel must not be {@literal null} or empty.
125-
* @return
126-
*/
127-
public Link withRel(String rel) {
128-
return new Link(href, rel);
129-
}
130-
131113
/**
132114
* Returns a {@link Link} pointing to the same URI but with the {@code self} relation.
133115
*
@@ -195,46 +177,35 @@ private UriTemplate getUriTemplate() {
195177
return template;
196178
}
197179

198-
/*
180+
/*
199181
* (non-Javadoc)
200-
* @see java.lang.Object#equals(java.lang.Object)
182+
* @see java.lang.Object#toString()
201183
*/
202184
@Override
203-
public boolean equals(Object obj) {
185+
public String toString() {
186+
String linkString = String.format("<%s>;rel=\"%s\"", href, rel);
204187

205-
if (this == obj) {
206-
return true;
188+
if (hreflang != null) {
189+
linkString += ";hreflang=\"" + hreflang + "\"";
207190
}
208191

209-
if (!(obj instanceof Link)) {
210-
return false;
192+
if (media != null) {
193+
linkString += ";media=\"" + media + "\"";
211194
}
212195

213-
Link that = (Link) obj;
214-
215-
return this.href.equals(that.href) && this.rel.equals(that.rel);
216-
}
217-
218-
/*
219-
* (non-Javadoc)
220-
* @see java.lang.Object#hashCode()
221-
*/
222-
@Override
223-
public int hashCode() {
196+
if (title != null) {
197+
linkString += ";title=\"" + title + "\"";
198+
}
224199

225-
int result = 17;
226-
result += 31 * href.hashCode();
227-
result += 31 * rel.hashCode();
228-
return result;
229-
}
200+
if (type != null) {
201+
linkString += ";type=\"" + type + "\"";
202+
}
230203

231-
/*
232-
* (non-Javadoc)
233-
* @see java.lang.Object#toString()
234-
*/
235-
@Override
236-
public String toString() {
237-
return String.format("<%s>;rel=\"%s\"", href, rel);
204+
if (deprecation != null) {
205+
linkString += ";deprecation=\"" + deprecation + "\"";
206+
}
207+
208+
return linkString;
238209
}
239210

240211
/**
@@ -263,7 +234,29 @@ public static Link valueOf(String element) {
263234
throw new IllegalArgumentException("Link does not provide a rel attribute!");
264235
}
265236

266-
return new Link(matcher.group(1), attributes.get("rel"));
237+
Link link = new Link(matcher.group(1), attributes.get("rel"));
238+
239+
if (attributes.containsKey("hreflang")) {
240+
link = link.withHreflang(attributes.get("hreflang"));
241+
}
242+
243+
if (attributes.containsKey("media")) {
244+
link = link.withMedia(attributes.get("media"));
245+
}
246+
247+
if (attributes.containsKey("title")) {
248+
link = link.withTitle(attributes.get("title"));
249+
}
250+
251+
if (attributes.containsKey("type")) {
252+
link = link.withType(attributes.get("type"));
253+
}
254+
255+
if (attributes.containsKey("deprecation")) {
256+
link = link.withDeprecation(attributes.get("deprecation"));
257+
}
258+
259+
return link;
267260

268261
} else {
269262
throw new IllegalArgumentException(String.format("Given link header %s is not RFC5988 compliant!", element));
@@ -283,7 +276,7 @@ private static Map<String, String> getAttributeMap(String source) {
283276
}
284277

285278
Map<String, String> attributes = new HashMap<String, String>();
286-
Pattern keyAndValue = Pattern.compile("(\\w+)=\"(\\p{Lower}[\\p{Lower}\\p{Digit}\\.\\-]*|" + URI_PATTERN + ")\"");
279+
Pattern keyAndValue = Pattern.compile("(\\w+)=\"(\\p{Lower}[\\p{Lower}\\p{Digit}\\.\\-\\s]*|" + URI_PATTERN + ")\"");
287280
Matcher matcher = keyAndValue.matcher(source);
288281

289282
while (matcher.find()) {

src/main/java/org/springframework/hateoas/Links.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2016 the original author or authors.
2+
* Copyright 2013-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,10 +29,11 @@
2929
* Value object to represent a list of {@link Link}s.
3030
*
3131
* @author Oliver Gierke
32+
* @author Greg Turnquist
3233
*/
3334
public class Links implements Iterable<Link> {
3435

35-
private static final Pattern LINK_HEADER_PATTERN = Pattern.compile("(<[^>]*>;rel=\"[^\"]*\")");
36+
private static final Pattern LINK_HEADER_PATTERN = Pattern.compile("(<[^>]*>(;\\w+=\"[^\"]*\")+)");
3637

3738
static final Links NO_LINKS = new Links(Collections.<Link> emptyList());
3839

src/main/java/org/springframework/hateoas/hal/LinkMixin.java

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,13 +28,46 @@
2828
*
2929
* @author Alexander Baetz
3030
* @author Oliver Gierke
31+
* @author Greg Turnquist
3132
*/
32-
@JsonIgnoreProperties(value = "rel")
33+
@JsonIgnoreProperties({"rel", "media"})
3334
abstract class LinkMixin extends Link {
3435

3536
private static final long serialVersionUID = 4720588561299667409L;
3637

37-
/*
38+
/*
39+
* (non-Javadoc)
40+
* @see org.springframework.hateoas.Link#getHreflang()
41+
*/
42+
@Override
43+
@JsonInclude(Include.NON_NULL)
44+
public abstract String getHreflang();
45+
46+
/*
47+
* (non-Javadoc)
48+
* @see org.springframework.hateoas.Link#getTitle()
49+
*/
50+
@Override
51+
@JsonInclude(Include.NON_NULL)
52+
public abstract String getTitle();
53+
54+
/*
55+
* (non-Javadoc)
56+
* @see org.springframework.hateoas.Link#getType()
57+
*/
58+
@Override
59+
@JsonInclude(Include.NON_NULL)
60+
public abstract String getType();
61+
62+
/*
63+
* (non-Javadoc)
64+
* @see org.springframework.hateoas.Link#getDeprecation()
65+
*/
66+
@Override
67+
@JsonInclude(Include.NON_NULL)
68+
public abstract String getDeprecation();
69+
70+
/*
3871
* (non-Javadoc)
3972
* @see org.springframework.hateoas.Link#isTemplate()
4073
*/

src/test/java/org/springframework/hateoas/AbstractJackson2MarshallingIntegrationTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,34 @@
1+
/*
2+
* Copyright 2012-2017 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+
*/
116
package org.springframework.hateoas;
217

318
import java.io.StringWriter;
419
import java.io.Writer;
520

621
import org.junit.Before;
722

23+
import com.fasterxml.jackson.annotation.JsonInclude;
824
import com.fasterxml.jackson.databind.ObjectMapper;
925

1026
/**
1127
* Base class to test objects against the Jackson 2.0 {@link ObjectMapper}.
1228
*
1329
* @author Oliver Gierke
1430
* @author Jon Brisbin
31+
* @author Greg Turnquist
1532
*/
1633
public abstract class AbstractJackson2MarshallingIntegrationTest {
1734

@@ -20,6 +37,7 @@ public abstract class AbstractJackson2MarshallingIntegrationTest {
2037
@Before
2138
public void setUp() {
2239
mapper = new ObjectMapper();
40+
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
2341
}
2442

2543
protected String write(Object object) throws Exception {

src/test/java/org/springframework/hateoas/LinkUnitTest.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
2828
* Unit tests for {@link Link}.
2929
*
3030
* @author Oliver Gierke
31+
* @author Greg Turnquist
3132
*/
3233
public class LinkUnitTest {
3334

@@ -103,21 +104,46 @@ public void returnsNullForNullOrEmptyLink() {
103104
assertThat(Link.valueOf(""), is(nullValue()));
104105
}
105106

107+
/**
108+
* @see #54
109+
* @see #100
110+
*/
106111
@Test
107112
public void parsesRFC5988HeaderIntoLink() {
108113

109114
assertThat(Link.valueOf("</something>;rel=\"foo\""), is(new Link("/something", "foo")));
110115
assertThat(Link.valueOf("</something>;rel=\"foo\";title=\"Some title\""), is(new Link("/something", "foo")));
116+
assertThat(Link.valueOf("</customer/1>;rel=\"self\";hreflang=\"en\";media=\"pdf\";title=\"pdf customer copy\";type=\"portable document\";deprecation=\"http://example.com/customers/deprecated\""),
117+
is(new Link("/customer/1")
118+
.withHreflang("en")
119+
.withMedia("pdf")
120+
.withTitle("pdf customer copy")
121+
.withType("portable document")
122+
.withDeprecation("http://example.com/customers/deprecated")));
123+
}
124+
125+
/**
126+
* @see #100
127+
*/
128+
@Test
129+
public void ignoresUnrecognizedAttributes() {
130+
Link link = Link.valueOf("</something>;rel=\"foo\";unknown=\"should fail\"");
131+
132+
assertThat(link.getHref(), is("/something"));
133+
assertThat(link.getRel(), is("foo"));
111134
}
112135

113136
@Test(expected = IllegalArgumentException.class)
114137
public void rejectsMissingRelAttribute() {
115-
Link.valueOf("</something>);title=\"title\"");
138+
Link.valueOf("</something>;title=\"title\"");
116139
}
117140

118141
@Test(expected = IllegalArgumentException.class)
119142
public void rejectsLinkWithoutAttributesAtAll() {
120-
Link.valueOf("</something>);title=\"title\"");
143+
144+
Link link = Link.valueOf("</something>");
145+
146+
System.out.println(link);
121147
}
122148

123149
@Test(expected = IllegalArgumentException.class)

0 commit comments

Comments
 (0)