juneau-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jamesbog...@apache.org
Subject juneau git commit: RestContext refactor.
Date Mon, 01 Jan 2018 18:58:18 GMT
Repository: juneau
Updated Branches:
  refs/heads/master c36b16d70 -> f5eccb628


RestContext refactor.

Project: http://git-wip-us.apache.org/repos/asf/juneau/repo
Commit: http://git-wip-us.apache.org/repos/asf/juneau/commit/f5eccb62
Tree: http://git-wip-us.apache.org/repos/asf/juneau/tree/f5eccb62
Diff: http://git-wip-us.apache.org/repos/asf/juneau/diff/f5eccb62

Branch: refs/heads/master
Commit: f5eccb628e1624d881b61bf346c54e4fa1379b72
Parents: c36b16d
Author: JamesBognar <jamesbognar@apache.org>
Authored: Mon Jan 1 13:58:15 2018 -0500
Committer: JamesBognar <jamesbognar@apache.org>
Committed: Mon Jan 1 13:58:15 2018 -0500

----------------------------------------------------------------------
 .../org/apache/juneau/rest/RestContext.java     |  61 ++++++++++-
 .../apache/juneau/rest/RestContextBuilder.java  | 103 ++++++++++++-------
 .../juneau/rest/annotation/RestResource.java    |  33 ++++--
 3 files changed, 146 insertions(+), 51 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/juneau/blob/f5eccb62/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContext.java
