DATAREST-93 - More cleanups.

Merged core and repository modules into core. Renamed some packages for consistency in naming and in preparation to break up some package cycles. Removed @BaseUri and the according resolver. Refactored controllers a bit to have more reusable chunks of code.
This commit is contained in:
Oliver Gierke
2013-07-18 16:39:11 +02:00
parent 0f325bb0a0
commit d2c2ec8262
165 changed files with 1736 additions and 1661 deletions

View File

@@ -10,7 +10,7 @@ ext {
// Spring
springVersion = "3.2.3.RELEASE"
hateoasVersion = "0.7.0.BUILD-SNAPSHOT"
hateoasVersion = "0.8.0.BUILD-SNAPSHOT"
springPluginVersion = "0.8.0.RELEASE"
springSecurityVersion = "3.1.3.RELEASE"
sdCommonsVersion = "1.6.0.BUILD-SNAPSHOT"
@@ -124,7 +124,9 @@ configure(subprojects) { subproject ->
}
}
project("spring-data-rest-core") {
apply plugin: "maven"
description = "Spring Data REST core components."
@@ -134,63 +136,27 @@ project("spring-data-rest-core") {
dependencies {
// Spring Data Commons
compile "org.springframework.data:spring-data-commons:$sdCommonsVersion"
compile "org.springframework.hateoas:spring-hateoas:$hateoasVersion"
compile "org.springframework.plugin:spring-plugin-core:$springPluginVersion"
// Spring
compile "org.springframework:spring-aop:$springVersion"
compile "org.springframework:spring-core:$springVersion"
compile "org.springframework:spring-beans:$springVersion"
compile "org.springframework:spring-web:$springVersion"
compile "org.springframework:spring-tx:$springVersion"
runtime "cglib:cglib-nodep:$cglibVersion"
// Spring HATEOAS
compile("org.springframework.hateoas:spring-hateoas:$hateoasVersion") {
exclude module: "spring-webmvc"
}
// Spring Data Commons
providedCompile("org.springframework.data:spring-data-commons:$sdCommonsVersion") {
force = true
exclude module: "slf4j-api"
exclude module: "jcl-over-slf4j"
}
}
}
project("spring-data-rest-repository") {
apply plugin: "maven"
description = "Spring Data REST Repository integration."
dependencies {
// Exporter core
compile project(":spring-data-rest-core")
// JSR-305
compile "com.google.code.findbugs:jsr305:2.0.1"
// JSR 303 Validation
compile("javax.validation:validation-api:1.0.0.GA", optional)
// JODA
compile("joda-time:joda-time:$jodaVersion", optional)
// Jackson JSON
compile "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion"
compile("com.fasterxml.jackson.datatype:jackson-datatype-joda:$jacksonVersion", optional)
compile("com.fasterxml.jackson.datatype:jackson-datatype-hibernate4:$jacksonVersion", optional)
// ROME
//compile "rome:rome:1.0"
// JPA
compile("org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Final", optional)
compile("org.springframework:spring-orm:$springVersion", optional)
// Spring Plugin
compile "org.springframework.plugin:spring-plugin-core:$springPluginVersion"
// Spring Data
compile("org.springframework.data:spring-data-jpa:$sdJpaVersion", optional)
compile("org.springframework.data:spring-data-mongodb:$sdMongoVersion", optional)
// JSR 303 Validation
compile("javax.validation:validation-api:1.0.0.GA", optional)
testCompile("org.springframework.data:spring-data-jpa:$sdJpaVersion", optional)
testCompile("org.springframework.data:spring-data-mongodb:$sdMongoVersion", optional)
// Evo Inflector
compile "org.atteo:evo-inflector:${evoVersion}"
@@ -199,6 +165,13 @@ project("spring-data-rest-repository") {
testCompile "org.hsqldb:hsqldb:$hsqldbVersion"
testCompile "org.hibernate:hibernate-entitymanager:$hibernateVersion"
testRuntime "org.hibernate:hibernate-validator:$hibernateValidatorVersion"
// JPA
testRuntime("org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Final", optional)
testRuntime("org.springframework:spring-orm:$springVersion", optional)
}
}
@@ -207,34 +180,39 @@ project("spring-data-rest-webmvc") {
description = "Spring Data REST MVC components."
dependencies {
// Repository Exporter support
compile project(":spring-data-rest-repository")
compile project(":spring-data-rest-core")
// Spring
compile "org.springframework:spring-web:$springVersion"
compile "org.springframework:spring-webmvc:$springVersion"
compile("org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Final", optional)
compile("org.springframework:spring-orm:$springVersion", optional)
// APIs
compile("javax.servlet:javax.servlet-api:3.0.1", provided)
// Jackson
compile "com.fasterxml.jackson.datatype:jackson-datatype-joda:$jacksonVersion"
compile "com.fasterxml.jackson.datatype:jackson-datatype-hibernate4:$jacksonVersion"
// Testing
testCompile "org.eclipse.jetty:jetty-servlet:$jettyVersion"
testCompile "org.eclipse.jetty:jetty-webapp:$jettyVersion"
testCompile "org.mozilla:rhino:1.7R4"
// Spring Data
testCompile "org.springframework:spring-core:$springVersion"
testCompile "org.springframework:spring-tx:$springVersion"
testCompile "org.springframework.data:spring-data-jpa:$sdJpaVersion"
testCompile "org.springframework.data:spring-data-mongodb:$sdMongoVersion"
testCompile("org.springframework.data:spring-data-gemfire:$sdGemfireVersion") {
exclude group: "junit"
exclude group: "org.hamcrest"
exclude module: "spring-test"
}
testCompile "org.springframework.data:spring-data-gemfire:$sdGemfireVersion"
// JODA
testCompile "joda-time:joda-time:$jodaVersion"
// Jackson
testCompile "com.fasterxml.jackson.datatype:jackson-datatype-joda:$jacksonVersion"
testCompile "com.fasterxml.jackson.datatype:jackson-datatype-hibernate4:$jacksonVersion"
// Hibernate
testRuntime "org.hibernate:hibernate-entitymanager:$hibernateVersion"
testRuntime "org.hibernate:hibernate-validator:$hibernateValidatorVersion"
@@ -363,7 +341,6 @@ configure(rootProject) {
dependencies {
// For integration testing
testCompile project(":spring-data-rest-core")
testCompile project(":spring-data-rest-repository")
testCompile project(":spring-data-rest-webmvc")
}

View File

@@ -1,6 +1,6 @@
rootProject.name = "spring-data-rest"
include "spring-data-rest-core",
"spring-data-rest-repository",
"spring-data-rest-webmvc",
"spring-data-rest-example"
"spring-data-rest-example",
"spring-data-rest-tck"

View File

@@ -1,110 +0,0 @@
/*
* Copyright 2012-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.convert;
import java.util.Stack;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.util.Assert;
/**
* This {@link ConversionService} implementation delegates the actual conversion to the {@literal ConversionService} it
* finds in its internal {@link Stack} that claims to be able to convert a given class. It will roll through the
* {@literal ConversionService}s until it finds one that can convert the given type.
*
* @author Jon Brisbin
* @authot Oliver Gierke
*/
public class DelegatingConversionService implements ConversionService {
private final Stack<ConversionService> conversionServices;
public DelegatingConversionService(ConversionService... svcs) {
this.conversionServices = new Stack<ConversionService>();
for (ConversionService svc : svcs) {
Assert.notNull(svc);
conversionServices.add(svc);
}
}
/*
* (non-Javadoc)
* @see org.springframework.core.convert.ConversionService#canConvert(java.lang.Class, java.lang.Class)
*/
@Override
public boolean canConvert(Class<?> from, Class<?> to) {
for (ConversionService svc : conversionServices) {
if (svc.canConvert(from, to)) {
return true;
}
}
return false;
}
/*
* (non-Javadoc)
* @see org.springframework.core.convert.ConversionService#canConvert(org.springframework.core.convert.TypeDescriptor, org.springframework.core.convert.TypeDescriptor)
*/
@Override
public boolean canConvert(TypeDescriptor from, TypeDescriptor to) {
for (ConversionService svc : conversionServices) {
if (svc.canConvert(from, to)) {
return true;
}
}
return false;
}
/*
* (non-Javadoc)
* @see org.springframework.core.convert.ConversionService#convert(java.lang.Object, java.lang.Class)
*/
@Override
public <T> T convert(Object o, Class<T> type) {
for (ConversionService svc : conversionServices) {
if (svc.canConvert(o.getClass(), type)) {
return svc.convert(o, type);
}
}
throw new ConverterNotFoundException(TypeDescriptor.forObject(o), TypeDescriptor.valueOf(type));
}
/*
* (non-Javadoc)
* @see org.springframework.core.convert.ConversionService#convert(java.lang.Object, org.springframework.core.convert.TypeDescriptor, org.springframework.core.convert.TypeDescriptor)
*/
@Override
public Object convert(Object o, TypeDescriptor from, TypeDescriptor to) {
for (ConversionService svc : conversionServices) {
if (svc.canConvert(from, to)) {
return svc.convert(o, from, to);
}
}
throw new ConverterNotFoundException(from, to);
}
}

View File

@@ -1,110 +0,0 @@
/*
* Copyright 2012-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.convert;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import org.springframework.core.convert.ConversionFailedException;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
import org.springframework.core.convert.converter.Converter;
/**
* @author Jon Brisbin
*/
public class ISO8601DateConverter implements ConditionalGenericConverter, Converter<String[], Date> {
public static final ConditionalGenericConverter INSTANCE = new ISO8601DateConverter();
private static final Set<ConvertiblePair> CONVERTIBLE_PAIRS = new HashSet<ConvertiblePair>();
static {
CONVERTIBLE_PAIRS.add(new ConvertiblePair(String.class, Date.class));
CONVERTIBLE_PAIRS.add(new ConvertiblePair(Date.class, String.class));
}
/*
* (non-Javadoc)
* @see org.springframework.core.convert.converter.ConditionalConverter#matches(org.springframework.core.convert.TypeDescriptor, org.springframework.core.convert.TypeDescriptor)
*/
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
if (String.class.isAssignableFrom(sourceType.getType())) {
return Date.class.isAssignableFrom(targetType.getType());
}
return Date.class.isAssignableFrom(sourceType.getType()) && String.class.isAssignableFrom(targetType.getType());
}
/*
* (non-Javadoc)
* @see org.springframework.core.convert.converter.GenericConverter#getConvertibleTypes()
*/
@Override
public Set<ConvertiblePair> getConvertibleTypes() {
return CONVERTIBLE_PAIRS;
}
/*
* (non-Javadoc)
* @see org.springframework.core.convert.converter.GenericConverter#convert(java.lang.Object, org.springframework.core.convert.TypeDescriptor, org.springframework.core.convert.TypeDescriptor)
*/
@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
DateFormat dateFmt = iso8601DateFormat();
if (String.class.isAssignableFrom(sourceType.getType())) {
return dateFmt.format(source);
}
try {
return dateFmt.parse(source.toString());
} catch (ParseException e) {
throw new ConversionFailedException(sourceType, targetType, source, e);
}
}
/*
* (non-Javadoc)
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
*/
@Override
public Date convert(String[] source) {
if (source.length == 0) {
return null;
}
try {
return iso8601DateFormat().parse(source[0]);
} catch (ParseException e) {
throw new ConversionFailedException(TypeDescriptor.valueOf(String[].class), TypeDescriptor.valueOf(Date.class),
source[0], new IllegalArgumentException(
"Source does not conform to ISO8601 date format (YYYY-MM-DDTHH:MM:SS-0000"));
}
}
private DateFormat iso8601DateFormat() {
return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
}
}

View File

@@ -1,5 +0,0 @@
/**
* {@link org.springframework.core.convert.ConversionService} and {@link org.springframework.core.convert.converter.Converter} integration for Spring Data REST.
*/
package org.springframework.data.rest.convert;

View File

@@ -63,6 +63,10 @@ public class Path {
return new Path(this.path + cleanUp(path), false);
}
public Path slash(Path path) {
return slash(path.toString());
}
/*
* (non-Javadoc)
* @see java.lang.Object#hashCode()
@@ -79,13 +83,14 @@ public class Path {
}
String trimmed = path.trim().replaceAll(" ", "");
trimmed = SLASH + trimmed.substring(getFirstNoneSlashIndex(trimmed));
while (trimmed.endsWith("/")) {
trimmed = trimmed.substring(0, trimmed.length() - 1);
}
return trimmed;
trimmed = trimmed.substring(getFirstNoneSlashIndex(trimmed));
return trimmed.contains("://") ? trimmed : SLASH + trimmed;
}
/*

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository;
package org.springframework.data.rest.core;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.validation.Errors;

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.repository;
package org.springframework.data.rest.core;
import java.net.URI;
import java.util.HashSet;

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository;
package org.springframework.data.rest.core;
import static org.springframework.util.ReflectionUtils.*;

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.annotation;
package org.springframework.data.rest.core.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.annotation;
package org.springframework.data.rest.core.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.annotation;
package org.springframework.data.rest.core.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.annotation;
package org.springframework.data.rest.core.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.annotation;
package org.springframework.data.rest.core.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.annotation;
package org.springframework.data.rest.core.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.annotation;
package org.springframework.data.rest.core.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.annotation;
package org.springframework.data.rest.core.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.annotation;
package org.springframework.data.rest.core.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.annotation;
package org.springframework.data.rest.core.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.annotation;
package org.springframework.data.rest.core.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.annotation;
package org.springframework.data.rest.core.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.annotation;
package org.springframework.data.rest.core.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.config;
package org.springframework.data.rest.core.config;
import java.net.URI;
import java.util.ArrayList;

View File

@@ -13,9 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.config;
package org.springframework.data.rest.core.config;
import static org.springframework.data.rest.repository.support.ResourceMappingUtils.*;
import static org.springframework.data.rest.core.support.ResourceMappingUtils.*;
import java.util.HashMap;
import java.util.Map;

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.config;
package org.springframework.data.rest.core.config;
import java.util.HashMap;
import java.util.Map;

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.context;
package org.springframework.data.rest.core.event;
import static org.springframework.core.GenericTypeResolver.*;

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.context;
package org.springframework.data.rest.core.event;
/**
* Event that is emitted after a new entity is saved.

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.context;
package org.springframework.data.rest.core.event;
/**
* Emitted after the entity is deleted from the repository.

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.context;
package org.springframework.data.rest.core.event;
/**
* Emitted after a link to a related object is deleted from the parent.

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.context;
package org.springframework.data.rest.core.event;
/**
* Emitted after saving a linked object to its parent in the repository.

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.context;
package org.springframework.data.rest.core.event;
/**
* Emitted after a save to the repository.

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.context;
package org.springframework.data.rest.core.event;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
@@ -12,17 +12,17 @@ import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationListener;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.data.rest.repository.annotation.HandleAfterCreate;
import org.springframework.data.rest.repository.annotation.HandleAfterDelete;
import org.springframework.data.rest.repository.annotation.HandleAfterLinkDelete;
import org.springframework.data.rest.repository.annotation.HandleAfterLinkSave;
import org.springframework.data.rest.repository.annotation.HandleAfterSave;
import org.springframework.data.rest.repository.annotation.HandleBeforeCreate;
import org.springframework.data.rest.repository.annotation.HandleBeforeDelete;
import org.springframework.data.rest.repository.annotation.HandleBeforeLinkDelete;
import org.springframework.data.rest.repository.annotation.HandleBeforeLinkSave;
import org.springframework.data.rest.repository.annotation.HandleBeforeSave;
import org.springframework.data.rest.repository.annotation.RepositoryEventHandler;
import org.springframework.data.rest.core.annotation.HandleAfterCreate;
import org.springframework.data.rest.core.annotation.HandleAfterDelete;
import org.springframework.data.rest.core.annotation.HandleAfterLinkDelete;
import org.springframework.data.rest.core.annotation.HandleAfterLinkSave;
import org.springframework.data.rest.core.annotation.HandleAfterSave;
import org.springframework.data.rest.core.annotation.HandleBeforeCreate;
import org.springframework.data.rest.core.annotation.HandleBeforeDelete;
import org.springframework.data.rest.core.annotation.HandleBeforeLinkDelete;
import org.springframework.data.rest.core.annotation.HandleBeforeLinkSave;
import org.springframework.data.rest.core.annotation.HandleBeforeSave;
import org.springframework.data.rest.core.annotation.RepositoryEventHandler;
import org.springframework.util.ClassUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.context;
package org.springframework.data.rest.core.event;
/**
* Event emitted before an entity is saved for the first time.

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.context;
package org.springframework.data.rest.core.event;
/**
* Emitted before an entity is deleted from the repository.

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.context;
package org.springframework.data.rest.core.event;
/**
* Emitted before a link to a related object is deleted from the parent.

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.context;
package org.springframework.data.rest.core.event;
/**
* Emitted before a linked object is saved to the repository.

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.context;
package org.springframework.data.rest.core.event;
/**
* Emitted before an entity is saved into the repository.

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.context;
package org.springframework.data.rest.core.event;
/**
* An event to encapsulate an exception occurring anywhere within the REST exporter.

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.context;
package org.springframework.data.rest.core.event;
/**
* Base class for {@link RepositoryEvent}s that deal with saving/updating or deleting a linked object.

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.context;
package org.springframework.data.rest.core.event;
import org.springframework.context.ApplicationEvent;

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.context;
package org.springframework.data.rest.core.event;
import static org.springframework.beans.factory.BeanFactoryUtils.*;
import static org.springframework.core.annotation.AnnotationUtils.*;
@@ -16,17 +16,17 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.support.Repositories;
import org.springframework.data.rest.core.RepositoryConstraintViolationException;
import org.springframework.data.rest.core.ValidationErrors;
import org.springframework.data.rest.core.annotation.HandleAfterDelete;
import org.springframework.data.rest.core.annotation.HandleAfterLinkDelete;
import org.springframework.data.rest.core.annotation.HandleAfterLinkSave;
import org.springframework.data.rest.core.annotation.HandleAfterSave;
import org.springframework.data.rest.core.annotation.HandleBeforeDelete;
import org.springframework.data.rest.core.annotation.HandleBeforeLinkDelete;
import org.springframework.data.rest.core.annotation.HandleBeforeLinkSave;
import org.springframework.data.rest.core.annotation.HandleBeforeSave;
import org.springframework.data.rest.core.util.MapUtils;
import org.springframework.data.rest.repository.RepositoryConstraintViolationException;
import org.springframework.data.rest.repository.ValidationErrors;
import org.springframework.data.rest.repository.annotation.HandleAfterDelete;
import org.springframework.data.rest.repository.annotation.HandleAfterLinkDelete;
import org.springframework.data.rest.repository.annotation.HandleAfterLinkSave;
import org.springframework.data.rest.repository.annotation.HandleAfterSave;
import org.springframework.data.rest.repository.annotation.HandleBeforeDelete;
import org.springframework.data.rest.repository.annotation.HandleBeforeLinkDelete;
import org.springframework.data.rest.repository.annotation.HandleBeforeLinkSave;
import org.springframework.data.rest.repository.annotation.HandleBeforeSave;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.validation.Errors;

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.invoke;
package org.springframework.data.rest.core.invoke;
import java.lang.reflect.Method;

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.repository.invoke;
package org.springframework.data.rest.core.invoke;
import java.io.Serializable;
@@ -59,7 +59,7 @@ class CrudRepositoryInvoker extends ReflectionRepositoryInvoker {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.invoke.RepositoryInvoker#invokeFindAll(org.springframework.data.domain.Sort)
* @see org.springframework.data.rest.core.invoke.RepositoryInvoker#invokeFindAll(org.springframework.data.domain.Sort)
*/
@Override
public Iterable<Object> invokeFindAll(Sort pageable) {
@@ -68,7 +68,7 @@ class CrudRepositoryInvoker extends ReflectionRepositoryInvoker {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.invoke.RepositoryInvoker#invokeFindAll(org.springframework.data.domain.Pageable)
* @see org.springframework.data.rest.core.invoke.RepositoryInvoker#invokeFindAll(org.springframework.data.domain.Pageable)
*/
@Override
public Iterable<Object> invokeFindAll(Pageable pageable) {
@@ -77,7 +77,7 @@ class CrudRepositoryInvoker extends ReflectionRepositoryInvoker {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.invoke.RepositoryInvoker#invokeFindOne(java.io.Serializable)
* @see org.springframework.data.rest.core.invoke.RepositoryInvoker#invokeFindOne(java.io.Serializable)
*/
@Override
public Object invokeFindOne(Serializable id) {
@@ -86,7 +86,7 @@ class CrudRepositoryInvoker extends ReflectionRepositoryInvoker {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.invoke.ReflectionRepositoryInvoker#invokeSave(java.lang.Object)
* @see org.springframework.data.rest.core.invoke.ReflectionRepositoryInvoker#invokeSave(java.lang.Object)
*/
@Override
public Object invokeSave(Object entity) {
@@ -95,7 +95,7 @@ class CrudRepositoryInvoker extends ReflectionRepositoryInvoker {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.invoke.RepositoryInvoker#invokeDelete(java.io.Serializable)
* @see org.springframework.data.rest.core.invoke.RepositoryInvoker#invokeDelete(java.io.Serializable)
*/
@Override
public void invokeDelete(Serializable id) {

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.repository.invoke;
package org.springframework.data.rest.core.invoke;
import java.io.Serializable;
@@ -50,7 +50,7 @@ class PagingAndSortingRepositoryInvoker extends CrudRepositoryInvoker {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.invoke.CrudRepositoryInvoker#invokeFindAll(org.springframework.data.domain.Sort)
* @see org.springframework.data.rest.core.invoke.CrudRepositoryInvoker#invokeFindAll(org.springframework.data.domain.Sort)
*/
@Override
public Iterable<Object> invokeFindAll(Sort sort) {
@@ -59,7 +59,7 @@ class PagingAndSortingRepositoryInvoker extends CrudRepositoryInvoker {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.invoke.CrudRepositoryInvoker#invokeFindAll(org.springframework.data.domain.Pageable)
* @see org.springframework.data.rest.core.invoke.CrudRepositoryInvoker#invokeFindAll(org.springframework.data.domain.Pageable)
*/
@Override
public Iterable<Object> invokeFindAll(Pageable pageable) {

View File

@@ -13,25 +13,29 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.repository.invoke;
package org.springframework.data.rest.core.invoke;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.support.ArgumentConvertingMethodInvoker;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.core.CrudMethods;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.rest.repository.annotation.RestResource;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.hateoas.core.AnnotationAttribute;
import org.springframework.hateoas.core.MethodParameters;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
/**
* Base {@link RepositoryInvoker} using reflection to invoke methods on Spring Data Repositories.
@@ -40,6 +44,8 @@ import org.springframework.util.Assert;
*/
class ReflectionRepositoryInvoker implements RepositoryInvoker {
private static final AnnotationAttribute PARAM_ANNOTATION = new AnnotationAttribute(Param.class);
private final Object repository;
private final CrudMethods methods;
private final RepositoryInformation information;
@@ -68,7 +74,7 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.invoke.RepositoryInvocationInformation#exposesFindAll()
* @see org.springframework.data.rest.core.invoke.RepositoryInvocationInformation#exposesFindAll()
*/
@Override
public boolean exposesFindAll() {
@@ -76,7 +82,7 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker {
}
/* (non-Javadoc)
* @see org.springframework.data.rest.repository.invoke.RepositoryInvoker#invokeFindAll(org.springframework.data.domain.Sort)
* @see org.springframework.data.rest.core.invoke.RepositoryInvoker#invokeFindAll(org.springframework.data.domain.Sort)
*/
@Override
@SuppressWarnings("unchecked")
@@ -86,17 +92,32 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.invoke.RepositoryInvoker#invokeFindAll(org.springframework.data.domain.Pageable)
* @see org.springframework.data.rest.core.invoke.RepositoryInvoker#invokeFindAll(org.springframework.data.domain.Pageable)
*/
@Override
@SuppressWarnings("unchecked")
public Iterable<Object> invokeFindAll(Pageable pageable) {
return (Iterable<Object>) invoke(methods.getFindAllMethod(), pageable);
if (!exposesFindAll()) {
return Collections.emptyList();
}
Method method = methods.getFindAllMethod();
Class<?>[] types = method.getParameterTypes();
if (types.length == 0) {
return invoke(method);
}
if (Sort.class.isAssignableFrom(types[0])) {
return invoke(method, pageable == null ? null : pageable.getSort());
}
return invoke(method, pageable);
}
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.invoke.RepositoryInvocationInformation#exposesSave()
* @see org.springframework.data.rest.core.invoke.RepositoryInvocationInformation#exposesSave()
*/
@Override
public boolean exposesSave() {
@@ -104,7 +125,7 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker {
}
/* (non-Javadoc)
* @see org.springframework.data.rest.repository.invoke.RepositoryInvoker#invokeSave(java.lang.Object)
* @see org.springframework.data.rest.core.invoke.RepositoryInvoker#invokeSave(java.lang.Object)
*/
@Override
public Object invokeSave(Object object) {
@@ -113,7 +134,7 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.invoke.RepositoryInvocationInformation#exposesFindOne()
* @see org.springframework.data.rest.core.invoke.RepositoryInvocationInformation#exposesFindOne()
*/
@Override
public boolean exposesFindOne() {
@@ -122,7 +143,7 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.invoke.RepositoryInvoker#invokeFindOne(java.io.Serializable)
* @see org.springframework.data.rest.core.invoke.RepositoryInvoker#invokeFindOne(java.io.Serializable)
*/
@Override
public Object invokeFindOne(Serializable id) {
@@ -131,7 +152,7 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.invoke.RepositoryInvocationInformation#exposesDelete()
* @see org.springframework.data.rest.core.invoke.RepositoryInvocationInformation#exposesDelete()
*/
@Override
public boolean exposesDelete() {
@@ -140,7 +161,7 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.invoke.RepositoryInvoker#invokeDelete(java.io.Serializable)
* @see org.springframework.data.rest.core.invoke.RepositoryInvoker#invokeDelete(java.io.Serializable)
*/
@Override
public void invokeDelete(Serializable id) {
@@ -162,7 +183,7 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.invoke.RepositoryInvoker#invokeQueryMethod(java.lang.reflect.Method, java.util.Map, org.springframework.data.domain.Pageable, org.springframework.data.domain.Sort)
* @see org.springframework.data.rest.core.invoke.RepositoryInvoker#invokeQueryMethod(java.lang.reflect.Method, java.util.Map, org.springframework.data.domain.Pageable, org.springframework.data.domain.Sort)
*/
@Override
public Object invokeQueryMethod(Method method, Map<String, String[]> parameters, Pageable pageable, Sort sort) {
@@ -171,7 +192,7 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker {
private Object[] prepareParameters(Method method, Map<String, String[]> rawParameters, Pageable pageable, Sort sort) {
List<MethodParameter> parameters = new MethodParameters(method).getParameters();
List<MethodParameter> parameters = new MethodParameters(method, PARAM_ANNOTATION).getParameters();
if (parameters.isEmpty()) {
return new Object[0];
@@ -192,43 +213,32 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker {
} else {
String parameterName = param.getParameterName();
String[] parameterValue = rawParameters.get(parameterName);
Object value = parameterValue.length == 1 ? parameterValue[0] : parameterValue;
if (value == null) {
if (parameterName.startsWith("arg")) {
throw new IllegalArgumentException("No @Param annotation found on query method " + method.getName()
+ " for parameter " + parameterName);
} else {
throw new IllegalArgumentException("No query parameter specified for " + method.getName() + " param '"
+ parameterName + "'");
}
if (!StringUtils.hasText(parameterName)) {
throw new IllegalArgumentException("No @Param annotation found on query method " + method.getName()
+ " for parameter " + parameterName);
}
result[i] = conversionService.convert(parameterValue, targetType);
String[] parameterValue = rawParameters.get(parameterName);
Object value = parameterValue == null ? null : parameterValue.length == 1 ? parameterValue[0] : parameterValue;
result[i] = conversionService.convert(value, TypeDescriptor.forObject(value), new TypeDescriptor(param));
}
}
return result;
}
private Object invoke(Method method, Object... arguments) {
BeanWrapperImpl wrapper = new BeanWrapperImpl();
wrapper.setConversionService(conversionService);
ArgumentConvertingMethodInvoker invoker = new ArgumentConvertingMethodInvoker();
invoker.setTargetObject(repository);
invoker.setTargetMethod(method.getName());
invoker.setArguments(arguments);
invoker.setTypeConverter(wrapper);
try {
invoker.prepare();
return invoker.invoke();
} catch (Exception e) {
throw new IllegalStateException(e);
}
/**
* Invokes the given method with the given arguments on the backing repository.
*
* @param method
* @param arguments
* @return
*/
@SuppressWarnings("unchecked")
private <T> T invoke(Method method, Object... arguments) {
return (T) ReflectionUtils.invokeMethod(method, repository, arguments);
}
/**

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.repository.invoke;
package org.springframework.data.rest.core.invoke;
/**
* Meta-information about the methods a repository exposes.

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.repository.invoke;
package org.springframework.data.rest.core.invoke;
import java.io.Serializable;
import java.lang.reflect.Method;

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.repository.invoke;
package org.springframework.data.rest.core.invoke;
import java.io.Serializable;
import java.util.HashMap;

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.invoke;
package org.springframework.data.rest.core.invoke;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
@@ -10,7 +10,7 @@ import org.springframework.core.MethodParameter;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.repository.support.Methods;
import org.springframework.data.rest.core.support.Methods;
/**
* An abstraction to encapsulate metadata about a repository method.

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.invoke;
package org.springframework.data.rest.core.invoke;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.repository.mapping;
package org.springframework.data.rest.core.mapping;
/**
* A custom resource mapping for collection resources.

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.repository.mapping;
package org.springframework.data.rest.core.mapping;
import org.atteo.evo.inflector.English;
import org.springframework.hateoas.RelProvider;
@@ -38,7 +38,7 @@ class EvoInflectorTypeBasedCollectionResourceMapping extends TypeBasedCollection
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.mapping.TypeBasedCollectionResourceMapping#getDefaultPathFor(java.lang.Class)
* @see org.springframework.data.rest.core.mapping.TypeBasedCollectionResourceMapping#getDefaultPathFor(java.lang.Class)
*/
@Override
protected String getDefaultPathFor(Class<?> type) {

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.repository.mapping;
package org.springframework.data.rest.core.mapping;
import java.lang.reflect.Method;

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.repository.mapping;
package org.springframework.data.rest.core.mapping;
import org.springframework.context.annotation.Primary;
import org.springframework.core.annotation.AnnotationUtils;
@@ -57,7 +57,7 @@ class RepositoryAwareResourceInformation implements ResourceMetadata {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.mapping.ResourceMetadata#getDomainType()
* @see org.springframework.data.rest.core.mapping.ResourceMetadata#getDomainType()
*/
@Override
public Class<?> getDomainType() {
@@ -66,7 +66,7 @@ class RepositoryAwareResourceInformation implements ResourceMetadata {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.mapping.DelegatingResourceInformation#isManaged(org.springframework.data.mapping.PersistentProperty)
* @see org.springframework.data.rest.core.mapping.DelegatingResourceInformation#isManaged(org.springframework.data.mapping.PersistentProperty)
*/
@Override
public boolean isManagedResource(PersistentProperty<?> property) {
@@ -77,7 +77,7 @@ class RepositoryAwareResourceInformation implements ResourceMetadata {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.mapping.ResourceMetadataProvider#getMappingFor(org.springframework.data.mapping.PersistentProperty)
* @see org.springframework.data.rest.core.mapping.ResourceMetadataProvider#getMappingFor(org.springframework.data.mapping.PersistentProperty)
*/
@Override
public ResourceMapping getMappingFor(PersistentProperty<?> property) {
@@ -86,7 +86,7 @@ class RepositoryAwareResourceInformation implements ResourceMetadata {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.mapping.ResourceMetadataProvider#hasMappingFor(org.springframework.data.mapping.PersistentProperty)
* @see org.springframework.data.rest.core.mapping.ResourceMetadataProvider#hasMappingFor(org.springframework.data.mapping.PersistentProperty)
*/
@Override
public boolean isExported(PersistentProperty<?> property) {
@@ -95,7 +95,7 @@ class RepositoryAwareResourceInformation implements ResourceMetadata {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.mapping.CollectionResourceMapping#isExported()
* @see org.springframework.data.rest.core.mapping.CollectionResourceMapping#isExported()
*/
@Override
public Boolean isExported() {
@@ -104,7 +104,7 @@ class RepositoryAwareResourceInformation implements ResourceMetadata {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.mapping.CollectionResourceMapping#getCollectionRel()
* @see org.springframework.data.rest.core.mapping.CollectionResourceMapping#getCollectionRel()
*/
@Override
public String getRel() {
@@ -113,7 +113,7 @@ class RepositoryAwareResourceInformation implements ResourceMetadata {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.mapping.CollectionResourceMapping#getSingleResourceRel()
* @see org.springframework.data.rest.core.mapping.CollectionResourceMapping#getSingleResourceRel()
*/
@Override
public String getSingleResourceRel() {
@@ -122,7 +122,7 @@ class RepositoryAwareResourceInformation implements ResourceMetadata {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.mapping.CollectionResourceMapping#getPath()
* @see org.springframework.data.rest.core.mapping.CollectionResourceMapping#getPath()
*/
@Override
public Path getPath() {
@@ -131,7 +131,7 @@ class RepositoryAwareResourceInformation implements ResourceMetadata {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.mapping.ResourceMetadata#getSearchResourceMappings()
* @see org.springframework.data.rest.core.mapping.ResourceMetadata#getSearchResourceMappings()
*/
@Override
public SearchResourceMappings getSearchResourceMappings() {

View File

@@ -13,14 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.repository.mapping;
package org.springframework.data.rest.core.mapping;
import java.lang.reflect.Modifier;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.data.rest.core.Path;
import org.springframework.data.rest.repository.annotation.RestResource;
import org.springframework.data.rest.repository.support.RepositoriesUtils;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.data.rest.core.support.RepositoriesUtils;
import org.springframework.hateoas.RelProvider;
import org.springframework.hateoas.core.EvoInflectorRelProvider;
import org.springframework.util.Assert;
@@ -68,7 +68,7 @@ class RepositoryCollectionResourceMapping implements CollectionResourceMapping {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.mapping.ResourceMapping#getPath()
* @see org.springframework.data.rest.core.mapping.ResourceMapping#getPath()
*/
@Override
public Path getPath() {
@@ -79,7 +79,7 @@ class RepositoryCollectionResourceMapping implements CollectionResourceMapping {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.mapping.ResourceMapping#getRel()
* @see org.springframework.data.rest.core.mapping.ResourceMapping#getRel()
*/
@Override
public String getRel() {
@@ -88,7 +88,7 @@ class RepositoryCollectionResourceMapping implements CollectionResourceMapping {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.mapping.CollectionResourceMapping#getSingleResourceRel()
* @see org.springframework.data.rest.core.mapping.CollectionResourceMapping#getSingleResourceRel()
*/
@Override
public String getSingleResourceRel() {
@@ -97,7 +97,7 @@ class RepositoryCollectionResourceMapping implements CollectionResourceMapping {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.mapping.ResourceMapping#isExported()
* @see org.springframework.data.rest.core.mapping.ResourceMapping#isExported()
*/
@Override
public Boolean isExported() {

View File

@@ -13,13 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.repository.mapping;
package org.springframework.data.rest.core.mapping;
import java.lang.reflect.Method;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.data.rest.core.Path;
import org.springframework.data.rest.repository.annotation.RestResource;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
@@ -50,17 +50,14 @@ class RepositoryMethodResourceMapping implements MethodResourceMapping {
this.isExported = annotation != null ? annotation.exported() : true;
this.rel = annotation != null ? annotation.rel() : method.getName();
Path resourcePath = resourceMapping.getPath();
String toAppend = annotation == null || !StringUtils.hasText(annotation.path()) ? method.getName() : annotation
.path();
this.path = resourcePath.slash(toAppend);
this.path = annotation == null || !StringUtils.hasText(annotation.path()) ? new Path(method.getName()) : new Path(
annotation.path());
this.method = method;
}
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.mapping.ResourceMapping#isExported()
* @see org.springframework.data.rest.core.mapping.ResourceMapping#isExported()
*/
@Override
public Boolean isExported() {
@@ -69,7 +66,7 @@ class RepositoryMethodResourceMapping implements MethodResourceMapping {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.mapping.ResourceMapping#getRel()
* @see org.springframework.data.rest.core.mapping.ResourceMapping#getRel()
*/
@Override
public String getRel() {
@@ -78,7 +75,7 @@ class RepositoryMethodResourceMapping implements MethodResourceMapping {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.mapping.ResourceMapping#getPath()
* @see org.springframework.data.rest.core.mapping.ResourceMapping#getPath()
*/
@Override
public Path getPath() {
@@ -87,7 +84,7 @@ class RepositoryMethodResourceMapping implements MethodResourceMapping {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.mapping.MethodResourceMapping#getMethod()
* @see org.springframework.data.rest.core.mapping.MethodResourceMapping#getMethod()
*/
@Override
public Method getMethod() {

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.repository.mapping;
package org.springframework.data.rest.core.mapping;
import org.springframework.data.rest.core.Path;

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.repository.mapping;
package org.springframework.data.rest.core.mapping;
import java.lang.reflect.Method;
import java.util.ArrayList;
@@ -25,8 +25,8 @@ import java.util.Map;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.support.Repositories;
import org.springframework.data.rest.config.RepositoryRestConfiguration;
import org.springframework.data.rest.repository.support.RepositoriesUtils;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.core.support.RepositoriesUtils;
import org.springframework.hateoas.RelProvider;
import org.springframework.hateoas.core.EvoInflectorRelProvider;
import org.springframework.util.Assert;
@@ -122,17 +122,25 @@ public class ResourceMappings implements Iterable<ResourceMetadata> {
}
Class<?> domainType = RepositoriesUtils.getDomainType(type);
RepositoryInformation repositoryInformation = repositories.getRepositoryInformationFor(domainType);
List<MethodResourceMapping> mappings = new ArrayList<MethodResourceMapping>();
ResourceMetadata repositoryMapping = getMappingFor(repositoryInformation.getRepositoryInterface());
for (Method queryMethod : repositoryInformation.getQueryMethods()) {
mappings.add(new RepositoryMethodResourceMapping(queryMethod, repositoryMapping));
if (searchCache.containsKey(domainType)) {
return searchCache.get(domainType);
}
SearchResourceMappings searchMappings = new SearchResourceMappings(mappings);
searchCache.put(type, searchMappings);
return searchMappings;
RepositoryInformation repositoryInformation = repositories.getRepositoryInformationFor(domainType);
List<MethodResourceMapping> mappings = new ArrayList<MethodResourceMapping>();
ResourceMetadata resourceMapping = getMappingFor(domainType);
if (resourceMapping.isExported()) {
for (Method queryMethod : repositoryInformation.getQueryMethods()) {
mappings.add(new RepositoryMethodResourceMapping(queryMethod, resourceMapping));
}
}
SearchResourceMappings searchResourceMappings = new SearchResourceMappings(mappings);
searchCache.put(type, searchResourceMappings);
searchCache.put(domainType, searchResourceMappings);
return searchResourceMappings;
}
/**
@@ -176,7 +184,7 @@ public class ResourceMappings implements Iterable<ResourceMetadata> {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.mapping.ResourceMetadataProvider#getMappingFor(org.springframework.data.mapping.PersistentProperty)
* @see org.springframework.data.rest.core.mapping.ResourceMetadataProvider#getMappingFor(org.springframework.data.mapping.PersistentProperty)
*/
ResourceMapping getMappingFor(PersistentProperty<?> property) {
return getMappingFor(property.getActualType());
@@ -184,7 +192,7 @@ public class ResourceMappings implements Iterable<ResourceMetadata> {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.mapping.ResourceMetadataProvider#hasMappingFor(org.springframework.data.mapping.PersistentProperty)
* @see org.springframework.data.rest.core.mapping.ResourceMetadataProvider#hasMappingFor(org.springframework.data.mapping.PersistentProperty)
*/
public boolean isMapped(PersistentProperty<?> property) {

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.repository.mapping;
package org.springframework.data.rest.core.mapping;
import org.springframework.data.mapping.PersistentProperty;

View File

@@ -13,11 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.repository.mapping;
package org.springframework.data.rest.core.mapping;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.springframework.data.rest.core.Path;
import org.springframework.util.Assert;
@@ -29,10 +31,13 @@ import org.springframework.util.Assert;
*/
public class SearchResourceMappings implements Iterable<MethodResourceMapping>, ResourceMapping {
private static final String AMBIGUOUS_MAPPING = "Ambiguous search mapping detected. Both %s and "
+ "%s are mapped to %s! Tweak configuration to get to unambiguous paths!";
private static final Path PATH = new Path("/search");
private static final String REL = "search";
private final List<MethodResourceMapping> mappings;
private final Map<Path, MethodResourceMapping> mappings;
/**
* Creates a new {@link SearchResourceMappings} from the given
@@ -42,7 +47,20 @@ public class SearchResourceMappings implements Iterable<MethodResourceMapping>,
public SearchResourceMappings(List<MethodResourceMapping> mappings) {
Assert.notNull(mappings, "MethodResourceMappings must not be null!");
this.mappings = mappings;
this.mappings = new HashMap<Path, MethodResourceMapping>(mappings.size());
for (MethodResourceMapping mapping : mappings) {
MethodResourceMapping existing = this.mappings.get(mapping.getPath());
if (existing != null) {
throw new IllegalStateException(String.format(AMBIGUOUS_MAPPING, existing.getMethod(), mapping.getMethod(),
existing.getPath()));
}
this.mappings.put(mapping.getPath(), mapping);
}
}
/**
@@ -55,18 +73,13 @@ public class SearchResourceMappings implements Iterable<MethodResourceMapping>,
Assert.hasText(path, "Path must not be null or empty!");
for (MethodResourceMapping mapping : mappings) {
if (mapping.getPath().matches(path)) {
return mapping.getMethod();
}
}
return null;
MethodResourceMapping mapping = mappings.get(new Path(path));
return mapping == null ? null : mapping.getMethod();
}
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.mapping.ResourceMapping#getPath()
* @see org.springframework.data.rest.core.mapping.ResourceMapping#getPath()
*/
@Override
public Path getPath() {
@@ -75,7 +88,7 @@ public class SearchResourceMappings implements Iterable<MethodResourceMapping>,
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.mapping.ResourceMapping#getRel()
* @see org.springframework.data.rest.core.mapping.ResourceMapping#getRel()
*/
@Override
public String getRel() {
@@ -84,18 +97,19 @@ public class SearchResourceMappings implements Iterable<MethodResourceMapping>,
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.mapping.ResourceMapping#isExported()
* @see org.springframework.data.rest.core.mapping.ResourceMapping#isExported()
*/
@Override
public Boolean isExported() {
return !mappings.isEmpty();
}
/* (non-Javadoc)
/*
* (non-Javadoc)
* @see java.lang.Iterable#iterator()
*/
@Override
public Iterator<MethodResourceMapping> iterator() {
return mappings.iterator();
return mappings.values().iterator();
}
}

View File

@@ -13,13 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.repository.mapping;
package org.springframework.data.rest.core.mapping;
import java.lang.reflect.Modifier;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.data.rest.core.Path;
import org.springframework.data.rest.repository.annotation.RestResource;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.hateoas.RelProvider;
import org.springframework.hateoas.core.EvoInflectorRelProvider;
import org.springframework.util.Assert;
@@ -64,7 +64,7 @@ class TypeBasedCollectionResourceMapping implements CollectionResourceMapping {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.mapping.ResourceMapping#getPath()
* @see org.springframework.data.rest.core.mapping.ResourceMapping#getPath()
*/
@Override
public Path getPath() {
@@ -76,7 +76,7 @@ class TypeBasedCollectionResourceMapping implements CollectionResourceMapping {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.mapping.ResourceMapping#isExported()
* @see org.springframework.data.rest.core.mapping.ResourceMapping#isExported()
*/
@Override
public Boolean isExported() {
@@ -85,7 +85,7 @@ class TypeBasedCollectionResourceMapping implements CollectionResourceMapping {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.mapping.ResourceMapping#getRel()
* @see org.springframework.data.rest.core.mapping.ResourceMapping#getRel()
*/
@Override
public String getRel() {
@@ -99,7 +99,7 @@ class TypeBasedCollectionResourceMapping implements CollectionResourceMapping {
/*
* (non-Javadoc)
* @see org.springframework.data.rest.repository.mapping.CollectionResourceMapping#getSingleResourceRel()
* @see org.springframework.data.rest.core.mapping.CollectionResourceMapping#getSingleResourceRel()
*/
@Override
public String getSingleResourceRel() {

View File

@@ -1,5 +0,0 @@
/**
* Core components used across Spring Data REST.
*/
package org.springframework.data.rest.core;

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.support;
package org.springframework.data.rest.core.support;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.support;
package org.springframework.data.rest.core.support;
import java.lang.reflect.Method;

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.repository.support;
package org.springframework.data.rest.core.support;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.data.repository.Repository;
@@ -27,13 +27,20 @@ import org.springframework.data.repository.core.support.DefaultRepositoryMetadat
*/
public class RepositoriesUtils {
public static Class<?> getDomainType(Class<?> repositoryType) {
/**
* Resolves the domain type from the given type. Will resolve the repository domain type if the given type is a
* repository or return the type as is if not.
*
* @param type must not be {@literal null}.
* @return
*/
public static Class<?> getDomainType(Class<?> type) {
if (!isRepositoryInterface(repositoryType)) {
return null;
if (!isRepositoryInterface(type)) {
return type;
}
return getMetadataFor(repositoryType).getDomainType();
return getMetadataFor(type).getDomainType();
}
public static boolean isRepositoryInterface(Class<?> type) {

View File

@@ -13,9 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.repository.support;
package org.springframework.data.rest.core.support;
import org.springframework.data.rest.repository.mapping.ResourceMappings;
import org.springframework.data.rest.core.mapping.ResourceMappings;
import org.springframework.hateoas.RelProvider;
/**

View File

@@ -13,9 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.repository.support;
package org.springframework.data.rest.core.support;
import static org.springframework.data.rest.repository.support.ResourceStringUtils.*;
import static org.springframework.data.rest.core.support.ResourceStringUtils.*;
import static org.springframework.core.annotation.AnnotationUtils.*;
import static org.springframework.util.StringUtils.*;
@@ -24,9 +24,9 @@ import java.lang.reflect.Method;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.rest.config.RepositoryRestConfiguration;
import org.springframework.data.rest.config.ResourceMapping;
import org.springframework.data.rest.repository.annotation.RestResource;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.core.config.ResourceMapping;
/**
* Helper methods to get the default rel and path values or to use values supplied by annotations.

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.repository.support;
package org.springframework.data.rest.core.support;
/**
* Helper methods aiming at handling String representations of resources.

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.repository.support;
package org.springframework.data.rest.core.support;
import org.springframework.hateoas.RelProvider;
import org.springframework.util.StringUtils;

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.convert;
package org.springframework.data.rest.core.util;
import java.util.HashSet;
import java.util.Set;

View File

@@ -1,5 +0,0 @@
/**
* Spring Data REST
*/
package org.springframework.data.rest;

View File

@@ -1,84 +0,0 @@
/*
* Copyright 2012-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.convert;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
import java.util.UUID;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.core.convert.ConversionService;
import org.springframework.format.support.DefaultFormattingConversionService;
/**
* Tests to ensure the {@link DelegatingConversionService} properly delegates conversions to the
* {@link org.springframework.core.convert.ConversionService} that is appropriate for the given source and return types.
*
* @author Jon Brisbin
* @author Oliver Gierke
*/
@RunWith(MockitoJUnitRunner.class)
public class DelegatingConversionServiceUnitTests {
private static final UUID RANDOM_UUID = UUID.fromString("9deccfd7-f892-4e26-a4d5-c92893392e78");
@Mock ConversionService conversionService;
DelegatingConversionService delegatingConversionService;
@Before
public void setup() {
DefaultFormattingConversionService cs = new DefaultFormattingConversionService(false);
cs.addConverter(UUIDConverter.INSTANCE);
delegatingConversionService = new DelegatingConversionService(conversionService, cs);
when(conversionService.canConvert(String.class, UUID.class)).thenReturn(false);
when(conversionService.canConvert(UUID.class, String.class)).thenReturn(false);
}
@Test
public void shouldDelegateToProperConversionService() {
assertThat(delegatingConversionService.canConvert(String.class, UUID.class), is(true));
assertThat(delegatingConversionService.convert(RANDOM_UUID.toString(), UUID.class), is(RANDOM_UUID));
verifyConversionService();
}
@Test
public void shouldConvertUUIDToString() {
assertThat(delegatingConversionService.canConvert(UUID.class, String.class), is(true));
assertThat(delegatingConversionService.convert(RANDOM_UUID, String.class), is(RANDOM_UUID.toString()));
verifyConversionService();
}
private void verifyConversionService() {
verify(conversionService, times(0)).convert(Matchers.any(String.class), eq(UUID.class));
verify(conversionService, times(0)).convert(Matchers.any(UUID.class), eq(String.class));
}
}

View File

@@ -0,0 +1,44 @@
/*
* 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.core;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.rest.core.domain.jpa.Person;
import org.springframework.data.rest.core.domain.jpa.PersonRepository;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
/**
* Base class for integration tests loading {@link RepositoryTestsConfig} and populating the {@link PersonRepository}
* with a {@link Person}.
*
* @author Oliver Gierke
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = RepositoryTestsConfig.class)
@Transactional
public abstract class AbstractIntegrationTests {
@Autowired PersonRepository repository;
@Before
public void populateDatabase() {
repository.save(new Person("John", "Doe"));
}
}

View File

@@ -62,4 +62,9 @@ public class PathUnitTests {
public void doesNotMatchIfDifferent() {
assertThat(new Path("/foobar").matches("barfoo"), is(false));
}
@Test
public void doesNotPrefixAbsoluteUris() {
assertThat(new Path("http://localhost").toString(), is("http://localhost"));
}
}

View File

@@ -1,26 +1,21 @@
package org.springframework.data.rest.repository;
package org.springframework.data.rest.core;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.rest.config.RepositoryRestConfiguration;
import org.springframework.data.rest.config.ResourceMapping;
import org.springframework.data.rest.repository.domain.jpa.ConfiguredPersonRepository;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.core.config.ResourceMapping;
import org.springframework.data.rest.core.domain.jpa.ConfiguredPersonRepository;
/**
* Tests to check that {@link ResourceMapping}s are handled correctly.
*
* @author Jon Brisbin
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = RepositoryTestsConfig.class)
@SuppressWarnings("deprecation")
public class RepositoryRestConfigurationIntegrationTests {
public class RepositoryRestConfigurationIntegrationTests extends AbstractIntegrationTests {
@Autowired RepositoryRestConfiguration config;

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository;
package org.springframework.data.rest.core;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
@@ -7,11 +7,12 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.repository.support.DomainClassConverter;
import org.springframework.data.repository.support.Repositories;
import org.springframework.data.rest.config.RepositoryRestConfiguration;
import org.springframework.data.rest.repository.domain.jpa.ConfiguredPersonRepository;
import org.springframework.data.rest.repository.domain.jpa.JpaRepositoryConfig;
import org.springframework.data.rest.repository.domain.jpa.Person;
import org.springframework.data.rest.repository.domain.jpa.PersonRepository;
import org.springframework.data.rest.core.UriDomainClassConverter;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.core.domain.jpa.ConfiguredPersonRepository;
import org.springframework.data.rest.core.domain.jpa.JpaRepositoryConfig;
import org.springframework.data.rest.core.domain.jpa.Person;
import org.springframework.data.rest.core.domain.jpa.PersonRepository;
import org.springframework.format.support.DefaultFormattingConversionService;
/**

View File

@@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.config;
package org.springframework.data.rest.core.config;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.springframework.data.rest.repository.support.ResourceMappingUtils.*;
import static org.springframework.data.rest.core.support.ResourceMappingUtils.*;
import java.lang.reflect.Method;
@@ -25,9 +25,9 @@ import org.junit.Test;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.repository.annotation.RestResource;
import org.springframework.data.rest.repository.domain.jpa.Person;
import org.springframework.data.rest.repository.mapping.ResourceMapping;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.data.rest.core.domain.jpa.Person;
import org.springframework.data.rest.core.mapping.ResourceMapping;
import org.springframework.hateoas.RelProvider;
import org.springframework.hateoas.core.EvoInflectorRelProvider;
@@ -43,7 +43,7 @@ public class ResourceMappingUnitTests {
@Test
public void shouldDetectPathAndRemoveLeadingSlashIfAny() {
org.springframework.data.rest.config.ResourceMapping mapping = new org.springframework.data.rest.config.ResourceMapping(
org.springframework.data.rest.core.config.ResourceMapping mapping = new org.springframework.data.rest.core.config.ResourceMapping(
findRel(AnnotatedWithLeadingSlashPersonRepository.class),
findPath(AnnotatedWithLeadingSlashPersonRepository.class),
findExported(AnnotatedWithLeadingSlashPersonRepository.class));
@@ -59,7 +59,7 @@ public class ResourceMappingUnitTests {
public void shouldDetectPathAndRemoveLeadingSlashIfAnyOnMethod() throws Exception {
Method method = AnnotatedWithLeadingSlashPersonRepository.class.getMethod("findByFirstName", String.class,
Pageable.class);
org.springframework.data.rest.config.ResourceMapping mapping = new org.springframework.data.rest.config.ResourceMapping(
org.springframework.data.rest.core.config.ResourceMapping mapping = new org.springframework.data.rest.core.config.ResourceMapping(
findRel(method), findPath(method), findExported(method));
// The rel attribute defaults to class name
@@ -73,7 +73,7 @@ public class ResourceMappingUnitTests {
public void shouldReturnDefaultIfPathContainsOnlySlashTextOnMethod() throws Exception {
Method method = AnnotatedWithLeadingSlashPersonRepository.class.getMethod("findByLastName", String.class,
Pageable.class);
org.springframework.data.rest.config.ResourceMapping mapping = new org.springframework.data.rest.config.ResourceMapping(
org.springframework.data.rest.core.config.ResourceMapping mapping = new org.springframework.data.rest.core.config.ResourceMapping(
findRel(method), findPath(method), findExported(method));
// The rel defaults to method name

View File

@@ -1,14 +1,32 @@
package org.springframework.data.rest.repository.context;
package org.springframework.data.rest.core.context;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.rest.repository.domain.jpa.Person;
import org.springframework.data.rest.repository.domain.jpa.PersonRepository;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.rest.core.RepositoryTestsConfig;
import org.springframework.data.rest.core.domain.jpa.AnnotatedPersonEventHandler;
import org.springframework.data.rest.core.domain.jpa.Person;
import org.springframework.data.rest.core.domain.jpa.PersonBeforeSaveHandler;
import org.springframework.data.rest.core.domain.jpa.PersonRepository;
import org.springframework.data.rest.core.event.AfterCreateEvent;
import org.springframework.data.rest.core.event.AfterDeleteEvent;
import org.springframework.data.rest.core.event.AfterLinkDeleteEvent;
import org.springframework.data.rest.core.event.AfterLinkSaveEvent;
import org.springframework.data.rest.core.event.AfterSaveEvent;
import org.springframework.data.rest.core.event.AnnotatedHandlerBeanPostProcessor;
import org.springframework.data.rest.core.event.BeforeCreateEvent;
import org.springframework.data.rest.core.event.BeforeDeleteEvent;
import org.springframework.data.rest.core.event.BeforeLinkDeleteEvent;
import org.springframework.data.rest.core.event.BeforeLinkSaveEvent;
import org.springframework.data.rest.core.event.BeforeSaveEvent;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
/**
* Tests around the {@link org.springframework.context.ApplicationEvent} handling abstractions.
@@ -16,9 +34,30 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
* @author Jon Brisbin
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = RepositoryEventTestsConfig.class)
@ContextConfiguration
@Transactional
public class RepositoryEventIntegrationTests {
@Configuration
@Import({ RepositoryTestsConfig.class })
static class RepositoryEventTestsConfig {
@Bean
public PersonBeforeSaveHandler personBeforeSaveHandler() {
return new PersonBeforeSaveHandler();
}
@Bean
public AnnotatedPersonEventHandler beforeSaveHandler() {
return new AnnotatedPersonEventHandler();
}
@Bean
public AnnotatedHandlerBeanPostProcessor annotatedHandlerBeanPostProcessor() {
return new AnnotatedHandlerBeanPostProcessor();
}
}
@Autowired ApplicationContext appCtx;
@Autowired PersonRepository people;
Person person;

View File

@@ -0,0 +1,46 @@
package org.springframework.data.rest.core.context;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.rest.core.RepositoryConstraintViolationException;
import org.springframework.data.rest.core.RepositoryTestsConfig;
import org.springframework.data.rest.core.domain.jpa.Person;
import org.springframework.data.rest.core.event.BeforeSaveEvent;
import org.springframework.data.rest.core.event.ValidatingRepositoryEventListener;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
/**
* Tests to check the {@link org.springframework.validation.Validator} integration.
*
* @author Jon Brisbin
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@Transactional
public class ValidatorIntegrationTests {
@Configuration
@Import({ RepositoryTestsConfig.class })
static class ValidatorTestsConfig {
@Bean
public ValidatingRepositoryEventListener validatingListener() {
return new ValidatingRepositoryEventListener();
}
}
@Autowired ApplicationContext appCtx;
@Test(expected = RepositoryConstraintViolationException.class)
public void shouldValidateLastName() throws Exception {
appCtx.publishEvent(new BeforeSaveEvent(new Person()));
}
}

View File

@@ -0,0 +1,45 @@
package org.springframework.data.rest.core.domain.jpa;
import org.springframework.data.rest.core.annotation.HandleAfterCreate;
import org.springframework.data.rest.core.annotation.HandleAfterDelete;
import org.springframework.data.rest.core.annotation.HandleAfterLinkDelete;
import org.springframework.data.rest.core.annotation.HandleAfterLinkSave;
import org.springframework.data.rest.core.annotation.HandleAfterSave;
import org.springframework.data.rest.core.annotation.HandleBeforeCreate;
import org.springframework.data.rest.core.annotation.HandleBeforeDelete;
import org.springframework.data.rest.core.annotation.HandleBeforeLinkDelete;
import org.springframework.data.rest.core.annotation.HandleBeforeLinkSave;
import org.springframework.data.rest.core.annotation.HandleBeforeSave;
import org.springframework.data.rest.core.annotation.RepositoryEventHandler;
/**
* @author Jon Brisbin
*/
@RepositoryEventHandler(Person.class)
public class AnnotatedPersonEventHandler {
@HandleAfterCreate
@HandleAfterDelete
@HandleAfterSave
public void handleAfter(Person p) {
throw new RuntimeException();
}
@HandleAfterLinkDelete
@HandleAfterLinkSave
public void handleAfterLink(Person p, Object o) {
throw new RuntimeException();
}
@HandleBeforeCreate
@HandleBeforeDelete
@HandleBeforeSave
public void handleBefore(Person p) {
throw new RuntimeException();
}
@HandleBeforeLinkDelete
@HandleBeforeLinkSave
public void handleBeforeLink(Person p, Object o) {
throw new RuntimeException();
}
}

View File

@@ -1,11 +1,11 @@
package org.springframework.data.rest.repository.domain.jpa;
package org.springframework.data.rest.core.domain.jpa;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.rest.repository.annotation.RestResource;
import org.springframework.data.rest.core.annotation.RestResource;
/**
* A repository to manage {@link org.springframework.data.rest.repository.domain.jpa.Person}s.
* A repository to manage {@link org.springframework.data.rest.core.domain.jpa.Person}s.
*
* @author Jon Brisbin
*/

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.domain.jpa;
package org.springframework.data.rest.core.domain.jpa;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.NoRepositoryBean;

View File

@@ -0,0 +1,29 @@
/*
* 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.core.domain.jpa;
import javax.persistence.Entity;
import javax.persistence.Id;
/**
* @author Oliver Gierke
*/
@Entity
public class CreditCard {
@Id Long id;
String creditCardNumber;
}

View File

@@ -0,0 +1,26 @@
/*
* 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.core.domain.jpa;
import org.springframework.data.repository.CrudRepository;
/**
* @author Oliver Gierke
*/
interface CreditCardRepository extends CrudRepository<CreditCard, Long> {
CreditCard findByCreditCardNumber(String creditCardNumber);
}

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.repository.domain.jpa;
package org.springframework.data.rest.core.domain.jpa;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
@@ -48,7 +48,7 @@ public class JpaRepositoryConfig {
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource ms = new ResourceBundleMessageSource();
ms.setBasename("org.springframework.data.rest.repository.ValidationErrors");
ms.setBasename("org.springframework.data.rest.core.ValidationErrors");
return ms;
}

View File

@@ -0,0 +1,48 @@
/*
* 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.core.domain.jpa;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
/**
* @author Oliver Gierke
*/
@Entity
@Table(name = "ORDERS")
public class Order {
private @Id Long id;
private @ManyToOne Person creator;
public Order(Person creator) {
this.creator = creator;
}
protected Order() {
}
public Long getId() {
return id;
}
public Person getCreator() {
return creator;
}
}

View File

@@ -0,0 +1,25 @@
/*
* 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.core.domain.jpa;
import org.springframework.data.repository.CrudRepository;
/**
* @author Oliver Gierke
*/
public interface OrderRepository extends CrudRepository<Order, Long> {
}

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.domain.jpa;
package org.springframework.data.rest.core.domain.jpa;
import java.util.ArrayList;
import java.util.Calendar;

View File

@@ -1,6 +1,6 @@
package org.springframework.data.rest.repository.domain.jpa;
package org.springframework.data.rest.core.domain.jpa;
import org.springframework.data.rest.repository.context.AbstractRepositoryEventListener;
import org.springframework.data.rest.core.event.AbstractRepositoryEventListener;
/**
* @author Jon Brisbin

View File

@@ -1,9 +1,9 @@
package org.springframework.data.rest.repository.domain.jpa;
package org.springframework.data.rest.core.domain.jpa;
import static org.springframework.util.ClassUtils.*;
import static org.springframework.util.StringUtils.*;
import org.springframework.data.rest.repository.annotation.HandleBeforeSave;
import org.springframework.data.rest.core.annotation.HandleBeforeSave;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.domain.jpa;
package org.springframework.data.rest.core.domain.jpa;
import java.util.Date;
@@ -7,9 +7,9 @@ import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.convert.ISO8601DateConverter;
import org.springframework.data.rest.repository.annotation.ConvertWith;
import org.springframework.data.rest.repository.annotation.RestResource;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.DateTimeFormat.ISO;
/**
* A repository to manage {@link Person}s.
@@ -20,12 +20,11 @@ import org.springframework.data.rest.repository.annotation.RestResource;
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
@RestResource(rel = "firstname", path = "firstname")
public Page<Person> findByFirstName(@Param("firstName") String firstName, Pageable pageable);
Page<Person> findByFirstName(@Param("firstName") String firstName, Pageable pageable);
public Page<Person> findByCreatedGreaterThan(@Param("date") Date date, Pageable pageable);
Page<Person> findByCreatedGreaterThan(@Param("date") Date date, Pageable pageable);
@Query("select p from Person p where p.created > :date")
public Page<Person> findByCreatedUsingISO8601Date(@Param("date") @ConvertWith(ISO8601DateConverter.class) Date date,
Page<Person> findByCreatedUsingISO8601Date(@Param("date") @DateTimeFormat(iso = ISO.DATE_TIME) Date date,
Pageable pageable);
}

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.domain.jpa;
package org.springframework.data.rest.core.domain.jpa;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.NoRepositoryBean;

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.domain.mongodb;
package org.springframework.data.rest.core.domain.mongodb;
import java.net.UnknownHostException;

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.domain.mongodb;
package org.springframework.data.rest.core.domain.mongodb;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.domain.mongodb;
package org.springframework.data.rest.core.domain.mongodb;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.domain.mongodb;
package org.springframework.data.rest.core.domain.mongodb;
import org.bson.types.ObjectId;
import org.springframework.data.repository.CrudRepository;

View File

@@ -0,0 +1,122 @@
/*
* 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.core.invoke;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.support.Repositories;
import org.springframework.data.rest.core.AbstractIntegrationTests;
import org.springframework.data.rest.core.domain.jpa.Order;
import org.springframework.data.rest.core.domain.jpa.OrderRepository;
import org.springframework.data.rest.core.domain.jpa.Person;
import org.springframework.data.rest.core.domain.jpa.PersonRepository;
/**
* Integration tests for {@link ReflectionRepositoryInvoker}.
*
* @author Oliver Gierke
*/
public class ReflectionRepositoryInvokerIntegrationTests extends AbstractIntegrationTests {
@Autowired Repositories repositories;
@Autowired ConversionService conversionService;
@Autowired PersonRepository repository;
@Autowired OrderRepository orderRepository;
RepositoryInformation information;
RepositoryInvoker invoker;
@Before
public void setUp() {
information = repositories.getRepositoryInformationFor(Person.class);
invoker = new ReflectionRepositoryInvoker(repository, information, conversionService);
}
@Test
public void invokesFindOneWithStringIdCorrectly() {
Person person = repository.findAll().iterator().next();
assertThat(person, is(notNullValue()));
Object result = invoker.invokeFindOne(person.getId().toString());
assertThat(result, is(instanceOf(Person.class)));
}
@Test
public void invokesFindAllWithoutPageableCorrectly() {
Iterable<Object> result = invoker.invokeFindAll((Pageable) null);
assertThat(result, is(instanceOf(Page.class)));
}
@Test
public void invokesFindAllWithPageableCorrectly() {
Iterable<Object> result = invoker.invokeFindAll(new PageRequest(0, 10));
assertThat(result, is(instanceOf(Page.class)));
}
@Test
public void fallsBackToPlainFindAllIfRepositoryIsNotPaging() {
ReflectionRepositoryInvoker invoker = new ReflectionRepositoryInvoker(orderRepository,
repositories.getRepositoryInformationFor(Order.class), conversionService);
Iterable<Object> result = invoker.invokeFindAll(new PageRequest(0, 10));
assertThat(result, is(instanceOf(List.class)));
}
@Test
public void invokesQueryMethod() throws Exception {
HashMap<String, String[]> parameters = new HashMap<String, String[]>();
parameters.put("firstName", new String[] { "John" });
Method method = PersonRepository.class.getMethod("findByFirstName", String.class, Pageable.class);
Object result = invoker.invokeQueryMethod(method, parameters, null, null);
assertThat(result, is(instanceOf(Page.class)));
}
@Test
public void considersFormattingAnnotationsOnQueryMethodParameters() throws Exception {
HashMap<String, String[]> parameters = new HashMap<String, String[]>();
parameters.put("date", new String[] { "2013-07-18T10:49:00.000+02:00" });
Method method = PersonRepository.class.getMethod("findByCreatedUsingISO8601Date", Date.class, Pageable.class);
Object result = invoker.invokeQueryMethod(method, parameters, null, null);
assertThat(result, is(instanceOf(Page.class)));
Page<?> page = (Page<?>) result;
assertThat(page.getNumberOfElements(), is(1));
}
}

View File

@@ -1,4 +1,4 @@
package org.springframework.data.rest.repository.invoke;
package org.springframework.data.rest.core.invoke;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
@@ -11,8 +11,9 @@ import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.domain.Pageable;
import org.springframework.data.rest.repository.domain.jpa.PersonRepository;
import org.springframework.data.rest.repository.support.Methods;
import org.springframework.data.rest.core.domain.jpa.PersonRepository;
import org.springframework.data.rest.core.invoke.RepositoryMethod;
import org.springframework.data.rest.core.support.Methods;
import org.springframework.util.ReflectionUtils;
/**

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.repository.mapping;
package org.springframework.data.rest.core.mapping;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
@@ -21,7 +21,10 @@ import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.data.repository.Repository;
import org.springframework.data.rest.core.Path;
import org.springframework.data.rest.repository.annotation.RestResource;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.data.rest.core.mapping.CollectionResourceMapping;
import org.springframework.data.rest.core.mapping.RepositoryCollectionResourceMapping;
import org.springframework.data.rest.core.mapping.ResourceMapping;
/**
* Unit tests for {@link RepositoryCollectionResourceMapping}.
@@ -35,7 +38,7 @@ public class RepositoryCollectionResourceMappingUnitTests {
CollectionResourceMapping mapping = new RepositoryCollectionResourceMapping(PersonRepository.class);
assertThat(mapping.getPath(), is(new Path("person")));
assertThat(mapping.getPath(), is(new Path("persons")));
assertThat(mapping.getRel(), is("persons"));
assertThat(mapping.getSingleResourceRel(), is("person"));
assertThat(mapping.isExported(), is(true));

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.rest.repository.mapping;
package org.springframework.data.rest.core.mapping;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
@@ -23,32 +23,34 @@ import java.lang.reflect.Method;
import org.junit.Test;
import org.springframework.data.repository.Repository;
import org.springframework.data.rest.core.Path;
import org.springframework.data.rest.repository.annotation.RestResource;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.data.rest.core.mapping.RepositoryCollectionResourceMapping;
import org.springframework.data.rest.core.mapping.RepositoryMethodResourceMapping;
import org.springframework.data.rest.core.mapping.ResourceMapping;
/**
* @author Oliver Gierke
*/
public class RepositoryMethodResourceMappingUnitTests {
RepositoryCollectionResourceMapping resourceMapping = new RepositoryCollectionResourceMapping(
PersonRepository.class);
RepositoryCollectionResourceMapping resourceMapping = new RepositoryCollectionResourceMapping(PersonRepository.class);
@Test
public void foo() throws Exception {
Method method = PersonRepository.class.getMethod("findByLastname", String.class);
ResourceMapping mapping = new RepositoryMethodResourceMapping(method, resourceMapping);
assertThat(mapping.getPath(), is(new Path("person/findByLastname")));
assertThat(mapping.getPath(), is(new Path("findByLastname")));
}
@Test
public void usesConfiguredNameWithLeadingSlash() throws Exception {
Method method = PersonRepository.class.getMethod("findByFirstname", String.class);
ResourceMapping mapping = new RepositoryMethodResourceMapping(method, resourceMapping);
assertThat(mapping.getPath(), is(new Path("person/bar")));
assertThat(mapping.getPath(), is(new Path("bar")));
}
static class Person {}
@@ -56,10 +58,10 @@ public class RepositoryMethodResourceMappingUnitTests {
interface PersonRepository extends Repository<Person, Long> {
Iterable<Person> findByLastname(String lastname);
@RestResource(path = "/bar")
Iterable<Person> findByFirstname(String firstname);
@RestResource(path = "foo")
Iterable<Person> findByEmailAddress(String email);
}

Some files were not shown because too many files have changed in this diff Show More