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

commit 54ea42fc9b069652f09232bc0edf107e322f1898
Author: JamesBognar <[email protected]>
AuthorDate: Mon Feb 1 12:39:33 2021 -0500

    REST refactoring.
---
 .../org/apache/juneau/cp/FileFinderBuilder.java    |  11 +-
 .../org/apache/juneau/rest/BasicStaticFiles.java   | 124 ++++++++++++++++-----
 .../java/org/apache/juneau/rest/RestContext.java   |  62 +++++++++--
 .../java/org/apache/juneau/rest/StaticFiles.java   |  85 +-------------
 .../org/apache/juneau/rest/StaticFilesBuilder.java |  23 ++--
 .../apache/juneau/rest/SwaggerProviderBuilder.java |  11 +-
 6 files changed, 184 insertions(+), 132 deletions(-)

diff --git 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/cp/FileFinderBuilder.java
 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/cp/FileFinderBuilder.java
index e539519..b95c290 100644
--- 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/cp/FileFinderBuilder.java
+++ 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/cp/FileFinderBuilder.java
@@ -42,7 +42,7 @@ public class FileFinderBuilder {
         */
        public FileFinder build() {
                try {
-                       Class<? extends FileFinder> ic = isConcrete(implClass) 
? implClass : BasicFileFinder.class;
+                       Class<? extends FileFinder> ic = isConcrete(implClass) 
? implClass : getDefaultImplClass();
                        return 
BeanFactory.of(beanFactory).addBeans(FileFinderBuilder.class, 
this).createBean(ic);
                } catch (ExecutableException e) {
                        throw new RuntimeException(e.getCause().getMessage(), 
e.getCause());
@@ -50,6 +50,15 @@ public class FileFinderBuilder {
        }
 
        /**
+        * Specifies the default implementation class if not specified via 
{@link #implClass(Class)}.
+        *
+        * @return The default implementation class if not specified via {@link 
#implClass(Class)}.
+        */
+       protected Class<? extends FileFinder> getDefaultImplClass() {
+               return BasicFileFinder.class;
+       }
+
+       /**
         * Adds a class subpackage to the lookup paths.
         *
         * @param c The class whose package will be added to the lookup paths.  
Must not be <jk>null</jk>.
diff --git 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/BasicStaticFiles.java
 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/BasicStaticFiles.java
index b3e091c..2bf53b9 100644
--- 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/BasicStaticFiles.java
+++ 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/BasicStaticFiles.java
@@ -12,46 +12,110 @@
 // 
***************************************************************************************************************************
 package org.apache.juneau.rest;
 
+import static org.apache.juneau.internal.FileUtils.*;
+import static org.apache.juneau.internal.ObjectUtils.*;
+
+import java.io.*;
+import java.util.*;
+
+import javax.activation.*;
+
+import org.apache.http.*;
+import org.apache.juneau.collections.*;
+import org.apache.juneau.cp.*;
+import org.apache.juneau.http.*;
+import org.apache.juneau.http.exception.*;
 import org.apache.juneau.http.header.*;
+import org.apache.juneau.internal.*;
 
 /**
- * Basic implementation of resource finder used for finding static files 
served up for REST resources.
+ * API for retrieving localized static files from either the classpath or file 
system.
  *
  * <p>
- * This implementation has the following attributes:
- * <ul>
- *     <li>Looks for files in the following locations:
- *             <li><js>"static"</js> working subdirectory.
- *             <li><js>"htdocs"</js> working subdirectory.
- *             <li><js>"htdocs"</js> subpackage from this class and all parent 
classes.
- *             <li><js>"htdocs"</js> root package from this class and all 
parent classes.
- *     </li>
- *     <li>Caches any files smaller than 1MB into memory.
- *     <li>Ignores any <js>".class"</js> or <js>".properties"</js> files found.
- *     <li>Adds header <js>"Cache-Control: max-age=86400, public"</js> to all 
resources.
- * </ul>
- *
- * <ul class='seealso'>
- *     <li class='link'>{@link RestContext#REST_staticFiles}
- * </ul>
+ * Provides the same functionality as {@link BasicFileFinder} but adds support 
for returning files as {@link BasicHttpResource}
+ * objects with arbitrary headers.
  */
-public class BasicStaticFiles extends StaticFiles {
+public class BasicStaticFiles extends BasicFileFinder implements StaticFiles {
+
+       private final Header[] headers;
+       private final MimetypesFileTypeMap mimeTypes;
+       private final int hashCode;
+
+       /**
+        * Creates a new builder for this object.
+        *
+        * @return A new builder for this object.
+        */
+       public static StaticFilesBuilder create() {
+               return new StaticFilesBuilder();
+       }
+
+       /**
+        * Constructor.
+        *
+        * @param builder The builder object.
+        */
+       public BasicStaticFiles(StaticFilesBuilder builder) {
+               super(builder);
+               this.headers = builder.headers.toArray(new 
Header[builder.headers.size()]);
+               this.mimeTypes = builder.mimeTypes;
+               this.hashCode = HashCode.of(hashCode(), headers);
+       }
 
        /**
         * Constructor.
         *
-        * @param context The context of the REST resource this finder belongs 
to.
+        * <p>
+        * Can be used when subclassing and overriding the {@link 
#resolve(String, Locale)} method.
+        */
+       protected BasicStaticFiles() {
+               super();
+               this.headers = new Header[0];
+               this.mimeTypes = null;
+               this.hashCode = HashCode.of(hashCode(), headers);
+       }
+
+       /**
+        * Resolve the specified path.
+        *
+        * <p>
+        * Subclasses can override this method to provide specialized handling.
+        *
+        * @param path The path to resolve to a static file.
+        * @param locale Optional locale.
+        * @return The resource, or <jk>null</jk> if not found.
         */
-       public BasicStaticFiles(RestContext context) {
-               super(StaticFiles
-                       .create()
-                       .dir("static")
-                       .dir("htdocs")
-                       .cp(context.getResourceClass(), "htdocs", true)
-                       .cp(context.getResourceClass(), "/htdocs", true)
-                       .caching(1_000_000)
-                       .exclude("(?i).*\\.(class|properties)")
-                       .headers(CacheControl.of("max-age=86400, public"))
-               );
+       @Override /* StaticFiles */
+       public Optional<BasicHttpResource> resolve(String path, Locale locale) {
+               try {
+                       Optional<InputStream> is = getStream(path);
+                       if (! is.isPresent())
+                               return Optional.empty();
+                       return Optional.of(
+                               BasicHttpResource
+                                       .of(is.get())
+                                       .header(ContentType.of(mimeTypes == 
null ? null : mimeTypes.getContentType(getFileName(path))))
+                                       .headers(headers)
+                       );
+               } catch (IOException e) {
+                       throw new InternalServerError(e);
+               }
+       }
+
+       @Override /* FileFinder */
+       public OMap toMap() {
+               return super.toMap()
+                       .a("headers", headers)
+               ;
+       }
+
+       @Override
+       public int hashCode() {
+               return hashCode;
+       }
+
+       @Override /* Object */
+       public boolean equals(Object o) {
+               return super.equals(o) && o instanceof BasicStaticFiles && 
eq(this, (BasicStaticFiles)o, (x,y)->eq(x.headers, y.headers));
        }
 }
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 f74c4a4..699b333 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
@@ -2913,7 +2913,7 @@ public class RestContext extends BeanContext {
         *      // and resolve methods for special handling of special cases 
and adds a Foo header to all requests.</jc>
         *      <jk>public class</jk> MyStaticFiles <jk>extends</jk> 
StaticFiles {
         *
-        *              <jk>public</jk> MyStaticFiles() {
+        *              <jk>public</jk> MyStaticFiles() <jk>extends</jk> 
BasicStaticFiles {
         *                      <jk>super</jk>(
         *                              <jk>new</jk> StaticFilesBuilder()
         *                                      .dir(<js>"/files"</js>)
@@ -3688,14 +3688,18 @@ public class RestContext extends BeanContext {
                if (resource instanceof FileFinder)
                        x = (FileFinder)resource;
 
-               if (x == null)
-                       x = getInstanceProperty(REST_fileFinder, 
FileFinder.class, null, beanFactory);
+               Object o = getProperty(REST_fileFinder);
+               if (o instanceof FileFinder)
+                       x = (FileFinder)o;
 
                if (x == null)
                        x = beanFactory.getBean(FileFinder.class).orElse(null);
 
-               if (x == null)
-                       x = getInstanceProperty(REST_fileFinderDefault, 
BasicFileFinder.class, null, beanFactory);
+               if (x == null) {
+                       o = getProperty(REST_fileFinderDefault);
+                       if (o instanceof FileFinder)
+                               x = (FileFinder)o;
+               }
 
                if (x == null)
                        x = createFileFinderBuilder(resource, 
beanFactory).build();
@@ -3722,10 +3726,28 @@ public class RestContext extends BeanContext {
         * @return The file finder builder for this REST resource.
         * @throws Exception If file finder builder could not be instantiated.
         */
+       @SuppressWarnings("unchecked")
        protected FileFinderBuilder createFileFinderBuilder(Object resource, 
BeanFactory beanFactory) throws Exception {
 
+               Class<? extends FileFinder> c = null;
+
+               Object o = getProperty(REST_fileFinder);
+               if (o instanceof Class)
+                       c = (Class<? extends FileFinder>)o;
+
+               if (c == null) {
+                       o = getProperty(REST_fileFinderDefault);
+                       if (o instanceof Class)
+                               c = (Class<? extends FileFinder>)o;
+               }
+
+               if (c == null)
+                       c = BasicFileFinder.class;
+
                FileFinderBuilder x = FileFinder
                        .create()
+                       .beanFactory(beanFactory)
+                       .implClass(c)
                        .dir("static")
                        .dir("htdocs")
                        .cp(getResourceClass(), "htdocs", true)
@@ -3785,17 +3807,19 @@ public class RestContext extends BeanContext {
                if (resource instanceof StaticFiles)
                        x = (StaticFiles)resource;
 
-               if (x == null)
-                       x = getInstanceProperty(REST_staticFiles, 
StaticFiles.class, null, beanFactory);
+               Object o = getProperty(REST_staticFiles);
+               if (o instanceof StaticFiles)
+                       x = (StaticFiles)o;
 
                if (x == null)
                        x = beanFactory.getBean(StaticFiles.class).orElse(null);
 
-               if (x == null)
-                       x = getInstanceProperty(REST_staticFilesDefault, 
StaticFiles.class, null, beanFactory);
+               o = getProperty(REST_staticFilesDefault);
+               if (o instanceof StaticFiles)
+                       x = (StaticFiles)o;
 
                if (x == null)
-                       x = createStaticFilesBuilder(resource, 
beanFactory).build();
+                       x = (StaticFiles)createStaticFilesBuilder(resource, 
beanFactory).build();
 
                x = BeanFactory
                        .of(beanFactory, resource)
@@ -3819,10 +3843,28 @@ public class RestContext extends BeanContext {
         * @return The static files builder for this REST resource.
         * @throws Exception If static files builder could not be instantiated.
         */
+       @SuppressWarnings("unchecked")
        protected StaticFilesBuilder createStaticFilesBuilder(Object resource, 
BeanFactory beanFactory) throws Exception {
 
+               Class<? extends StaticFiles> c = null;
+
+               Object o = getProperty(REST_staticFiles);
+               if (o instanceof Class)
+                       c = (Class<? extends StaticFiles>)o;
+
+               if (c == null) {
+                       o = getProperty(REST_staticFilesDefault);
+                       if (o instanceof Class)
+                               c = (Class<? extends StaticFiles>)o;
+               }
+
+               if (c == null)
+                       c = BasicStaticFiles.class;
+
                StaticFilesBuilder x = StaticFiles
                        .create()
+                       .beanFactory(beanFactory)
+                       .implClass((Class<? extends FileFinder>)c)
                        .dir("static")
                        .dir("htdocs")
                        .cp(getResourceClass(), "htdocs", true)
diff --git 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/StaticFiles.java
 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/StaticFiles.java
index 639b275..77cc3d8 100644
--- 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/StaticFiles.java
+++ 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/StaticFiles.java
@@ -12,37 +12,17 @@
 // 
***************************************************************************************************************************
 package org.apache.juneau.rest;
 
-import static org.apache.juneau.internal.FileUtils.*;
-import static org.apache.juneau.internal.ObjectUtils.*;
-
-import java.io.*;
 import java.util.*;
 
-import javax.activation.*;
-
-import org.apache.http.*;
-import org.apache.juneau.collections.*;
-import org.apache.juneau.cp.*;
 import org.apache.juneau.http.*;
-import org.apache.juneau.http.exception.*;
-import org.apache.juneau.http.header.*;
-import org.apache.juneau.internal.*;
 
 /**
  * API for retrieving localized static files from either the classpath or file 
system.
- *
- * <p>
- * Provides the same functionality as {@link BasicFileFinder} but adds support 
for returning files as {@link BasicHttpResource}
- * objects with arbitrary headers.
  */
-public class StaticFiles extends BasicFileFinder {
+public interface StaticFiles {
 
        /** Represents no static files */
-       public static final class Null extends StaticFiles {}
-
-       private final Header[] headers;
-       private final MimetypesFileTypeMap mimeTypes;
-       private final int hashCode;
+       public abstract class Null implements StaticFiles {}
 
        /**
         * Creates a new builder for this object.
@@ -54,70 +34,11 @@ public class StaticFiles extends BasicFileFinder {
        }
 
        /**
-        * Constructor.
-        *
-        * @param builder The builder object.
-        */
-       public StaticFiles(StaticFilesBuilder builder) {
-               super(builder);
-               this.headers = builder.headers.toArray(new 
Header[builder.headers.size()]);
-               this.mimeTypes = builder.mimeTypes;
-               this.hashCode = HashCode.of(hashCode(), headers);
-       }
-
-       /**
-        * Constructor.
-        *
-        * <p>
-        * Can be used when subclassing and overriding the {@link 
#resolve(String, Locale)} method.
-        */
-       public StaticFiles() {
-               super();
-               this.headers = new Header[0];
-               this.mimeTypes = null;
-               this.hashCode = HashCode.of(hashCode(), headers);
-       }
-
-       /**
         * Resolve the specified path.
         *
-        * <p>
-        * Subclasses can override this method to provide specialized handling.
-        *
         * @param path The path to resolve to a static file.
         * @param locale Optional locale.
         * @return The resource, or <jk>null</jk> if not found.
         */
-       public Optional<BasicHttpResource> resolve(String path, Locale locale) {
-               try {
-                       Optional<InputStream> is = getStream(path);
-                       if (! is.isPresent())
-                               return Optional.empty();
-                       return Optional.of(
-                               BasicHttpResource
-                                       .of(is.get())
-                                       .header(ContentType.of(mimeTypes == 
null ? null : mimeTypes.getContentType(getFileName(path))))
-                                       .headers(headers)
-                       );
-               } catch (IOException e) {
-                       throw new InternalServerError(e);
-               }
-       }
-
-       @Override /* FileFinder */
-       public OMap toMap() {
-               return super.toMap()
-                       .a("headers", headers)
-               ;
-       }
-
-       @Override
-       public int hashCode() {
-               return hashCode;
-       }
-
-       @Override /* Object */
-       public boolean equals(Object o) {
-               return super.equals(o) && o instanceof StaticFiles && eq(this, 
(StaticFiles)o, (x,y)->eq(x.headers, y.headers));
-       }
+       public Optional<BasicHttpResource> resolve(String path, Locale locale);
 }
diff --git 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/StaticFilesBuilder.java
 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/StaticFilesBuilder.java
index 3c23395..a8e73cb 100644
--- 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/StaticFilesBuilder.java
+++ 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/StaticFilesBuilder.java
@@ -24,21 +24,16 @@ import org.apache.juneau.internal.*;
 import org.apache.juneau.utils.*;
 
 /**
- * Builder class for {@link StaticFiles} objects.
+ * Builder class for {@link BasicStaticFiles} objects.
  */
 public class StaticFilesBuilder extends FileFinderBuilder {
 
        List<Header> headers = AList.create();
        MimetypesFileTypeMap mimeTypes = new ExtendedMimetypesFileTypeMap();
 
-       /**
-        * Create a new {@link StaticFiles} using this builder.
-        *
-        * @return A new {@link StaticFiles}
-        */
        @Override
-       public StaticFiles build() {
-               return new StaticFiles(this);
+       protected Class<? extends FileFinder> getDefaultImplClass() {
+               return BasicStaticFiles.class;
        }
 
        /**
@@ -118,5 +113,17 @@ public class StaticFilesBuilder extends FileFinderBuilder {
                return this;
        }
 
+       @Override
+       public StaticFilesBuilder implClass(Class<? extends FileFinder> value) {
+               super.implClass(value);
+               return this;
+       }
+
+       @Override
+       public StaticFilesBuilder beanFactory(BeanFactory value) {
+               super.beanFactory(value);
+               return this;
+       }
+
        // </FluentSetters>
 }
diff --git 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/SwaggerProviderBuilder.java
 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/SwaggerProviderBuilder.java
index faae720..d99693b 100644
--- 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/SwaggerProviderBuilder.java
+++ 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/SwaggerProviderBuilder.java
@@ -45,7 +45,7 @@ public class SwaggerProviderBuilder {
         */
        public SwaggerProvider build() {
                try {
-                       Class<? extends SwaggerProvider> ic = 
isConcrete(implClass) ? implClass : BasicSwaggerProvider.class;
+                       Class<? extends SwaggerProvider> ic = 
isConcrete(implClass) ? implClass : getDefaultImplClass();
                        return 
BeanFactory.of(beanFactory).addBeans(SwaggerProviderBuilder.class, 
this).createBean(ic);
                } catch (Exception e) {
                        throw toHttpException(e, InternalServerError.class);
@@ -53,6 +53,15 @@ public class SwaggerProviderBuilder {
        }
 
        /**
+        * Specifies the default implementation class if not specified via 
{@link #implClass(Class)}.
+        *
+        * @return The default implementation class if not specified via {@link 
#implClass(Class)}.
+        */
+       protected Class<? extends SwaggerProvider> getDefaultImplClass() {
+               return BasicSwaggerProvider.class;
+       }
+
+       /**
         * Specifies the bean factory to use for instantiating the {@link 
SwaggerProvider} object.
         *
         * @param value The new value for this setting.

Reply via email to