From 0127ca8d4640625bf8a75fde6dc8236567bf39fc Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 25 Jun 2013 09:53:34 +0200 Subject: [PATCH] DATAREST-93 - Cleanups in RepositoryEntityController. Extracted RepositorySchemaController to handle schema exposure. Turned DomainObjectMerger property into constructor argument. --- .../webmvc/RepositoryEntityController.java | 25 +++----- .../webmvc/RepositorySchemaController.java | 63 +++++++++++++++++++ 2 files changed, 70 insertions(+), 18 deletions(-) create mode 100644 spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositorySchemaController.java 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 44ec00290..08d15f487 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 @@ -45,8 +45,6 @@ import org.springframework.data.rest.repository.context.BeforeDeleteEvent; import org.springframework.data.rest.repository.context.BeforeSaveEvent; import org.springframework.data.rest.repository.invoke.RepositoryMethodInvoker; import org.springframework.data.rest.repository.support.DomainObjectMerger; -import org.springframework.data.rest.webmvc.json.JsonSchema; -import org.springframework.data.rest.webmvc.json.PersistentEntityToJsonSchemaConverter; import org.springframework.data.web.PagedResourcesAssembler; import org.springframework.hateoas.EntityLinks; import org.springframework.hateoas.Link; @@ -80,18 +78,17 @@ class RepositoryEntityController extends AbstractRepositoryRestController implem private final RepositoryRestConfiguration config; private final DomainClassConverter converter; private final ConversionService conversionService; + private final DomainObjectMerger domainObjectMerger; private final TransactionOperations txOperations; private ApplicationEventPublisher publisher; - @Autowired private DomainObjectMerger domainObjectMerger; - @Autowired private PersistentEntityToJsonSchemaConverter jsonSchemaConverter; @Autowired public RepositoryEntityController(Repositories repositories, RepositoryRestConfiguration config, EntityLinks entityLinks, PagedResourcesAssembler assembler, PersistentEntityResourceAssembler perAssembler, DomainClassConverter converter, - @Qualifier("defaultConversionService") ConversionService conversionService) { + @Qualifier("defaultConversionService") ConversionService conversionService, DomainObjectMerger domainObjectMerger) { super(assembler, perAssembler); @@ -100,6 +97,7 @@ class RepositoryEntityController extends AbstractRepositoryRestController implem this.config = config; this.converter = converter; this.conversionService = conversionService; + this.domainObjectMerger = domainObjectMerger; this.txOperations = null; } @@ -109,16 +107,8 @@ class RepositoryEntityController extends AbstractRepositoryRestController implem * @see org.springframework.context.ApplicationEventPublisherAware#setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) */ @Override - public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { - this.publisher = applicationEventPublisher; - - } - - @RequestMapping(value = BASE_MAPPING + "/schema", method = RequestMethod.GET, - produces = { "application/schema+json" }) - @ResponseBody - public JsonSchema schema(RepositoryRestRequest repoRequest) { - return jsonSchemaConverter.convert(repoRequest.getPersistentEntity().getType()); + public void setApplicationEventPublisher(ApplicationEventPublisher publisher) { + this.publisher = publisher; } @RequestMapping(value = BASE_MAPPING, method = RequestMethod.GET, produces = { "application/json", @@ -276,9 +266,8 @@ class RepositoryEntityController extends AbstractRepositoryRestController implem public ResponseEntity deleteEntity(final RepositoryRestRequest repoRequest, @PathVariable final String id) throws ResourceNotFoundException, HttpRequestMethodNotSupportedException { final RepositoryMethodInvoker repoMethodInvoker = repoRequest.getRepositoryMethodInvoker(); - if (null == repoMethodInvoker - || (!repoMethodInvoker.hasFindOne() && !(repoMethodInvoker.hasDeleteOne() || repoMethodInvoker - .hasDeleteOneById()))) { + if (null == repoMethodInvoker || !repoMethodInvoker.hasFindOne() + && !(repoMethodInvoker.hasDeleteOne() || repoMethodInvoker.hasDeleteOneById())) { throw new HttpRequestMethodNotSupportedException("DELETE"); } ResourceMapping methodMapping = repoRequest.getRepositoryResourceMapping().getResourceMappingFor("delete"); diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositorySchemaController.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositorySchemaController.java new file mode 100644 index 000000000..d151512c3 --- /dev/null +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositorySchemaController.java @@ -0,0 +1,63 @@ +/* + * Copyright 2013 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.rest.webmvc; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.rest.webmvc.json.JsonSchema; +import org.springframework.data.rest.webmvc.json.PersistentEntityToJsonSchemaConverter; +import org.springframework.util.Assert; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +/** + * Controller to expose a JSON schema via {@code / repository}/schema}. + * + * @author Jon Brisbin + * @author Oliver Gierke + */ +@RestController +class RepositorySchemaController { + + private static final String BASE_MAPPING = "/{repository}"; + + private final PersistentEntityToJsonSchemaConverter jsonSchemaConverter; + + /** + * Creates a new {@link RepositorySchemaController} using the given {@link PersistentEntityToJsonSchemaConverter}. + * + * @param jsonSchemaConverter must not be {@literal null}. + */ + @Autowired + public RepositorySchemaController(PersistentEntityToJsonSchemaConverter jsonSchemaConverter) { + + Assert.notNull(jsonSchemaConverter, "PersistentEntityToJsonSchemaConverter must not be null!"); + this.jsonSchemaConverter = jsonSchemaConverter; + } + + /** + * Exposes a JSON schema for the repository referenced. + * + * @param repoRequest will never be {@literal null}. + * @return + */ + @RequestMapping(value = BASE_MAPPING + "/schema", method = RequestMethod.GET, + produces = { "application/schema+json" }) + @ResponseBody + public JsonSchema schema(RepositoryRestRequest repoRequest) { + return jsonSchemaConverter.convert(repoRequest.getPersistentEntity().getType()); + } +}