You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@juneau.apache.org by ja...@apache.org on 2018/05/23 23:02:43 UTC

[juneau] branch master updated: Add support for Inherit.class and None.class in annotations.

This is an automated email from the ASF dual-hosted git repository.

jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git


The following commit(s) were added to refs/heads/master by this push:
     new 257e85f  Add support for Inherit.class and None.class in annotations.
257e85f is described below

commit 257e85f74bd65b4a9b8cde17495fdeb3f7d0004f
Author: JamesBognar <ja...@apache.org>
AuthorDate: Wed May 23 19:02:26 2018 -0400

    Add support for Inherit.class and None.class in annotations.
---
 juneau-doc/src/main/javadoc/overview.html          |  12 +++
 .../juneau/microservice/BasicRestServletJena.java  |   4 +-
 .../microservice/BasicRestServletJenaGroup.java    |   4 +-
 .../org/apache/juneau/rest/RestContextBuilder.java |   9 +-
 .../org/apache/juneau/rest/RestJavaMethod.java     |  35 +------
 .../apache/juneau/rest/annotation/RestMethod.java  |   3 -
 .../juneau/rest/annotation/RestResource.java       |  22 ++++-
 .../java/org/apache/juneau/rest/util/Utils.java    | 105 +++++++++++++--------
 .../rest/annotation/RestMethodInheritTest.java     |   6 +-
 9 files changed, 112 insertions(+), 88 deletions(-)

diff --git a/juneau-doc/src/main/javadoc/overview.html b/juneau-doc/src/main/javadoc/overview.html
index 66cc6f1..2d585fd 100644
--- a/juneau-doc/src/main/javadoc/overview.html
+++ b/juneau-doc/src/main/javadoc/overview.html
@@ -21765,6 +21765,18 @@
 					<li class='jc'>{@link org.apache.juneau.rest.Inherit}
 					<li class='jc'>{@link org.apache.juneau.rest.None}
 				</ul>
