#137 - Merged Link and LinkTemplate.

Link now exposes additional methods like isTemplate(), getTemplateVariables() and expand(…).
This commit is contained in:
Oliver Gierke
2014-01-19 18:38:53 +01:00
parent bc97010cad
commit 6e9caf2556
13 changed files with 164 additions and 153 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2014 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.
@@ -18,16 +18,20 @@ package org.springframework.hateoas;
import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import com.fasterxml.jackson.annotation.JsonIgnore;
/**
* Value object for links.
*
@@ -46,10 +50,9 @@ 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 private String rel;
@XmlAttribute private String href;
@XmlTransient @JsonIgnore private UriTemplate template;
/**
* Creates a new link to the given URI with the self rel.
@@ -74,6 +77,7 @@ public class Link implements Serializable {
this.href = href;
this.rel = rel;
this.template = new UriTemplate(href);
}
/**
@@ -120,6 +124,56 @@ public class Link implements Serializable {
return withRel(Link.REL_SELF);
}
/**
* Returns the variable names contained in the template.
*
* @return
*/
@org.codehaus.jackson.annotate.JsonIgnore
@JsonIgnore
public List<String> getVariableNames() {
return getUriTemplate().getVariableNames();
}
/**
* Returns whether the link is templated.
*
* @return
*/
@org.codehaus.jackson.annotate.JsonIgnore
public boolean isTemplate() {
return !getUriTemplate().getVariables().isEmpty();
}
/**
* Turns the current template into a {@link Link} by expanding it using the given parameters.
*
* @param arguments
* @return
*/
public Link expand(Object... arguments) {
return new Link(getUriTemplate().expand(arguments).toString(), getRel());
}
/**
* Turns the current template into a {@link Link} by expanding it using the given parameters.
*
* @param arguments must not be {@literal null}.
* @return
*/
public Link expand(Map<String, ? extends Object> arguments) {
return new Link(getUriTemplate().expand(arguments).toString(), getRel());
}
private UriTemplate getUriTemplate() {
if (template == null) {
this.template = new UriTemplate(href);
}
return template;
}
/*
* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)

View File

@@ -1,87 +0,0 @@
/*
* Copyright 2014 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.util.List;
import java.util.Map;
/**
* A link template.
*
* @author Oliver Gierke
*/
public class LinkTemplate extends Link {
private static final long serialVersionUID = 770560851448247262L;
private UriTemplate uriTemplate;
/**
* Creates a new {@link LinkTemplate} for the given template string and relation type.
*
* @param template must not be {@literal null} or empty.
* @param rel must not be {@literal null} or empty.
*/
public LinkTemplate(String template, String rel) {
super(template, rel);
this.uriTemplate = new UriTemplate(template);
}
/**
* Default constructor for the marshalling frameworks.
*/
protected LinkTemplate() {}
/**
* Returns the variable names contained in the template.
*
* @return
*/
public List<String> getVariableNames() {
return uriTemplate.getVariableNames();
}
/**
* Returns whether the link is templated.
*
* @return
*/
public boolean isTemplate() {
return true;
}
/**
* Turns the current template into a {@link Link} by expanding it using the given parameters.
*
* @param arguments
* @return
*/
public Link toLink(Object... arguments) {
return new Link(uriTemplate.expand(arguments).toString(), getRel());
}
/**
* Turns the current template into a {@link Link} by expanding it using the given parameters.
*
* @param arguments
* @return
*/
public Link toLink(Map<String, ? extends Object> arguments) {
return new Link(uriTemplate.expand(arguments).toString(), getRel());
}
}

View File

