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 3359727 Context API refactoring.
3359727 is described below
commit 3359727077ed88d9dd4cde25fa0ac46c7087550f
Author: JamesBognar <[email protected]>
AuthorDate: Sun Sep 19 18:06:23 2021 -0400
Context API refactoring.
---
.../java/org/apache/juneau/rest/RestOpContext.java | 49 +-----
.../apache/juneau/rest/RestOpContextBuilder.java | 193 +++++++++++++--------
.../rest/annotation/RestDeleteAnnotation.java | 2 +-
.../juneau/rest/annotation/RestGetAnnotation.java | 2 +-
.../juneau/rest/annotation/RestOpAnnotation.java | 2 +-
.../juneau/rest/annotation/RestPostAnnotation.java | 2 +-
.../juneau/rest/annotation/RestPutAnnotation.java | 2 +-
7 files changed, 129 insertions(+), 123 deletions(-)
diff --git
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpContext.java
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpContext.java
index b2ee601..41c386a 100644
---
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpContext.java
+++
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpContext.java
@@ -154,7 +154,7 @@ public class RestOpContext extends Context implements
Comparable<RestOpContext>
converters = bs.add(RestConverter[].class,
builder.converters().build().asArray());
guards = bs.add(RestGuard[].class,
builder.getGuards().asArray());
- RestMatcherList matchers = createMatchers(r, builder,
bs);
+ RestMatcherList matchers = builder.getMatchers(context);
optionalMatchers = matchers.getOptionalEntries();
requiredMatchers = matchers.getRequiredEntries();
@@ -215,53 +215,6 @@ public class RestOpContext extends Context implements
Comparable<RestOpContext>
}
/**
- * Instantiates the method matchers for this REST resource method.
- *
- * <p>
- * Instantiates based on the following logic:
- * <ul>
- * <li>Looks for matchers set via any of the following:
- * <ul>
- * <li>{@link RestOp#matchers()}.
- * </ul>
- * <li>Looks for a static or non-static <c>createMatchers()</>
method that returns <c>{@link RestMatcher}[]</c> on the
- * resource class with any of the following arguments:
- * <ul>
- * <li>{@link java.lang.reflect.Method} - The Java
method this context belongs to.
- * <li>{@link RestContext}
- * <li>{@link BeanStore}
- * <li>Any {@doc RestInjection injected beans}.
- * </ul>
- * <li>Resolves it via the bean store registered in this context.
- * <li>Instantiates a <c>RestMatcher[0]</c>.
- * </ul>
- *
- * @param resource The REST resource object.
- * @param builder The builder for this object.
- * @param beanStore The bean store to use for retrieving and creating
beans.
- * @return The method matchers for this REST resource method.
- * @throws Exception If method matchers could not be instantiated.
- */
- protected RestMatcherList createMatchers(Object resource,
RestOpContextBuilder builder, BeanStore beanStore) throws Exception {
-
- RestMatcherList.Builder x =
builder.restMatchers.beanStore(beanStore);
-
- String clientVersion = builder.clientVersion;
- if (clientVersion != null)
- x.append(new
ClientVersionMatcher(context.getClientVersionHeader(), mi));
-
- x = BeanStore
- .of(beanStore, resource)
- .addBean(RestMatcherList.Builder.class, x)
- .createMethodFinder(RestMatcherList.Builder.class,
resource)
- .find("createMatchers", Method.class)
- .withDefault(x)
- .run();
-
- return x.build();
- }
-
- /**
* Instantiates the path matchers for this method.
*
* @param resource The REST resource object.
diff --git
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpContextBuilder.java
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpContextBuilder.java
index 38160f8..35924ef 100644
---
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpContextBuilder.java
+++
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpContextBuilder.java
@@ -13,7 +13,6 @@
package org.apache.juneau.rest;
import static java.util.Arrays.*;
-import static org.apache.juneau.assertions.Assertions.*;
import static org.apache.juneau.internal.ExceptionUtils.*;
import static org.apache.juneau.rest.HttpRuntimeException.*;
import static java.util.Optional.*;
@@ -65,6 +64,7 @@ public class RestOpContextBuilder extends ContextBuilder {
private ParserGroup.Builder parsers;
private HttpPartSerializer.Creator partSerializer;
private HttpPartParser.Creator partParser;
+ private RestMatcherList.Builder matchers;
PartList.Builder defaultFormData, defaultQueryData;
NamedAttributeList defaultRequestAttributes;
@@ -640,7 +640,7 @@ public class RestOpContextBuilder extends ContextBuilder {
.find("createGuards")
.run(x -> v.set(x));
- // Replace with bean from: public [static] RestGuardList
createConverters(<args>)
+ // Replace with bean from: public [static] RestGuardList
createGuards(<args>)
beanStore
.beanCreateMethodFinder(RestGuardList.class)
.addBean(RestGuardList.Builder.class, v.get())
@@ -665,6 +665,127 @@ public class RestOpContextBuilder extends ContextBuilder {
return guards.build();
}
+
//-----------------------------------------------------------------------------------------------------------------
+ // matchers
+
//-----------------------------------------------------------------------------------------------------------------
+
+ /**
+ * Returns the builder for the {@link RestMatcherList} object in the
REST context.
+ *
+ * @return The builder for the {@link RestMatcherList} object in the
REST context.
+ */
+ public final RestMatcherList.Builder matchers() {
+ if (matchers == null)
+ matchers = createMatchers(beanStore(), resource());
+ return matchers;
+ }
+
+ /**
+ * Instantiates the method matchers for this REST resource method.
+ *
+ * <p>
+ * Associates one or more {@link RestMatcher RestMatchers} with the
specified method.
+ *
+ * <p>
+ * If multiple matchers are specified, <b>ONE</b> matcher must pass.
+ * <br>Note that this is different than guards where <b>ALL</b> guards
needs to pass.
+ *
+ * <ul class='notes'>
+ * <li>
+ * When defined as a class, the implementation must have
one of the following constructors:
+ * <ul>
+ * <li><code><jk>public</jk> T(RestContext)</code>
+ * <li><code><jk>public</jk> T()</code>
+ * <li><code><jk>public static</jk> T
<jsm>create</jsm>(RestContext)</code>
+ * <li><code><jk>public static</jk> T
<jsm>create</jsm>()</code>
+ * </ul>
+ * <li>
+ * Inner classes of the REST resource class are allowed.
+ * </ul>
+ *
+ * <ul class='seealso'>
+ * <li class='ja'>{@link RestOp#matchers()}
+ * <li class='ja'>{@link RestGet#matchers()}
+ * <li class='ja'>{@link RestPut#matchers()}
+ * <li class='ja'>{@link RestPost#matchers()}
+ * <li class='ja'>{@link RestDelete#matchers()}
+ * </ul>
+ *
+ * <p>
+ * Instantiates based on the following logic:
+ * <ul>
+ * <li>Looks for matchers set via any of the following:
+ * <ul>
+ * <li>{@link RestOp#matchers()}.
+ * </ul>
+ * <li>Looks for a static or non-static <c>createMatchers()</>
method that returns <c>{@link RestMatcher}[]</c> on the
+ * resource class with any of the following arguments:
+ * <ul>
+ * <li>{@link java.lang.reflect.Method} - The Java
method this context belongs to.
+ * <li>{@link RestContext}
+ * <li>{@link BeanStore}
+ * <li>Any {@doc RestInjection injected beans}.
+ * </ul>
+ * <li>Resolves it via the bean store registered in this context.
+ * <li>Instantiates a <c>RestMatcher[0]</c>.
+ * </ul>
+ *
+ * @param beanStore
+ * The factory used for creating beans and retrieving injected
beans.
+ * @param resource
+ * The REST servlet/bean instance that this context is defined
against.
+ * @return The rest converter list builder for this REST method.
+ */
+ protected RestMatcherList.Builder createMatchers(BeanStore beanStore,
Supplier<?> resource) {
+
+ // Default value.
+ Value<RestMatcherList.Builder> v = Value.of(
+ RestMatcherList
+ .create()
+ .beanStore(beanStore)
+ );
+
+ // Specify the implementation class if its set as a default.
+ defaultClasses()
+ .get(RestMatcherList.class)
+ .ifPresent(x -> v.get().type(x));
+
+ // Replace with builder from bean store.
+ beanStore
+ .getBean(RestMatcherList.Builder.class)
+ .map(x -> x.copy())
+ .ifPresent(x->v.set(x));
+
+ // Replace with bean from bean store.
+ beanStore
+ .getBean(RestMatcherList.class)
+ .ifPresent(x->v.get().impl(x));
+
+ // Replace with builder from: public [static]
RestMatcherList.Builder createMatchers(<args>)
+ beanStore
+ .beanCreateMethodFinder(RestMatcherList.Builder.class)
+ .addBean(RestMatcherList.Builder.class, v.get())
+ .find("createMatchers")
+ .run(x -> v.set(x));
+
+ // Replace with bean from: public [static] RestMatcherList
createMatchers(<args>)
+ beanStore
+ .beanCreateMethodFinder(RestMatcherList.class)
+ .addBean(RestMatcherList.Builder.class, v.get())
+ .find("createMatchers")
+ .run(x -> v.get().impl(x));
+
+ return v.get();
+ }
+
+ final RestMatcherList getMatchers(RestContext restContext) {
+ RestMatcherList.Builder b = matchers();
+ if (clientVersion != null)
+ b.append(new
ClientVersionMatcher(restContext.getClientVersionHeader(),
MethodInfo.of(restMethod)));
+
+ return b.build();
+ }
+
/**
* When enabled, append <js>"/*"</js> to path patterns if not already
present.
*
@@ -1037,74 +1158,6 @@ public class RestOpContextBuilder extends ContextBuilder
{
}
/**
- * Method-level matchers.
- *
- * <p>
- * Associates one or more {@link RestMatcher RestMatchers} with the
specified method.
- *
- * <p>
- * If multiple matchers are specified, <b>ONE</b> matcher must pass.
- * <br>Note that this is different than guards where <b>ALL</b> guards
needs to pass.
- *
- * <ul class='seealso'>
- * <li class='ja'>{@link RestOp#matchers()}
- * <li class='ja'>{@link RestGet#matchers()}
- * <li class='ja'>{@link RestPut#matchers()}
- * <li class='ja'>{@link RestPost#matchers()}
- * <li class='ja'>{@link RestDelete#matchers()}
- * </ul>
- *
- * @param values The new values for this setting.
- * @return This object (for method chaining).
- */
- @FluentSetter
- public RestOpContextBuilder matchers(RestMatcher...values) {
- restMatchers.append(values);
- return this;
- }
-
- /**
- * Method-level matchers.
- *
- * <p>
- * Associates one or more {@link RestMatcher RestMatchers} with the
specified method.
- *
- * <p>
- * If multiple matchers are specified, <b>ONE</b> matcher must pass.
- * <br>Note that this is different than guards where <b>ALL</b> guards
needs to pass.
- *
- * <ul class='notes'>
- * <li>
- * When defined as a class, the implementation must have
one of the following constructors:
- * <ul>
- * <li><code><jk>public</jk> T(RestContext)</code>
- * <li><code><jk>public</jk> T()</code>
- * <li><code><jk>public static</jk> T
<jsm>create</jsm>(RestContext)</code>
- * <li><code><jk>public static</jk> T
<jsm>create</jsm>()</code>
- * </ul>
- * <li>
- * Inner classes of the REST resource class are allowed.
- * </ul>
- *
- * <ul class='seealso'>
- * <li class='ja'>{@link RestOp#matchers()}
- * <li class='ja'>{@link RestGet#matchers()}
- * <li class='ja'>{@link RestPut#matchers()}
- * <li class='ja'>{@link RestPost#matchers()}
- * <li class='ja'>{@link RestDelete#matchers()}
- * </ul>
- *
- * @param values The new values for this setting.
- * @return This object (for method chaining).
- * @throws IllegalArgumentException if any class does not extend from
{@link RestMatcher}.
- */
- @FluentSetter
- public RestOpContextBuilder matchers(Class<?>...values) {
- restMatchers.append(assertClassArrayArgIsType("values",
RestMatcher.class, values));
- return this;
- }
-
- /**
* The maximum allowed input size (in bytes) on HTTP requests.
*
* <p>
diff --git
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestDeleteAnnotation.java
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestDeleteAnnotation.java
index b0dc4dd..1e09a3a 100644
---
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestDeleteAnnotation.java
+++
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestDeleteAnnotation.java
@@ -477,7 +477,7 @@ public class RestDeleteAnnotation {
strings(a.defaultQueryData()).map(x ->
basicPart(x)).forEach(x -> b.defaultQueryData(x));
string(a.defaultAccept()).map(x ->
accept(x)).ifPresent(x -> b.defaultRequestHeaders(x));
b.guards().append(a.guards());
- b.matchers(a.matchers());
+ b.matchers().append(a.matchers());
string(a.clientVersion()).ifPresent(x ->
b.clientVersion(x));
string(a.defaultCharset()).map(Charset::forName).ifPresent(x ->
b.defaultCharset(x));
strings(a.path()).forEach(x -> b.path(x));
diff --git
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestGetAnnotation.java
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestGetAnnotation.java
index 4b2365b..a617021 100644
---
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestGetAnnotation.java
+++
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestGetAnnotation.java
@@ -537,7 +537,7 @@ public class RestGetAnnotation {
string(a.defaultAccept()).map(x ->
accept(x)).ifPresent(x -> b.defaultRequestHeaders(x));
b.converters().append(a.converters());
b.guards().append(a.guards());
- b.matchers(a.matchers());
+ b.matchers().append(a.matchers());
string(a.clientVersion()).ifPresent(x ->
b.clientVersion(x));
string(a.defaultCharset()).map(Charset::forName).ifPresent(x ->
b.defaultCharset(x));
strings(a.path()).forEach(x -> b.path(x));
diff --git
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestOpAnnotation.java
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestOpAnnotation.java
index 38db134..59c1001 100644
---
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestOpAnnotation.java
+++
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestOpAnnotation.java
@@ -644,7 +644,7 @@ public class RestOpAnnotation {
string(a.defaultContentType()).map(x ->
contentType(x)).ifPresent(x -> b.defaultRequestHeaders(x));
b.converters().append(a.converters());
b.guards().append(a.guards());
- b.matchers(a.matchers());
+ b.matchers().append(a.matchers());
string(a.clientVersion()).ifPresent(x ->
b.clientVersion(x));
string(a.defaultCharset()).map(Charset::forName).ifPresent(x ->
b.defaultCharset(x));
string(a.maxInput()).ifPresent(x -> b.maxInput(x));
diff --git
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestPostAnnotation.java
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestPostAnnotation.java
index 01ea83c..344dff5 100644
---
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestPostAnnotation.java
+++
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestPostAnnotation.java
@@ -628,7 +628,7 @@ public class RestPostAnnotation {
string(a.defaultContentType()).map(x ->
contentType(x)).ifPresent(x -> b.defaultRequestHeaders(x));
b.converters().append(a.converters());
b.guards().append(a.guards());
- b.matchers(a.matchers());
+ b.matchers().append(a.matchers());
string(a.clientVersion()).ifPresent(x ->
b.clientVersion(x));
string(a.defaultCharset()).map(Charset::forName).ifPresent(x ->
b.defaultCharset(x));
string(a.maxInput()).ifPresent(x -> b.maxInput(x));
diff --git
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestPutAnnotation.java
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestPutAnnotation.java
index 725cc4b..cc422de 100644
---
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestPutAnnotation.java
+++
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/annotation/RestPutAnnotation.java
@@ -628,7 +628,7 @@ public class RestPutAnnotation {
string(a.defaultContentType()).map(x ->
contentType(x)).ifPresent(x -> b.defaultRequestHeaders(x));
b.converters().append(a.converters());
b.guards().append(a.guards());
- b.matchers(a.matchers());
+ b.matchers().append(a.matchers());
string(a.clientVersion()).ifPresent(x ->
b.clientVersion(x));
string(a.defaultCharset()).map(Charset::forName).ifPresent(x ->
b.defaultCharset(x));
string(a.maxInput()).ifPresent(x -> b.maxInput(x));