This is an automated email from the ASF dual-hosted git repository.

thiagohp pushed a commit to branch rest
in repository https://gitbox.apache.org/repos/asf/tapestry-5.git


The following commit(s) were added to refs/heads/rest by this push:
     new bd5b1f4  TAP5-2696: caching in OpenAPI dispatcher when production mode 
is on
bd5b1f4 is described below

commit bd5b1f4b8be1d3c8714fa721ea6691218f5acf3b
Author: Thiago H. de Paula Figueiredo <thi...@arsmachina.com.br>
AuthorDate: Wed Nov 3 08:37:14 2021 -0300

    TAP5-2696: caching in OpenAPI dispatcher when production mode is on
---
 .../services/OpenApiDescriptionDispatcher.java     | 34 ++++++++++++++++++----
 1 file changed, 28 insertions(+), 6 deletions(-)

diff --git 
a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/OpenApiDescriptionDispatcher.java
 
b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/OpenApiDescriptionDispatcher.java
index a3e42de..fc2f848 100644
--- 
a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/OpenApiDescriptionDispatcher.java
+++ 
b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/OpenApiDescriptionDispatcher.java
@@ -36,12 +36,16 @@ public class OpenApiDescriptionDispatcher implements 
Dispatcher
     
     private final String path;
     private final OpenApiDescriptionGenerator openApiDescriptionGenerator;
+    private byte[] cachedDescription;
+    private final boolean productionMode;
     
     public 
OpenApiDescriptionDispatcher(@Symbol(SymbolConstants.OPENAPI_DESCRIPTION_PATH) 
final String path,
-            final OpenApiDescriptionGenerator openApiDescriptionGenerator)
+            final OpenApiDescriptionGenerator openApiDescriptionGenerator,
+            final @Symbol(SymbolConstants.PRODUCTION_MODE) boolean 
productionMode)
     {
         this.path = path;
         this.openApiDescriptionGenerator = openApiDescriptionGenerator;
+        this.productionMode = productionMode;
     }
     
     public boolean dispatch(Request request, Response response) throws 
IOException
@@ -49,14 +53,32 @@ public class OpenApiDescriptionDispatcher implements 
Dispatcher
         boolean dispatched = false;
         if (path.equals(request.getPath()))
         {
-            final byte[] definition = openApiDescriptionGenerator
+            final byte[] description = getDescriptionAsByteArray();
+            response.setContentLength(description.length);
+            response.getOutputStream("application/json").write(description);
+            dispatched = true;
+        }
+        return dispatched;
+    }
+
+    private byte[] getDescriptionAsByteArray() 
+    {
+        byte[] bytes;
+        if (productionMode && cachedDescription != null)
+        {
+            bytes = cachedDescription;
+        }
+        else
+        {
+            bytes = openApiDescriptionGenerator
                     .generate(new JSONObject())
                     .toCompactString()
                     .getBytes(Charset.forName("UTF-8"));
-            response.setContentLength(definition.length);
-            response.getOutputStream("application/json").write(definition);
-            dispatched = true;
+            if (productionMode)
+            {
+                cachedDescription = bytes;
+            }
         }
-        return dispatched;
+        return bytes;
     }
 }

Reply via email to