Skip to content

Add extra attributes to Link and use for HAL mediatype #567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 64 additions & 71 deletions src/main/java/org/springframework/hateoas/Link.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,6 +15,13 @@
*/
package org.springframework.hateoas;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.experimental.Wither;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -37,9 +44,14 @@
* Value object for links.
*
* @author Oliver Gierke
* @author Greg Turnquist
*/
@XmlType(name = "link", namespace = Link.ATOM_NAMESPACE)
@JsonIgnoreProperties("templated")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PACKAGE)
@Getter
@EqualsAndHashCode(of = {"rel", "href", "hreflang", "media", "title", "deprecation"})
public class Link implements Serializable {

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

@XmlAttribute private String rel;
@XmlAttribute private String href;
@XmlAttribute @Wither private String rel;
@XmlAttribute @Wither private String href;
@XmlAttribute @Wither private String hreflang;
@XmlAttribute @Wither private String media;
@XmlAttribute @Wither private String title;
@XmlAttribute @Wither private String type;
@XmlAttribute @Wither private String deprecation;
@XmlTransient @JsonIgnore private UriTemplate template;

/**
Expand Down Expand Up @@ -85,49 +102,14 @@ public Link(String href, String rel) {
*/
public Link(UriTemplate template, String rel) {

Assert.notNull(template, "UriTempalte must not be null!");
Assert.notNull(template, "UriTemplate must not be null!");
Assert.hasText(rel, "Rel must not be null or empty!");

this.template = template;
this.href = template.toString();
this.rel = rel;
}

/**
* Empty constructor required by the marshalling framework.
*/
protected Link() {

}

/**
* Returns the actual URI the link is pointing to.
*
* @return
*/
public String getHref() {
return href;
}

/**
* Returns the rel of the link.
*
* @return
*/
public String getRel() {
return rel;
}

/**
* Returns a {@link Link} pointing to the same URI but with the given relation.
*
* @param rel must not be {@literal null} or empty.
* @return
*/
public Link withRel(String rel) {
return new Link(href, rel);
}

/**
* Returns a {@link Link} pointing to the same URI but with the {@code self} relation.
*
Expand Down Expand Up @@ -195,46 +177,35 @@ private UriTemplate getUriTemplate() {
return template;
}

/*
/*
* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
* @see java.lang.Object#toString()
*/
@Override
public boolean equals(Object obj) {
public String toString() {
String linkString = String.format("<%s>;rel=\"%s\"", href, rel);

if (this == obj) {
return true;
if (hreflang != null) {
linkString += ";hreflang=\"" + hreflang + "\"";
}

if (!(obj instanceof Link)) {
return false;
if (media != null) {
linkString += ";media=\"" + media + "\"";
}

Link that = (Link) obj;

return this.href.equals(that.href) && this.rel.equals(that.rel);
}

/*
* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
if (title != null) {
linkString += ";title=\"" + title + "\"";
}

int result = 17;
result += 31 * href.hashCode();
result += 31 * rel.hashCode();
return result;
}
if (type != null) {
linkString += ";type=\"" + type + "\"";
}

/*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return String.format("<%s>;rel=\"%s\"", href, rel);
if (deprecation != null) {
linkString += ";deprecation=\"" + deprecation + "\"";
}

return linkString;
}

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

return new Link(matcher.group(1), attributes.get("rel"));
Link link = new Link(matcher.group(1), attributes.get("rel"));

if (attributes.containsKey("hreflang")) {
link = link.withHreflang(attributes.get("hreflang"));
}

if (attributes.containsKey("media")) {
link = link.withMedia(attributes.get("media"));
}

if (attributes.containsKey("title")) {
link = link.withTitle(attributes.get("title"));
}

if (attributes.containsKey("type")) {
link = link.withType(attributes.get("type"));
}

if (attributes.containsKey("deprecation")) {
link = link.withDeprecation(attributes.get("deprecation"));
}

return link;

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

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

while (matcher.find()) {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/springframework/hateoas/Links.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2016 the original author or authors.
* Copyright 2013-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,10 +29,11 @@
* Value object to represent a list of {@link Link}s.
*
* @author Oliver Gierke
* @author Greg Turnquist
*/
public class Links implements Iterable<Link> {

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

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

Expand Down
39 changes: 36 additions & 3 deletions src/main/java/org/springframework/hateoas/hal/LinkMixin.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors.
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,13 +28,46 @@
*
* @author Alexander Baetz
* @author Oliver Gierke
* @author Greg Turnquist
*/
@JsonIgnoreProperties(value = "rel")
@JsonIgnoreProperties({"rel", "media"})
abstract class LinkMixin extends Link {

private static final long serialVersionUID = 4720588561299667409L;

/*
/*
* (non-Javadoc)
* @see org.springframework.hateoas.Link#getHreflang()
*/
@Override
@JsonInclude(Include.NON_NULL)
public abstract String getHreflang();

/*
* (non-Javadoc)
* @see org.springframework.hateoas.Link#getTitle()
*/
@Override
@JsonInclude(Include.NON_NULL)
public abstract String getTitle();

/*
* (non-Javadoc)
* @see org.springframework.hateoas.Link#getType()
*/
@Override
@JsonInclude(Include.NON_NULL)
public abstract String getType();

/*
* (non-Javadoc)
* @see org.springframework.hateoas.Link#getDeprecation()
*/
@Override
@JsonInclude(Include.NON_NULL)
public abstract String getDeprecation();

/*
* (non-Javadoc)
* @see org.springframework.hateoas.Link#isTemplate()
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
/*
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.hateoas;

import java.io.StringWriter;
import java.io.Writer;

import org.junit.Before;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
* Base class to test objects against the Jackson 2.0 {@link ObjectMapper}.
*
* @author Oliver Gierke
* @author Jon Brisbin
* @author Greg Turnquist
*/
public abstract class AbstractJackson2MarshallingIntegrationTest {

Expand All @@ -20,6 +37,7 @@ public abstract class AbstractJackson2MarshallingIntegrationTest {
@Before
public void setUp() {
mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}

protected String write(Object object) throws Exception {
Expand Down
32 changes: 29 additions & 3 deletions src/test/java/org/springframework/hateoas/LinkUnitTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,6 +28,7 @@
* Unit tests for {@link Link}.
*
* @author Oliver Gierke
* @author Greg Turnquist
*/
public class LinkUnitTest {

Expand Down Expand Up @@ -103,21 +104,46 @@ public void returnsNullForNullOrEmptyLink() {
assertThat(Link.valueOf(""), is(nullValue()));
}

/**
* @see #54
* @see #100
*/
@Test
public void parsesRFC5988HeaderIntoLink() {

assertThat(Link.valueOf("</something>;rel=\"foo\""), is(new Link("/something", "foo")));
assertThat(Link.valueOf("</something>;rel=\"foo\";title=\"Some title\""), is(new Link("/something", "foo")));
assertThat(Link.valueOf("</customer/1>;rel=\"self\";hreflang=\"en\";media=\"pdf\";title=\"pdf customer copy\";type=\"portable document\";deprecation=\"http://example.com/customers/deprecated\""),
is(new Link("/customer/1")
.withHreflang("en")
.withMedia("pdf")
.withTitle("pdf customer copy")
.withType("portable document")
.withDeprecation("http://example.com/customers/deprecated")));
}

/**
* @see #100
*/
@Test
public void ignoresUnrecognizedAttributes() {
Link link = Link.valueOf("</something>;rel=\"foo\";unknown=\"should fail\"");

assertThat(link.getHref(), is("/something"));
assertThat(link.getRel(), is("foo"));
}

@Test(expected = IllegalArgumentException.class)
public void rejectsMissingRelAttribute() {
Link.valueOf("</something>);title=\"title\"");
Link.valueOf("</something>;title=\"title\"");
}

@Test(expected = IllegalArgumentException.class)
public void rejectsLinkWithoutAttributesAtAll() {
Link.valueOf("</something>);title=\"title\"");

Link link = Link.valueOf("</something>");

System.out.println(link);
}

@Test(expected = IllegalArgumentException.class)
Expand Down
Loading