From ad2496374c884f46caa9c59b002a0f329e71b091 Mon Sep 17 00:00:00 2001 From: Jon Brisbin Date: Thu, 25 Apr 2013 15:07:21 -0500 Subject: [PATCH] #92 Add special Resource subclass that is returned by listRepositories method so that ResourceProcessors can easily add links to it. --- .../rest/webmvc/RepositoryController.java | 35 +++++++++---------- .../rest/webmvc/RepositoryLinksResource.java | 16 +++++++++ 2 files changed, 32 insertions(+), 19 deletions(-) create mode 100644 spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryLinksResource.java diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryController.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryController.java index 877226c27..c09b24e65 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryController.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryController.java @@ -1,8 +1,5 @@ package org.springframework.data.rest.webmvc; -import static java.util.Collections.*; -import static org.springframework.data.rest.repository.support.ResourceMappingUtils.*; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.convert.ConversionService; import org.springframework.data.repository.support.DomainClassConverter; @@ -10,12 +7,13 @@ import org.springframework.data.repository.support.Repositories; import org.springframework.data.rest.config.RepositoryRestConfiguration; import org.springframework.data.rest.config.ResourceMapping; import org.springframework.hateoas.EntityLinks; -import org.springframework.hateoas.Resource; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; +import static org.springframework.data.rest.repository.support.ResourceMappingUtils.getResourceMapping; + /** * @author Jon Brisbin */ @@ -25,15 +23,15 @@ public class RepositoryController extends AbstractRepositoryRestController { @Autowired public RepositoryController(Repositories repositories, - RepositoryRestConfiguration config, - DomainClassConverter domainClassConverter, - ConversionService conversionService, - EntityLinks entityLinks) { + RepositoryRestConfiguration config, + DomainClassConverter domainClassConverter, + ConversionService conversionService, + EntityLinks entityLinks) { super(repositories, - config, - domainClassConverter, - conversionService, - entityLinks); + config, + domainClassConverter, + conversionService, + entityLinks); } @RequestMapping( @@ -44,16 +42,15 @@ public class RepositoryController extends AbstractRepositoryRestController { } ) @ResponseBody - public Resource listRepositories() - throws ResourceNotFoundException { - Resource links = new Resource(emptyList()); - for(Class domainType : repositories) { + public RepositoryLinksResource listRepositories() throws ResourceNotFoundException { + RepositoryLinksResource resource = new RepositoryLinksResource(); + for (Class domainType : repositories) { ResourceMapping repoMapping = getResourceMapping(config, repositories.getRepositoryInformationFor(domainType)); - if(repoMapping.isExported()) { - links.add(entityLinks.linkToCollectionResource(domainType)); + if (repoMapping.isExported()) { + resource.add(entityLinks.linkToCollectionResource(domainType)); } } - return links; + return resource; } } diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryLinksResource.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryLinksResource.java new file mode 100644 index 000000000..8c1101e6e --- /dev/null +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryLinksResource.java @@ -0,0 +1,16 @@ +package org.springframework.data.rest.webmvc; + +import org.springframework.hateoas.Resources; + +import java.util.Collections; + +/** + * @author Jon Brisbin + */ +public class RepositoryLinksResource extends Resources { + + public RepositoryLinksResource() { + super(Collections.emptyList()); + } + +}