Skip to content

Commit

Permalink
Merge pull request #8 from blackdoor/feature/#5_uri-templates
Browse files Browse the repository at this point in the history
The href in a HalLink can now be an RFC6570 URI template
  • Loading branch information
nrktkt committed Feb 2, 2016
2 parents 5213e11 + 2af4886 commit 383c99c
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 20 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ addons:
apt:
packages:
- oracle-java8-installer
cache:
directories:
- $HOME/.m2
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>black.door</groupId>
<artifactId>hate</artifactId>
<version>v1r1t0</version>
<version>v1r2t0</version>

<dependencies>
<dependency>
Expand All @@ -23,7 +23,13 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.4</version>
<version>2.7.0</version>
</dependency>

<dependency>
<groupId>com.damnhandy</groupId>
<artifactId>handy-uri-templates</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand Down
125 changes: 114 additions & 11 deletions src/main/java/black/door/hate/HalLink.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,136 @@
package black.door.hate;

import com.damnhandy.uri.template.UriTemplate;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Builder;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Getter;
import lombok.NonNull;
import lombok.SneakyThrows;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Objects;

/**
* Created by nfischer on 12/8/2015.
*/
@Builder
@Getter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class HalLink implements LinkOrResource{
@NonNull
private URI href;

private Boolean templated;
private String type;
private URL deprecation;
private String name;
private URI profile;
private String title;
private String hreflang;
private @NonNull final String href;
private final Boolean templated;
private final String type;
private final URL deprecation;
private final String name;
private final URI profile;
private final String title;
private final String hreflang;

@java.beans.ConstructorProperties({"href", "templated", "type", "deprecation", "name", "profile", "title", "hreflang"})
HalLink(String href, Boolean templated, String type, URL deprecation, String name, URI profile, String title, String hreflang) {
this.href = href;
this.templated = templated;
this.type = type;
this.deprecation = deprecation;
this.name = name;
this.profile = profile;
this.title = title;
this.hreflang = hreflang;
}


public static HalLinkBuilder builder() {
return new HalLinkBuilder();
}

@JsonIgnore
@SneakyThrows(URISyntaxException.class)
public URI getHrefAsUri(){
return new URI(href);
}

@JsonIgnore
public UriTemplate getHrefAsTemplate(){
return UriTemplate.fromTemplate(href);
}

@Override
public HalLink asLink() {
return this;
}

@Override
@SneakyThrows(JsonProcessingException.class)
public String toString(){
return new ObjectMapper().writeValueAsString(this);
}

public static class HalLinkBuilder {
private String href;
private Boolean templated;
private String type;
private URL deprecation;
private String name;
private URI profile;
private String title;
private String hreflang;

HalLinkBuilder() {
}

public HalLink.HalLinkBuilder href(UriTemplate href) {
this.href = href.expandPartial();
templated = true;
return this;
}

public HalLink.HalLinkBuilder href(URI href) {
this.href = href.toASCIIString();
if(Objects.equals(templated, true))
templated = null;
return this;
}

public HalLink.HalLinkBuilder type(String type) {
this.type = type;
return this;
}

public HalLink.HalLinkBuilder deprecation(URL deprecation) {
this.deprecation = deprecation;
return this;
}

public HalLink.HalLinkBuilder name(String name) {
this.name = name;
return this;
}

public HalLink.HalLinkBuilder profile(URI profile) {
this.profile = profile;
return this;
}

public HalLink.HalLinkBuilder title(String title) {
this.title = title;
return this;
}

public HalLink.HalLinkBuilder hreflang(String hreflang) {
this.hreflang = hreflang;
return this;
}

public HalLink build() {
return new HalLink(href, templated, type, deprecation, name, profile, title, hreflang);
}

public String toString() {
return "black.door.hate.HalLink.HalLinkBuilder(href=" + this.href + ", templated=" + this.templated + ", type=" + this.type + ", deprecation=" + this.deprecation + ", name=" + this.name + ", profile=" + this.profile + ", title=" + this.title + ", hreflang=" + this.hreflang + ")";
}
}
}
10 changes: 4 additions & 6 deletions src/main/java/black/door/hate/HalRepresentation.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Getter;
import lombok.SneakyThrows;

import java.io.IOException;
import java.net.URI;
Expand Down Expand Up @@ -58,17 +59,14 @@ public class HalRepresentation implements java.io.Serializable {
this.properties = properties;
}


public String serialize() throws JsonProcessingException {
return WRITER.writeValueAsString(this);
}

@SneakyThrows(JsonProcessingException.class)
public String toString(){
try {
return serialize();
} catch (JsonProcessingException e) {
e.printStackTrace();
return null;
}
return serialize();
}

public static HalRepresentationBuilder paginated(
Expand Down
12 changes: 11 additions & 1 deletion src/test/java/black/door/hate/HalRepresentationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,17 @@ public void testPagination() throws Exception{
assertEquals(20, rep.getMultiEmbedded().get("orders").size());
assertEquals("/orders/20",
rep.getMultiEmbedded().get("orders").get(0).asEmbedded().getLinks().get("self").asLink()
.getHref().toASCIIString());
.getHref());
}

@Test
public void testLinkToString() throws JsonProcessingException {
Order o = new Order(1, 1, "USD", "status", new Basket(2), new Customer(3));

HalLink link = o.asLink();
System.out.println(link.toString());
assertTrue(link.toString().length() > 0);
assertTrue(o.asEmbedded().serialize().contains(link.toString()));
}

@Test
Expand Down

0 comments on commit 383c99c

Please sign in to comment.