Changed `getCurieInformation(…)` to receive a `Links` instance with the previously added links and is forced to return a collection of curies so that multiple ones can be resolved transparently. Made Curie value class protected to be able to reuse it from extensions of DefaultCurieProvider. Switched to the custom HATEOAS UriTemplate instance instead of the standard Spring MVC one. Polished JavaDoc of DefaultCurieProvider. Removed some of the deprecation warnings that were introduced by the upgrade to Jackson 2.3. Polished (read: reactivated) some test cases and slightly changed the internals of UriTemplate works. Added TemplateVariables.asList().
This commit is contained in:
@@ -90,6 +90,15 @@ public class TemplateVariables implements Iterable<TemplateVariable> {
|
||||
return concat(variables.variables);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the contained {@link TemplateVariable}s as {@link List}.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public List<TemplateVariable> asList() {
|
||||
return this.variables;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Iterable#iterator()
|
||||
|
||||
@@ -40,7 +40,7 @@ public class UriTemplate implements Iterable<TemplateVariable> {
|
||||
|
||||
private static final Pattern VARIABLE_REGEX = Pattern.compile("\\{([\\?\\&#/]?)([\\w\\,]+)\\}");
|
||||
|
||||
private final List<TemplateVariable> variables = new ArrayList<TemplateVariable>();
|
||||
private final TemplateVariables variables;;
|
||||
private String baseUri;
|
||||
|
||||
/**
|
||||
@@ -53,24 +53,27 @@ public class UriTemplate implements Iterable<TemplateVariable> {
|
||||
Assert.hasText(template, "Template must not be null or empty!");
|
||||
|
||||
Matcher matcher = VARIABLE_REGEX.matcher(template);
|
||||
int baseUriEndIndex = template.length();
|
||||
List<TemplateVariable> variables = new ArrayList<TemplateVariable>();
|
||||
|
||||
while (matcher.find()) {
|
||||
|
||||
if (baseUri == null) {
|
||||
this.baseUri = template.substring(0, matcher.start(0));
|
||||
int start = matcher.start(0);
|
||||
|
||||
if (start < baseUriEndIndex) {
|
||||
baseUriEndIndex = start;
|
||||
}
|
||||
|
||||
VariableType type = VariableType.from(matcher.group(1));
|
||||
String[] names = matcher.group(2).split(",");
|
||||
|
||||
for (String name : names) {
|
||||
this.variables.add(new TemplateVariable(name, type));
|
||||
variables.add(new TemplateVariable(name, type));
|
||||
}
|
||||
}
|
||||
|
||||
if (this.baseUri == null) {
|
||||
this.baseUri = template;
|
||||
}
|
||||
this.variables = new TemplateVariables(variables);
|
||||
this.baseUri = template.substring(0, baseUriEndIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -94,7 +97,7 @@ public class UriTemplate implements Iterable<TemplateVariable> {
|
||||
* @return
|
||||
*/
|
||||
public List<TemplateVariable> getVariables() {
|
||||
return this.variables;
|
||||
return this.variables.asList();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -162,6 +165,15 @@ public class UriTemplate implements Iterable<TemplateVariable> {
|
||||
return this.variables.iterator();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return baseUri + variables.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends the value for the given {@link TemplateVariable} to the given {@link UriComponentsBuilder}.
|
||||
*
|
||||
|
||||
@@ -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.
|
||||
@@ -15,13 +15,17 @@
|
||||
*/
|
||||
package org.springframework.hateoas.hal;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.springframework.hateoas.Link;
|
||||
import org.springframework.hateoas.Links;
|
||||
|
||||
/**
|
||||
* API to provide HAL curie information for links.
|
||||
*
|
||||
* @see http://tools.ietf.org/html/draft-kelly-json-hal#section-8.2
|
||||
* @author Oliver Gierke
|
||||
* @since 0.9
|
||||
*/
|
||||
public interface CurieProvider {
|
||||
|
||||
@@ -38,7 +42,8 @@ public interface CurieProvider {
|
||||
* Returns an object to render as the base curie information. Implementations have to make sure, the retunred
|
||||
* instances renders as defined in the spec.
|
||||
*
|
||||
* @param links the {@link Links} that have been added to the response so far.
|
||||
* @return must not be {@literal null}.
|
||||
*/
|
||||
Object getCurieInformation();
|
||||
Collection<? extends Object> getCurieInformation(Links links);
|
||||
}
|
||||
|
||||
@@ -15,13 +15,20 @@
|
||||
*/
|
||||
package org.springframework.hateoas.hal;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.springframework.hateoas.IanaRels;
|
||||
import org.springframework.hateoas.Link;
|
||||
import org.springframework.hateoas.Links;
|
||||
import org.springframework.hateoas.UriTemplate;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.util.UriTemplate;
|
||||
|
||||
/**
|
||||
* Default implementation of {@link CurieProvider} rendering a single configurable {@link UriTemplate} based curie.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @since 0.9
|
||||
*/
|
||||
public class DefaultCurieProvider implements CurieProvider {
|
||||
|
||||
@@ -48,8 +55,8 @@ public class DefaultCurieProvider implements CurieProvider {
|
||||
* @see org.springframework.hateoas.hal.CurieProvider#getCurieInformation()
|
||||
*/
|
||||
@Override
|
||||
public Curie getCurieInformation() {
|
||||
return curie;
|
||||
public Collection<? extends Object> getCurieInformation(Links links) {
|
||||
return Collections.singleton(curie);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -70,8 +77,7 @@ public class DefaultCurieProvider implements CurieProvider {
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
private static class Curie extends Link {
|
||||
protected static class Curie extends Link {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
|
||||
@@ -18,7 +18,6 @@ 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;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
@@ -28,6 +27,7 @@ import java.util.Map;
|
||||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.hateoas.Link;
|
||||
import org.springframework.hateoas.Links;
|
||||
import org.springframework.hateoas.RelProvider;
|
||||
import org.springframework.hateoas.Resource;
|
||||
import org.springframework.hateoas.ResourceSupport;
|
||||
@@ -132,6 +132,7 @@ public class Jackson2HalModule extends SimpleModule {
|
||||
|
||||
// sort links according to their relation
|
||||
Map<String, List<Object>> sortedLinks = new LinkedHashMap<String, List<Object>>();
|
||||
List<Link> links = new ArrayList<Link>();
|
||||
|
||||
boolean prefixingRequired = curieProvider != null;
|
||||
boolean curiedLinkPresent = false;
|
||||
@@ -148,13 +149,15 @@ public class Jackson2HalModule extends SimpleModule {
|
||||
sortedLinks.put(rel, new ArrayList<Object>());
|
||||
}
|
||||
|
||||
links.add(link);
|
||||
sortedLinks.get(rel).add(link);
|
||||
}
|
||||
|
||||
if (prefixingRequired && curiedLinkPresent) {
|
||||
Object curieInformation = curieProvider.getCurieInformation();
|
||||
List<Object> curies = new ArrayList<Object>();
|
||||
curies.add(Arrays.asList(curieInformation));
|
||||
|
||||
ArrayList<Object> curies = new ArrayList<Object>();
|
||||
curies.add(curieProvider.getCurieInformation(new Links(links)));
|
||||
|
||||
sortedLinks.put("curies", curies);
|
||||
}
|
||||
|
||||
@@ -164,7 +167,7 @@ public class Jackson2HalModule extends SimpleModule {
|
||||
JavaType mapType = typeFactory.constructMapType(HashMap.class, keyType, valueType);
|
||||
|
||||
MapSerializer serializer = MapSerializer.construct(new String[] {}, mapType, true, null,
|
||||
provider.findKeySerializer(keyType, null), new OptionalListJackson2Serializer(property));
|
||||
provider.findKeySerializer(keyType, null), new OptionalListJackson2Serializer(property), null);
|
||||
|
||||
serializer.serialize(sortedLinks, jgen, provider);
|
||||
}
|
||||
@@ -270,7 +273,7 @@ public class Jackson2HalModule extends SimpleModule {
|
||||
JavaType mapType = typeFactory.constructMapType(HashMap.class, keyType, valueType);
|
||||
|
||||
MapSerializer serializer = MapSerializer.construct(new String[] {}, mapType, true, null,
|
||||
provider.findKeySerializer(keyType, null), new OptionalListJackson2Serializer(property));
|
||||
provider.findKeySerializer(keyType, null), new OptionalListJackson2Serializer(property), null);
|
||||
|
||||
serializer.serialize(builder.asMap(), jgen, provider);
|
||||
}
|
||||
@@ -451,6 +454,7 @@ public class Jackson2HalModule extends SimpleModule {
|
||||
|
||||
private static final long serialVersionUID = 6420432361123210955L;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public HalLinkListDeserializer() {
|
||||
super(List.class);
|
||||
}
|
||||
@@ -524,6 +528,7 @@ public class Jackson2HalModule extends SimpleModule {
|
||||
this(null, vc);
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private HalResourcesDeserializer(Class<?> type, JavaType contentType) {
|
||||
|
||||
super(type);
|
||||
|
||||
Reference in New Issue
Block a user