DATACMNS-418 - Use TemplateVariables value type for pagination templates.
Changed Hateoas(Pageable|Sort)HandlerMethodArgumentResolver to use newly introduced TypeVariables abstraction to avoid having to deal with string representations manually. The registered TemplateVariables carry a "pagination.….description" key to be resolved against a MessageSource.
This commit is contained in:
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.data.web;
|
||||
|
||||
import static org.springframework.hateoas.TemplateVariable.VariableType.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
@@ -22,9 +24,11 @@ import java.util.List;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.hateoas.TemplateVariable;
|
||||
import org.springframework.hateoas.TemplateVariable.VariableType;
|
||||
import org.springframework.hateoas.TemplateVariables;
|
||||
import org.springframework.hateoas.mvc.UriComponentsContributor;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.util.UriComponents;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
@@ -90,40 +94,27 @@ public class HateoasPageableHandlerMethodArgumentResolver extends PageableHandle
|
||||
* @return
|
||||
* @since 1.7
|
||||
*/
|
||||
public String getPaginationTemplateVariables(MethodParameter parameter, UriComponents template) {
|
||||
public TemplateVariables getPaginationTemplateVariables(MethodParameter parameter, UriComponents template) {
|
||||
|
||||
String pagePropertyName = getParameterNameToUse(getPageParameterName(), parameter);
|
||||
String sizePropertyName = getParameterNameToUse(getSizeParameterName(), parameter);
|
||||
|
||||
List<String> names = new ArrayList<String>();
|
||||
List<TemplateVariable> names = new ArrayList<TemplateVariable>();
|
||||
MultiValueMap<String, String> queryParameters = template.getQueryParams();
|
||||
boolean existingFound = false;
|
||||
boolean append = !queryParameters.isEmpty();
|
||||
|
||||
for (String propertyName : Arrays.asList(pagePropertyName, sizePropertyName)) {
|
||||
|
||||
if (!queryParameters.containsKey(propertyName)) {
|
||||
names.add(propertyName);
|
||||
} else {
|
||||
existingFound = true;
|
||||
|
||||
VariableType type = append ? REQUEST_PARAM_CONTINUED : REQUEST_PARAM;
|
||||
String description = String.format("pagination.%s.description", propertyName);
|
||||
names.add(new TemplateVariable(propertyName, type, description));
|
||||
}
|
||||
}
|
||||
|
||||
String sortTemplateVariables = sortResolver.getSortTemplateVariables(parameter, template);
|
||||
|
||||
if (StringUtils.hasText(sortTemplateVariables)) {
|
||||
names.add(sortTemplateVariables.substring(2, sortTemplateVariables.length() - 1));
|
||||
}
|
||||
|
||||
if (names.isEmpty()) {
|
||||
return "";
|
||||
}
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append(existingFound ? "{&" : "{?");
|
||||
builder.append(StringUtils.collectionToCommaDelimitedString(names));
|
||||
builder.append("}");
|
||||
|
||||
return builder.toString();
|
||||
TemplateVariables pagingVariables = new TemplateVariables(names);
|
||||
return pagingVariables.concat(sortResolver.getSortTemplateVariables(parameter, template));
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -15,10 +15,15 @@
|
||||
*/
|
||||
package org.springframework.data.web;
|
||||
|
||||
import static org.springframework.hateoas.TemplateVariable.VariableType.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.hateoas.TemplateVariable;
|
||||
import org.springframework.hateoas.TemplateVariable.VariableType;
|
||||
import org.springframework.hateoas.TemplateVariables;
|
||||
import org.springframework.hateoas.mvc.UriComponentsContributor;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
@@ -44,12 +49,19 @@ public class HateoasSortHandlerMethodArgumentResolver extends SortHandlerMethodA
|
||||
* @return
|
||||
* @since 1.7
|
||||
*/
|
||||
public String getSortTemplateVariables(MethodParameter parameter, UriComponents template) {
|
||||
public TemplateVariables getSortTemplateVariables(MethodParameter parameter, UriComponents template) {
|
||||
|
||||
String sortParameter = getSortParameter(parameter);
|
||||
MultiValueMap<String, String> queryParameters = template.getQueryParams();
|
||||
boolean append = !queryParameters.isEmpty();
|
||||
|
||||
return queryParameters.containsKey(sortParameter) ? "" : String.format("{?%s}", sortParameter);
|
||||
if (queryParameters.containsKey(sortParameter)) {
|
||||
return TemplateVariables.NONE;
|
||||
}
|
||||
|
||||
String description = String.format("pagination.%s.description", sortParameter);
|
||||
VariableType type = append ? REQUEST_PARAM_CONTINUED : REQUEST_PARAM;
|
||||
return new TemplateVariables(new TemplateVariable(sortParameter, type, description));
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -29,6 +29,8 @@ import org.springframework.hateoas.PagedResources.PageMetadata;
|
||||
import org.springframework.hateoas.Resource;
|
||||
import org.springframework.hateoas.ResourceAssembler;
|
||||
import org.springframework.hateoas.ResourceSupport;
|
||||
import org.springframework.hateoas.TemplateVariables;
|
||||
import org.springframework.hateoas.UriTemplate;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
||||
import org.springframework.web.util.UriComponents;
|
||||
@@ -143,8 +145,8 @@ public class PagedResourcesAssembler<T> implements ResourceAssembler<Page<T>, Pa
|
||||
}
|
||||
|
||||
UriComponents uriComponents = UriComponentsBuilder.fromUriString(uri).build();
|
||||
resources.add(new Link(uri + pageableResolver.getPaginationTemplateVariables(getMethodParameter(), uriComponents),
|
||||
Link.REL_SELF));
|
||||
TemplateVariables variables = pageableResolver.getPaginationTemplateVariables(getMethodParameter(), uriComponents);
|
||||
resources.add(new Link(new UriTemplate(uri, variables), Link.REL_SELF));
|
||||
|
||||
return resources;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user