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 2019/05/29 23:14:29 UTC

[juneau] branch master updated: RestContext refactor.

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 706df60  RestContext refactor.
706df60 is described below

commit 706df6023827437cbe8b02314e981d85ae3687ee
Author: JamesBognar <ja...@apache.org>
AuthorDate: Wed May 29 19:14:10 2019 -0400

    RestContext refactor.
---
 .../java/org/apache/juneau/rest/RestContext.java   | 33 ++++++++++++
 .../org/apache/juneau/rest/RestContextBuilder.java | 37 +++++++++++++
 .../org/apache/juneau/rest/RestMethodContext.java  | 10 ++++
 .../juneau/rest/RestMethodContextBuilder.java      |  3 +-
 .../rest/annotation/RestMethodConfigApply.java     | 63 ++++++++++++++++++++++
 .../rest/annotation/RestResourceConfigApply.java   | 12 +++--
 .../org/apache/juneau/rest/util/RestUtils.java     |  5 ++
 7 files changed, 158 insertions(+), 5 deletions(-)

diff --git a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContext.java b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContext.java
index b8e7f95..315f928 100644
--- a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContext.java
+++ b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContext.java
@@ -2586,6 +2586,39 @@ public final class RestContext extends BeanContext {
 	public static final String REST_produces = PREFIX + "produces.ls";
 
 	/**
+	 * Configuration property:  Properties.
+	 *
+	 * <h5 class='section'>Property:</h5>
+	 * <ul>
+	 * 	<li><b>Name:</b>  <js>"RestContext.properties.sms"</js>
+	 * 	<li><b>Data type:</b>  <code>Map&lt;String,String&gt;</code>
+	 * 	<li><b>Default:</b>  empty map
+	 * 	<li><b>Session property:</b>  <jk>false</jk>
+	 * 	<li><b>Annotations:</b>
+	 * 		<ul>
+	 * 			<li class='ja'>{@link RestResource#properties()}
+	 * 			<li class='ja'>{@link RestResource#flags()}
+	 * 			<li class='ja'>{@link RestMethod#properties()}
+	 * 			<li class='ja'>{@link RestMethod#flags()}
+	 * 		</ul>
+	 * 	<li><b>Methods:</b>
+	 * 		<ul>
+	 * 			<li class='jm'>{@link RestContextBuilder#property(String,Object)}
+	 * 			<li class='jm'>{@link RestContextBuilder#properties(Map)}
+	 * 		</ul>
+	 * </ul>
+	 *
+	 * <h5 class='section'>Description:</h5>
+	 * <p>
+	 * Defines arbitrary properties that can be retrieved via the following methods:
+	 * <ul>
+	 * 	<li class='jm'>{@link RestRequest#getProperties()}
+	 * 	<li class='jm'>{@link RestResponse#getProperties()}
+	 * </ul>
+	 */
+	public static final String REST_properties = PREFIX + "properties.sms";
+
+	/**
 	 * Configuration property:  Supported content media types.
 	 *
 	 * <h5 class='section'>Property:</h5>
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 9886fc0..1f5d81d 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
@@ -2010,6 +2010,41 @@ public class RestContextBuilder extends BeanContextBuilder implements ServletCon
 	}
 
 	/**
+	 * Configuration property:  Properties.
+	 *
+	 * <p>
+	 * Defines arbitrary properties that can be retrieved via the following methods:
+	 * <ul>
+	 * 	<li class='jm'>{@link RestRequest#getProperties()}
+	 * 	<li class='jm'>{@link RestResponse#getProperties()}
+	 * </ul>
+	 *
+	 * @param values The values to set on this setting.
+	 * @return This object (for method chaining).
+	 */
+	public RestContextBuilder properties(Map<String,Object> values) {
+		return addTo(REST_properties, values);
+	}
+
+	/**
+	 * Configuration property:  Properties.
+	 *
+	 * <p>
+	 * Defines arbitrary properties that can be retrieved via the following methods:
+	 * <ul>
+	 * 	<li class='jm'>{@link RestRequest#getProperties()}
+	 * 	<li class='jm'>{@link RestResponse#getProperties()}
+	 * </ul>
+	 *
+	 * @param name The key to add to the properties.
+	 * @param value The value to add to the properties.
+	 * @return This object (for method chaining).
+	 */
+	public RestContextBuilder property(String name, Object value) {
+		return addTo(REST_properties, name, value);
+	}
+
+	/**
 	 * Configuration property:  Resource authority path.
 	 *
 	 * <p>
@@ -2637,6 +2672,7 @@ public class RestContextBuilder extends BeanContextBuilder implements ServletCon
 	public RestContextBuilder set(String name, Object value) {
 		super.set(name, value);
 		this.properties.put(name, value);
+		addTo(REST_properties, name, value);
 		return this;
 	}
 
@@ -2645,6 +2681,7 @@ public class RestContextBuilder extends BeanContextBuilder implements ServletCon
 		super.set(properties);
 		this.properties.clear();
 		this.properties.putAll(properties);
+		addTo(REST_properties, properties);
 		return this;
 	}
 
diff --git a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestMethodContext.java b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestMethodContext.java
index 0a9f416..c63e3bd 100644
--- a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestMethodContext.java
+++ b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestMethodContext.java
@@ -94,6 +94,16 @@ public class RestMethodContext extends BeanContext implements Comparable<RestMet
 		defaultCharset = getProperty(REST_defaultCharset, String.class, context.getDefaultCharset());
 		maxInput = StringUtils.parseLongWithSuffix(getProperty(REST_maxInput, String.class, String.valueOf(context.getMaxInput())));
 
+//		Object[] empty = new Object[0];
+//		boolean needsNewContext =
+//			b.hasConfigAnnotations
+//			|| getArrayProperty(REST_serializers, Object.class, empty).length > 0
+//			|| getArrayProperty(REST_parsers, Object.class, empty).length > 0
+//			|| getArrayProperty(BEAN_beanFilters, Object.class, empty).length > 0
+//			|| getArrayProperty(BEAN_pojoSwaps, Object.class, empty).length > 0
+//			|| getArrayProperty(BEAN_includeProperties, Object.class, empty).length > 0
+//			|| getArrayProperty(BEAN_excludeProperties, Object.class, empty).length > 0;
+
 		this.pathPattern = b.pathPattern;
 		this.methodParams = b.methodParams;
 		this.guards = b.guards;
diff --git a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestMethodContextBuilder.java b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestMethodContextBuilder.java
index d879101..2c5a2de 100644
--- a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestMethodContextBuilder.java
+++ b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestMethodContextBuilder.java
@@ -46,6 +46,7 @@ public class RestMethodContextBuilder extends BeanContextBuilder {
 
 	RestContext context;
 	java.lang.reflect.Method method;
+	boolean hasConfigAnnotations;
 
 	String httpMethod;
 	UrlPathPattern pathPattern;
@@ -87,7 +88,6 @@ public class RestMethodContextBuilder extends BeanContextBuilder {
 
 			// If this method doesn't have any config annotations (e.g. @BeanConfig), then we want to
 			// reuse the serializers/parsers on the class.
-			boolean hasConfigAnnotations = false;
 			for (AnnotationInfo<?> ai : mi.getAnnotationListMethodOnlyParentFirst(ConfigAnnotationFilter.INSTANCE)) {
 				hasConfigAnnotations = ! ai.isType(RestMethod.class);
 				if (hasConfigAnnotations)
@@ -193,6 +193,7 @@ public class RestMethodContextBuilder extends BeanContextBuilder {
 				psb.set(p1, true);
 			if (hasConfigAnnotations)
 				psb.applyAnnotations(configAnnotationList, sr);
+
 			this.propertyStore = psb.build();
 
 			if (sgb != null) {
diff --git a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestMethodConfigApply.java b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestMethodConfigApply.java
index d57741f..614ee68 100644
--- a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestMethodConfigApply.java
+++ b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestMethodConfigApply.java
@@ -12,10 +12,16 @@
 // ***************************************************************************************************************************
 package org.apache.juneau.rest.annotation;
 
+import static org.apache.juneau.BeanContext.*;
+import static org.apache.juneau.internal.ArrayUtils.*;
+import static org.apache.juneau.internal.StringUtils.*;
 import static org.apache.juneau.rest.RestContext.*;
+import static org.apache.juneau.rest.util.RestUtils.*;
 
 import org.apache.juneau.*;
+import org.apache.juneau.internal.*;
 import org.apache.juneau.reflect.*;
+import org.apache.juneau.rest.util.*;
 import org.apache.juneau.svl.*;
 
 /**
@@ -33,9 +39,63 @@ public class RestMethodConfigApply extends ConfigApply<RestMethod> {
 		super(c, r);
 	}
 
+	@SuppressWarnings("deprecation")
 	@Override
 	public void apply(AnnotationInfo<RestMethod> ai, PropertyStoreBuilder psb) {
 		RestMethod a = ai.getAnnotation();
+		String s = null;
+
+		for (Property p1 : a.properties()) {
+			psb.set(p1.name(), string(p1.value()));  // >>> DEPRECATED - Remove in 9.0 <<<
+			psb.addTo(REST_properties, string(p1.name()), string(p1.value()));
+		}
+
+		for (String p1 : a.flags()) {
+			psb.set(p1, true);  // >>> DEPRECATED - Remove in 9.0 <<<
+			psb.addTo(REST_properties, string(p1), true);
+		}
+
+		if (a.serializers().length > 0)
+			psb.set(REST_serializers, merge(ObjectUtils.toType(psb.peek(REST_serializers), Object[].class), a.serializers()));
+
+		if (a.parsers().length > 0)
+			psb.set(REST_parsers, merge(ObjectUtils.toType(psb.peek(REST_parsers), Object[].class), a.parsers()));
+
+		psb.addTo(REST_encoders, a.encoders());
+
+		if (a.produces().length > 0)
+			psb.set(REST_produces, strings(a.produces()));
+
+		if (a.consumes().length > 0)
+			psb.set(REST_consumes, strings(a.consumes()));
+
+		for (String header : strings(a.defaultRequestHeaders())) {
+			String[] h = RestUtils.parseHeader(header);
+			if (h == null)
+				throw new FormattedRuntimeException("Invalid default request header specified: ''{0}''.  Must be in the format: ''Header-Name: header-value''", header);
+			if (isNotEmpty(h[1]))
+				psb.addTo(REST_defaultRequestHeaders, h[0], h[1]);
+		}
+
+		if (a.defaultAccept().length() > 0) {
+			s = string(a.defaultAccept());
+			if (isNotEmpty(s))
+				psb.addTo(REST_defaultRequestHeaders, "Accept", s);
+		}
+
+		if (a.defaultContentType().length() > 0) {
+			s = string(a.defaultContentType());
+			if (isNotEmpty(s))
+				psb.addTo(REST_defaultRequestHeaders, "Content-Type", s);
+		}
+
+		psb.addTo(REST_converters, a.converters());
+
+		psb.addTo(REST_guards, reverse(a.guards()));
+
+		psb.set(BEAN_beanFilters, merge(ObjectUtils.toType(psb.peek(BEAN_beanFilters), Object[].class), a.beanFilters()));
+
+		psb.set(BEAN_pojoSwaps, merge(ObjectUtils.toType(psb.peek(BEAN_pojoSwaps), Object[].class), a.pojoSwaps()));
 
 		if (! a.defaultCharset().isEmpty())
 			psb.set(REST_defaultCharset, string(a.defaultCharset()));
@@ -43,6 +103,9 @@ public class RestMethodConfigApply extends ConfigApply<RestMethod> {
 		if (! a.maxInput().isEmpty())
 			psb.set(REST_maxInput, string(a.maxInput()));
 
+		if (! a.maxInput().isEmpty())
+			psb.set(REST_maxInput, string(a.maxInput()));
+
 		if (! a.rolesDeclared().isEmpty())
 			psb.set(REST_rolesDeclared, string(a.rolesDeclared()));
 
diff --git a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestResourceConfigApply.java b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestResourceConfigApply.java
index c470a59..d6ae447 100644
--- a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestResourceConfigApply.java
+++ b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestResourceConfigApply.java
@@ -52,11 +52,15 @@ public class RestResourceConfigApply extends ConfigApply<RestResource> {
 		String s = null;
 		ClassInfo c = ai.getClassOn();
 
-		for (Property p : a.properties())
-			psb.set(string(p.name()), string(p.value()));
+		for (Property p1 : a.properties()) {
+			psb.set(p1.name(), string(p1.value()));  // >>> DEPRECATED - Remove in 9.0 <<<
+			psb.addTo(REST_properties, string(p1.name()), string(p1.value()));
+		}
 
-		for (String p : a.flags())
-			psb.set(p, true);
+		for (String p1 : a.flags()) {
+			psb.set(p1, true);  // >>> DEPRECATED - Remove in 9.0 <<<
+			psb.addTo(REST_properties, string(p1), true);
+		}
 
 		if (a.serializers().length > 0)
 			psb.set(REST_serializers, merge(ObjectUtils.toType(psb.peek(REST_serializers), Object[].class), a.serializers()));
diff --git a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/util/RestUtils.java b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/util/RestUtils.java
index 8a57fae..cd16850 100644
--- a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/util/RestUtils.java
+++ b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/util/RestUtils.java
@@ -180,6 +180,8 @@ public final class RestUtils {
 	public static String[] parseHeader(String s) {
 		int i = s.indexOf(':');
 		if (i == -1)
+			i = s.indexOf('=');
+		if (i == -1)
 			return null;
 		String name = s.substring(0, i).trim().toLowerCase(Locale.ENGLISH);
 		String val = s.substring(i+1).trim();
@@ -395,6 +397,9 @@ public final class RestUtils {
 	 */
 	public static Object[] merge(Object[] fromParent, Object[] fromChild) {
 
+		if (fromParent == null)
+			fromParent = new Object[0];
+
 		if (ArrayUtils.contains(None.class, fromChild))
 			return new Object[0];