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 b15844b  Context API refactoring.
b15844b is described below

commit b15844b202903c0f3275cffb05b6703362cd5c00
Author: JamesBognar <[email protected]>
AuthorDate: Sun Sep 19 17:17:39 2021 -0400

    Context API refactoring.
---
 .../java/org/apache/juneau/rest/RestOpContext.java | 57 +-------------
 .../apache/juneau/rest/RestOpContextBuilder.java   | 89 ++++++++++------------
 2 files changed, 42 insertions(+), 104 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 cd1962d..957fb69 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
@@ -151,9 +151,7 @@ public class RestOpContext extends Context implements 
Comparable<RestOpContext>
                        encoders = bs.add(EncoderGroup.class, 
builder.getEncoders().orElse(context.getEncoders()));
                        serializers = bs.add(SerializerGroup.class, 
builder.getSerializers().orElse(context.getSerializers()));
                        parsers = bs.add(ParserGroup.class, 
builder.getParsers().orElse(context.getParsers()));
-
-                       partSerializer = createPartSerializer(r, builder, bs);
-                       bs.addBean(HttpPartSerializer.class, partSerializer);
+                       partSerializer = bs.add(HttpPartSerializer.class, 
builder.getPartSerializer().orElse(context.getPartSerializer()));
 
                        partParser = createPartParser(r, builder, bs);
                        bs.addBean(HttpPartParser.class, partParser);
@@ -327,59 +325,6 @@ public class RestOpContext extends Context implements 
Comparable<RestOpContext>
        }
 
        /**
-        * Instantiates the HTTP part serializer for this REST resource.
-        *
-        * <p>
-        * Instantiates based on the following logic:
-        * <ul>
-        *      <li>Returns the resource class itself is an instance of {@link 
HttpPartSerializer}.
-        *      <li>Looks for part serializer set via any of the following:
-        *              <ul>
-        *                      <li>{@link RestContextBuilder#partSerializer()}
-        *                      <li>{@link Rest#partSerializer()}.
-        *              </ul>
-        *      <li>Looks for a static or non-static 
<c>createPartSerializer()</> method that returns <c>{@link 
HttpPartSerializer}</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 an {@link OpenApiSerializer}.
-        * </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 HTTP part serializer for this REST resource.
-        * @throws Exception If serializer could not be instantiated.
-        */
-       protected HttpPartSerializer createPartSerializer(Object resource, 
RestOpContextBuilder builder, BeanStore beanStore) throws Exception {
-
-               HttpPartSerializer g = 
beanStore.getBean(HttpPartSerializer.class).orElse(null);
-
-               if (g != null)
-                       return g;
-
-               HttpPartSerializer.Creator x = builder.partSerializer;
-
-               if (x == null)
-                       x = builder.restContext.builder.partSerializer();
-
-               x = BeanStore
-                       .of(beanStore, resource)
-                       .addBean(HttpPartSerializer.Creator.class, x)
-                       .createMethodFinder(HttpPartSerializer.Creator.class, 
resource)
-                       .find("createPartSerializer", Method.class)
-                       .thenFind("createPartSerializer")
-                       .withDefault(x)
-                       .run();
-
-               return x.create();
-       }
-
-       /**
         * Instantiates the HTTP part parser for this REST resource.
         *
         * <p>
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 21b4933..35c5b04 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
@@ -39,7 +39,6 @@ import org.apache.juneau.rest.annotation.*;
 import org.apache.juneau.rest.converters.*;
 import org.apache.juneau.serializer.*;
 import org.apache.juneau.svl.*;
-import org.apache.juneau.uon.*;
 
 import java.lang.reflect.Method;
 import java.nio.charset.*;
@@ -135,7 +134,7 @@ public class RestOpContextBuilder extends ContextBuilder {
                        if (context.builder.parsers().canApply(al))
                                parsers().apply(al);
                        if (context.builder.partSerializer().canApply(al))
-                               getPartSerializer().apply(al);
+                               partSerializer().apply(al);
                        if (context.builder.partParser().canApply(al))
                                getPartParser().apply(al);
 
@@ -350,6 +349,46 @@ public class RestOpContextBuilder extends ContextBuilder {
        }
 
        
//-----------------------------------------------------------------------------------------------------------------
+       // partSerializer
+       
//-----------------------------------------------------------------------------------------------------------------
+
+       /**
+        * Returns the builder for the {@link HttpPartSerializer} object in the 
REST context.
+        *
+        * @return The builder for the {@link HttpPartSerializer} object in the 
REST context.
+        */
+       public final HttpPartSerializer.Creator partSerializer() {
+               if (partSerializer == null)
+                       partSerializer = createPartSerializer(beanStore(), 
parent, resource());
+               return partSerializer;
+       }
+
+       /**
+        * Constructs the part serializer 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 part serializer builder for this REST resource.
+        */
+       protected HttpPartSerializer.Creator createPartSerializer(BeanStore 
beanStore, RestContextBuilder parent, Supplier<?> resource) {
+
+               // Default value.
+               Value<HttpPartSerializer.Creator> v = Value.of(
+                       parent.partSerializer().copy()
+               );
+
+               return v.get();
+       }
+
+       final Optional<HttpPartSerializer> getPartSerializer() {
+               return partSerializer == null ? empty() : 
of(partSerializer.create());
+       }
+
+       
//-----------------------------------------------------------------------------------------------------------------
        // converters
        
//-----------------------------------------------------------------------------------------------------------------
 
@@ -520,52 +559,6 @@ public class RestOpContextBuilder extends ContextBuilder {
                return partParser;
        }
 
-       /**
-        * Returns the HTTP part serializer creator containing the part 
serializer for serializing POJOs to HTTP parts.
-        *
-        * <p>
-        * The default value is {@link OpenApiSerializer} which serializes 
based on OpenAPI rules, but defaults to UON notation for beans and maps, and
-        * plain text for everything else.
-        *
-        * <p>
-        * <br>Other options include:
-        * <ul>
-        *      <li class='jc'>{@link SimplePartSerializer} - Always serializes 
to plain text.
-        *      <li class='jc'>{@link UonSerializer} - Always serializers to 
UON.
-        * </ul>
-        *
-        * @return The HTTP part serializer creator.
-        */
-       public HttpPartSerializer.Creator getPartSerializer() {
-               if (partSerializer == null)
-                       partSerializer = 
restContext.builder.partSerializer().copy();
-               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;
-//     }
-
        
//----------------------------------------------------------------------------------------------------
        // Properties
        
//----------------------------------------------------------------------------------------------------

Reply via email to