From eaf88b18e39a7ce8b9e62e9b644a044231ffe3d0 Mon Sep 17 00:00:00 2001 From: Jon Brisbin Date: Tue, 5 Mar 2013 07:41:14 -0600 Subject: [PATCH] Fix for DATAREST-71. --- .../webmvc/RepositoryEntityController.java | 615 +++++++++--------- 1 file changed, 309 insertions(+), 306 deletions(-) diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryEntityController.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryEntityController.java index aab4cb9a4..8e7e9bce9 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryEntityController.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryEntityController.java @@ -46,343 +46,346 @@ import org.springframework.web.bind.annotation.ResponseBody; @RequestMapping("/{repository}") public class RepositoryEntityController extends AbstractRepositoryRestController { - @Autowired - private DomainObjectMerger domainObjectMerger; - @Autowired - private PersistentEntityToJsonSchemaConverter jsonSchemaConverter; + @Autowired + private DomainObjectMerger domainObjectMerger; + @Autowired + private PersistentEntityToJsonSchemaConverter jsonSchemaConverter; - public RepositoryEntityController(Repositories repositories, - RepositoryRestConfiguration config, - DomainClassConverter domainClassConverter, - ConversionService conversionService) { - super(repositories, config, domainClassConverter, conversionService); - } + public RepositoryEntityController(Repositories repositories, + RepositoryRestConfiguration config, + DomainClassConverter domainClassConverter, + ConversionService conversionService) { + super(repositories, config, domainClassConverter, conversionService); + } - @RequestMapping( - value = "/schema", - method = RequestMethod.GET, - produces = { - "application/schema+json" - } - ) - @ResponseBody - public JsonSchema schema(RepositoryRestRequest repoRequest) { - return jsonSchemaConverter.convert(repoRequest.getPersistentEntity().getType()); - } + @RequestMapping( + value = "/schema", + method = RequestMethod.GET, + produces = { + "application/schema+json" + } + ) + @ResponseBody + public JsonSchema schema(RepositoryRestRequest repoRequest) { + return jsonSchemaConverter.convert(repoRequest.getPersistentEntity().getType()); + } - @SuppressWarnings({"unchecked"}) - @RequestMapping( - method = RequestMethod.GET, - produces = { - "application/json", - "application/x-spring-data-verbose+json" - } - ) - @ResponseBody - public Resources> listEntities(RepositoryRestRequest repoRequest) - throws ResourceNotFoundException { - List> resources = new ArrayList>(); - List links = new ArrayList(); + @SuppressWarnings({"unchecked"}) + @RequestMapping( + method = RequestMethod.GET, + produces = { + "application/json", + "application/x-spring-data-verbose+json" + } + ) + @ResponseBody + public Resources> listEntities(RepositoryRestRequest repoRequest) + throws ResourceNotFoundException { + List> resources = new ArrayList>(); + List links = new ArrayList(); - Iterable results; - RepositoryMethodInvoker repoMethodInvoker = repoRequest.getRepositoryMethodInvoker(); - boolean hasPagingParams = (null != repoRequest.getRequest().getParameter(config.getPageParamName())); - boolean hasSortParams = (null != repoRequest.getRequest().getParameter(config.getSortParamName())); - if(repoMethodInvoker.hasFindAllPageable() && hasPagingParams) { - results = repoMethodInvoker.findAll(new PageRequest(repoRequest.getPagingAndSorting().getPageNumber(), - repoRequest.getPagingAndSorting().getPageSize(), - repoRequest.getPagingAndSorting().getSort())); - } else if(repoMethodInvoker.hasFindAllSorted() && hasSortParams) { - results = repoMethodInvoker.findAll(repoRequest.getPagingAndSorting().getSort()); - } else if(repoMethodInvoker.hasFindAll()) { - results = repoMethodInvoker.findAll(); - } else { - throw new ResourceNotFoundException(); - } + Iterable results; + RepositoryMethodInvoker repoMethodInvoker = repoRequest.getRepositoryMethodInvoker(); + if(null == repoMethodInvoker) { + throw new ResourceNotFoundException(); + } + boolean hasPagingParams = (null != repoRequest.getRequest().getParameter(config.getPageParamName())); + boolean hasSortParams = (null != repoRequest.getRequest().getParameter(config.getSortParamName())); + if(repoMethodInvoker.hasFindAllPageable() && hasPagingParams) { + results = repoMethodInvoker.findAll(new PageRequest(repoRequest.getPagingAndSorting().getPageNumber(), + repoRequest.getPagingAndSorting().getPageSize(), + repoRequest.getPagingAndSorting().getSort())); + } else if(repoMethodInvoker.hasFindAllSorted() && hasSortParams) { + results = repoMethodInvoker.findAll(repoRequest.getPagingAndSorting().getSort()); + } else if(repoMethodInvoker.hasFindAll()) { + results = repoMethodInvoker.findAll(); + } else { + throw new ResourceNotFoundException(); + } - for(Object o : results) { - resources.add(new PersistentEntityResource(repoRequest.getPersistentEntity(), - o, - repoRequest.buildEntitySelfLink(o, conversionService)) - .setBaseUri(repoRequest.getBaseUri())); - } + for(Object o : results) { + resources.add(new PersistentEntityResource(repoRequest.getPersistentEntity(), + o, + repoRequest.buildEntitySelfLink(o, conversionService)) + .setBaseUri(repoRequest.getBaseUri())); + } - if(!repoMethodInvoker.getQueryMethods().isEmpty()) { - ResourceMapping repoMapping = repoRequest.getRepositoryResourceMapping(); - links.add(new Link(buildUri(repoRequest.getBaseUri(), repoMapping.getPath(), "search").toString(), - repoMapping.getRel() + ".search")); - } + if(!repoMethodInvoker.getQueryMethods().isEmpty()) { + ResourceMapping repoMapping = repoRequest.getRepositoryResourceMapping(); + links.add(new Link(buildUri(repoRequest.getBaseUri(), repoMapping.getPath(), "search").toString(), + repoMapping.getRel() + ".search")); + } - return new Resources>(resources, links); - } + return new Resources>(resources, links); + } - @RequestMapping( - method = RequestMethod.GET, - produces = { - "application/javascript" - } - ) - @ResponseBody - public JsonpResponse>> jsonpListEntities(RepositoryRestRequest repoRequest) - throws ResourceNotFoundException { - return jsonpWrapResponse(repoRequest, listEntities(repoRequest), HttpStatus.OK); - } + @RequestMapping( + method = RequestMethod.GET, + produces = { + "application/javascript" + } + ) + @ResponseBody + public JsonpResponse>> jsonpListEntities(RepositoryRestRequest repoRequest) + throws ResourceNotFoundException { + return jsonpWrapResponse(repoRequest, listEntities(repoRequest), HttpStatus.OK); + } - @SuppressWarnings({"unchecked"}) - @RequestMapping( - method = RequestMethod.GET, - produces = { - "application/x-spring-data-compact+json", - "text/uri-list" - } - ) - @ResponseBody - public Resources> listEntitiesCompact(RepositoryRestRequest repoRequest) - throws ResourceNotFoundException { - Resources> resources = listEntities(repoRequest); - List links = new ArrayList(resources.getLinks()); + @SuppressWarnings({"unchecked"}) + @RequestMapping( + method = RequestMethod.GET, + produces = { + "application/x-spring-data-compact+json", + "text/uri-list" + } + ) + @ResponseBody + public Resources> listEntitiesCompact(RepositoryRestRequest repoRequest) + throws ResourceNotFoundException { + Resources> resources = listEntities(repoRequest); + List links = new ArrayList(resources.getLinks()); - for(Resource resource : resources.getContent()) { - PersistentEntityResource persistentEntityResource = (PersistentEntityResource)resource; - links.add(resourceLink(repoRequest, persistentEntityResource)); - } + for(Resource resource : resources.getContent()) { + PersistentEntityResource persistentEntityResource = (PersistentEntityResource)resource; + links.add(resourceLink(repoRequest, persistentEntityResource)); + } - return new Resources>(EMPTY_RESOURCE_LIST, links); - } + return new Resources>(EMPTY_RESOURCE_LIST, links); + } - @SuppressWarnings({"unchecked"}) - @RequestMapping( - method = RequestMethod.POST, - consumes = { - "application/json" - }, - produces = { - "application/json", - "text/uri-list" - } - ) - @ResponseBody - public ResponseEntity> createNewEntity(RepositoryRestRequest repoRequest, - PersistentEntityResource incoming) { - RepositoryMethodInvoker repoMethodInvoker = repoRequest.getRepositoryMethodInvoker(); - if(!repoMethodInvoker.hasSaveOne()) { - throw new NoSuchMethodError(); - } + @SuppressWarnings({"unchecked"}) + @RequestMapping( + method = RequestMethod.POST, + consumes = { + "application/json" + }, + produces = { + "application/json", + "text/uri-list" + } + ) + @ResponseBody + public ResponseEntity> createNewEntity(RepositoryRestRequest repoRequest, + PersistentEntityResource incoming) { + RepositoryMethodInvoker repoMethodInvoker = repoRequest.getRepositoryMethodInvoker(); + if(null == repoMethodInvoker || !repoMethodInvoker.hasSaveOne()) { + throw new NoSuchMethodError(); + } - applicationContext.publishEvent(new BeforeSaveEvent(incoming.getContent())); - Object obj = repoMethodInvoker.save(incoming.getContent()); - applicationContext.publishEvent(new AfterSaveEvent(obj)); + applicationContext.publishEvent(new BeforeSaveEvent(incoming.getContent())); + Object obj = repoMethodInvoker.save(incoming.getContent()); + applicationContext.publishEvent(new AfterSaveEvent(obj)); - Link selfLink = repoRequest.buildEntitySelfLink(obj, conversionService); - HttpHeaders headers = new HttpHeaders(); - headers.setLocation(URI.create(selfLink.getHref())); + Link selfLink = repoRequest.buildEntitySelfLink(obj, conversionService); + HttpHeaders headers = new HttpHeaders(); + headers.setLocation(URI.create(selfLink.getHref())); - if(config.isReturnBodyOnCreate()) { - return resourceResponse(headers, - new PersistentEntityResource(repoRequest.getPersistentEntity(), - obj, - selfLink) - .setBaseUri(repoRequest.getBaseUri()), - HttpStatus.CREATED); - } else { - return resourceResponse(headers, - null, - HttpStatus.CREATED); - } - } + if(config.isReturnBodyOnCreate()) { + return resourceResponse(headers, + new PersistentEntityResource(repoRequest.getPersistentEntity(), + obj, + selfLink) + .setBaseUri(repoRequest.getBaseUri()), + HttpStatus.CREATED); + } else { + return resourceResponse(headers, + null, + HttpStatus.CREATED); + } + } - @SuppressWarnings({"unchecked"}) - @RequestMapping( - method = RequestMethod.POST, - consumes = { - "application/json" - }, - produces = { - "application/javascript" - } - ) - @ResponseBody - public JsonpResponse> jsonpCreateNewEntity(RepositoryRestRequest repoRequest, - PersistentEntityResource incoming) { - return jsonpWrapResponse(repoRequest, createNewEntity(repoRequest, incoming)); - } + @SuppressWarnings({"unchecked"}) + @RequestMapping( + method = RequestMethod.POST, + consumes = { + "application/json" + }, + produces = { + "application/javascript" + } + ) + @ResponseBody + public JsonpResponse> jsonpCreateNewEntity(RepositoryRestRequest repoRequest, + PersistentEntityResource incoming) { + return jsonpWrapResponse(repoRequest, createNewEntity(repoRequest, incoming)); + } - @SuppressWarnings({"unchecked"}) - @RequestMapping( - value = "/{id}", - method = RequestMethod.GET, - produces = { - "application/json", - "application/x-spring-data-compact+json", - "text/uri-list" - } - ) - @ResponseBody - public Resource getSingleEntity(RepositoryRestRequest repoRequest, - @PathVariable String id) - throws ResourceNotFoundException { - RepositoryMethodInvoker repoMethodInvoker = repoRequest.getRepositoryMethodInvoker(); - if(!repoMethodInvoker.hasFindOne()) { - throw new ResourceNotFoundException(); - } + @SuppressWarnings({"unchecked"}) + @RequestMapping( + value = "/{id}", + method = RequestMethod.GET, + produces = { + "application/json", + "application/x-spring-data-compact+json", + "text/uri-list" + } + ) + @ResponseBody + public Resource getSingleEntity(RepositoryRestRequest repoRequest, + @PathVariable String id) + throws ResourceNotFoundException { + RepositoryMethodInvoker repoMethodInvoker = repoRequest.getRepositoryMethodInvoker(); + if(null == repoMethodInvoker || !repoMethodInvoker.hasFindOne()) { + throw new ResourceNotFoundException(); + } - Object domainObj = domainClassConverter.convert(id, - STRING_TYPE, - TypeDescriptor.valueOf(repoRequest.getPersistentEntity() - .getType())); - if(null == domainObj) { - throw new ResourceNotFoundException(); - } + Object domainObj = domainClassConverter.convert(id, + STRING_TYPE, + TypeDescriptor.valueOf(repoRequest.getPersistentEntity() + .getType())); + if(null == domainObj) { + throw new ResourceNotFoundException(); + } - PersistentEntityResource per = PersistentEntityResource.wrap(repoRequest.getPersistentEntity(), - domainObj, - repoRequest.getBaseUri()); - per.add(repoRequest.buildEntitySelfLink(domainObj, conversionService)); - return per; - } + PersistentEntityResource per = PersistentEntityResource.wrap(repoRequest.getPersistentEntity(), + domainObj, + repoRequest.getBaseUri()); + per.add(repoRequest.buildEntitySelfLink(domainObj, conversionService)); + return per; + } - @SuppressWarnings({"unchecked"}) - @RequestMapping( - value = "/{id}", - method = RequestMethod.GET, - produces = { - "application/javascript" - } - ) - @ResponseBody - public JsonpResponse> jsonpGetSingleEntity(RepositoryRestRequest repoRequest, - @PathVariable String id) - throws ResourceNotFoundException { - return jsonpWrapResponse(repoRequest, - getSingleEntity(repoRequest, id), - HttpStatus.OK); - } + @SuppressWarnings({"unchecked"}) + @RequestMapping( + value = "/{id}", + method = RequestMethod.GET, + produces = { + "application/javascript" + } + ) + @ResponseBody + public JsonpResponse> jsonpGetSingleEntity(RepositoryRestRequest repoRequest, + @PathVariable String id) + throws ResourceNotFoundException { + return jsonpWrapResponse(repoRequest, + getSingleEntity(repoRequest, id), + HttpStatus.OK); + } - @SuppressWarnings({"unchecked"}) - @RequestMapping( - value = "/{id}", - method = RequestMethod.PUT, - consumes = { - "application/json" - }, - produces = { - "application/json", - "text/uri-list" - } - ) - @ResponseBody - public ResponseEntity> updateEntity(RepositoryRestRequest repoRequest, - PersistentEntityResource incoming, - @PathVariable String id) - throws ResourceNotFoundException { - RepositoryMethodInvoker repoMethodInvoker = repoRequest.getRepositoryMethodInvoker(); - if(!repoMethodInvoker.hasSaveOne() || !repoMethodInvoker.hasFindOne()) { - throw new NoSuchMethodError(); - } + @SuppressWarnings({"unchecked"}) + @RequestMapping( + value = "/{id}", + method = RequestMethod.PUT, + consumes = { + "application/json" + }, + produces = { + "application/json", + "text/uri-list" + } + ) + @ResponseBody + public ResponseEntity> updateEntity(RepositoryRestRequest repoRequest, + PersistentEntityResource incoming, + @PathVariable String id) + throws ResourceNotFoundException { + RepositoryMethodInvoker repoMethodInvoker = repoRequest.getRepositoryMethodInvoker(); + if(null == repoMethodInvoker || !repoMethodInvoker.hasSaveOne() || !repoMethodInvoker.hasFindOne()) { + throw new NoSuchMethodError(); + } - Object domainObj = domainClassConverter.convert(id, - STRING_TYPE, - TypeDescriptor.valueOf(repoRequest.getPersistentEntity() - .getType())); - if(null == domainObj) { - BeanWrapper incomingWrapper = BeanWrapper.create(incoming.getContent(), conversionService); - PersistentProperty idProp = incoming.getPersistentEntity().getIdProperty(); - incomingWrapper.setProperty(idProp, conversionService.convert(id, idProp.getType())); - return createNewEntity(repoRequest, incoming); - } + Object domainObj = domainClassConverter.convert(id, + STRING_TYPE, + TypeDescriptor.valueOf(repoRequest.getPersistentEntity() + .getType())); + if(null == domainObj) { + BeanWrapper incomingWrapper = BeanWrapper.create(incoming.getContent(), conversionService); + PersistentProperty idProp = incoming.getPersistentEntity().getIdProperty(); + incomingWrapper.setProperty(idProp, conversionService.convert(id, idProp.getType())); + return createNewEntity(repoRequest, incoming); + } - domainObjectMerger.merge(incoming.getContent(), domainObj); + domainObjectMerger.merge(incoming.getContent(), domainObj); - applicationContext.publishEvent(new BeforeSaveEvent(incoming.getContent())); - Object obj = repoMethodInvoker.save(domainObj); - applicationContext.publishEvent(new AfterSaveEvent(obj)); + applicationContext.publishEvent(new BeforeSaveEvent(incoming.getContent())); + Object obj = repoMethodInvoker.save(domainObj); + applicationContext.publishEvent(new AfterSaveEvent(obj)); - if(config.isReturnBodyOnUpdate()) { - PersistentEntityResource per = PersistentEntityResource.wrap(repoRequest.getPersistentEntity(), - obj, - repoRequest.getBaseUri()); - per.add(repoRequest.buildEntitySelfLink(obj, conversionService)); - return resourceResponse(null, - per, - HttpStatus.OK); - } else { - return resourceResponse(null, - null, - HttpStatus.NO_CONTENT); - } - } + if(config.isReturnBodyOnUpdate()) { + PersistentEntityResource per = PersistentEntityResource.wrap(repoRequest.getPersistentEntity(), + obj, + repoRequest.getBaseUri()); + per.add(repoRequest.buildEntitySelfLink(obj, conversionService)); + return resourceResponse(null, + per, + HttpStatus.OK); + } else { + return resourceResponse(null, + null, + HttpStatus.NO_CONTENT); + } + } - @SuppressWarnings({"unchecked"}) - @RequestMapping( - value = "/{id}", - method = RequestMethod.PUT, - consumes = { - "application/json" - }, - produces = { - "application/javascript" - } - ) - @ResponseBody - public JsonpResponse> jsonpUpdateEntity(RepositoryRestRequest repoRequest, - PersistentEntityResource incoming, - @PathVariable String id) - throws ResourceNotFoundException { - return jsonpWrapResponse(repoRequest, updateEntity(repoRequest, incoming, id)); - } + @SuppressWarnings({"unchecked"}) + @RequestMapping( + value = "/{id}", + method = RequestMethod.PUT, + consumes = { + "application/json" + }, + produces = { + "application/javascript" + } + ) + @ResponseBody + public JsonpResponse> jsonpUpdateEntity(RepositoryRestRequest repoRequest, + PersistentEntityResource incoming, + @PathVariable String id) + throws ResourceNotFoundException { + return jsonpWrapResponse(repoRequest, updateEntity(repoRequest, incoming, id)); + } - @SuppressWarnings({"unchecked"}) - @RequestMapping( - value = "/{id}", - method = RequestMethod.DELETE - ) - @ResponseBody - public ResponseEntity deleteEntity(RepositoryRestRequest repoRequest, - @PathVariable String id) - throws ResourceNotFoundException { - RepositoryMethodInvoker repoMethodInvoker = repoRequest.getRepositoryMethodInvoker(); - if(!repoMethodInvoker.hasFindOne() && - !(repoMethodInvoker.hasDeleteOne() || repoMethodInvoker.hasDeleteOneById())) { - throw new NoSuchMethodError(); - } + @SuppressWarnings({"unchecked"}) + @RequestMapping( + value = "/{id}", + method = RequestMethod.DELETE + ) + @ResponseBody + public ResponseEntity deleteEntity(RepositoryRestRequest repoRequest, + @PathVariable String id) + throws ResourceNotFoundException { + RepositoryMethodInvoker repoMethodInvoker = repoRequest.getRepositoryMethodInvoker(); + if(null == repoMethodInvoker || (!repoMethodInvoker.hasFindOne() + && !(repoMethodInvoker.hasDeleteOne() || repoMethodInvoker.hasDeleteOneById()))) { + throw new NoSuchMethodError(); + } - Object domainObj = domainClassConverter.convert(id, - STRING_TYPE, - TypeDescriptor.valueOf(repoRequest.getPersistentEntity() - .getType())); - if(null == domainObj) { - throw new ResourceNotFoundException(); - } + Object domainObj = domainClassConverter.convert(id, + STRING_TYPE, + TypeDescriptor.valueOf(repoRequest.getPersistentEntity() + .getType())); + if(null == domainObj) { + throw new ResourceNotFoundException(); + } - applicationContext.publishEvent(new BeforeDeleteEvent(domainObj)); - if(repoMethodInvoker.hasDeleteOneById()) { - Class idType = (Class)repoRequest.getPersistentEntity() - .getIdProperty() - .getType(); - Object idVal = conversionService.convert(id, idType); - repoMethodInvoker.delete((Serializable)idVal); - } else if(repoMethodInvoker.hasDeleteOne()) { - repoMethodInvoker.delete(domainObj); - } - applicationContext.publishEvent(new AfterDeleteEvent(domainObj)); + applicationContext.publishEvent(new BeforeDeleteEvent(domainObj)); + if(repoMethodInvoker.hasDeleteOneById()) { + Class idType = (Class)repoRequest.getPersistentEntity() + .getIdProperty() + .getType(); + Object idVal = conversionService.convert(id, idType); + repoMethodInvoker.delete((Serializable)idVal); + } else if(repoMethodInvoker.hasDeleteOne()) { + repoMethodInvoker.delete(domainObj); + } + applicationContext.publishEvent(new AfterDeleteEvent(domainObj)); - return new ResponseEntity(HttpStatus.NO_CONTENT); - } + return new ResponseEntity(HttpStatus.NO_CONTENT); + } - @SuppressWarnings({"unchecked"}) - @RequestMapping( - value = "{id}", - method = RequestMethod.DELETE, - produces = { - "application/javascript" - } - ) - @ResponseBody - public JsonpResponse jsonpDeleteEntity(RepositoryRestRequest repoRequest, - @PathVariable String id) - throws ResourceNotFoundException { - return jsonpWrapResponse(repoRequest, deleteEntity(repoRequest, id)); - } + @SuppressWarnings({"unchecked"}) + @RequestMapping( + value = "{id}", + method = RequestMethod.DELETE, + produces = { + "application/javascript" + } + ) + @ResponseBody + public JsonpResponse jsonpDeleteEntity(RepositoryRestRequest repoRequest, + @PathVariable String id) + throws ResourceNotFoundException { + return jsonpWrapResponse(repoRequest, deleteEntity(repoRequest, id)); + } }