Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 3dc54953575b152fef74662dd57a9b362e8af332 @jbrisbin jbrisbin committed Mar 8, 2012
Showing with 3,379 additions and 0 deletions.
  1. +4 −0 .gitignore
  2. +76 −0 build.gradle
  3. +6 −0 core/build.gradle
  4. +8 −0 core/src/main/java/org/springframework/data/rest/core/Handler.java
  5. +14 −0 core/src/main/java/org/springframework/data/rest/core/Link.java
  6. +33 −0 core/src/main/java/org/springframework/data/rest/core/SimpleLink.java
  7. +211 −0 core/src/main/java/org/springframework/data/rest/core/util/BeanUtils.java
  8. +89 −0 core/src/main/java/org/springframework/data/rest/core/util/FluentBeanDeserializer.java
  9. +75 −0 core/src/main/java/org/springframework/data/rest/core/util/FluentBeanSerializer.java
  10. +132 −0 core/src/main/java/org/springframework/data/rest/core/util/FluentBeanUtils.java
  11. +118 −0 core/src/main/java/org/springframework/data/rest/core/util/UriUtils.java
  12. +48 −0 core/src/test/groovy/org/springframework/data/rest/core/UriUtilsSpec.groovy
  13. +18 −0 core/src/test/resources/logback.xml
  14. +25 −0 gradle.properties
  15. BIN gradle/wrapper/gradle-wrapper.jar
  16. +6 −0 gradle/wrapper/gradle-wrapper.properties
  17. +164 −0 gradlew
  18. +90 −0 gradlew.bat
  19. +22 −0 repository/build.gradle
  20. +145 −0 repository/src/main/java/org/springframework/data/rest/repository/JpaEntityMetadata.java
  21. +166 −0 repository/src/main/java/org/springframework/data/rest/repository/JpaRepositoryMetadata.java
  22. +22 −0 rest/build.gradle
  23. +84 −0 rest/src/main/java/org/springframework/data/rest/mvc/JsonView.java
  24. +61 −0 rest/src/main/java/org/springframework/data/rest/mvc/RepositoryRestConfiguration.java
  25. +908 −0 rest/src/main/java/org/springframework/data/rest/mvc/RepositoryRestController.java
  26. +81 −0 rest/src/main/java/org/springframework/data/rest/mvc/RepositoryRestMvcConfiguration.java
  27. +31 −0 rest/src/main/java/org/springframework/data/rest/mvc/ServerHttpRequestMethodArgumentResolver.java
  28. +68 −0 rest/src/main/java/org/springframework/data/rest/mvc/UriListView.java
  29. +8 −0 rest/src/main/webapp/WEB-INF/repositories.xml
  30. +48 −0 rest/src/main/webapp/WEB-INF/web.xml
  31. +225 −0 rest/src/test/java/org/springframework/data/rest/test/RestBuilder.java
  32. +61 −0 rest/src/test/java/org/springframework/data/rest/test/mvc/Address.java
  33. +9 −0 rest/src/test/java/org/springframework/data/rest/test/mvc/AddressRepository.java
  34. +77 −0 rest/src/test/java/org/springframework/data/rest/test/mvc/Person.java
  35. +63 −0 rest/src/test/java/org/springframework/data/rest/test/mvc/PersonLoader.java
  36. +9 −0 rest/src/test/java/org/springframework/data/rest/test/mvc/PersonRepository.java
  37. +80 −0 rest/src/test/java/org/springframework/data/rest/test/mvc/Profile.java
  38. +9 −0 rest/src/test/java/org/springframework/data/rest/test/mvc/ProfileRepository.java
  39. +16 −0 rest/src/test/resouces/META-INF/persistence.xml
  40. +18 −0 rest/src/test/resouces/META-INF/spring-data-rest/repositories-export.xml
  41. +29 −0 rest/src/test/resouces/META-INF/spring-data-rest/shared.xml
  42. +19 −0 rest/src/test/resouces/logback.xml
  43. +3 −0 settings.gradle