----------------------------------------------------------------------
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 dab4722..64c041e 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
@@ -611,6 +611,62 @@ public final class RestContext extends BeanContext {
 	 */
 	public static final String REST_infoProvider = PREFIX + "infoProvider.o";
 	
+	/**
+	 * <b>Configuration property:</b>  Resource path.   
+	 *
+	 * <ul>
+	 * 	<li><b>Name:</b> <js>"RestContext.path.s"</js>
+	 * 	<li><b>Data type:</b> <code>String</code>
+	 * 	<li><b>Default:</b> <jk>null</jk>
+	 * 	<li><b>Session-overridable:</b> <jk>false</jk>
+	 * </ul>
+	 * 
+	 * <p>
+	 * Identifies the URL subpath relative to the parent resource.
+	 *
+	 * <p>
+	 * <h5 class='section'>Notes:</h5>
+	 * <ul class='spaced-list'>
+	 * 	<li>Property: {@link RestContext#REST_path}
+	 * 	<li>Annotation:  {@link RestResource#path()} 
+	 * 	<li>Method: {@link RestContextBuilder#path(String)} 
+	 * 	<li>This annotation is ignored on top-level servlets (i.e. servlets defined in
<code>web.xml</code> files).
+	 * 		<br>Therefore, implementers can optionally specify a path value for documentation
purposes.
+	 * 	<li>Typically, this setting is only applicable to resources defined as children
through the 
+	 * 		{@link RestResource#children()} annotation.
+	 * 		<br>However, it may be used in other ways (e.g. defining paths for top-level
resources in microservices).
+	 *	</ul>
+	 */
+	public static final String REST_path = PREFIX + "path.s";
+
+	/**
+	 * <b>Configuration property:</b>  Resource context path. 
+	 *
+	 * <ul>
+	 * 	<li><b>Name:</b> <js>"RestContext.contextPath.s"</js>
+	 * 	<li><b>Data type:</b> <code>String</code>
+	 * 	<li><b>Default:</b> <jk>null</jk>
+	 * 	<li><b>Session-overridable:</b> <jk>false</jk>
+	 * </ul>
+	 * 
+	 * <p>
+	 * Overrides the context path value for this resource and any child resources.
+	 *
+	 * <p>
+	 * This setting is useful if you want to use <js>"context:/child/path"</js>
URLs in child resource POJOs but
+	 * the context path is not actually specified on the servlet container.
+	 * The net effect is that the {@link RestRequest#getContextPath()} and {@link RestRequest#getServletPath()}
methods
+	 * will return this value instead of the actual context path of the web app.
+	 * 
+	 * <p>
+	 * <h5 class='section'>Notes:</h5>
+	 * <ul class='spaced-list'>
+	 * 	<li>Property: {@link RestContext#REST_contextPath}
+	 * 	<li>Annotation:  {@link RestResource#contextPath()} 
+	 * 	<li>Method: {@link RestContextBuilder#contextPath(String)} 
+	 *	</ul>
+	 */
+	public static final String REST_contextPath = PREFIX + "contextPath.s";
 	
 	//-------------------------------------------------------------------------------------------------------------------
 	// Instance
@@ -709,8 +765,10 @@ public final class RestContext extends BeanContext {
 		this.resource = builder.resource;
 		this.builder = builder;
 		this.parentContext = builder.parentContext;
+		
 		PropertyStore ps = getPropertyStore();
 
+		contextPath = nullIfEmpty(getProperty(REST_contextPath, String.class, null));
 		allowHeaderParams = getProperty(REST_allowHeaderParams, boolean.class, true);
 		allowBodyParam = getProperty(REST_allowBodyParam, boolean.class, true);
 		allowedMethodParams = Collections.unmodifiableSet(new LinkedHashSet<>(Arrays.asList(StringUtils.split(getProperty(REST_allowedMethodParams,
String.class, "HEAD,OPTIONS")))));
@@ -756,7 +814,6 @@ public final class RestContext extends BeanContext {
 			this.msgs = b.messageBundle;
 			this.childResources = Collections.synchronizedMap(new LinkedHashMap<String,RestContext>());
 // Not unmodifiable on purpose so that children can be replaced.
 			this.fullPath = b.fullPath;
-			this.contextPath = nullIfEmpty(b.contextPath);
 			this.widgets = Collections.unmodifiableMap(b.widgets);
 
 			supportedContentTypes = getListProperty(REST_supportedContentTypes, MediaType.class, serializers.getSupportedMediaTypes());
@@ -1031,7 +1088,6 @@ public final class RestContext extends BeanContext {
 		MessageBundle messageBundle;
 		String fullPath;
 		Map<String,Widget> widgets;
-		String contextPath;
 
 		@SuppressWarnings("unchecked")
 		Builder(RestContextBuilder rcb, PropertyStore ps) throws Exception {
@@ -1069,7 +1125,6 @@ public final class RestContext extends BeanContext {
 			partParser = resolve(resource, HttpPartParser.class, rcb.partParser, parsers.getPropertyStore());
 			encoders = rcb.encoders.build();
 			beanContext = BeanContext.create().apply(ps).add(properties).build();
-			contextPath = rcb.contextPath;
 
 			mimetypesFileTypeMap = rcb.mimeTypes;
 

http://git-wip-us.apache.org/repos/asf/juneau/blob/f5eccb62/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContextBuilder.java
----------------------------------------------------------------------
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 bc80909..bb2fd45 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
@@ -65,10 +65,11 @@ import org.apache.juneau.utils.*;
  * <p>
  * To interact with this object, simply implement the following init method in your resource
class:
  * <p class='bcode'>
- * 	<jk>public synchronized void</jk> init(RestConfig config) <jk>throws</jk>
Exception {
- * 		config.addPojoSwaps(CalendarSwap.<jsf>RFC2822DTZ</jsf>.<jk>class</jk>);
- * 		config.setProperty(<jsf>PARSER_debug</jsf>, <jk>true</jk>);
- * 		<jk>super</jk>.init(config); <jc>// Make sure this is the last line!
(or just leave it out entirely)</jc>
+ * 	<jk>public synchronized void</jk> init(RestContextBuilder builder) <jk>throws</jk>
Exception {
+ * 		builder
+ * 			.pojoSwaps(CalendarSwap.<jsf>RFC2822DTZ</jsf>.<jk>class</jk>)
+ * 			.set(<jsf>PARSER_debug</jsf>, <jk>true</jk>);
+ * 		<jk>super</jk>.init(builder); <jc>// Make sure this is the last line!
(or just leave it out entirely)</jc>
  * 	}
  * </p>
  *
@@ -85,6 +86,7 @@ public class RestContextBuilder extends BeanContextBuilder implements ServletCon
 
 	final ServletConfig inner;
 	
+	Class<?> resourceClass;
 	Object resource;
 	ServletContext servletContext;
 
@@ -108,12 +110,9 @@ public class RestContextBuilder extends BeanContextBuilder implements
ServletCon
 	List<Object> staticFiles;
 	RestContext parentContext;
 	String path;
-	String contextPath;
 	HtmlDocBuilder htmlDocBuilder;
 	List<Class<? extends Widget>> widgets = new ArrayList<>();
 
-	Class<?> resourceClass;
-
 	/**
 	 * Constructor for top-level servlets when using dependency injection.
 	 *
@@ -409,22 +408,6 @@ public class RestContextBuilder extends BeanContextBuilder implements
ServletCon
 	}
 
 	/**
-	 * Specifies the override context path for this resource.
-	 *
-	 * <p>
-	 * This is the programmatic equivalent to the
-	 * {@link RestResource#contextPath() @RestResource.contextPath()} annotation.
-	 *
-	 * @param contextPath The context path for this resource and any child resources.
-	 * @return This object (for method chaining).
-	 */
-	public RestContextBuilder contextPath(String contextPath) {
-		if (! contextPath.isEmpty())
-			this.contextPath = contextPath;
-		return this;
-	}
-
-	/**
 	 * Adds class-level serializers to this resource.
 	 *
 	 * <p>
@@ -702,22 +685,6 @@ public class RestContextBuilder extends BeanContextBuilder implements
ServletCon
 	}
 
 	/**
-	 * Sets the URL path of the resource <js>"/foobar"</js>.
-	 *
-	 * <p>
-	 * This is the programmatic equivalent to the {@link RestResource#path() @RestResource.path()}
annotation.
-	 *
-	 * @param path The URL path of this resource.
-	 * @return This object (for method chaining).
-	 */
-	public RestContextBuilder path(String path) {
-		if (startsWith(path, '/'))
-			path = path.substring(1);
-		this.path = path;
-		return this;
-	}
-
-	/**
 	 * Defines widgets that can be used in conjunction with string variables of the form <js>"$W{name}"</js>to
quickly
 	 * generate arbitrary replacement text.
 	 *
@@ -828,6 +795,64 @@ public class RestContextBuilder extends BeanContextBuilder implements
ServletCon
 	//----------------------------------------------------------------------------------------------------
 
 	/**
+	 * <b>Configuration property:</b>  Resource path.   
+	 *
+	 * <p>
+	 * Identifies the URL subpath relative to the parent resource.
+	 *
+	 * <p>
+	 * <h5 class='section'>Notes:</h5>
+	 * <ul class='spaced-list'>
+	 * 	<li>Property: {@link RestContext#REST_path}
+	 * 	<li>Annotation:  {@link RestResource#path()} 
+	 * 	<li>Method: {@link RestContextBuilder#path(String)} 
+	 * 	<li>This annotation is ignored on top-level servlets (i.e. servlets defined in
<code>web.xml</code> files).
+	 * 		<br>Therefore, implementers can optionally specify a path value for documentation
purposes.
+	 * 	<li>Typically, this setting is only applicable to resources defined as children
through the 
+	 * 		{@link RestResource#children()} annotation.
+	 * 		<br>However, it may be used in other ways (e.g. defining paths for top-level
resources in microservices).
+	 *	</ul>
+	 *
+	 * @param path The URL path of this resource.
+	 * @return This object (for method chaining).
+	 */
+	public RestContextBuilder path(String path) {
+		if (startsWith(path, '/'))
+			path = path.substring(1);
+		this.path = path;
+		return this;
+	}
+
+	/**
+	 * <b>Configuration property:</b>  Resource context path. 
+	 *
+	 * <p>
+	 * Overrides the context path value for this resource and any child resources.
+	 *
+	 * <p>
+	 * This setting is useful if you want to use <js>"context:/child/path"</js>
URLs in child resource POJOs but
+	 * the context path is not actually specified on the servlet container.
+	 * The net effect is that the {@link RestRequest#getContextPath()} and {@link RestRequest#getServletPath()}
methods
+	 * will return this value instead of the actual context path of the web app.
+	 * 
+	 * <p>
+	 * <h5 class='section'>Notes:</h5>
+	 * <ul class='spaced-list'>
+	 * 	<li>Property: {@link RestContext#REST_contextPath}
+	 * 	<li>Annotation:  {@link RestResource#contextPath()} 
+	 * 	<li>Method: {@link RestContextBuilder#contextPath(String)} 
+	 *	</ul>
+	 *
+	 * @param contextPath The context path for this resource and any child resources.
+	 * @return This object (for method chaining).
+	 */
+	public RestContextBuilder contextPath(String contextPath) {
+		if (! contextPath.isEmpty())
+			set(REST_contextPath, contextPath);
+		return this;
+	}
+
+	/**
 	 * <b>Configuration property:</b>  Allow header URL parameters.
 	 *
 	 * <p>

http://git-wip-us.apache.org/repos/asf/juneau/blob/f5eccb62/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestResource.java
----------------------------------------------------------------------
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 a0d95c5..b6335bd 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
@@ -433,19 +433,23 @@ public @interface RestResource {
 	Class<?>[] children() default {};
 
 	/**
-	 * Identifies the URL subpath relative to the parent resource.
-	 *
-	 * <p>
-	 * Typically, this annotation is only applicable to resources defined as children through
the {@link #children()}
-	 * annotation.
-	 * However, it may be used in other ways (e.g. defining paths for top-level resources in
microservices).
+	 * Resource path.   
 	 *
 	 * <p>
-	 * This annotation is ignored on top-level servlets (i.e. servlets defined in <code>web.xml</code>
files).
-	 * Therefore, implementers can optionally specify a path value for documentation purposes.
+	 * Identifies the URL subpath relative to the parent resource.
 	 *
 	 * <p>
-	 * The programmatic equivalent to this annotation is the {@link RestContextBuilder#path(String)}
method.
+	 * <h5 class='section'>Notes:</h5>
+	 * <ul class='spaced-list'>
+	 * 	<li>Property: {@link RestContext#REST_path}
+	 * 	<li>Annotation:  {@link RestResource#path()} 
+	 * 	<li>Method: {@link RestContextBuilder#path(String)} 
+	 * 	<li>This annotation is ignored on top-level servlets (i.e. servlets defined in
<code>web.xml</code> files).
+	 * 		<br>Therefore, implementers can optionally specify a path value for documentation
purposes.
+	 * 	<li>Typically, this setting is only applicable to resources defined as children
through the 
+	 * 		{@link RestResource#children()} annotation.
+	 * 		<br>However, it may be used in other ways (e.g. defining paths for top-level
resources in microservices).
+	 *	</ul>
 	 */
 	String path() default "";
 
@@ -778,6 +782,9 @@ public @interface RestResource {
 	HtmlDoc htmldoc() default @HtmlDoc;
 
 	/**
+	 * Resource context path. 
+	 * 
+	 * <p>
 	 * Overrides the context path value for this resource and any child resources.
 	 *
 	 * <p>
@@ -785,6 +792,14 @@ public @interface RestResource {
 	 * the context path is not actually specified on the servlet container.
 	 * The net effect is that the {@link RestRequest#getContextPath()} and {@link RestRequest#getServletPath()}
methods
 	 * will return this value instead of the actual context path of the web app.
+	 * 
+	 * <p>
+	 * <h5 class='section'>Notes:</h5>
+	 * <ul class='spaced-list'>
+	 * 	<li>Property: {@link RestContext#REST_contextPath}
+	 * 	<li>Annotation:  {@link RestResource#contextPath()} 
+	 * 	<li>Method: {@link RestContextBuilder#contextPath(String)} 
+	 *	</ul>
 	 */
 	String contextPath() default "";
 


Mime
View raw message