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 ba7b314 Context API refactoring.
ba7b314 is described below
commit ba7b314a56d313cd2a2fae3e1a4fa4433f855921
Author: JamesBognar <[email protected]>
AuthorDate: Sat Aug 28 18:52:52 2021 -0400
Context API refactoring.
---
.../java/org/apache/juneau/rest/RestContext.java | 83 ++--------------------
.../org/apache/juneau/rest/RestContextBuilder.java | 65 ++++++++++++-----
.../org/apache/juneau/rest/annotation/Rest.java | 2 +-
.../juneau/rest/annotation/RestAnnotation.java | 16 +----
4 files changed, 56 insertions(+), 110 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 3375875..9de62f4 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
@@ -23,6 +23,7 @@ import static org.apache.juneau.rest.ResponseProcessor.*;
import static org.apache.juneau.rest.logging.RestLoggingDetail.*;
import static java.util.Collections.*;
import static java.util.logging.Level.*;
+import static java.util.Optional.*;
import java.io.*;
import java.lang.reflect.*;
@@ -1648,80 +1649,6 @@ public class RestContext extends BeanContext {
public static final String REST_partSerializer = PREFIX +
".partSerializer.o";
/**
- * Configuration property: Resource path.
- *
- * <h5 class='section'>Property:</h5>
- * <ul class='spaced-list'>
- * <li><b>ID:</b> {@link
org.apache.juneau.rest.RestContext#REST_path REST_path}
- * <li><b>Name:</b> <js>"RestContext.path.s"</js>
- * <li><b>Data type:</b> <c>String</c>
- * <li><b>System property:</b> <c>RestContext.path.</c>
- * <li><b>Environment variable:</b> <c>RESTCONTEXT_PATH</c>
- * <li><b>Default:</b> <jk>null</jk>
- * <li><b>Session property:</b> <jk>false</jk>
- * <li><b>Annotations:</b>
- * <ul>
- * <li class='ja'>{@link
org.apache.juneau.rest.annotation.Rest#path()}
- * </ul>
- * <li><b>Methods:</b>
- * <ul>
- * <li class='jm'>{@link
org.apache.juneau.rest.RestContextBuilder#path(String)}
- * </ul>
- * </ul>
- *
- * <h5 class='section'>Description:</h5>
- * <p>
- * Identifies the URL subpath relative to the ascendant resource.
- *
- * <p>
- * This setting is critical for the routing of HTTP requests from
ascendant to child resources.
- *
- * <h5 class='section'>Example:</h5>
- * <p class='bcode w800'>
- * <jc>// Option #1 - Defined via annotation.</jc>
- * <ja>@Rest</ja>(path=<js>"/myResource"</js>)
- * <jk>public class</jk> MyResource {
- *
- * <jc>// Option #2 - Defined via builder passed in
through resource constructor.</jc>
- * <jk>public</jk> MyResource(RestContextBuilder
<jv>builder</jv>) <jk>throws</jk> Exception {
- *
- * <jc>// Using method on builder.</jc>
- * <jv>builder</jv>.path(<js>"/myResource"</js>);
- *
- * <jc>// Same, but using property.</jc>
- * <jv>builder</jv>.set(<jsf>REST_path</jsf>,
<js>"/myResource"</js>);
- * }
- *
- * <jc>// Option #3 - Defined via builder passed in
through init method.</jc>
- * <ja>@RestHook</ja>(<jsf>INIT</jsf>)
- * <jk>public void</jk> init(RestContextBuilder
<jv>builder</jv>) <jk>throws</jk> Exception {
- * <jv>builder</jv>.path(<js>"/myResource"</js>);
- * }
- * }
- * </p>
- *
- * <p>
- * <ul class='notes'>
- * <li>
- * This annotation is ignored on top-level servlets (i.e.
servlets defined in <c>web.xml</c> 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 Rest#children() @Rest(children)} annotation.
- * <br>However, it may be used in other ways (e.g.
defining paths for top-level resources in microservices).
- * <li>
- * Slashes are trimmed from the path ends.
- * <br>As a convention, you may want to start your path
with <js>'/'</js> simple because it make it easier to read.
- * <li>
- * This path is available through the following method:
- * <ul>
- * <li class='jm'>{@link RestContext#getPath()
RestContext.getPath()}
- * </ul>
- * </ul>
- */
- public static final String REST_path = PREFIX + ".path.s";
-
- /**
* Configuration property: Supported accept media types.
*
* <h5 class='section'>Property:</h5>
@@ -2860,8 +2787,8 @@ public class RestContext extends BeanContext {
debugEnablement = createDebugEnablement(r, cp, bf);
- fullPath = (builder.parentContext == null ? "" :
(builder.parentContext.fullPath + '/')) + builder.getPath();
- path = builder.getPath();
+ path = ofNullable(builder.path).orElse("");
+ fullPath = (builder.parentContext == null ? "" :
(builder.parentContext.fullPath + '/')) + path;
String p = path;
if (! p.endsWith("/*"))
@@ -5133,7 +5060,7 @@ public class RestContext extends BeanContext {
* If path is not specified, returns <js>""</js>.
*
* <ul class='seealso'>
- * <li class='jf'>{@link #REST_path}
+ * <li class='jf'>{@link RestContextBuilder#path(String)}
* </ul>
*
* @return The servlet path.
@@ -5150,7 +5077,7 @@ public class RestContext extends BeanContext {
* If path is not specified, returns <js>""</js>.
*
* <ul class='seealso'>
- * <li class='jf'>{@link #REST_path}
+ * <li class='jm'>{@link RestContextBuilder#path(String)}
* </ul>
*
* @return The full path.
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 d34c20c..cee0ab8 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
@@ -112,7 +112,7 @@ public class RestContextBuilder extends BeanContextBuilder
implements ServletCon
Config config;
VarResolverBuilder varResolverBuilder;
- String allowedHeaderParams, allowedMethodHeaders, allowedMethodParams,
clientVersionHeader, uriAuthority, uriContext;
+ String allowedHeaderParams, allowedMethodHeaders, allowedMethodParams,
clientVersionHeader, uriAuthority, uriContext, path;
UriRelativity uriRelativity;
UriResolution uriResolution;
Charset defaultCharset;
@@ -455,16 +455,6 @@ public class RestContextBuilder extends BeanContextBuilder
implements ServletCon
return varResolverBuilder;
}
- /**
- * Returns the REST path defined on this builder.
- *
- * @return The REST path defined on this builder.
- */
- public String getPath() {
- Object p = peek(REST_path);
- return p == null ? "" : p.toString();
- }
-
//----------------------------------------------------------------------------------------------------
// Properties
//----------------------------------------------------------------------------------------------------
@@ -1741,13 +1731,56 @@ public class RestContextBuilder extends
BeanContextBuilder implements ServletCon
}
/**
- * <i><l>RestContext</l> configuration property: </i> Resource
path.
+ * Resource path.
*
* <p>
* Identifies the URL subpath relative to the parent resource.
*
+ * <p>
+ * This setting is critical for the routing of HTTP requests from
ascendant to child resources.
+ *
+ * <h5 class='section'>Example:</h5>
+ * <p class='bcode w800'>
+ * <jc>// Option #1 - Defined via annotation.</jc>
+ * <ja>@Rest</ja>(path=<js>"/myResource"</js>)
+ * <jk>public class</jk> MyResource {
+ *
+ * <jc>// Option #2 - Defined via builder passed in
through resource constructor.</jc>
+ * <jk>public</jk> MyResource(RestContextBuilder
<jv>builder</jv>) <jk>throws</jk> Exception {
+ *
+ * <jc>// Using method on builder.</jc>
+ * <jv>builder</jv>.path(<js>"/myResource"</js>);
+ * }
+ *
+ * <jc>// Option #3 - Defined via builder passed in
through init method.</jc>
+ * <ja>@RestHook</ja>(<jsf>INIT</jsf>)
+ * <jk>public void</jk> init(RestContextBuilder
<jv>builder</jv>) <jk>throws</jk> Exception {
+ * <jv>builder</jv>.path(<js>"/myResource"</js>);
+ * }
+ * }
+ * </p>
+ *
+ * <p>
+ * <ul class='notes'>
+ * <li>
+ * This annotation is ignored on top-level servlets (i.e.
servlets defined in <c>web.xml</c> 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 Rest#children() @Rest(children)} annotation.
+ * <br>However, it may be used in other ways (e.g.
defining paths for top-level resources in microservices).
+ * <li>
+ * Slashes are trimmed from the path ends.
+ * <br>As a convention, you may want to start your path
with <js>'/'</js> simple because it make it easier to read.
+ * <li>
+ * This path is available through the following method:
+ * <ul>
+ * <li class='jm'>{@link RestContext#getPath()
RestContext.getPath()}
+ * </ul>
+ * </ul>
+ *
* <ul class='seealso'>
- * <li class='jf'>{@link RestContext#REST_path}
+ * <li class='ja'>{@link Rest#path}
* </ul>
*
* @param value The new value for this setting.
@@ -1755,9 +1788,9 @@ public class RestContextBuilder extends
BeanContextBuilder implements ServletCon
*/
@FluentSetter
public RestContextBuilder path(String value) {
- if (startsWith(value, '/'))
- value = value.substring(1);
- set(REST_path, value);
+ value = trimLeadingSlashes(value);
+ if (! isEmpty(value))
+ path = value;
return this;
}
diff --git
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/Rest.java
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/Rest.java
index 024228e..9e3a544 100644
---
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/Rest.java
+++
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/Rest.java
@@ -878,7 +878,7 @@ public @interface Rest {
* </ul>
*
* <ul class='seealso'>
- * <li class='jf'>{@link RestContext#REST_path}
+ * <li class='jm'>{@link RestContextBuilder#path(String)}
* </ul>
*/
String path() default "";
diff --git
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestAnnotation.java
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestAnnotation.java
index 4a39841..ffebf75 100644
---
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestAnnotation.java
+++
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestAnnotation.java
@@ -14,7 +14,6 @@ package org.apache.juneau.rest.annotation;
import static org.apache.juneau.http.HttpHeaders.*;
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.*;
@@ -1061,7 +1060,7 @@ public class RestAnnotation {
b.prependTo(REST_messages, Tuple2.of(c.inner(),
string(a.messages())));
b.setIf(a.fileFinder() != FileFinder.Null.class,
REST_fileFinder, a.fileFinder());
b.setIf(a.staticFiles() != StaticFiles.Null.class,
REST_staticFiles, a.staticFiles());
- b.setIfNotEmpty(REST_path,
trimLeadingSlash(string(a.path())));
+ value(a.path()).ifPresent(x -> b.path(x));
value(a.clientVersionHeader()).ifPresent(x ->
b.clientVersionHeader(x));
b.setIf(a.beanStore() != BeanStore.Null.class,
REST_beanStore, a.beanStore());
b.setIf(a.callLogger() != RestLogger.Null.class,
REST_callLogger, a.callLogger());
@@ -1078,12 +1077,6 @@ public class RestAnnotation {
b.setIfNotEmpty(REST_debug, string(a.debug()));
b.setIfNotEmpty(REST_debugOn, string(a.debugOn()));
}
-
- private String trimLeadingSlash(String value) {
- if (startsWith(value, '/'))
- return value.substring(1);
- return value;
- }
}
/**
@@ -1113,17 +1106,10 @@ public class RestAnnotation {
b.setIfNotEmpty(REST_consumes,
stringList(a.consumes()));
b.prependTo(REST_converters, a.converters());
b.prependTo(REST_guards, reverse(a.guards()));
- b.setIfNotEmpty(REST_path,
trimLeadingSlash(string(a.path())));
value(a.defaultCharset()).map(Charset::forName).ifPresent(x ->
b.defaultCharset(x));
value(a.maxInput()).ifPresent(x -> b.maxInput(x));
cdStream(a.rolesDeclared()).forEach(x ->
b.addTo(REST_rolesDeclared, x));
b.addToIfNotEmpty(REST_roleGuard,
string(a.roleGuard()));
}
-
- private String trimLeadingSlash(String value) {
- if (startsWith(value, '/'))
- return value.substring(1);
- return value;
- }
}
}
\ No newline at end of file