+				<br>These can be used in the following locations:
+				<ul>
+					<li class='ja'>{@link org.apache.juneau.rest.annotation.RestResource#serializers()}
+					<li class='ja'>{@link org.apache.juneau.rest.annotation.RestResource#parsers()}
+					<li class='ja'>{@link org.apache.juneau.rest.annotation.RestResource#beanFilters()}
+					<li class='ja'>{@link org.apache.juneau.rest.annotation.RestResource#pojoSwaps()}
+					<li class='ja'>{@link org.apache.juneau.rest.annotation.RestMethod#serializers()}
+					<li class='ja'>{@link org.apache.juneau.rest.annotation.RestMethod#parsers()}
+					<li class='ja'>{@link org.apache.juneau.rest.annotation.RestMethod#beanFilters()}
+					<li class='ja'>{@link org.apache.juneau.rest.annotation.RestMethod#pojoSwaps()}
+				</ul>
+				<br>One advantage is that you now have control over the precedence of serializers and parsers by where you insert the <code>Inherit</code> class. 
 		</ul>
 	
 		<h5 class='topic w800'>juneau-rest-client</h5>
diff --git a/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/BasicRestServletJena.java b/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/BasicRestServletJena.java
index c0711e8..2732bfc 100755
--- a/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/BasicRestServletJena.java
+++ b/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/BasicRestServletJena.java
@@ -12,7 +12,6 @@
 // ***************************************************************************************************************************
 package org.apache.juneau.microservice;
 
-import org.apache.juneau.html.*;
 import org.apache.juneau.jena.*;
 import org.apache.juneau.rest.*;
 import org.apache.juneau.rest.annotation.*;
@@ -23,7 +22,7 @@ import org.apache.juneau.rest.annotation.*;
 @SuppressWarnings("serial")
 @RestResource(
 	serializers={
-		HtmlDocSerializer.class,  // HTML must be listed first because Internet Explore does not include text/html in their Accept header.
+		Inherit.class,
 		RdfSerializer.Xml.class,
 		RdfSerializer.XmlAbbrev.class,
 		RdfSerializer.Turtle.class,
@@ -31,6 +30,7 @@ import org.apache.juneau.rest.annotation.*;
 		RdfSerializer.N3.class
 	},
 	parsers={
+		Inherit.class,
 		RdfParser.Xml.class,
 		RdfParser.Turtle.class,
 		RdfParser.NTriple.class,
diff --git a/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/BasicRestServletJenaGroup.java b/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/BasicRestServletJenaGroup.java
index dde9662..ae5ba04 100644
--- a/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/BasicRestServletJenaGroup.java
+++ b/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/BasicRestServletJenaGroup.java
@@ -12,7 +12,6 @@
 // ***************************************************************************************************************************
 package org.apache.juneau.microservice;
 
-import org.apache.juneau.html.*;
 import org.apache.juneau.jena.*;
 import org.apache.juneau.rest.*;
 import org.apache.juneau.rest.annotation.*;
@@ -23,7 +22,7 @@ import org.apache.juneau.rest.annotation.*;
 @SuppressWarnings("serial")
 @RestResource(
 	serializers={
-		HtmlDocSerializer.class,  // HTML must be listed first because Internet Explore does not include text/html in their Accept header.
+		Inherit.class,
 		RdfSerializer.Xml.class,
 		RdfSerializer.XmlAbbrev.class,
 		RdfSerializer.Turtle.class,
@@ -31,6 +30,7 @@ import org.apache.juneau.rest.annotation.*;
 		RdfSerializer.N3.class
 	},
 	parsers={
+		Inherit.class,
 		RdfParser.Xml.class,
 		RdfParser.Turtle.class,
 		RdfParser.NTriple.class,
diff --git a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContextBuilder.java b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContextBuilder.java
index 9252be5..cf68b1e 100644
--- a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContextBuilder.java
+++ b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContextBuilder.java
@@ -18,6 +18,7 @@ import static org.apache.juneau.internal.StringUtils.*;
 import static org.apache.juneau.internal.ClassUtils.*;
 import static org.apache.juneau.parser.Parser.*;
 import static org.apache.juneau.rest.RestContext.*;
+import static org.apache.juneau.rest.util.Utils.*;
 import static org.apache.juneau.serializer.Serializer.*;
 
 import java.lang.reflect.Method;
@@ -165,8 +166,8 @@ public class RestContextBuilder extends BeanContextBuilder implements ServletCon
 					set(vr.resolve(p.name()), vr.resolve(p.value()));
 				for (String p : r.flags())
 					set(p, true);
-				serializers(r.serializers());
-				parsers(r.parsers());
+				serializers(false, merge(ObjectUtils.toType(psb.peek(REST_serializers), Object[].class), r.serializers()));
+				parsers(false, merge(ObjectUtils.toType(psb.peek(REST_parsers), Object[].class), r.parsers()));
 				encoders(r.encoders());
 				if (r.produces().length > 0)
 					produces(false, resolveVars(vr, r.produces()));
@@ -178,8 +179,8 @@ public class RestContextBuilder extends BeanContextBuilder implements ServletCon
 				converters(r.converters());
 				guards(reverse(r.guards()));
 				children(r.children());
-				beanFilters((Object[])r.beanFilters());
-				pojoSwaps(r.pojoSwaps());
+				beanFilters(false, merge(ObjectUtils.toType(psb.peek(BEAN_beanFilters), Object[].class), r.beanFilters()));
+				pojoSwaps(false, merge(ObjectUtils.toType(psb.peek(BEAN_pojoSwaps), Object[].class), r.pojoSwaps()));
 				paramResolvers(r.paramResolvers());
 				if (r.serializerListener() != SerializerListener.Null.class)
 					serializerListener(r.serializerListener());
diff --git a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestJavaMethod.java b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestJavaMethod.java
index ae7384f..a70d892 100644
--- a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestJavaMethod.java
+++ b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestJavaMethod.java
@@ -20,6 +20,7 @@ import static org.apache.juneau.internal.StringUtils.*;
 import static org.apache.juneau.internal.Utils.*;
 import static org.apache.juneau.rest.RestContext.*;
 import static org.apache.juneau.rest.util.RestUtils.*;
+import static org.apache.juneau.rest.util.Utils.*;
 
 import java.lang.annotation.*;
 import java.lang.reflect.*;
@@ -166,21 +167,16 @@ public class RestJavaMethod implements Comparable<RestJavaMethod>  {
 					hdb.style("INHERIT", "$W{"+w.getName()+".style}");
 				}
 
-//				ASet<String> inherit = new ASet<String>().appendAll(StringUtils.split(m.inherit()));
-//				if (inherit.contains("*")) 
-//					inherit.appendAll("SERIALIZERS","PARSERS","TRANSFORMS","PROPERTIES","ENCODERS");
-
 				SerializerGroupBuilder sgb = null;
 				ParserGroupBuilder pgb = null;
 				ParserBuilder uepb = null;
 				BeanContextBuilder bcb = null;
 				PropertyStore cps = context.getPropertyStore();
 				
-				
-				Object[] mSerializers = resolve(cps.getArrayProperty(REST_serializers, Object.class), m.serializers());
-				Object[] mParsers = resolve(cps.getArrayProperty(REST_parsers, Object.class), m.parsers());
-				Object[] mPojoSwaps = resolve(cps.getArrayProperty(BEAN_pojoSwaps, Object.class), m.pojoSwaps());
-				Object[] mBeanFilters = resolve(cps.getArrayProperty(BEAN_beanFilters, Object.class), m.beanFilters());
+				Object[] mSerializers = merge(cps.getArrayProperty(REST_serializers, Object.class), m.serializers());
+				Object[] mParsers = merge(cps.getArrayProperty(REST_parsers, Object.class), m.parsers());
+				Object[] mPojoSwaps = merge(cps.getArrayProperty(BEAN_pojoSwaps, Object.class), m.pojoSwaps());
+				Object[] mBeanFilters = merge(cps.getArrayProperty(BEAN_beanFilters, Object.class), m.beanFilters());
 				
 				if (m.serializers().length > 0 || m.parsers().length > 0 || m.properties().length > 0 || m.flags().length > 0
 						|| m.beanFilters().length > 0 || m.pojoSwaps().length > 0 || m.bpi().length > 0
@@ -404,27 +400,6 @@ public class RestJavaMethod implements Comparable<RestJavaMethod>  {
 		}
 	}
 	
-	static Object[] resolve(Object[] fromClass, Object[] fromAnnotation) {
-		
-		if (ArrayUtils.contains(None.class, fromAnnotation)) 
-			return ArrayUtils.remove(None.class, fromAnnotation);
-		
-		if (fromAnnotation.length == 0)
-			return fromClass;
-		
-		if (! ArrayUtils.contains(Inherit.class, fromAnnotation))
-			return fromAnnotation;
-		
-		List<Object> l = new ArrayList<>(fromClass.length + fromAnnotation.length);
-		for (Object o : fromAnnotation) {
-			if (o == Inherit.class)
-				l.addAll(Arrays.asList(fromClass));
-			else
-				l.add(o);
-		}
-		return l.toArray(new Object[l.size()]);
-	}
-
 	/**
 	 * Returns <jk>true</jk> if this Java method has any guards or matchers.
 	 */
diff --git a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestMethod.java b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestMethod.java
index 8144782f..eaf4f01 100644
--- a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestMethod.java
+++ b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestMethod.java
@@ -629,9 +629,6 @@ public @interface RestMethod {
 	 * Serializers. 
 	 * 
 	 * <p>
-	 * Overrides the list of serializers assigned at the method level.
-	 * 
-	 * <p>
 	 * If no value is specified, the serializers are inherited from the class.
 	 * <br>Otherwise, this value overrides the serializers defined on the class.
 	 * 
diff --git a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestResource.java b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestResource.java
index 1263201..719d023 100644
--- a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestResource.java
+++ b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestResource.java
@@ -542,14 +542,21 @@ public @interface RestResource {
 	 * Parsers. 
 	 * 
 	 * <p>
-	 * Adds class-level parsers to this resource.
+	 * If no value is specified, the parsers are inherited from parent class.
+	 * <br>Otherwise, this value overrides the parsers defined on the parent class.
+	 * 
+	 * <p>
+	 * Use {@link Inherit} to inherit parsers defined on the parent class.
+	 * 
+	 * <p>
+	 * Use {@link None} to suppress inheriting parsers defined on the parent class.
 	 * 
 	 * <h5 class='section'>See Also:</h5>
 	 * <ul>
 	 * 	<li class='jf'>{@link RestContext#REST_parsers}
 	 * </ul>
 	 */
-	Class<? extends Parser>[] parsers() default {};
+	Class<?>[] parsers() default {};
 
 	/**
 	 * HTTP part parser. 
@@ -709,14 +716,21 @@ public @interface RestResource {
 	 * Serializers. 
 	 * 
 	 * <p>
-	 * Adds class-level serializers to this resource.
+	 * If no value is specified, the serializers are inherited from parent class.
+	 * <br>Otherwise, this value overrides the serializers defined on the parent class.
+	 * 
+	 * <p>
+	 * Use {@link Inherit} to inherit serializers defined on the parent class.
+	 * 
+	 * <p>
+	 * Use {@link None} to suppress inheriting serializers defined on the parent class.
 	 * 
 	 * <h5 class='section'>See Also:</h5>
 	 * <ul>
 	 * 	<li class='jf'>{@link RestContext#REST_serializers}
 	 * </ul>
 	 */
-	Class<? extends Serializer>[] serializers() default {};
+	Class<?>[] serializers() default {};
 
 	/**
 	 * Optional site name.
diff --git a/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/BasicRestServletJena.java b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/util/Utils.java
old mode 100755
new mode 100644
similarity index 50%
copy from juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/BasicRestServletJena.java
copy to juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/util/Utils.java
index c0711e8..671495a
--- a/juneau-microservice/juneau-microservice-server/src/main/java/org/apache/juneau/microservice/BasicRestServletJena.java
+++ b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/util/Utils.java
@@ -1,40 +1,65 @@
-// ***************************************************************************************************************************
-// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
-// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
-// * to you 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.apache.juneau.microservice;
-
-import org.apache.juneau.html.*;
-import org.apache.juneau.jena.*;
-import org.apache.juneau.rest.*;
-import org.apache.juneau.rest.annotation.*;
-
-/**
- * Resource servlet with additional RDF support.
- */
-@SuppressWarnings("serial")
-@RestResource(
-	serializers={
-		HtmlDocSerializer.class,  // HTML must be listed first because Internet Explore does not include text/html in their Accept header.
-		RdfSerializer.Xml.class,
-		RdfSerializer.XmlAbbrev.class,
-		RdfSerializer.Turtle.class,
-		RdfSerializer.NTriple.class,
-		RdfSerializer.N3.class
-	},
-	parsers={
-		RdfParser.Xml.class,
-		RdfParser.Turtle.class,
-		RdfParser.NTriple.class,
-		RdfParser.N3.class
-	}
-)
-public abstract class BasicRestServletJena extends BasicRestServlet {}
+// ***************************************************************************************************************************
+// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
+// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
+// * to you 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.apache.juneau.rest.util;
+
+import java.util.*;
+
+import org.apache.juneau.internal.*;
+import org.apache.juneau.rest.*;
+
+/**
+ * Various reusable utility methods used by the REST server API.
+ */
+public final class Utils {
+	
+	/**
+	 * Merges the specified parent and child arrays.
+	 * 
+	 * <p>
+	 * The general concept is to allow child values to override parent values.
+	 * 
+	 * <p>
+	 * The rules are:
+	 * <ul>
+	 * 	<li>If the child array is not empty, then the child array is returned.
+	 * 	<li>If the child array is empty, then the parent array is returned.
+	 * 	<li>If the child array contains {@link None}, then an empty array is always returned.
+	 * 	<li>If the child array contains {@link Inherit}, then the contents of the parent array are inserted into the position of the {@link Inherit} entry.
+	 * </ul>
+	 * 
+	 * @param fromParent The parent array.
+	 * @param fromChild The child array.
+	 * @return A new merged array.
+	 */
+	public static Object[] merge(Object[] fromParent, Object[] fromChild) {
+		
+		if (ArrayUtils.contains(None.class, fromChild)) 
+			return new Object[0];
+		
+		if (fromChild.length == 0)
+			return fromParent;
+		
+		if (! ArrayUtils.contains(Inherit.class, fromChild))
+			return fromChild;
+		
+		List<Object> l = new ArrayList<>(fromParent.length + fromChild.length);
+		for (Object o : fromChild) {
+			if (o == Inherit.class)
+				l.addAll(Arrays.asList(fromParent));
+			else
+				l.add(o);
+		}
+		return l.toArray(new Object[l.size()]);
+	}
+
+}
diff --git a/juneau-rest/juneau-rest-server/src/test/java/org/apache/juneau/rest/annotation/RestMethodInheritTest.java b/juneau-rest/juneau-rest-server/src/test/java/org/apache/juneau/rest/annotation/RestMethodInheritTest.java
index 7666da6..2598ada 100644
--- a/juneau-rest/juneau-rest-server/src/test/java/org/apache/juneau/rest/annotation/RestMethodInheritTest.java
+++ b/juneau-rest/juneau-rest-server/src/test/java/org/apache/juneau/rest/annotation/RestMethodInheritTest.java
@@ -131,7 +131,7 @@ public class RestMethodInheritTest {
 	@RestResource(serializers={S1.class,S2.class})
 	public static class A {}
 	
-	@RestResource(serializers={S3.class,S4.class})
+	@RestResource(serializers={S3.class,S4.class,Inherit.class})
 	public static class A01 extends A {}
 
 	@RestResource
@@ -174,7 +174,7 @@ public class RestMethodInheritTest {
 	@RestResource(parsers={P1.class,P2.class})
 	public static class B {}
 
-	@RestResource(parsers={P3.class,P4.class})
+	@RestResource(parsers={P3.class,P4.class,Inherit.class})
 	public static class B01 extends B {}
 
 	@RestResource
@@ -217,7 +217,7 @@ public class RestMethodInheritTest {
 	@RestResource(pojoSwaps={F1Swap.class})
 	public static class D {}
 
-	@RestResource(pojoSwaps={F2Swap.class})
+	@RestResource(pojoSwaps={F2Swap.class,Inherit.class})
 	public static class D01 extends D {}
 
 	@RestResource(serializers=JsonSerializer.Simple.class)

-- 
To stop receiving notification emails like this one, please contact
jamesbognar@apache.org.