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 4f36b33 Context API refactoring.
4f36b33 is described below
commit 4f36b33a74a3d98a3c17bba5a985ee76bf959f3c
Author: JamesBognar <[email protected]>
AuthorDate: Sun Sep 19 16:39:33 2021 -0400
Context API refactoring.
---
.../java/org/apache/juneau/rest/RestContext.java | 12 +++
.../org/apache/juneau/rest/RestContextBuilder.java | 91 ++++++++++++++++++++-
.../java/org/apache/juneau/rest/RestOpContext.java | 53 +-----------
.../apache/juneau/rest/RestOpContextBuilder.java | 93 ++++++++++++++++------
.../juneau/rest/annotation/RestAnnotation.java | 2 +-
.../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 +-
10 files changed, 179 insertions(+), 82 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 a9583ad..15af18d 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
@@ -44,6 +44,7 @@ import org.apache.juneau.collections.*;
import org.apache.juneau.config.*;
import org.apache.juneau.cp.*;
import org.apache.juneau.dto.swagger.Swagger;
+import org.apache.juneau.encoders.*;
import org.apache.juneau.html.annotation.*;
import org.apache.juneau.http.annotation.Response;
import org.apache.juneau.httppart.*;
@@ -166,6 +167,7 @@ public class RestContext extends Context {
private final Class<? extends RestOpArg>[] restOpArgs, hookMethodArgs;
private final BeanContext beanContext;
+ private final EncoderGroup encoders;
private final HttpPartSerializer partSerializer;
private final HttpPartParser partParser;
private final JsonSchemaGenerator jsonSchemaGenerator;
@@ -273,6 +275,7 @@ public class RestContext extends Context {
uriRelativity = builder.uriRelativity;
beanContext = bs.add(BeanContext.class,
builder.beanContext().build());
+ encoders = bs.add(EncoderGroup.class,
builder.encoders().build());
logger = bs.add(Logger.class, builder.logger());
thrownStore = bs.add(ThrownStore.class,
builder.thrownStore().build());
methodExecStore = bs.add(MethodExecStore.class,
builder.methodExecStore().thrownStoreOnce(thrownStore).build());
@@ -374,6 +377,15 @@ public class RestContext extends Context {
}
/**
+ * Returns the encoders associated with this context.
+ *
+ * @return The encoders associated with this context.
+ */
+ public EncoderGroup getEncoders() {
+ return encoders;
+ }
+
+ /**
* Returns the time statistics gatherer for the specified method.
*
* @param m The method to get statistics for.
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 3f7b60e..ab0d433 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
@@ -149,6 +149,7 @@ public class RestContextBuilder extends ContextBuilder
implements ServletConfig
private RestChildren.Builder restChildren;
private SwaggerProvider.Builder swaggerProvider;
private BeanContextBuilder beanContext;
+ private EncoderGroup.Builder encoders;
String
allowedHeaderParams = env("RestContext.allowedHeaderParams",
"Accept,Content-Type"),
@@ -172,7 +173,6 @@ public class RestContextBuilder extends ContextBuilder
implements ServletConfig
Class<? extends RestOperations> operationsClass = RestOperations.class;
// TODO
- EncoderGroup.Builder encoders =
EncoderGroup.create().add(IdentityEncoder.INSTANCE);
SerializerGroup.Builder serializers = SerializerGroup.create();
ParserGroup.Builder parsers = ParserGroup.create();
@@ -926,6 +926,95 @@ public class RestContextBuilder extends ContextBuilder
implements ServletConfig
}
//-----------------------------------------------------------------------------------------------------------------
+ // encoders
+
//-----------------------------------------------------------------------------------------------------------------
+
+ /**
+ * Returns the builder for the {@link EncoderGroup} object in the REST
context.
+ *
+ * @return The builder for the {@link EncoderGroup} object in the REST
context.
+ */
+ public final EncoderGroup.Builder encoders() {
+ if (encoders == null)
+ encoders = createEncoders(beanStore(), resource());
+ return encoders;
+ }
+
+ /**
+ * Instantiates the entries for this REST resource method.
+ *
+ * <p>
+ * Instantiates based on the following logic:
+ * <ul>
+ * <li>Looks for encoders set via any of the following:
+ * <ul>
+ * <li>{@link RestOpContextBuilder#encoders()}
+ * <li>{@link RestOp#encoders()}.
+ * <li>{@link Rest#encoders()}.
+ * </ul>
+ * <li>Looks for a static or non-static <c>createEncoders()</>
method that returns <c>{@link Encoder}[]</c> on the
+ * resource class with any of the following arguments:
+ * <ul>
+ * <li>{@link 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>Encoder[0]</c>.
+ * </ul>
+ *
+ * @param resource
+ * The REST servlet/bean instance that this context is defined
against.
+ * @param beanStore
+ * The factory used for creating beans and retrieving injected
beans.
+ * <br>Created by {@link RestContextBuilder#beanStore()}.
+ * @return The encoder group builder for this REST resource.
+ */
+ protected EncoderGroup.Builder createEncoders(BeanStore beanStore,
Supplier<?> resource) {
+
+ // Default value.
+ Value<EncoderGroup.Builder> v = Value.of(
+ EncoderGroup
+ .create()
+ .beanStore(beanStore)
+ .add(IdentityEncoder.INSTANCE)
+ );
+
+ // Specify the implementation class if its set as a default.
+ defaultClasses()
+ .get(EncoderGroup.class)
+ .ifPresent(x -> v.get().type(x));
+
+ // Replace with builder from bean store.
+ beanStore
+ .getBean(EncoderGroup.Builder.class)
+ .map(x -> x.copy())
+ .ifPresent(x->v.set(x));
+
+ // Replace with bean from bean store.
+ beanStore
+ .getBean(EncoderGroup.class)
+ .ifPresent(x->v.get().impl(x));
+
+ // Replace with builder from: public [static]
EncoderGroup.Builder createEncoders(<args>)
+ beanStore
+ .beanCreateMethodFinder(EncoderGroup.Builder.class)
+ .addBean(EncoderGroup.Builder.class, v.get())
+ .find("createEncoders")
+ .run(x -> v.set(x));
+
+ // Replace with bean from: public [static] EncoderGroup
createEncoders(<args>)
+ beanStore
+ .beanCreateMethodFinder(EncoderGroup.class)
+ .addBean(EncoderGroup.Builder.class, v.get())
+ .find("createEncoders")
+ .run(x -> v.get().impl(x));
+
+ return v.get();
+ }
+
+
//-----------------------------------------------------------------------------------------------------------------
// methodExecStore
//-----------------------------------------------------------------------------------------------------------------
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 22df3a0..49a4aa1 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
@@ -148,6 +148,7 @@ public class RestOpContext extends Context implements
Comparable<RestOpContext>
bs.addBean(BeanStore.class, bs);
beanContext = bs.add(BeanContext.class,
builder.getBeanContext().orElse(context.getBeanContext()));
+ encoders = bs.add(EncoderGroup.class,
builder.getEncoders().orElse(context.getEncoders()));
serializers = createSerializers(r, builder, bs);
bs.addBean(SerializerGroup.class, serializers);
@@ -173,8 +174,6 @@ public class RestOpContext extends Context implements
Comparable<RestOpContext>
bs.addBean(UrlPathMatcher[].class, pathMatchers);
bs.addBean(UrlPathMatcher.class, pathMatchers.length >
0 ? pathMatchers[0] : null);
- encoders = createEncoders(r, builder, bs);
- bs.addBean(EncoderGroup.class, encoders);
jsonSchemaGenerator = createJsonSchemaGenerator(r,
builder, bs);
bs.addBean(JsonSchemaGenerator.class,
jsonSchemaGenerator);
@@ -332,54 +331,6 @@ public class RestOpContext extends Context implements
Comparable<RestOpContext>
}
/**
- * Instantiates the entries for this REST resource method.
- *
- * <p>
- * Instantiates based on the following logic:
- * <ul>
- * <li>Looks for encoders set via any of the following:
- * <ul>
- * <li>{@link RestOpContextBuilder#getEncoders()}
- * <li>{@link RestOp#encoders()}.
- * <li>{@link Rest#encoders()}.
- * </ul>
- * <li>Looks for a static or non-static <c>createEncoders()</>
method that returns <c>{@link Encoder}[]</c> on the
- * resource class with any of the following arguments:
- * <ul>
- * <li>{@link 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>Encoder[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 encoders for this REST resource method.
- * @throws Exception If encoders could not be instantiated.
- */
- protected EncoderGroup createEncoders(Object resource,
RestOpContextBuilder builder, BeanStore beanStore) throws Exception {
-
- EncoderGroup.Builder x = builder.encoders;
- if (x == null)
- x = builder.restContext.builder.encoders;
-
- x = BeanStore
- .of(beanStore, resource)
- .addBean(EncoderGroup.Builder.class, x)
- .createMethodFinder(EncoderGroup.Builder.class,
resource)
- .find("createEncoders", Method.class)
- .thenFind("createEncoders")
- .withDefault(x)
- .run();
-
- return x.build();
- }
-
- /**
* Instantiates the serializers for this REST resource.
*
* <p>
@@ -387,7 +338,7 @@ public class RestOpContext extends Context implements
Comparable<RestOpContext>
* <ul>
* <li>Looks for serializers set via any of the following:
* <ul>
- * <li>{@link
RestOpContextBuilder#getSerializers()}
+ * <li>{@link RestOpContext#getSerializers()}
* <li>{@link Rest#serializers()}.
* </ul>
* <li>Looks for a static or non-static <c>createSerializers()</>
method that returns <c>{@link Serializer}[]</c> on the
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 bb81df2..b97b71d 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
@@ -226,7 +226,47 @@ public class RestOpContextBuilder extends ContextBuilder {
}
final Optional<BeanContext> getBeanContext() {
- return beanContext == null ? empty() :
of(beanContext().build());
+ return beanContext == null ? empty() : of(beanContext.build());
+ }
+
+
//-----------------------------------------------------------------------------------------------------------------
+ // encoders
+
//-----------------------------------------------------------------------------------------------------------------
+
+ /**
+ * Returns the builder for the {@link EncoderGroup} object in the REST
context.
+ *
+ * @return The builder for the {@link EncoderGroup} object in the REST
context.
+ */
+ public final EncoderGroup.Builder encoders() {
+ if (encoders == null)
+ encoders = createEncoders(beanStore(), parent,
resource());
+ return encoders;
+ }
+
+ /**
+ * Constructs the encoder group builder for this REST method.
+ *
+ * @param beanStore
+ * The factory used for creating beans and retrieving injected
beans.
+ * @param parent
+ * The builder for the REST resource class.
+ * @param resource
+ * The REST servlet/bean instance that this context is defined
against.
+ * @return The encoder group builder for this REST resource.
+ */
+ protected EncoderGroup.Builder createEncoders(BeanStore beanStore,
RestContextBuilder parent, Supplier<?> resource) {
+
+ // Default value.
+ Value<EncoderGroup.Builder> v = Value.of(
+ parent.encoders().copy()
+ );
+
+ return v.get();
+ }
+
+ final Optional<EncoderGroup> getEncoders() {
+ return encoders == null ? empty() : of(encoders.build());
}
//-----------------------------------------------------------------------------------------------------------------
@@ -363,6 +403,11 @@ public class RestOpContextBuilder extends ContextBuilder {
return v.get();
}
+
+
+
+ private int TODO;
+
/**
* When enabled, append <js>"/*"</js> to path patterns if not already
present.
*
@@ -470,29 +515,29 @@ public class RestOpContextBuilder extends ContextBuilder {
return partSerializer;
}
- /**
- * Returns the parser group builder containing the parsers for
converting HTTP request bodies into POJOs.
- *
- * <p>
- * This method can be used to override encoders defined at the class
level via {@link RestContextBuilder#getEncoders()}.
- * On first call, the builder from the class context is copied into a
modifiable builder for this method.
- * If never called, then the builder from the class context is used.
- *
- * <p>
- * The builder is initialized with encoders defined via the {@link
Rest#parsers()} annotation.
- * That annotation is applied from parent-to-child order with child
entries given priority over parent entries.
- *
- * <ul class='seealso'>
- * <li class='link'>{@doc RestEncoders}
- * </ul>
- *
- * @return The encoder group builder for this context builder.
- */
- public EncoderGroup.Builder getEncoders() {
- if (encoders == null)
- encoders = restContext.builder.encoders.copy();
- return encoders;
- }
+// /**
+// * Returns the parser group builder containing the parsers for
converting HTTP request bodies into POJOs.
+// *
+// * <p>
+// * This method can be used to override encoders defined at the class
level via {@link RestContextBuilder#getEncoders()}.
+// * On first call, the builder from the class context is copied into a
modifiable builder for this method.
+// * If never called, then the builder from the class context is used.
+// *
+// * <p>
+// * The builder is initialized with encoders defined via the {@link
Rest#parsers()} annotation.
+// * That annotation is applied from parent-to-child order with child
entries given priority over parent entries.
+// *
+// * <ul class='seealso'>
+// * <li class='link'>{@doc RestEncoders}
+// * </ul>
+// *
+// * @return The encoder group builder for this context builder.
+// */
+// public EncoderGroup.Builder getEncoders() {
+// if (encoders == null)
+// encoders = restContext.builder.encoders().copy();
+// return encoders;
+// }
//----------------------------------------------------------------------------------------------------
// Properties
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 6296221..4100a66 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
@@ -1076,7 +1076,7 @@ public class RestAnnotation {
b.responseProcessors().add(a.responseProcessors());
b.children((Object[])a.children());
b.restOpArgs(a.restOpArgs());
- classes(a.encoders()).ifPresent(x ->
b.getEncoders().add(x));
+ classes(a.encoders()).ifPresent(x ->
b.encoders().add(x));
type(a.contextClass()).ifPresent(x -> b.type(x));
string(a.uriContext()).ifPresent(x -> b.uriContext(x));
string(a.uriAuthority()).ifPresent(x ->
b.uriAuthority(x));
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 a21c379..34b947b 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
@@ -469,7 +469,7 @@ public class RestDeleteAnnotation {
b.httpMethod("delete");
- classes(a.encoders()).ifPresent(x ->
b.getEncoders().set(x));
+ classes(a.encoders()).ifPresent(x ->
b.encoders().set(x));
type(a.contextClass()).ifPresent(x -> b.type(x));
strings(a.defaultRequestHeaders()).map(x ->
stringHeader(x)).forEach(x -> b.defaultRequestHeaders(x));
strings(a.defaultResponseHeaders()).map(x ->
stringHeader(x)).forEach(x -> b.defaultResponseHeaders(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 270420e..ed033ac 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
@@ -527,7 +527,7 @@ public class RestGetAnnotation {
b.httpMethod("get");
classes(a.serializers()).ifPresent(x ->
b.getSerializers().set(x));
- classes(a.encoders()).ifPresent(x ->
b.getEncoders().set(x));
+ classes(a.encoders()).ifPresent(x ->
b.encoders().set(x));
type(a.contextClass()).ifPresent(x -> b.type(x));
strings(a.produces()).map(MediaType::of).forEach(x ->
b.produces(x));
strings(a.defaultRequestHeaders()).map(x ->
stringHeader(x)).forEach(x -> b.defaultRequestHeaders(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 26d305d..c6d4765 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
@@ -631,7 +631,7 @@ public class RestOpAnnotation {
classes(a.serializers()).ifPresent(x ->
b.getSerializers().set(x));
classes(a.parsers()).ifPresent(x ->
b.getParsers().set(x));
- classes(a.encoders()).ifPresent(x ->
b.getEncoders().set(x));
+ classes(a.encoders()).ifPresent(x ->
b.encoders().set(x));
type(a.contextClass()).ifPresent(x -> b.type(x));
strings(a.produces()).map(MediaType::of).forEach(x ->
b.produces(x));
strings(a.consumes()).map(MediaType::of).forEach(x ->
b.consumes(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 094bb36..dd1f3dd 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
@@ -615,7 +615,7 @@ public class RestPostAnnotation {
classes(a.serializers()).ifPresent(x ->
b.getSerializers().set(x));
classes(a.parsers()).ifPresent(x ->
b.getParsers().set(x));
- classes(a.encoders()).ifPresent(x ->
b.getEncoders().set(x));
+ classes(a.encoders()).ifPresent(x ->
b.encoders().set(x));
type(a.contextClass()).ifPresent(x -> b.type(x));
strings(a.produces()).map(MediaType::of).forEach(x ->
b.produces(x));
strings(a.consumes()).map(MediaType::of).forEach(x ->
b.consumes(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 1b7d856..35efb14 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
@@ -615,7 +615,7 @@ public class RestPutAnnotation {
classes(a.serializers()).ifPresent(x ->
b.getSerializers().set(x));
classes(a.parsers()).ifPresent(x ->
b.getParsers().set(x));
- classes(a.encoders()).ifPresent(x ->
b.getEncoders().set(x));
+ classes(a.encoders()).ifPresent(x ->
b.encoders().set(x));
type(a.contextClass()).ifPresent(x -> b.type(x));
strings(a.produces()).map(MediaType::of).forEach(x ->
b.produces(x));
strings(a.consumes()).map(MediaType::of).forEach(x ->
b.consumes(x));