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 0fbd6e0 REST refactoring.
0fbd6e0 is described below
commit 0fbd6e003746512ae234db21822271ad93d4763b
Author: JamesBognar <[email protected]>
AuthorDate: Tue Jan 12 09:45:13 2021 -0500
REST refactoring.
---
.../java/org/apache/juneau/rest/RestContext.java | 96 +++++---
.../org/apache/juneau/rest/RestMethodContext.java | 250 ++++++++++++++++++---
2 files changed, 285 insertions(+), 61 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 03db723..8a8c3d4 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
@@ -3278,61 +3278,59 @@ public class RestContext extends BeanContext {
this.builder = builder;
this.resource = builder.resource instanceof Supplier ?
(Supplier<?>)builder.resource : ()->builder.resource;
- Object resource = getResource();
+ Object r = getResource();
parentContext = builder.parentContext;
- ClassInfo rci = ClassInfo.of(resource).resolved();
+ ClassInfo rci = ClassInfo.of(r).resolved();
- rootBeanFactory = createRootBeanFactory(resource);
+ rootBeanFactory = createRootBeanFactory(r);
- beanFactory = createBeanFactory(resource);
+ beanFactory = createBeanFactory(r);
beanFactory.addBean(BeanFactory.class, beanFactory);
PropertyStore ps = getPropertyStore();
beanFactory.addBean(PropertyStore.class, ps);
- logger = createLogger(resource, beanFactory);
+ logger = createLogger(r, beanFactory);
beanFactory.addBean(Logger.class, logger);
- stackTraceStore = createStackTraceStore(resource,
beanFactory);
+ stackTraceStore = createStackTraceStore(r, beanFactory);
beanFactory.addBean(StackTraceStore.class,
stackTraceStore);
- varResolver = createVarResolver(resource, beanFactory);
+ varResolver = createVarResolver(r, beanFactory);
beanFactory.addBean(VarResolver.class, varResolver);
config =
builder.config.resolving(varResolver.createSession());
beanFactory.addBean(Config.class, config);
- responseHandlers = createResponseHandlers(resource,
beanFactory);
+ responseHandlers = createResponseHandlers(r,
beanFactory);
beanFactory.addBean(ResponseHandler[].class,
responseHandlers);
- callLogger = createCallLogger(resource, beanFactory);
+ callLogger = createCallLogger(r, beanFactory);
beanFactory.addBean(RestLogger.class, callLogger);
- Serializer[] _serializers = createSerializers(resource,
beanFactory);
- beanFactory.addBean(Serializer[].class, _serializers);
- serializers =
SerializerGroup.create().append(_serializers).build();
+ serializers = createSerializers(r, beanFactory, ps);
+ beanFactory.addBean(SerializerGroup.class, serializers);
- Parser[] _parsers = createParsers(resource,
beanFactory);
- beanFactory.addBean(Parser[].class, _parsers);
- parsers = ParserGroup.create().append(_parsers).build();
+ parsers = createParsers(r, beanFactory, ps);
+ beanFactory.addBean(ParserGroup.class, parsers);
- partSerializer = createPartSerializer(resource,
beanFactory);
+ partSerializer = createPartSerializer(r, beanFactory);
beanFactory.addBean(HttpPartSerializer.class,
partSerializer);
- partParser = createPartParser(resource, beanFactory);
+ partParser = createPartParser(r, beanFactory);
beanFactory.addBean(HttpPartParser.class, partParser);
- jsonSchemaGenerator =
createJsonSchemaGenerator(resource, beanFactory);
+ jsonSchemaGenerator = createJsonSchemaGenerator(r,
beanFactory);
beanFactory.addBean(JsonSchemaGenerator.class,
jsonSchemaGenerator);
- fileFinder = createFileFinder(resource, beanFactory);
+ fileFinder = createFileFinder(r, beanFactory);
beanFactory.addBean(FileFinder.class, fileFinder);
- staticFiles = createStaticFiles(resource, beanFactory);
+ staticFiles = createStaticFiles(r, beanFactory);
beanFactory.addBean(StaticFiles.class, staticFiles);
- RestMethodParam[] _paramResolvers =
createParamResolvers(resource, beanFactory);
+ RestMethodParam[] _paramResolvers =
createParamResolvers(r, beanFactory);
beanFactory.addBean(RestMethodParam[].class,
_paramResolvers);
AMap<Class<?>,RestMethodParam> _paramResolvers2 =
AMap.of();
for (RestMethodParam rp : _paramResolvers)
@@ -3446,7 +3444,7 @@ public class RestContext extends BeanContext {
if (mi.isNotPublic())
throw new
RestServletException("@RestMethod method {0}.{1} must be defined as public.",
rci.inner().getName(), mi.getSimpleName());
- RestMethodContextBuilder rmcb =
new RestMethodContextBuilder(resource, mi.inner(), this);
+ RestMethodContextBuilder rmcb =
new RestMethodContextBuilder(r, mi.inner(), this);
RestMethodContext sm = new
RestMethodContext(rmcb);
String httpMethod =
sm.getHttpMethod();
@@ -3460,7 +3458,7 @@ public class RestContext extends BeanContext {
if
(rim.getMethodsByPath().isEmpty())
throw new
InternalServerError("Method {0} returns an interface {1} that doesn't define
any remote methods.", mi.getSignature(), interfaceClass.getFullName());
-
RestMethodContextBuilder smb = new RestMethodContextBuilder(resource,
mi.inner(), this);
+
RestMethodContextBuilder smb = new RestMethodContextBuilder(r, mi.inner(),
this);
smb.dotAll();
sm = new
RestMethodContext(smb) {
@@ -3625,7 +3623,7 @@ public class RestContext extends BeanContext {
if (oc == builder.resourceClass)
continue;
cb = RestContext.create(this,
builder.inner, oc, null);
- o = new BeanFactory(beanFactory,
resource).addBean(RestContextBuilder.class, cb).createBean(oc);
+ o = new BeanFactory(beanFactory,
r).addBean(RestContextBuilder.class, cb).createBean(oc);
} else {
cb = RestContext.create(this,
builder.inner, o.getClass(), o);
}
@@ -3642,7 +3640,7 @@ public class RestContext extends BeanContext {
childResources.put(cb.getPath(), cc);
}
- infoProvider = createInfoProvider(resource,
beanFactory);
+ infoProvider = createInfoProvider(r, beanFactory);
} catch (HttpException e) {
_initException = e;
@@ -3996,19 +3994,36 @@ public class RestContext extends BeanContext {
*
* @param resource The REST resource object.
* @param beanFactory The bean factory to use for retrieving and
creating beans.
+ * @param ps The property store to apply to all serialiers.
* @return The serializers for this REST resource.
* @throws Exception If serializers could not be instantiated.
* @seealso #REST_serializers
*/
- protected Serializer[] createSerializers(Object resource, BeanFactory
beanFactory) throws Exception {
- Serializer[] x = getInstanceArrayProperty(REST_serializers,
Serializer.class, null, beanFactory);
+ protected SerializerGroup createSerializers(Object resource,
BeanFactory beanFactory, PropertyStore ps) throws Exception {
+ Object x = getArrayProperty(REST_serializers, Object.class);
if (x == null)
x = beanFactory.createBeanViaMethod(Serializer[].class,
resource, "createSerializers");
if (x == null)
+ x = beanFactory.createBeanViaMethod(Class[].class,
resource, "createSerializers");
+ if (x == null) {
+ x =
beanFactory.createBeanViaMethod(SerializerGroup.class, resource,
"createSerializers");
+ if (x != null)
+ return (SerializerGroup)x;
+ }
+ if (x == null)
x =
beanFactory.getBean(Serializer[].class).orElse(null);
+ if (x == null) {
+ x =
beanFactory.getBean(SerializerGroup.class).orElse(null);
+ if (x != null)
+ return (SerializerGroup)x;
+ }
if (x == null)
x = new Serializer[0];
- return x;
+ return SerializerGroup
+ .create()
+ .append((Object[])x)
+ .apply(ps)
+ .build();
}
/**
@@ -4035,19 +4050,36 @@ public class RestContext extends BeanContext {
*
* @param resource The REST resource object.
* @param beanFactory The bean factory to use for retrieving and
creating beans.
+ * @param ps The property store to apply to all serialiers.
* @return The parsers for this REST resource.
* @throws Exception If parsers could not be instantiated.
* @seealso #REST_parsers
*/
- protected Parser[] createParsers(Object resource, BeanFactory
beanFactory) throws Exception {
- Parser[] x = getInstanceArrayProperty(REST_parsers,
Parser.class, null, beanFactory);
+ protected ParserGroup createParsers(Object resource, BeanFactory
beanFactory, PropertyStore ps) throws Exception {
+ Object x = getArrayProperty(REST_parsers, Object.class);
if (x == null)
x = beanFactory.createBeanViaMethod(Parser[].class,
resource, "createParsers");
if (x == null)
+ x = beanFactory.createBeanViaMethod(Class[].class,
resource, "createParsers");
+ if (x == null) {
+ x = beanFactory.createBeanViaMethod(ParserGroup.class,
resource, "createParsers");
+ if (x != null)
+ return (ParserGroup)x;
+ }
+ if (x == null)
x = beanFactory.getBean(Parser[].class).orElse(null);
+ if (x == null) {
+ x = beanFactory.getBean(ParserGroup.class).orElse(null);
+ if (x != null)
+ return (ParserGroup)x;
+ }
if (x == null)
x = new Parser[0];
- return x;
+ return ParserGroup
+ .create()
+ .append((Object[])x)
+ .apply(ps)
+ .build();
}
/**
@@ -5601,7 +5633,7 @@ public class RestContext extends BeanContext {
Class<?> c = o.getClass();
ResponseBeanMeta rbm = responseBeanMetas.get(c);
if (rbm == null) {
- rbm = ResponseBeanMeta.create(c,
serializers.getPropertyStore());
+ rbm = ResponseBeanMeta.create(c, getPropertyStore());
if (rbm == null)
rbm = ResponseBeanMeta.NULL;
responseBeanMetas.put(c, rbm);
diff --git
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestMethodContext.java
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestMethodContext.java
index dff885d..a6054e9 100644
---
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestMethodContext.java
+++
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestMethodContext.java
@@ -44,6 +44,7 @@ import org.apache.juneau.httppart.bean.*;
import org.apache.juneau.internal.*;
import org.apache.juneau.internal.HttpUtils;
import org.apache.juneau.jsonschema.*;
+import org.apache.juneau.oapi.*;
import org.apache.juneau.parser.*;
import org.apache.juneau.reflect.*;
import org.apache.juneau.rest.annotation.*;
@@ -554,51 +555,42 @@ public class RestMethodContext extends BeanContext
implements Comparable<RestMet
super(b.getPropertyStore());
try {
- this.context = b.context;
- this.method = b.method;
- this.methodInvoker = new MethodInvoker(method,
context.getMethodExecStats(method));
- this.mi = MethodInfo.of(method);
-
- // Need this to access methods in anonymous inner
classes.
- mi.setAccessible();
+ context = b.context;
+ method = b.method;
+ methodInvoker = new MethodInvoker(method,
context.getMethodExecStats(method));
+ mi = MethodInfo.of(method).accessible();
+ PropertyStore ps = getPropertyStore();
+ Object r = context.getResource();
- int hd = 0;
+ int _hierarchyDepth = 0;
Class<?> sc =
b.method.getDeclaringClass().getSuperclass();
while (sc != null) {
- hd++;
+ _hierarchyDepth++;
sc = sc.getSuperclass();
}
- hierarchyDepth = hd;
-
- PropertyStore ps = getPropertyStore();
- Object r = context.getResource();
+ hierarchyDepth = _hierarchyDepth;
- this.beanFactory = new
BeanFactory(context.getBeanFactory(), r)
+ beanFactory = new BeanFactory(context.getBeanFactory(),
r)
.addBean(RestMethodContext.class, this)
.addBean(Method.class, method);
+ beanFactory.addBean(BeanFactory.class, beanFactory);
+
+ serializers = createSerializers(r, beanFactory, ps);
+ beanFactory.addBean(SerializerGroup.class, serializers);
+
+ parsers = createParsers(r, beanFactory, ps);
+ beanFactory.addBean(ParserGroup.class, parsers);
String _httpMethod = getProperty(RESTMETHOD_httpMethod,
String.class, null);
if (_httpMethod == null)
_httpMethod =
HttpUtils.detectHttpMethod(method, true, "GET");
if ("METHOD".equals(_httpMethod))
_httpMethod = "*";
- this.httpMethod =
_httpMethod.toUpperCase(Locale.ENGLISH);
+ httpMethod = _httpMethod.toUpperCase(Locale.ENGLISH);
- this.defaultCharset = getProperty(REST_defaultCharset,
String.class, "utf-8");
+ defaultCharset = getProperty(REST_defaultCharset,
String.class, "utf-8");
- this.maxInput =
StringUtils.parseLongWithSuffix(getProperty(REST_maxInput, String.class,
"100M"));
-
- this.serializers = SerializerGroup
- .create()
- .append(getArrayProperty(REST_serializers,
Object.class))
- .apply(ps)
- .build();
-
- this.parsers = ParserGroup
- .create()
- .append(getArrayProperty(REST_parsers,
Object.class))
- .apply(ps)
- .build();
+ maxInput =
StringUtils.parseLongWithSuffix(getProperty(REST_maxInput, String.class,
"100M"));
HttpPartParser hpp = context.getPartParser();
if (hpp instanceof Parser) {
@@ -918,6 +910,206 @@ public class RestMethodContext extends BeanContext
implements Comparable<RestMet
return x;
}
+ /**
+ * Instantiates the serializers for this REST resource.
+ *
+ * <p>
+ * Instantiates based on the following logic:
+ * <ul>
+ * <li>Looks for {@link #REST_serializers} value set via any of
the following:
+ * <ul>
+ * <li>{@link
RestContextBuilder#serializers(Class...)}/{@link
RestContextBuilder#serializers(Serializer...)}
+ * <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 BeanFactory}
+ * <li>Any {@doc RestInjection injected beans}.
+ * </ul>
+ * <li>Resolves it via the bean factory registered in this context.
+ * <li>Instantiates a <c>Serializer[0]</c>.
+ * </ul>
+ *
+ * @param resource The REST resource object.
+ * @param beanFactory The bean factory to use for retrieving and
creating beans.
+ * @param ps The property store of this method.
+ * @return The serializers for this REST resource.
+ * @throws Exception If serializers could not be instantiated.
+ * @seealso #REST_serializers
+ */
+ protected SerializerGroup createSerializers(Object resource,
BeanFactory beanFactory, PropertyStore ps) throws Exception {
+ Object x = getArrayProperty(REST_serializers, Object.class);
+ if (x == null)
+ x = beanFactory.createBeanViaMethod(Serializer[].class,
resource, "createSerializers");
+ if (x == null)
+ x = beanFactory.createBeanViaMethod(Class[].class,
resource, "createSerializers");
+ if (x == null) {
+ x =
beanFactory.createBeanViaMethod(SerializerGroup.class, resource,
"createSerializers");
+ if (x != null)
+ return (SerializerGroup)x;
+ }
+ if (x == null)
+ x =
beanFactory.getBean(Serializer[].class).orElse(null);
+ if (x == null) {
+ x =
beanFactory.getBean(SerializerGroup.class).orElse(null);
+ if (x != null)
+ return (SerializerGroup)x;
+ }
+ if (x == null)
+ x = new Serializer[0];
+ return SerializerGroup
+ .create()
+ .append((Object[])x)
+ .apply(ps)
+ .build();
+ }
+
+ /**
+ * Instantiates the parsers for this REST resource.
+ *
+ * <p>
+ * Instantiates based on the following logic:
+ * <ul>
+ * <li>Looks for {@link #REST_parsers} value set via any of the
following:
+ * <ul>
+ * <li>{@link
RestContextBuilder#parsers(Class...)}/{@link
RestContextBuilder#parsers(Parser...)}
+ * <li>{@link Rest#parsers()}.
+ * </ul>
+ * <li>Looks for a static or non-static <c>createParsers()</>
method that returns <c>{@link Parser}[]</c> on the
+ * resource class with any of the following arguments:
+ * <ul>
+ * <li>{@link RestContext}
+ * <li>{@link BeanFactory}
+ * <li>Any {@doc RestInjection injected beans}.
+ * </ul>
+ * <li>Resolves it via the bean factory registered in this context.
+ * <li>Instantiates a <c>Parser[0]</c>.
+ * </ul>
+ *
+ * @param resource The REST resource object.
+ * @param beanFactory The bean factory to use for retrieving and
creating beans.
+ * @param ps The property store of this method.
+ * @return The parsers for this REST resource.
+ * @throws Exception If parsers could not be instantiated.
+ * @seealso #REST_parsers
+ */
+ protected ParserGroup createParsers(Object resource, BeanFactory
beanFactory, PropertyStore ps) throws Exception {
+ Object x = getArrayProperty(REST_parsers, Object.class);
+ if (x == null)
+ x = beanFactory.createBeanViaMethod(Parser[].class,
resource, "createParsers");
+ if (x == null)
+ x = beanFactory.createBeanViaMethod(Class[].class,
resource, "createParsers");
+ if (x == null) {
+ x = beanFactory.createBeanViaMethod(ParserGroup.class,
resource, "createParsers");
+ if (x != null)
+ return (ParserGroup)x;
+ }
+ if (x == null)
+ x = beanFactory.getBean(Parser[].class).orElse(null);
+ if (x == null) {
+ x = beanFactory.getBean(ParserGroup.class).orElse(null);
+ if (x != null)
+ return (ParserGroup)x;
+ }
+ if (x == null)
+ x = new Parser[0];
+ return ParserGroup
+ .create()
+ .append((Object[])x)
+ .apply(ps)
+ .build();
+ }
+
+ /**
+ * 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 {@link #REST_partSerializer} value set via any of
the following:
+ * <ul>
+ * <li>{@link
RestContextBuilder#partSerializer(Class)}/{@link
RestContextBuilder#partSerializer(HttpPartSerializer)}
+ * <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 BeanFactory}
+ * <li>Any {@doc RestInjection injected beans}.
+ * </ul>
+ * <li>Resolves it via the bean factory registered in this context.
+ * <li>Instantiates an {@link OpenApiSerializer}.
+ * </ul>
+ *
+ * @param resource The REST resource object.
+ * @param beanFactory The bean factory to use for retrieving and
creating beans.
+ * @return The HTTP part serializer for this REST resource.
+ * @throws Exception If serializer could not be instantiated.
+ * @seealso #REST_partSerializer
+ */
+ protected HttpPartSerializer createPartSerializer(Object resource,
BeanFactory beanFactory) throws Exception {
+ HttpPartSerializer x = null;
+ if (resource instanceof HttpPartSerializer)
+ x = (HttpPartSerializer)resource;
+ if (x == null)
+ x = getInstanceProperty(REST_partSerializer,
HttpPartSerializer.class, null, beanFactory);
+ if (x == null)
+ x =
beanFactory.createBeanViaMethod(HttpPartSerializer.class, resource,
"createPartSerializer");
+ if (x == null)
+ x =
beanFactory.getBean(HttpPartSerializer.class).orElse(null);
+ if (x == null)
+ x = new OpenApiSerializer(getPropertyStore());
+ return x;
+ }
+
+ /**
+ * 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 {@link #REST_partParser} value set via any of the
following:
+ * <ul>
+ * <li>{@link
RestContextBuilder#partParser(Class)}/{@link
RestContextBuilder#partParser(HttpPartParser)}
+ * <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 BeanFactory}
+ * <li>Any {@doc RestInjection injected beans}.
+ * </ul>
+ * <li>Resolves it via the bean factory registered in this context.
+ * <li>Instantiates an {@link OpenApiSerializer}.
+ * </ul>
+ *
+ * @param resource The REST resource object.
+ * @param beanFactory The bean factory to use for retrieving and
creating beans.
+ * @return The HTTP part parser for this REST resource.
+ * @throws Exception If parser could not be instantiated.
+ * @seealso #REST_partParser
+ */
+ protected HttpPartParser createPartParser(Object resource, BeanFactory
beanFactory) throws Exception {
+ HttpPartParser x = null;
+ if (resource instanceof HttpPartParser)
+ x = (HttpPartParser)resource;
+ if (x == null)
+ x = getInstanceProperty(REST_partParser,
HttpPartParser.class, null, beanFactory);
+ if (x == null)
+ x =
beanFactory.createBeanViaMethod(HttpPartParser.class, resource,
"createPartParser");
+ if (x == null)
+ x =
beanFactory.getBean(HttpPartParser.class).orElse(null);
+ if (x == null)
+ x = new OpenApiParser(getPropertyStore());
+ return x;
+ }
+
ResponsePartMeta getResponseHeaderMeta(Object o) {
if (o == null)
return null;