Skip to content
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

Modify the Link class to support RFC 5988 Target Attributes #238

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
#229: add support for curies to the HalEmbeddedBuilder class
  • Loading branch information
Jeff Stano committed Aug 22, 2014
commit 9d48cc56f15b4efeeeeb7c5d2fda6b144d160d90
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.List;
import java.util.Map;

import org.springframework.hateoas.Link;
import org.springframework.hateoas.RelProvider;
import org.springframework.hateoas.Resource;
import org.springframework.hateoas.core.EmbeddedWrapper;
Expand All @@ -32,7 +33,7 @@
/**
* Builder class that allows collecting objects under the relation types defined for the objects but moving from the
* single resource relation to the collection one, once more than one object of the same type is added.
*
*
* @author Oliver Gierke
* @author Dietrich Schulten
*/
Expand All @@ -42,27 +43,29 @@ class HalEmbeddedBuilder {

private final Map<String, Object> embeddeds = new HashMap<String, Object>();
private final RelProvider provider;
private final CurieProvider curieProvider;
private final EmbeddedWrappers wrappers;

/**
* Creates a new {@link HalEmbeddedBuilder} using the given {@link RelProvider} and prefer collection rels flag.
*
*
* @param provider can be {@literal null}.
* @param preferCollectionRels whether to prefer to ask the provider for collection rels.
*/
public HalEmbeddedBuilder(RelProvider provider, boolean preferCollectionRels) {
public HalEmbeddedBuilder(RelProvider provider, CurieProvider curieProvider, boolean preferCollectionRels) {

Assert.notNull(provider, "Relprovider must not be null!");

this.provider = provider;
this.curieProvider = curieProvider;
this.wrappers = new EmbeddedWrappers(preferCollectionRels);
}

/**
* Adds the given value to the embeddeds. Will skip doing so if the value is {@literal null} or the content of a
* {@link Resource} is {@literal null}.
*
* @param value can be {@literal null}.
*
* @param source can be {@literal null}.
*/
public void add(Object source) {

Expand Down Expand Up @@ -116,12 +119,19 @@ private String getDefaultedRelFor(EmbeddedWrapper wrapper, boolean forCollection
Class<?> type = wrapper.getRelTargetType();

String rel = forCollection ? provider.getCollectionResourceRelFor(type) : provider.getItemResourceRelFor(type);

if (curieProvider != null) {
Link embeddedLinkRel = new Link("http://localhost", rel);

rel = curieProvider.getNamespacedRelFrom(embeddedLinkRel);
}

return rel == null ? DEFAULT_REL : rel;
}

/**
* Returns the added objects keyed up by their relation types.
*
*
* @return
*/
public Map<String, Object> asMap() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@

/**
* Jackson 2 module implementation to render {@link Link} and {@link ResourceSupport} instances in HAL compatible JSON.
*
*
* @author Alexander Baetz
* @author Oliver Gierke
*/
Expand All @@ -90,7 +90,7 @@ public Jackson2HalModule() {

/**
* Returns whether the module was already registered in the given {@link ObjectMapper}.
*
*
* @param mapper must not be {@literal null}.
* @return
*/
Expand All @@ -102,7 +102,7 @@ public static boolean isAlreadyRegisteredIn(ObjectMapper mapper) {

/**
* Custom {@link JsonSerializer} to render Link instances in HAL compatible JSON.
*
*
* @author Alexander Baetz
* @author Oliver Gierke
*/
Expand Down Expand Up @@ -230,40 +230,42 @@ protected ContainerSerializer<?> _withValueTypeSerializer(TypeSerializer vts) {

/**
* Custom {@link JsonSerializer} to render {@link Resource}-Lists in HAL compatible JSON. Renders the list as a Map.
*
*
* @author Alexander Baetz
* @author Oliver Gierke
*/
public static class HalResourcesSerializer extends ContainerSerializer<Collection<?>> implements ContextualSerializer {

private final BeanProperty property;
private final RelProvider relProvider;
private final CurieProvider curieProvider;
private final boolean enforceEmbeddedCollections;

public HalResourcesSerializer(RelProvider relPorvider, boolean enforceEmbeddedCollections) {
this(null, relPorvider, enforceEmbeddedCollections);
public HalResourcesSerializer(RelProvider relPorvider, CurieProvider curieProvider, boolean enforceEmbeddedCollections) {
this(null, relPorvider, curieProvider, enforceEmbeddedCollections);
}

public HalResourcesSerializer(BeanProperty property, RelProvider relProvider, boolean enforceEmbeddedCollections) {
public HalResourcesSerializer(BeanProperty property, RelProvider relProvider, CurieProvider curieProvider, boolean enforceEmbeddedCollections) {

super(Collection.class, false);

this.property = property;
this.relProvider = relProvider;
this.curieProvider = curieProvider;
this.enforceEmbeddedCollections = enforceEmbeddedCollections;
}

/*
* (non-Javadoc)
*
*
* @see org.codehaus.jackson.map.ser.std.SerializerBase#serialize(java.lang.Object, org.codehaus.jackson.JsonGenerator,
* org.codehaus.jackson.map.SerializerProvider)
*/
@Override
public void serialize(Collection<?> value, JsonGenerator jgen, SerializerProvider provider) throws IOException,
JsonGenerationException {

HalEmbeddedBuilder builder = new HalEmbeddedBuilder(relProvider, enforceEmbeddedCollections);
HalEmbeddedBuilder builder = new HalEmbeddedBuilder(relProvider, curieProvider, enforceEmbeddedCollections);

for (Object resource : value) {
builder.add(resource);
Expand All @@ -275,7 +277,7 @@ public void serialize(Collection<?> value, JsonGenerator jgen, SerializerProvide
@Override
public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property)
throws JsonMappingException {
return new HalResourcesSerializer(property, relProvider, enforceEmbeddedCollections);
return new HalResourcesSerializer(property, relProvider, curieProvider, enforceEmbeddedCollections);
}

@Override
Expand Down Expand Up @@ -307,7 +309,7 @@ protected ContainerSerializer<?> _withValueTypeSerializer(TypeSerializer vts) {
/**
* Custom {@link JsonSerializer} to render Link instances in HAL compatible JSON. Renders the {@link Link} as
* immediate object if we have a single one or as array if we have multiple ones.
*
*
* @author Alexander Baetz
* @author Oliver Gierke
*/
Expand All @@ -323,7 +325,7 @@ public OptionalListJackson2Serializer() {

/**
* Creates a new {@link OptionalListJackson2Serializer} using the given {@link BeanProperty}.
*
*
* @param property
*/
public OptionalListJackson2Serializer(BeanProperty property) {
Expand Down Expand Up @@ -412,7 +414,7 @@ public JavaType getContentType() {

/*
* (non-Javadoc)
*
*
* @see com.fasterxml.jackson.databind.ser.ContainerSerializer#hasSingleElement(java.lang.Object)
*/
@Override
Expand All @@ -422,7 +424,7 @@ public boolean hasSingleElement(Object arg0) {

/*
* (non-Javadoc)
*
*
* @see com.fasterxml.jackson.databind.ser.ContainerSerializer#isEmpty(java.lang.Object)
*/
@Override
Expand All @@ -432,7 +434,7 @@ public boolean isEmpty(Object arg0) {

/*
* (non-Javadoc)
*
*
* @see com.fasterxml.jackson.databind.ser.ContextualSerializer#createContextual(com.fasterxml.jackson.databind.SerializerProvider,
* com.fasterxml.jackson.databind.BeanProperty)
*/
Expand Down Expand Up @@ -601,7 +603,7 @@ public HalHandlerInstantiator(RelProvider resolver, CurieProvider curieProvider,

Assert.notNull(resolver, "RelProvider must not be null!");
this.instanceMap.put(HalResourcesSerializer.class, new HalResourcesSerializer(resolver,
enforceEmbeddedCollections));
curieProvider, enforceEmbeddedCollections));
this.instanceMap.put(HalLinkListSerializer.class, new HalLinkListSerializer(curieProvider));
}

Expand Down Expand Up @@ -662,7 +664,7 @@ public TypeIdResolver typeIdResolverInstance(MapperConfig<?> config, Annotated a

/**
* {@link JsonSerializer} to only render {@link Boolean} values if they're set to {@literal true}.
*
*
* @author Oliver Gierke
* @since 0.9
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@

/**
* Unit tests for {@link HalEmbeddedBuilder}.
*
*
* @author Oliver Gierke
* @author Dietrich Schulten
*/
public class HalEmbeddedBuilderUnitTest {

RelProvider provider;
CurieProvider curieProvider;

@Before
public void setUp() {
Expand Down Expand Up @@ -81,7 +82,7 @@ public void correctlyPilesUpResourcesInCollectionRel() {
@Test
public void forcesCollectionRelToBeUsedIfConfigured() {

HalEmbeddedBuilder builder = new HalEmbeddedBuilder(provider, true);
HalEmbeddedBuilder builder = new HalEmbeddedBuilder(provider, curieProvider, true);
builder.add("Sample");

assertThat(builder.asMap().get("string"), is(nullValue()));
Expand All @@ -96,7 +97,7 @@ public void doesNotPreferCollectionsIfRelAwareWasAdded() {

EmbeddedWrappers wrappers = new EmbeddedWrappers(false);

HalEmbeddedBuilder builder = new HalEmbeddedBuilder(provider, true);
HalEmbeddedBuilder builder = new HalEmbeddedBuilder(provider, curieProvider, true);
builder.add(wrappers.wrap("MyValue", "foo"));

assertThat(builder.asMap().get("foo"), is(instanceOf(String.class)));
Expand All @@ -107,7 +108,7 @@ public void doesNotPreferCollectionsIfRelAwareWasAdded() {
*/
@Test(expected = IllegalArgumentException.class)
public void rejectsNullRelProvider() {
new HalEmbeddedBuilder(null, false);
new HalEmbeddedBuilder(null, curieProvider, false);
}

private static void assertHasValues(Map<String, Object> source, String rel, Object... values) {
Expand All @@ -120,7 +121,7 @@ private static void assertHasValues(Map<String, Object> source, String rel, Obje

private Map<String, Object> setUpBuilder(Object... values) {

HalEmbeddedBuilder builder = new HalEmbeddedBuilder(provider, false);
HalEmbeddedBuilder builder = new HalEmbeddedBuilder(provider, curieProvider, false);

for (Object value : values) {
builder.add(value);
Expand Down