@@ -67,6 +67,10 @@ public class UriTemplate implements Iterable<TemplateVariable> {
this.variables.add(new TemplateVariable(name, type));
}
}
if (this.baseUri == null) {
this.baseUri = template;
}
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2014 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.
@@ -26,8 +26,6 @@ import net.minidev.json.JSONArray;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.LinkDiscoverer;
import org.springframework.hateoas.LinkTemplate;
import org.springframework.hateoas.UriTemplate;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
@@ -145,9 +143,7 @@ public class JsonPathLinkDiscoverer implements LinkDiscoverer {
return Collections.unmodifiableList(links);
}
String href = parseResult.toString();
Link link = UriTemplate.isTemplate(href) ? new LinkTemplate(href, rel) : new Link(href, rel);
return Collections.unmodifiableList(Arrays.asList(link));
return Collections.unmodifiableList(Arrays.asList(new Link(parseResult.toString(), rel)));
}
/*

View File

@@ -20,8 +20,6 @@ import java.net.URI;
import org.springframework.hateoas.Identifiable;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.LinkBuilder;
import org.springframework.hateoas.LinkTemplate;
import org.springframework.hateoas.UriTemplate;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.util.UriComponents;
@@ -113,8 +111,7 @@ public abstract class LinkBuilderSupport<T extends LinkBuilder> implements LinkB
* @see org.springframework.hateoas.LinkBuilder#withRel(java.lang.String)
*/
public Link withRel(String rel) {
String href = this.toString();
return UriTemplate.isTemplate(href) ? new LinkTemplate(href, rel) : new Link(href, rel);
return new Link(this.toString(), rel);
}
/*

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2013-2014 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.
@@ -76,7 +76,6 @@ public class DefaultCurieProvider implements CurieProvider {
private static final long serialVersionUID = 1L;
private final String name;
private final boolean templated = true;
public Curie(String name, String href) {
@@ -87,9 +86,5 @@ public class DefaultCurieProvider implements CurieProvider {
public String getName() {
return name;
}
public boolean isTemplated() {
return templated;
}
}
}

View File

@@ -16,6 +16,7 @@
package org.springframework.hateoas.hal;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -27,7 +28,6 @@ import java.util.Map;
import org.springframework.beans.BeanUtils;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.LinkTemplate;
import org.springframework.hateoas.RelProvider;
import org.springframework.hateoas.Resource;
import org.springframework.hateoas.ResourceSupport;
@@ -47,6 +47,7 @@ import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.KeyDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -57,6 +58,7 @@ import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
import com.fasterxml.jackson.databind.deser.std.ContainerDeserializerBase;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
import com.fasterxml.jackson.databind.jsontype.TypeIdResolver;
import com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
@@ -64,6 +66,7 @@ import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.ContainerSerializer;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import com.fasterxml.jackson.databind.ser.std.MapSerializer;
import com.fasterxml.jackson.databind.ser.std.NonTypedScalarSerializerBase;
import com.fasterxml.jackson.databind.type.TypeFactory;
/**
@@ -81,7 +84,6 @@ public class Jackson2HalModule extends SimpleModule {
super("json-hal-module", new Version(1, 0, 0, null, "org.springframework.hateoas", "spring-hateoas"));
setMixInAnnotation(Link.class, LinkMixin.class);
setMixInAnnotation(LinkTemplate.class, LinkTemplateMixin.class);
setMixInAnnotation(ResourceSupport.class, ResourceSupportMixin.class);
setMixInAnnotation(Resources.class, ResourcesMixin.class);
}
@@ -654,4 +656,41 @@ public class Jackson2HalModule extends SimpleModule {
return (TypeIdResolver) findInstance(resolverClass);
}
}
/**
* {@link JsonSerializer} to only render {@link Boolean} values if they're set to {@literal true}.
*
* @author Oliver Gierke
* @since 0.9
*/
public static class TrueOnlyBooleanSerializer extends NonTypedScalarSerializerBase<Boolean> {
public TrueOnlyBooleanSerializer() {
super(Boolean.class);
}
@Override
public boolean isEmpty(Boolean value) {
return value == null || Boolean.FALSE.equals(value);
}
@Override
public void serialize(Boolean value, JsonGenerator jgen, SerializerProvider provider) throws IOException,
JsonGenerationException {
jgen.writeBoolean(value.booleanValue());
}
@Override
public JsonNode getSchema(SerializerProvider provider, Type typeHint) {
return createSchemaNode("boolean", true);
}
@Override
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint)
throws JsonMappingException {
if (visitor != null) {
visitor.expectBooleanFormat(typeHint);
}
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2014 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.
@@ -16,6 +16,11 @@
package org.springframework.hateoas.hal;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.hal.Jackson2HalModule.TrueOnlyBooleanSerializer;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
/**
* Custom mixin to avoid rel attributes being rendered for HAL.
@@ -25,6 +30,16 @@ import org.springframework.hateoas.Link;
*/
@org.codehaus.jackson.annotate.JsonIgnoreProperties(value = "rel")
@com.fasterxml.jackson.annotation.JsonIgnoreProperties(value = "rel")
class LinkMixin extends Link {
abstract class LinkMixin extends Link {
private static final long serialVersionUID = 4720588561299667409L;
/*
* (non-Javadoc)
* @see org.springframework.hateoas.Link#isTemplate()
*/
@Override
@JsonInclude(Include.NON_EMPTY)
@JsonSerialize(using = TrueOnlyBooleanSerializer.class)
public abstract boolean isTemplate();
}

View File

@@ -1,35 +0,0 @@
/*
* Copyright 2014 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.hal;
import java.util.List;
import org.springframework.hateoas.LinkTemplate;
import com.fasterxml.jackson.annotation.JsonIgnore;
/**
* Mixin for {@link LinkTemplate}s.
*
* @author Oliver Gierke
*/
abstract class LinkTemplateMixin extends LinkMixin {
private static final long serialVersionUID = -7227899604445243148L;
@JsonIgnore
public abstract List<String> getVariableNames();
}