DATAREST-31 - Search links are now rendered as link templates.

MethodResourceMapping now exposes the parameters a query method expects. The RepositorySearchController the uses these to append the 

Upgraded to Spring Data Commons 1.7.0.BUILD-SNAPSHOT.
This commit is contained in:
Oliver Gierke
2014-01-17 13:28:10 +01:00
parent 5ff6457911
commit 4b6f0f620a
7 changed files with 116 additions and 22 deletions

View File

@@ -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.
@@ -19,13 +19,14 @@ import static org.springframework.data.rest.webmvc.ControllerUtils.*;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.rest.core.invoke.RepositoryInvoker;
import org.springframework.data.rest.core.mapping.ResourceMapping;
import org.springframework.data.rest.core.mapping.MethodResourceMapping;
import org.springframework.data.rest.core.mapping.ResourceMappings;
import org.springframework.data.rest.core.mapping.ResourceMetadata;
import org.springframework.data.rest.core.mapping.SearchResourceMappings;
@@ -33,6 +34,7 @@ import org.springframework.data.web.PagedResourcesAssembler;
import org.springframework.hateoas.EntityLinks;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.LinkBuilder;
import org.springframework.hateoas.LinkTemplate;
import org.springframework.hateoas.Links;
import org.springframework.hateoas.Resource;
import org.springframework.hateoas.ResourceSupport;
@@ -40,6 +42,7 @@ import org.springframework.hateoas.Resources;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@@ -56,6 +59,7 @@ class RepositorySearchController extends AbstractRepositoryRestController {
private static final String SEARCH = "/search";
private static final String BASE_MAPPING = "/{repository}" + SEARCH;
private static final String PARAMETER_NAME_TEMPALTE_PATTERN = "{?%s}";
private final EntityLinks entityLinks;
private final ResourceMappings mappings;
@@ -223,15 +227,23 @@ class RepositorySearchController extends AbstractRepositoryRestController {
SearchResourceMappings searchMappings = mappings.getSearchResourceMappings(domainType);
LinkBuilder builder = entityLinks.linkFor(domainType).slash(searchMappings.getPath());
for (ResourceMapping mapping : searchMappings) {
for (MethodResourceMapping mapping : searchMappings) {
if (!mapping.isExported()) {
continue;
}
links.add(builder.slash(mapping.getPath()).withRel(mapping.getRel()));
String parameterTemplateVariable = getParameterTemplateVariable(mapping.getParameterNames());
String href = builder.slash(mapping.getPath()).toString().concat(parameterTemplateVariable);
links.add(new LinkTemplate(href, mapping.getRel()));
}
return new Links(links);
}
private static String getParameterTemplateVariable(Collection<String> parameters) {
String parameterString = StringUtils.collectionToCommaDelimitedString(parameters);
return parameters.isEmpty() ? "" : String.format(PARAMETER_NAME_TEMPALTE_PATTERN, parameterString);
}
}

View File

@@ -60,11 +60,10 @@ public class RepositorySearchControllerIntegrationTests extends AbstractControll
ResourceTester tester = ResourceTester.of(resource);
tester.assertNumberOfLinks(4);
tester.assertHasLink("findFirstPersonByFirstName", "http://localhost/people/search/findFirstPersonByFirstName");
tester.assertHasLink("firstname", "http://localhost/people/search/firstname");
tester.assertHasLink("findByCreatedUsingISO8601Date",
"http://localhost/people/search/findByCreatedUsingISO8601Date");
tester.assertHasLink("findByCreatedGreaterThan", "http://localhost/people/search/findByCreatedGreaterThan");
tester.assertHasLinkEndingWith("findFirstPersonByFirstName", "findFirstPersonByFirstName{?firstName}");
tester.assertHasLinkEndingWith("firstname", "firstname{?firstName}");
tester.assertHasLinkEndingWith("findByCreatedUsingISO8601Date", "findByCreatedUsingISO8601Date{?date}");
tester.assertHasLinkEndingWith("findByCreatedGreaterThan", "findByCreatedGreaterThan{?date}");
}
@Test(expected = ResourceNotFoundException.class)

View File

@@ -18,6 +18,7 @@ package org.springframework.data.rest.webmvc;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.hamcrest.Matcher;
import org.springframework.data.rest.core.Path;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.PagedResources;
@@ -61,19 +62,33 @@ public class ResourceTester {
}
/**
* Asserts that the {@link Resource} has a linke with the given rel and href.
* Asserts that the {@link Resource} has a link with the given rel and href.
*
* @param rel must not be {@literal null}.
* @param href can be {@literal null}, if so, only the presence of a {@link Link} with the given rel is checked.
*/
public Link assertHasLink(String rel, String href) {
return assertHasLinkMatching(rel, href == null ? null : is(href));
}
/**
* Asserts that the {@link Resource} has a link with the given rel and ending with the given href.
*
* @param rel must not be {@literal null}.
* @param href can be {@literal null}, if so, only the presence of a {@link Link} with the given rel is checked.
*/
public Link assertHasLinkEndingWith(String rel, String hrefEnd) {
return assertHasLinkMatching(rel, hrefEnd == null ? null : endsWith(hrefEnd));
}
private final Link assertHasLinkMatching(String rel, Matcher<String> hrefMatcher) {
Link link = resource.getLink(rel);
assertThat("Expected link with rel '" + rel + "' but didn't find it in " + resource.getLinks(), link,
is(notNullValue()));
if (href != null) {
assertThat(link.getHref(), is(href));
if (hrefMatcher != null) {
assertThat(link.getHref(), is(hrefMatcher));
}
return link;