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 d813408  REST refactoring.
d813408 is described below

commit d813408fb20e4f41b1e4d44688b7aca770bf5bd2
Author: JamesBognar <[email protected]>
AuthorDate: Wed Jan 13 09:06:33 2021 -0500

    REST refactoring.
---
 .../java/org/apache/juneau/rest/RestContext.java   |   2 -
 .../org/apache/juneau/rest/RestMethodContext.java  | 119 +++++++++++++++------
 2 files changed, 87 insertions(+), 34 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 0b29710..621f508 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
@@ -2743,7 +2743,6 @@ public class RestContext extends BeanContext {
         * This affects the returned values from the following:
         * <ul class='javatree'>
         *      <li class='jm'>{@link RestContext#getProduces() 
RestContext.getProduces()}
-        *      <li class='jm'>{@link RestRequest#getProduces()}
         *      <li class='jm'>{@link RestInfoProvider#getSwagger(RestRequest)} 
- Affects produces field.
         * </ul>
         */
@@ -2809,7 +2808,6 @@ public class RestContext extends BeanContext {
         * This affects the returned values from the following:
         * <ul class='javatree'>
         *      <li class='jm'>{@link RestContext#getConsumes() 
RestContext.getConsumes()}
-        *      <li class='jm'>{@link RestRequest#getConsumes()}
         *      <li class='jm'>{@link RestInfoProvider#getSwagger(RestRequest)} 
- Affects consumes field.
         * </ul>
         */
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 a50bca3..ab1b125 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
@@ -589,6 +589,18 @@ public class RestMethodContext extends BeanContext 
implements Comparable<RestMet
                        requiredMatchers = matchers.stream().filter(x -> 
x.required()).toArray(RestMatcher[]::new);
                        optionalMatchers = matchers.stream().filter(x -> ! 
x.required()).toArray(RestMatcher[]::new);
 
+                       pathMatchers = createPathMatchers(r, beanFactory, 
b.dotAll);
+                       beanFactory.addBean(UrlPathMatcher[].class, 
pathMatchers);
+
+                       encoders = createEncoders(r, beanFactory);
+                       beanFactory.addBean(EncoderGroup.class, encoders);
+
+                       jsonSchemaGenerator = createJsonSchemaGenerator(r, 
beanFactory, ps);
+                       beanFactory.addBean(JsonSchemaGenerator.class, 
jsonSchemaGenerator);
+
+                       supportedAcceptTypes = getListProperty(REST_produces, 
MediaType.class, serializers.getSupportedMediaTypes());
+                       supportedContentTypes = getListProperty(REST_consumes, 
MediaType.class, parsers.getSupportedMediaTypes());
+
                        int _hierarchyDepth = 0;
                        Class<?> sc = 
b.method.getDeclaringClass().getSuperclass();
                        while (sc != null) {
@@ -610,30 +622,7 @@ public class RestMethodContext extends BeanContext 
implements Comparable<RestMet
 
                        responseMeta = ResponseBeanMeta.create(mi, ps);
 
-                       boolean dotAll = b.dotAll;
-                       List<UrlPathMatcher> _pathMatchers = new ArrayList<>();
-                       for (String p : getArrayProperty(RESTMETHOD_paths, 
String.class)) {
-                               if (dotAll && ! p.endsWith("/*"))
-                                       p += "/*";
-                               _pathMatchers.add(UrlPathMatcher.of(p));
-                       }
-                       if (_pathMatchers.isEmpty()) {
-                               String p = HttpUtils.detectHttpPath(method, 
true);
-                               if (dotAll && ! p.endsWith("/*"))
-                                       p += "/*";
-                               _pathMatchers.add(UrlPathMatcher.of(p));
-                       }
-                       pathMatchers = _pathMatchers.toArray(new 
UrlPathMatcher[_pathMatchers.size()]);
-
-                       methodParams = context.findParams(mi, false, 
this.pathMatchers[this.pathMatchers.length-1]);
-
-                       this.encoders = EncoderGroup
-                               .create()
-                               .append(IdentityEncoder.INSTANCE)
-                               .append(createEncoders(r, beanFactory))
-                               .build();
-
-                       this.jsonSchemaGenerator = 
JsonSchemaGenerator.create().apply(ps).build();
+                       methodParams = context.findParams(mi, false, 
pathMatchers[this.pathMatchers.length-1]);
 
                        Map<String,Object> _reqHeaders = new 
TreeMap<>(String.CASE_INSENSITIVE_ORDER);
                        
_reqHeaders.putAll(getMapProperty(RESTMETHOD_reqHeaders, Object.class));
@@ -689,9 +678,6 @@ public class RestMethodContext extends BeanContext 
implements Comparable<RestMet
 
                        this.priority = getIntegerProperty(RESTMETHOD_priority, 
0);
 
-                       this.supportedAcceptTypes = 
getListProperty(REST_produces, MediaType.class, 
serializers.getSupportedMediaTypes());
-                       this.supportedContentTypes = 
getListProperty(REST_consumes, MediaType.class, 
parsers.getSupportedMediaTypes());
-
                        this.debug = context.getDebug(method);
                        this.callLogger = context.getCallLogger();
                } catch (ServletException e) {
@@ -902,16 +888,24 @@ public class RestMethodContext extends BeanContext 
implements Comparable<RestMet
         * @throws Exception If encoders could not be instantiated.
         * @seealso #REST_encoders
         */
-       protected Encoder[] createEncoders(Object resource, BeanFactory 
beanFactory) throws Exception {
+       protected EncoderGroup createEncoders(Object resource, BeanFactory 
beanFactory) throws Exception {
                Encoder[] x = getInstanceArrayProperty(REST_encoders, 
Encoder.class, null, beanFactory);
                if (x == null)
                        x = beanFactory.getBean(Encoder[].class).orElse(null);
                if (x == null)
                        x = new Encoder[0];
-               x = BeanFactory.of(beanFactory, resource)
-                       .addBean(Encoder[].class, x)
-                       .createBeanViaMethod(Encoder[].class, resource, 
"createEncoders", x);
-               return x;
+
+               EncoderGroup g = EncoderGroup
+                       .create()
+                       .append(IdentityEncoder.INSTANCE)
+                       .append(x)
+                       .build();
+
+               g = BeanFactory.of(beanFactory, resource)
+                       .addBean(EncoderGroup.class, g)
+                       .createBeanViaMethod(EncoderGroup.class, resource, 
"createEncoders", g);
+
+               return g;
        }
 
        /**
@@ -1122,6 +1116,67 @@ public class RestMethodContext extends BeanContext 
implements Comparable<RestMet
                return x;
        }
 
+       /**
+        * Instantiates the path matchers for this method.
+        *
+        * @param resource The REST resource object.
+        * @param beanFactory The bean factory to use for retrieving and 
creating beans.
+        * @param dotAll If {@link RestMethodContextBuilder#dotAll()} was 
specified.
+        * @return The HTTP part parser for this REST resource.
+        * @throws Exception If parser could not be instantiated.
+        * @seealso #RESTMETHOD_paths
+        */
+       protected UrlPathMatcher[] createPathMatchers(Object resource, 
BeanFactory beanFactory, boolean dotAll) throws Exception {
+               List<UrlPathMatcher> x = AList.of();
+               for (String p : getArrayProperty(RESTMETHOD_paths, 
String.class)) {
+                       if (dotAll && ! p.endsWith("/*"))
+                               p += "/*";
+                       x.add(UrlPathMatcher.of(p));
+               }
+               if (x.isEmpty()) {
+                       String p = HttpUtils.detectHttpPath(method, true);
+                       if (dotAll && ! p.endsWith("/*"))
+                               p += "/*";
+                       x.add(UrlPathMatcher.of(p));
+               }
+               UrlPathMatcher[] x2 = x.toArray(new UrlPathMatcher[x.size()]);;
+
+               x2 = BeanFactory.of(beanFactory, resource)
+                       .addBean(UrlPathMatcher[].class, x2)
+                       .createBeanViaMethod(UrlPathMatcher[].class, resource, 
"createPathMatchers", x2, Method.class);
+
+               return x2;
+       }
+
+       /**
+        * Instantiates the JSON-schema generator for this method.
+        *
+        * @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 JSON-schema generator for this method.
+        * @throws Exception If schema generator could not be instantiated.
+        */
+       protected JsonSchemaGenerator createJsonSchemaGenerator(Object 
resource, BeanFactory beanFactory, PropertyStore ps) throws Exception {
+               JsonSchemaGenerator x = null;
+               if (resource instanceof JsonSchemaGenerator)
+                       x = (JsonSchemaGenerator)resource;
+               if (x == null)
+                       x = 
beanFactory.getBean(JsonSchemaGenerator.class).orElse(null);
+               if (x == null)
+                       x = JsonSchemaGenerator.create().apply(ps).build();
+
+               x = BeanFactory.of(beanFactory, resource)
+                       .addBean(JsonSchemaGenerator.class, x)
+                       .createBeanViaMethod(JsonSchemaGenerator.class, 
resource, "createJsonSchemaGenerator", x, Method.class);
+               x = BeanFactory.of(beanFactory, resource)
+                       .addBean(JsonSchemaGenerator.class, x)
+                       .createBeanViaMethod(JsonSchemaGenerator.class, 
resource, "createJsonSchemaGenerator", x);
+
+               return x;
+       }
+
+
        ResponsePartMeta getResponseHeaderMeta(Object o) {
                if (o == null)
                        return null;

Reply via email to