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 6a5dfda Context API refactoring.
6a5dfda is described below
commit 6a5dfda1aad6dca0ae4fb7005ed678d38cbb8e08
Author: JamesBognar <[email protected]>
AuthorDate: Sun Sep 19 16:52:54 2021 -0400
Context API refactoring.
---
.../java/org/apache/juneau/rest/RestContext.java | 12 +++
.../org/apache/juneau/rest/RestContextBuilder.java | 69 +++++++++++++-
.../java/org/apache/juneau/rest/RestOpContext.java | 104 ++++++++++-----------
.../apache/juneau/rest/RestOpContextBuilder.java | 90 +++++++++++++-----
.../juneau/rest/annotation/RestAnnotation.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 +-
9 files changed, 201 insertions(+), 84 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 15af18d..c003e25 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
@@ -59,6 +59,7 @@ import org.apache.juneau.rest.logging.*;
import org.apache.juneau.http.header.*;
import org.apache.juneau.http.response.*;
import org.apache.juneau.rest.util.*;
+import org.apache.juneau.serializer.*;
import org.apache.juneau.svl.*;
import org.apache.juneau.utils.*;
@@ -168,6 +169,7 @@ public class RestContext extends Context {
private final Class<? extends RestOpArg>[] restOpArgs, hookMethodArgs;
private final BeanContext beanContext;
private final EncoderGroup encoders;
+ private final SerializerGroup serializers;
private final HttpPartSerializer partSerializer;
private final HttpPartParser partParser;
private final JsonSchemaGenerator jsonSchemaGenerator;
@@ -276,6 +278,7 @@ public class RestContext extends Context {
beanContext = bs.add(BeanContext.class,
builder.beanContext().build());
encoders = bs.add(EncoderGroup.class,
builder.encoders().build());
+ serializers = bs.add(SerializerGroup.class,
builder.serializers().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());
@@ -386,6 +389,15 @@ public class RestContext extends Context {
}
/**
+ * Returns the serializers associated with this context.
+ *
+ * @return The serializers associated with this context.
+ */
+ public SerializerGroup getSerializers() {
+ return serializers;
+ }
+
+ /**
* 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 ab0d433..6dc21d6 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
@@ -150,6 +150,7 @@ public class RestContextBuilder extends ContextBuilder
implements ServletConfig
private SwaggerProvider.Builder swaggerProvider;
private BeanContextBuilder beanContext;
private EncoderGroup.Builder encoders;
+ private SerializerGroup.Builder serializers;
String
allowedHeaderParams = env("RestContext.allowedHeaderParams",
"Accept,Content-Type"),
@@ -173,7 +174,6 @@ public class RestContextBuilder extends ContextBuilder
implements ServletConfig
Class<? extends RestOperations> operationsClass = RestOperations.class;
// TODO
- SerializerGroup.Builder serializers = SerializerGroup.create();
ParserGroup.Builder parsers = ParserGroup.create();
List<Object> children = new ArrayList<>();
@@ -1015,6 +1015,73 @@ public class RestContextBuilder extends ContextBuilder
implements ServletConfig
}
//-----------------------------------------------------------------------------------------------------------------
+ // serializers
+
//-----------------------------------------------------------------------------------------------------------------
+
+ /**
+ * Returns the builder for the {@link SerializerGroup} object in the
REST context.
+ *
+ * @return The builder for the {@link SerializerGroup} object in the
REST context.
+ */
+ public final SerializerGroup.Builder serializers() {
+ if (serializers == null)
+ serializers = createSerializers(beanStore(),
resource());
+ return serializers;
+ }
+
+ /**
+ * Instantiates the serializer group builder for this REST object.
+ *
+ * @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 serializer group builder for this REST resource.
+ */
+ protected SerializerGroup.Builder createSerializers(BeanStore
beanStore, Supplier<?> resource) {
+
+ // Default value.
+ Value<SerializerGroup.Builder> v = Value.of(
+ SerializerGroup
+ .create()
+ .beanStore(beanStore)
+ );
+
+ // Specify the implementation class if its set as a default.
+ defaultClasses()
+ .get(SerializerGroup.class)
+ .ifPresent(x -> v.get().type(x));
+
+ // Replace with builder from bean store.
+ beanStore
+ .getBean(SerializerGroup.Builder.class)
+ .map(x -> x.copy())
+ .ifPresent(x->v.set(x));
+
+ // Replace with bean from bean store.
+ beanStore
+ .getBean(SerializerGroup.class)
+ .ifPresent(x->v.get().impl(x));
+
+ // Replace with builder from: public [static]
SerializerGroup.Builder createSerializers(<args>)
+ beanStore
+ .beanCreateMethodFinder(SerializerGroup.Builder.class)
+ .addBean(SerializerGroup.Builder.class, v.get())
+ .find("createSerializers")
+ .run(x -> v.set(x));
+
+ // Replace with bean from: public [static] SerializerGroup
createSerializers(<args>)
+ beanStore
+ .beanCreateMethodFinder(SerializerGroup.class)
+ .addBean(SerializerGroup.Builder.class, v.get())
+ .find("createSerializers")
+ .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 49a4aa1..b224ead 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
@@ -149,9 +149,7 @@ public class RestOpContext extends Context implements
Comparable<RestOpContext>
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);
+ serializers = bs.add(SerializerGroup.class,
builder.getSerializers().orElse(context.getSerializers()));
parsers = createParsers(r, builder, bs);
bs.addBean(ParserGroup.class, parsers);
@@ -330,56 +328,56 @@ public class RestOpContext extends Context implements
Comparable<RestOpContext>
return x.build();
}
- /**
- * Instantiates the serializers for this REST resource.
- *
- * <p>
- * Instantiates based on the following logic:
- * <ul>
- * <li>Looks for serializers set via any of the following:
- * <ul>
- * <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
- * resource class with any of the following arguments:
- * <ul>
- * <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>Serializer[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 serializers for this REST resource.
- * @throws Exception If serializers could not be instantiated.
- */
- protected SerializerGroup createSerializers(Object resource,
RestOpContextBuilder builder, BeanStore beanStore) throws Exception {
-
- SerializerGroup g =
beanStore.getBean(SerializerGroup.class).orElse(null);
-
- if (g != null)
- return g;
-
- SerializerGroup.Builder x = builder.serializers;
- if (x == null)
- x = builder.restContext.builder.serializers;
-
- x = BeanStore
- .of(beanStore, resource)
- .addBean(SerializerGroup.Builder.class, x)
- .createMethodFinder(SerializerGroup.Builder.class,
resource)
- .find("createSerializers", Method.class)
- .thenFind("createSerializers")
- .withDefault(x)
- .run();
-
- return x.build();
- }
+// /**
+// * Instantiates the serializers for this REST resource.
+// *
+// * <p>
+// * Instantiates based on the following logic:
+// * <ul>
+// * <li>Looks for serializers set via any of the following:
+// * <ul>
+// * <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
+// * resource class with any of the following arguments:
+// * <ul>
+// * <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>Serializer[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 serializers for this REST resource.
+// * @throws Exception If serializers could not be instantiated.
+// */
+// protected SerializerGroup createSerializers(Object resource,
RestOpContextBuilder builder, BeanStore beanStore) throws Exception {
+//
+// SerializerGroup g =
beanStore.getBean(SerializerGroup.class).orElse(null);
+//
+// if (g != null)
+// return g;
+//
+// SerializerGroup.Builder x = builder.serializers;
+// if (x == null)
+// x = builder.restContext.builder.serializers;
+//
+// x = BeanStore
+// .of(beanStore, resource)
+// .addBean(SerializerGroup.Builder.class, x)
+// .createMethodFinder(SerializerGroup.Builder.class,
resource)
+// .find("createSerializers", Method.class)
+// .thenFind("createSerializers")
+// .withDefault(x)
+// .run();
+//
+// return x.build();
+// }
/**
* Instantiates the parsers for this REST resource.
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 b97b71d..8c604e2 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
@@ -130,8 +130,8 @@ public class RestOpContextBuilder extends ContextBuilder {
if (context.builder.beanContext().canApply(al))
beanContext().apply(al);
- if (context.builder.serializers.canApply(al))
- getSerializers().apply(al);
+ if (context.builder.serializers().canApply(al))
+ serializers().apply(al);
if (context.builder.parsers.canApply(al))
getParsers().apply(al);
if (context.builder.partSerializer().canApply(al))
@@ -270,6 +270,46 @@ public class RestOpContextBuilder extends ContextBuilder {
}
//-----------------------------------------------------------------------------------------------------------------
+ // serializers
+
//-----------------------------------------------------------------------------------------------------------------
+
+ /**
+ * Returns the builder for the {@link SerializerGroup} object in the
REST context.
+ *
+ * @return The builder for the {@link SerializerGroup} object in the
REST context.
+ */
+ public final SerializerGroup.Builder serializers() {
+ if (serializers == null)
+ serializers = createSerializers(beanStore(), parent,
resource());
+ return serializers;
+ }
+
+ /**
+ * Constructs the serializer 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 serializer group builder for this REST resource.
+ */
+ protected SerializerGroup.Builder createSerializers(BeanStore
beanStore, RestContextBuilder parent, Supplier<?> resource) {
+
+ // Default value.
+ Value<SerializerGroup.Builder> v = Value.of(
+ parent.serializers().copy()
+ );
+
+ return v.get();
+ }
+
+ final Optional<SerializerGroup> getSerializers() {
+ return serializers == null ? empty() : of(serializers.build());
+ }
+
+
//-----------------------------------------------------------------------------------------------------------------
// converters
//-----------------------------------------------------------------------------------------------------------------
@@ -429,29 +469,29 @@ public class RestOpContextBuilder extends ContextBuilder {
return this;
}
- /**
- * Returns the serializer group builder containing the serializers for
marshalling POJOs into response bodies.
- *
- * <p>
- * This method can be used to override serializers defined at the class
level via {@link RestContextBuilder#getSerializers()}.
- * 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 serializers defined via the {@link
RestOp#serializers()} (and related) 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 RestSerializers}
- * </ul>
- *
- * @return The serializer group builder for this context builder.
- */
- public SerializerGroup.Builder getSerializers() {
- if (serializers == null)
- serializers = restContext.builder.serializers.copy();
- return serializers;
- }
+// /**
+// * Returns the serializer group builder containing the serializers for
marshalling POJOs into response bodies.
+// *
+// * <p>
+// * This method can be used to override serializers defined at the class
level via {@link RestContextBuilder#getSerializers()}.
+// * 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 serializers defined via the {@link
RestOp#serializers()} (and related) 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 RestSerializers}
+// * </ul>
+// *
+// * @return The serializer group builder for this context builder.
+// */
+// public SerializerGroup.Builder getSerializers() {
+// if (serializers == null)
+// serializers = restContext.builder.serializers.copy();
+// return serializers;
+// }
/**
* Returns the parser group builder containing the parsers for
converting request bodies into POJOs.
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 4100a66..f1fbcc5 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
@@ -1062,7 +1062,7 @@ public class RestAnnotation {
Rest a = ai.getAnnotation();
ClassInfo c = ai.getClassOn();
- classes(a.serializers()).ifPresent(x ->
b.getSerializers().add(x));
+ classes(a.serializers()).ifPresent(x ->
b.serializers().add(x));
classes(a.parsers()).ifPresent(x ->
b.getParsers().add(x));
type(a.partSerializer()).ifPresent(x ->
b.partSerializer().type(x));
type(a.partParser()).ifPresent(x ->
b.partParser().type(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 ed033ac..56a8ee1 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
@@ -526,7 +526,7 @@ public class RestGetAnnotation {
b.httpMethod("get");
- classes(a.serializers()).ifPresent(x ->
b.getSerializers().set(x));
+ classes(a.serializers()).ifPresent(x ->
b.serializers().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));
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 c6d4765..77d01c2 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
@@ -629,7 +629,7 @@ public class RestOpAnnotation {
public void apply(AnnotationInfo<RestOp> ai,
RestOpContextBuilder b) {
RestOp a = ai.getAnnotation();
- classes(a.serializers()).ifPresent(x ->
b.getSerializers().set(x));
+ classes(a.serializers()).ifPresent(x ->
b.serializers().set(x));
classes(a.parsers()).ifPresent(x ->
b.getParsers().set(x));
classes(a.encoders()).ifPresent(x ->
b.encoders().set(x));
type(a.contextClass()).ifPresent(x -> b.type(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 dd1f3dd..2942db2 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
@@ -613,7 +613,7 @@ public class RestPostAnnotation {
b.httpMethod("post");
- classes(a.serializers()).ifPresent(x ->
b.getSerializers().set(x));
+ classes(a.serializers()).ifPresent(x ->
b.serializers().set(x));
classes(a.parsers()).ifPresent(x ->
b.getParsers().set(x));
classes(a.encoders()).ifPresent(x ->
b.encoders().set(x));
type(a.contextClass()).ifPresent(x -> b.type(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 35efb14..3db145a 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
@@ -613,7 +613,7 @@ public class RestPutAnnotation {
b.httpMethod("put");
- classes(a.serializers()).ifPresent(x ->
b.getSerializers().set(x));
+ classes(a.serializers()).ifPresent(x ->
b.serializers().set(x));
classes(a.parsers()).ifPresent(x ->
b.getParsers().set(x));
classes(a.encoders()).ifPresent(x ->
b.encoders().set(x));
type(a.contextClass()).ifPresent(x -> b.type(x));