#217 - Improve UriTempalte's handling of TemplateVariables.
TempalteVariables now concatenates all request parameter styles correctly. UriTemplate uses continued style of request parameters if the base URI already contains request parameters.
This commit is contained in:
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.hateoas;
|
||||
|
||||
import static org.springframework.hateoas.TemplateVariable.VariableType.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
@@ -119,6 +121,35 @@ public final class TemplateVariable implements Serializable {
|
||||
return this.type.canBeCombinedWith(variable.type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the given {@link TemplateVariable} is logically equivalent to the given one. This considers request
|
||||
* parameter variables equivalent independently from whether they're continued or not.
|
||||
*
|
||||
* @param variable
|
||||
* @return
|
||||
*/
|
||||
boolean isEquivalent(TemplateVariable variable) {
|
||||
return this.name.equals(variable.name) && isCombinable(variable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the current {@link TemplateVariable} is representing a request parameter.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
boolean isRequestParameterVariable() {
|
||||
return type.equals(REQUEST_PARAM) || type.equals(REQUEST_PARAM_CONTINUED);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the variable is a fragement one.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
boolean isFragment() {
|
||||
return type.equals(FRAGMENT);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.hateoas;
|
||||
|
||||
import static org.springframework.hateoas.TemplateVariable.VariableType.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@@ -23,6 +25,7 @@ import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.hateoas.TemplateVariable.VariableType;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
@@ -79,7 +82,7 @@ public final class TemplateVariables implements Iterable<TemplateVariable>, Seri
|
||||
result.addAll(this.variables);
|
||||
|
||||
for (TemplateVariable variable : variables) {
|
||||
if (!result.contains(variable)) {
|
||||
if (!containsEquivalentFor(variable)) {
|
||||
result.add(variable);
|
||||
}
|
||||
}
|
||||
@@ -106,6 +109,17 @@ public final class TemplateVariables implements Iterable<TemplateVariable>, Seri
|
||||
return this.variables;
|
||||
}
|
||||
|
||||
private boolean containsEquivalentFor(TemplateVariable candidate) {
|
||||
|
||||
for (TemplateVariable variable : this.variables) {
|
||||
if (variable.isEquivalent(candidate)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Iterable#iterator()
|
||||
@@ -121,6 +135,16 @@ public final class TemplateVariables implements Iterable<TemplateVariable>, Seri
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return toString(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the string representation of the template but forcing a continued style of expressing request parameters.
|
||||
*
|
||||
* @param appended
|
||||
* @return
|
||||
*/
|
||||
String toString(boolean appended) {
|
||||
|
||||
if (variables.isEmpty()) {
|
||||
return "";
|
||||
@@ -131,10 +155,13 @@ public final class TemplateVariables implements Iterable<TemplateVariable>, Seri
|
||||
|
||||
for (TemplateVariable variable : variables) {
|
||||
|
||||
VariableType type = variable.getType();
|
||||
type = appended && type.equals(REQUEST_PARAM) ? REQUEST_PARAM_CONTINUED : type;
|
||||
|
||||
if (previous == null) {
|
||||
builder.append("{").append(variable.getType().toString());
|
||||
builder.append("{").append(type.toString());
|
||||
} else if (!previous.isCombinable(variable)) {
|
||||
builder.append("}{").append(variable.getType().toString());
|
||||
builder.append("}{").append(type.toString());
|
||||
} else {
|
||||
builder.append(",");
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ import java.util.regex.Pattern;
|
||||
import org.springframework.hateoas.TemplateVariable.VariableType;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.util.UriComponents;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
/**
|
||||
@@ -106,7 +107,26 @@ public class UriTemplate implements Iterable<TemplateVariable>, Serializable {
|
||||
return this;
|
||||
}
|
||||
|
||||
return new UriTemplate(baseUri, this.variables.concat(variables));
|
||||
UriComponents components = UriComponentsBuilder.fromUriString(baseUri).build();
|
||||
List<TemplateVariable> result = new ArrayList<TemplateVariable>();
|
||||
|
||||
for (TemplateVariable variable : variables) {
|
||||
|
||||
boolean isRequestParam = variable.isRequestParameterVariable();
|
||||
boolean alreadyPresent = components.getQueryParams().containsKey(variable.getName());
|
||||
|
||||
if (isRequestParam && alreadyPresent) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (variable.isFragment() && StringUtils.hasText(components.getFragment())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
result.add(variable);
|
||||
}
|
||||
|
||||
return new UriTemplate(baseUri, this.variables.concat(result));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -212,7 +232,11 @@ public class UriTemplate implements Iterable<TemplateVariable>, Serializable {
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return baseUri + getOptionalVariables().toString();
|
||||
|
||||
UriComponents components = UriComponentsBuilder.fromUriString(baseUri).build();
|
||||
boolean hasQueryParameters = !components.getQueryParams().isEmpty();
|
||||
|
||||
return baseUri + getOptionalVariables().toString(hasQueryParameters);
|
||||
}
|
||||
|
||||
private TemplateVariables getOptionalVariables() {
|
||||
|
||||
Reference in New Issue
Block a user