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 ba4c7e5 Context API refactoring.
ba4c7e5 is described below
commit ba4c7e554f0a92e6c74991e4353b1a92452e10bd
Author: JamesBognar <[email protected]>
AuthorDate: Sun Sep 12 11:57:15 2021 -0400
Context API refactoring.
---
.../org/apache/juneau/rest/RequestHttpPart.java | 2 +-
.../java/org/apache/juneau/rest/RestContext.java | 56 +----------
.../org/apache/juneau/rest/RestContextBuilder.java | 104 +++++++++++++++++----
.../java/org/apache/juneau/rest/RestOpContext.java | 4 +-
.../apache/juneau/rest/RestOpContextBuilder.java | 4 +-
.../juneau/rest/annotation/RestAnnotation.java | 2 +-
6 files changed, 92 insertions(+), 80 deletions(-)
diff --git
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RequestHttpPart.java
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RequestHttpPart.java
index f55e6a3..10dbfee 100644
---
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RequestHttpPart.java
+++
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RequestHttpPart.java
@@ -74,7 +74,7 @@ public abstract class RequestHttpPart {
* Specifies the part parser to use for this part.
*
* <p>
- * If not specified, uses the part parser defined on the client by
calling {@link RestContextBuilder#getPartParser()}.
+ * If not specified, uses the part parser defined on the client by
calling {@link RestContextBuilder#partParser()}.
*
* @param value
* The new part parser to use for this part.
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 e7f3b92..c6fd518 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
@@ -50,7 +50,6 @@ import org.apache.juneau.httppart.bean.*;
import org.apache.juneau.internal.*;
import org.apache.juneau.jsonschema.*;
import org.apache.juneau.mstat.*;
-import org.apache.juneau.oapi.*;
import org.apache.juneau.parser.*;
import org.apache.juneau.reflect.*;
import org.apache.juneau.rest.annotation.*;
@@ -263,12 +262,10 @@ public class RestContext extends Context {
debugDefault = builder.debugDefault;
callLogger = bs.add(RestLogger.class,
builder.callLogger().beanStore(beanStore).loggerOnce(logger).thrownStoreOnce(thrownStore).build());
partSerializer = bs.add(HttpPartSerializer.class,
builder.partSerializer().create());
+ partParser = bs.add(HttpPartParser.class,
builder.partParser().create());
Object r = resource.get();
- partParser = createPartParser(r, builder, bs);
- bs.addBean(HttpPartParser.class, partParser);
-
jsonSchemaGenerator = createJsonSchemaGenerator(r,
builder, bs);
bs.addBean(JsonSchemaGenerator.class,
jsonSchemaGenerator);
@@ -619,57 +616,6 @@ public class RestContext extends Context {
}
/**
- * Instantiates the HTTP part parser for this REST resource.
- *
- * <p>
- * Instantiates based on the following logic:
- * <ul>
- * <li>Returns the resource class itself is an instance of {@link
HttpPartParser}.
- * <li>Looks for part parser value set via any of the following:
- * <ul>
- * <li>{@link RestContextBuilder#getPartParser()}
- * <li>{@link Rest#partParser()}.
- * </ul>
- * <li>Looks for a static or non-static <c>createPartParser()</>
method that returns <c>{@link HttpPartParser}</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 servlet or bean that this context defines.
- * @param builder
- * The builder for this object.
- * @param beanStore
- * The factory used for creating beans and retrieving injected
beans.
- * <br>Created by {@link RestContextBuilder#beanStore()}.
- * @return The HTTP part parser for this REST resource.
- * @throws Exception If parser could not be instantiated.
- */
- protected HttpPartParser createPartParser(Object resource,
RestContextBuilder builder, BeanStore beanStore) throws Exception {
-
- HttpPartParser.Creator x = builder.partParser;
-
- if (beanStore.hasBean(HttpPartParser.class))
-
x.impl(beanStore.getBean(HttpPartParser.class).orElse(null));
-
- x = BeanStore
- .of(beanStore, resource)
- .addBean(HttpPartParser.Creator.class, x)
- .beanCreateMethodFinder(HttpPartParser.Creator.class,
resource)
- .find("createPartParser")
- .withDefault(x)
- .run();
-
- return x.create();
- }
-
- /**
* Instantiates the REST method parameter resolvers for this REST
resource.
*
* <p>
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 8f769d2..caf1143 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
@@ -132,6 +132,7 @@ public class RestContextBuilder extends ContextBuilder
implements ServletConfig
private ResponseProcessorList.Builder responseProcessors;
private RestLogger.Builder callLogger;
private HttpPartSerializer.Creator partSerializer;
+ private HttpPartParser.Creator partParser;
String
allowedHeaderParams = env("RestContext.allowedHeaderParams",
"Accept,Content-Type"),
@@ -166,7 +167,6 @@ public class RestContextBuilder extends ContextBuilder
implements ServletConfig
EncoderGroup.Builder encoders =
EncoderGroup.create().add(IdentityEncoder.INSTANCE);
SerializerGroup.Builder serializers = SerializerGroup.create();
ParserGroup.Builder parsers = ParserGroup.create();
- HttpPartParser.Creator partParser =
HttpPartParser.creator().type(OpenApiParser.class);
Enablement debugDefault, debug;
@@ -1479,6 +1479,90 @@ public class RestContextBuilder extends ContextBuilder
implements ServletConfig
return v.get();
}
+ /**
+ * Returns the part parser builder for this context.
+ *
+ * @return The part parser builder for this context.
+ */
+ public final HttpPartParser.Creator partParser() {
+ if (partParser == null)
+ partParser = createPartParser(beanStore(), resource());
+ return partParser;
+ }
+
+ /**
+ * Instantiates the HTTP part parser for this REST resource.
+ *
+ * <p>
+ * Instantiates based on the following logic:
+ * <ul>
+ * <li>Returns the resource class itself is an instance of {@link
HttpPartParser}.
+ * <li>Looks for part parser set via any of the following:
+ * <ul>
+ * <li>{@link RestContextBuilder#partParser()}
+ * <li>{@link Rest#partParser()}.
+ * </ul>
+ * <li>Looks for a static or non-static <c>createPartParser()</>
method that returns <c>{@link HttpPartParser}</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 OpenApiParser}.
+ * </ul>
+ *
+ * @param beanStore
+ * The factory used for creating beans and retrieving injected
beans.
+ * @param resource
+ * The REST servlet or bean that this context defines.
+ * @return The HTTP part serializer for this REST resource.
+ */
+ protected HttpPartParser.Creator createPartParser(BeanStore beanStore,
Supplier<?> resource) {
+
+ Value<HttpPartParser.Creator> v = Value.empty();
+ Object r = resource.get();
+
+ // Get builder from bean store.
+ beanStore.getBean(HttpPartParser.Creator.class).map(x ->
x.copy()).ifPresent(x -> v.set(x));
+
+ // Create default.
+ if (v.isEmpty()) {
+ v.set(
+ HttpPartParser
+ .creator()
+ .type(OpenApiParser.class)
+ );
+ }
+
+ // Set implementation if in bean store.
+ beanStore.getBean(HttpPartParser.class).ifPresent(x ->
v.get().impl(x));
+
+ // Set default type.
+ defaultClasses.get(HttpPartParser.class).ifPresent(x ->
v.get().type(x));
+
+ // Call: public [static] HttpPartParser.Creator
createPartParser(<anything-in-bean-store>)
+ BeanStore
+ .of(beanStore, r)
+ .addBean(HttpPartParser.Creator.class, v.get())
+ .beanCreateMethodFinder(HttpPartParser.Creator.class,
resource)
+ .find("createPartParser")
+ .execute()
+ .ifPresent(x -> v.set(x));
+
+ // Call: public [static] HttpPartParser
createPartParser(<anything-in-bean-store>)
+ BeanStore
+ .of(beanStore, r)
+ .addBean(HttpPartParser.Creator.class, v.get())
+ .beanCreateMethodFinder(HttpPartParser.class, resource)
+ .find("createPartParser")
+ .execute()
+ .ifPresent(x -> v.get().impl(x));
+
+ return v.get();
+ }
+
//----------------------------------------------------------------------------------------------------
// Methods that give access to the config file, var resolver, and
properties.
//----------------------------------------------------------------------------------------------------
@@ -1536,24 +1620,6 @@ public class RestContextBuilder extends ContextBuilder
implements ServletConfig
}
/**
- * Returns the HTTP part parser creator containing the part parser for
parsing HTTP parts into POJOs.
- *
- * <p>
- * The default value is {@link OpenApiParser} which allows for both
plain-text and URL-Encoded-Object-Notation values.
- * <br>If your parts contain text that can be confused with UON (e.g.
<js>"(foo)"</js>), you can switch to
- * {@link SimplePartParser} which treats everything as plain text.
- *
- * <p>
- * When specified as a class, annotations on this class are applied to
the parser.
- * Otherwise when specified as an already-instantiated {@link
HttpPartParser}, annotations will not be applied.
- *
- * @return The HTTP part parser creator.
- */
- public HttpPartParser.Creator getPartParser() {
- return partParser;
- }
-
- /**
* Returns the encoder group builder containing the encoders for
compressing/decompressing input and output streams.
*
* <p>
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 f2609c9..e2749a5 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
@@ -578,7 +578,7 @@ public class RestOpContext extends BeanContext implements
Comparable<RestOpConte
* <li>Returns the resource class itself is an instance of {@link
HttpPartParser}.
* <li>Looks for part parser set via any of the following:
* <ul>
- * <li>{@link RestContextBuilder#getPartParser()}
+ * <li>{@link RestContextBuilder#partParser()}
* <li>{@link Rest#partParser()}.
* </ul>
* <li>Looks for a static or non-static <c>createPartParser()</>
method that returns <c>{@link HttpPartParser}</c> on the
@@ -608,7 +608,7 @@ public class RestOpContext extends BeanContext implements
Comparable<RestOpConte
HttpPartParser.Creator x = builder.partParser;
if (x == null)
- x = builder.restContext.builder.partParser;
+ x = builder.restContext.builder.partParser();
x = BeanStore
.of(beanStore, 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 b753d27..d2fc598 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
@@ -124,7 +124,7 @@ public class RestOpContextBuilder extends
BeanContextBuilder {
getParsers().apply(al);
if (context.builder.partSerializer().canApply(al))
getPartSerializer().apply(al);
- if (context.builder.partParser.canApply(al))
+ if (context.builder.partParser().canApply(al))
getPartParser().apply(al);
} catch (Exception e) {
@@ -213,7 +213,7 @@ public class RestOpContextBuilder extends
BeanContextBuilder {
*/
public HttpPartParser.Creator getPartParser() {
if (partParser == null)
- partParser = restContext.builder.partParser.copy();
+ partParser = restContext.builder.partParser().copy();
return partParser;
}
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 88034c2..4216564 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
@@ -1050,7 +1050,7 @@ public class RestAnnotation {
classes(a.serializers()).ifPresent(x ->
b.getSerializers().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.getPartParser().type(x));
+ type(a.partParser()).ifPresent(x ->
b.partParser().type(x));
strings(a.produces()).map(MediaType::of).forEach(x ->
b.produces(x));
strings(a.consumes()).map(MediaType::of).forEach(x ->
b.consumes(x));
strings(a.defaultRequestAttributes()).map(x ->
BasicNamedAttribute.ofPair(x)).forEach(x -> b.defaultRequestAttributes(x));