Changing the representation to be more consistent in key naming (breaking change). Also starting work on the ability to "enrich" a representation with e.g. more links to other resources, etc...

This commit is contained in:
Jon Brisbin
2012-08-06 12:25:26 -05:00
parent bd24e582fd
commit 7775af8be4
21 changed files with 646 additions and 125 deletions

View File

@@ -0,0 +1,39 @@
package org.springframework.data.rest.core;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.codehaus.jackson.annotate.JsonProperty;
import org.springframework.util.Assert;
/**
* @author Jon Brisbin
*/
public abstract class LinkAware<T extends LinkAware<? super T>> {
@JsonProperty("links")
protected List<Link> links = new ArrayList<Link>();
public List<Link> getLinks() {
return links;
}
@SuppressWarnings({"unchecked"})
public T setLinks(List<Link> links) {
if(null == links) {
this.links = Collections.emptyList();
} else {
this.links = links;
}
return (T)this;
}
@SuppressWarnings({"unchecked"})
public T addLink(Link link) {
Assert.notNull(link, "Link cannot be null!");
links.add(link);
return (T)this;
}
}

View File

@@ -3,8 +3,6 @@ package org.springframework.data.rest.core;
import java.util.ArrayList;
import java.util.List;
import org.codehaus.jackson.annotate.JsonProperty;
/**
* @author Jon Brisbin <jbrisbin@vmware.com>
*/
@@ -17,7 +15,6 @@ public class Links {
return this;
}
@JsonProperty("_links")
public List<Link> getLinks() {
return this.links;
}

View File

@@ -0,0 +1,33 @@
package org.springframework.data.rest.core;
import java.util.Map;
import org.codehaus.jackson.annotate.JsonAnyGetter;
import org.codehaus.jackson.annotate.JsonIgnore;
/**
* Abstraction that represents a Map-like REST resource plus a set of links.
*
* @author Jon Brisbin
*/
public class MapResource extends Resource<Map<String, Object>> {
public MapResource() {
}
public MapResource(Map<String, Object> resource) {
this.resource = resource;
}
@Override
@JsonIgnore
public Map<String, Object> getResource() {
return resource;
}
@JsonAnyGetter
public Map<String, Object> any() {
return resource;
}
}

View File

@@ -0,0 +1,18 @@
package org.springframework.data.rest.core;
/**
* Implementations of this interface will post-process objects to mutate them in ways meaningful to the context in
* which they are called.
*
* @author Jon Brisbin
*/
public interface PostProcessor<T> {
/**
* Possibly perform some mutation on the object as a post-processing step in a flow.
*
* @param obj
*/
T postProcess(T obj);
}

View File

@@ -0,0 +1,24 @@
package org.springframework.data.rest.core;
import java.net.URI;
/**
* Implementations of this interface are responsible for turning {@link URI}s into real objects.
*
* @author Jon Brisbin
*/
public interface Resolver<T> {
/**
* Take a {@link URI} and resolve it to an actual object.
*
* @param baseUri
* The base URI that this resource is relative to.
* @param uri
* The URI id of the resource.
*
* @return The resolved object or {@literal null} if not found.
*/
T resolve(URI baseUri, URI uri);
}

View File

@@ -0,0 +1,31 @@
package org.springframework.data.rest.core;
import org.codehaus.jackson.annotate.JsonPropertyOrder;
import org.codehaus.jackson.annotate.JsonUnwrapped;
/**
* Wraps a simple object or a bean plus a set of links.
*
* @author Jon Brisbin
*/
public class Resource<T> extends LinkAware<Resource<T>> {
@JsonUnwrapped
protected T resource;
public Resource() {
}
public Resource(T resource) {
this.resource = resource;
}
public T getResource() {
return resource;
}
public void setResource(T resource) {
this.resource = resource;
}
}

View File

@@ -0,0 +1,38 @@
package org.springframework.data.rest.core;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.codehaus.jackson.annotate.JsonProperty;
/**
* Abstraction for representing resources to the user agent.
*
* @author Jon Brisbin
*/
@SuppressWarnings({"unchecked"})
public class Resources extends LinkAware<Resources> {
@JsonProperty("content")
protected List<Resource<?>> resources = new ArrayList<Resource<?>>();
public List getResources() {
return resources;
}
public Resources setResources(List resources) {
if(null == resources) {
this.resources = Collections.emptyList();
} else {
this.resources = resources;
}
return this;
}
public Resources addResource(Resource<?> resource) {
resources.add((null == resource ? new Resource<Object>() : resource));
return this;
}
}