@@ -0,0 +1,4 @@
+.DS_Store
+.gradle/
+build/
+*.i*
@@ -0,0 +1,76 @@
+apply plugin: "base"
+
+allprojects {
+ apply plugin: "idea"
+ apply plugin: "maven"
+
+ group = "org.springframework.data.rest"
+ version = "$sdRestVersion"
+
+ releaseBuild = version.endsWith("RELEASE")
+ snapshotBuild = version.endsWith("SNAPSHOT")
+
+ sourceCompatibility = 6
+ targetCompatibility = 6
+
+ configurations.all {
+ exclude group: "commons-logging"
+ exclude module: "slf4j-log4j12"
+ exclude module: "groovy-all", version: "1.8.0-beta-3-SNAPSHOT"
+ }
+
+}
+
+subprojects {
+
+ repositories {
+ mavenCentral(artifactUrls: [
+ "http://maven.springframework.org/release",
+ "http://maven.springframework.org/milestone",
+ //"http://repository.jboss.org/maven2/",
+ //"http://download.java.net/maven/2/"
+ ])
+ mavenLocal()
+ }
+
+ apply plugin: "java"
+ apply plugin: "groovy"
+
+ [compileJava, compileTestJava]*.options*.compilerArgs = ["-Xlint:-serial", "-Xlint:-unchecked"]
+
+ dependencies {
+ groovy "org.codehaus.groovy:groovy:$groovyVersion"
+
+ // Logging
+ compile "org.slf4j:slf4j-api:$slf4jVersion"
+ runtime "org.slf4j:jcl-over-slf4j:$slf4jVersion"
+ runtime "ch.qos.logback:logback-classic:$logbackVersion"
+
+ // Jackson JSON
+ compile "org.codehaus.jackson:jackson-mapper-asl:$jacksonVersion"
+
+ // Spring
+ compile "org.springframework:spring-beans:$springVersion"
+ compile "org.springframework:spring-context:$springVersion"
+ compile "org.springframework:spring-core:$springVersion"
+ compile "org.springframework:spring-web:$springVersion"
+
+ // Testing
+ testCompile "org.spockframework:spock-core:$spockVersion"
+ testCompile "org.spockframework:spock-spring:$spockVersion"
+ testCompile "org.hamcrest:hamcrest-all:1.1"
+ testCompile "org.springframework:spring-test:$springVersion"
+ testRuntime "org.springframework:spring-context-support:$springVersion"
+
+ }
+
+}
+
+task wrapper(type: Wrapper) { gradleVersion = "1.0-milestone-8" }
+
+idea {
+ project.ipr.withXml { provider ->
+ provider.node.component.find { it.@name == 'VcsDirectoryMappings' }.mapping.@vcs = 'Git'
+ }
+ module.jdkName = "OpenJDK 1.7"
+}
@@ -0,0 +1,6 @@
+dependencies {
+
+ // Google Guava
+ compile "com.google.guava:guava:11.0.1"
+
+}
@@ -0,0 +1,8 @@
+package org.springframework.data.rest.core;
+
+/**
+ * @author Jon Brisbin <jon@jbrisbin.com>
+ */
+public interface Handler<T,V> {
+ V handle(T t);
+}
@@ -0,0 +1,14 @@
+package org.springframework.data.rest.core;
+
+import java.net.URI;
+
+/**
+ * @author Jon Brisbin <jon@jbrisbin.com>
+ */
+public interface Link {
+
+ String rel();
+
+ URI href();
+
+}
@@ -0,0 +1,33 @@
+package org.springframework.data.rest.core;
+
+import java.net.URI;
+
+/**
+ * @author Jon Brisbin <jon@jbrisbin.com>
+ */
+public class SimpleLink implements Link {
+
+ private String rel;
+ private URI href;
+
+ public SimpleLink(String rel, URI href) {
+ this.rel = rel;
+ this.href = href;
+ }
+
+ @Override public String rel() {
+ return rel;
+ }
+
+ @Override public URI href() {
+ return href;
+ }
+
+ @Override public String toString() {
+ return "SimpleLink{" +
+ "rel='" + rel + '\'' +
+ ", href=" + href +
+ '}';
+ }
+
+}
@@ -0,0 +1,211 @@
+package org.springframework.data.rest.core.util;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ExecutionException;
+
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
+import com.google.common.util.concurrent.UncheckedExecutionException;
+import org.springframework.core.convert.support.ConfigurableConversionService;
+import org.springframework.core.convert.support.DefaultConversionService;
+import org.springframework.util.ClassUtils;
+import org.springframework.util.ReflectionUtils;
+import org.springframework.util.StringUtils;
+
+/**
+ * @author Jon Brisbin <jon@jbrisbin.com>
+ */
+public abstract class BeanUtils {
+
+ private BeanUtils() {
+ }
+
+ public static ConfigurableConversionService CONVERSION_SERVICE = new DefaultConversionService();
+
+ private static final LoadingCache<Object[], Field> fields = CacheBuilder.newBuilder().build(
+ new CacheLoader<Object[], Field>() {
+ @Override public Field load(Object[] key) throws Exception {
+ Class<?> clazz = (Class<?>) key[0];
+ String name = (String) key[1];
+ Field f = ReflectionUtils.findField(clazz, name);
+ if (null != f) {
+ ReflectionUtils.makeAccessible(f);
+ return f;
+ } else {
+ throw new IllegalArgumentException("Field " + clazz.getName() + "." + name + " not found");
+ }
+ }
+ }
+ );
+ private static final LoadingCache<Object[], Method> methods = CacheBuilder.newBuilder().build(
+ new CacheLoader<Object[], Method>() {
+ @Override public Method load(Object[] key) throws Exception {
+ Class<?> clazz = (Class<?>) key[0];
+ String name = (String) key[1];
+ Integer paramCnt = key.length == 3 ? (Integer) key[2] : 0;
+
+ for (Method m : clazz.getDeclaredMethods()) {
+ if (m.getName().equals(name)) {
+ if (m.getParameterTypes().length == paramCnt) {
+ ReflectionUtils.makeAccessible(m);
+ return m;
+ }
+ }
+ }
+
+ throw new IllegalArgumentException("Method " + clazz.getName() + "." + name + " not found");
+ }
+ }
+ );
+
+ public static boolean hasProperty(String property, Object... objs) {
+ for (Object obj : objs) {
+ if (obj instanceof Map) {
+ return ((Map) obj).containsKey(property);
+ }
+ Class<?> type = obj.getClass();
+ try {
+ if (FluentBeanUtils.isFluentBean(type)) {
+ return null != methods.get(new Object[]{type, property});
+ } else {
+ if (null == methods.get(new Object[]{type, "get" + StringUtils.capitalize(property)})) {
+ return null != fields.get(new Object[]{type, property});
+ } else {
+ return true;
+ }
+ }
+ } catch (UncheckedExecutionException e) {
+ if (e.getCause().getClass() == IllegalArgumentException.class) {
+ return false;
+ } else {
+ throw new IllegalStateException(e);
+ }
+ } catch (ExecutionException e) {
+ throw new IllegalStateException(e);
+ }
+ }
+ return false;
+ }
+
+ @SuppressWarnings({"unchecked"})
+ public static <T> T findFirst(Class<T> clazz, List<?> stack) {
+ for (Object o : stack) {
+ if (ClassUtils.isAssignable(clazz, o.getClass())) {
+ return (T) o;
+ }
+ }
+ return null;
+ }
+
+ @SuppressWarnings({"unchecked"})
+ public static Object findFirst(Object o, Object... objs) {
+ for (Object obj : objs) {
+ if (o == obj || null != o && o.equals(obj)) {
+ return obj;
+ } else if (obj instanceof List) {
+ return Collections.binarySearch((List) obj, o);
+ } else if (obj instanceof Object[]) {
+ return Arrays.binarySearch((Object[]) obj, o);
+ }
+ }
+ return null;
+ }
+
+ public static Object findFirst(String property, Object... objs) {
+ for (Object obj : objs) {
+ if (obj instanceof Map) {
+ return ((Map) obj).get(property);
+ }
+ Class<?> type = obj.getClass();
+ try {
+ Field f = fields.get(new Object[]{type, property});
+ if (FluentBeanUtils.isFluentBean(type)) {
+ return FluentBeanUtils.get(property, obj);
+ } else {
+ Method getter = methods.get(new Object[]{type, "get" + StringUtils.capitalize(property)});
+ try {
+ if (null != getter) {
+ return getter.invoke(obj);
+ } else {
+ return f.get(obj);
+ }
+ } catch (IllegalAccessException e) {
+ throw new IllegalStateException(e);
+ } catch (InvocationTargetException e) {
+ throw new IllegalStateException(e);
+ }
+ }
+ } catch (IllegalArgumentException e) {
+ } catch (ExecutionException e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ return null;
+ }
+
+ public static boolean containsType(Class<?> type, List<Object> objs) {
+ return containsType(type, objs.toArray());
+ }
+
+ public static boolean containsType(Class<?> type, Object[] objs) {
+ for (Object obj : objs) {
+ if (null != obj && ClassUtils.isAssignable(obj.getClass(), type)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ @SuppressWarnings({"unchecked"})
+ public static Object invoke(String methodName, Object target, Object... args) {
+ return invoke(methodName, target, Object.class, args);
+ }
+
+ @SuppressWarnings({"unchecked"})
+ public static <T> T invoke(String methodName, Object target, Class<T> returnType, Object... args) {
+ if (null == target) {
+ return null;
+ }
+
+ Class<?> type = target.getClass();
+ try {
+ Method m = methods.get(new Object[]{type, methodName, args.length});
+ List<Object> newArgs = new ArrayList<Object>(args.length);
+ Class<?>[] paramTypes = m.getParameterTypes();
+ for (int i = 0; i < args.length; i++) {
+ Object o = args[i];
+ Class<?> oType = o.getClass();
+ Class<?> pType = paramTypes[i];
+ if (!ClassUtils.isAssignable(oType, pType)) {
+ newArgs.add(CONVERSION_SERVICE.convert(o, pType));
+ } else {
+ newArgs.add(o);
+ }
+ }
+
+ Object rtnVal = m.invoke(target, newArgs.toArray());
+ if ((returnType != Void.TYPE || returnType != Object.class)
+ && null != rtnVal
+ && !ClassUtils.isAssignable(returnType, rtnVal.getClass())) {
+ return CONVERSION_SERVICE.convert(rtnVal, returnType);
+ } else {
+ return (T) rtnVal;
+ }
+ } catch (IllegalArgumentException e) {
+ } catch (Exception e) {
+ throw new IllegalStateException(e);
+ }
+
+ return null;
+ }
+
+}
Oops, something went wrong.

0 comments on commit 3dc5495

Please sign in to comment.