You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ra...@apache.org on 2020/04/07 09:34:52 UTC

[sling-org-apache-sling-scripting-bundle-tracker] branch master updated: SLING-9339 - Implement lazy-reading for the request data

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

radu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-bundle-tracker.git


The following commit(s) were added to refs/heads/master by this push:
     new b978bda  SLING-9339 - Implement lazy-reading for the request data
b978bda is described below

commit b978bdabc14585e19acfd91400945f61a6dc0904
Author: Radu Cotescu <ra...@apache.org>
AuthorDate: Tue Apr 7 11:30:55 2020 +0200

    SLING-9339 - Implement lazy-reading for the request data
    
    * added an OnDemandReader
---
 .../tracker/internal/BundledScriptServlet.java     |  2 +-
 .../tracker/internal/request/OnDemandReader.java   | 97 ++++++++++++++++++++++
 .../internal/{ => request}/RequestWrapper.java     | 17 +++-
 3 files changed, 112 insertions(+), 4 deletions(-)

diff --git a/src/main/java/org/apache/sling/scripting/bundle/tracker/internal/BundledScriptServlet.java b/src/main/java/org/apache/sling/scripting/bundle/tracker/internal/BundledScriptServlet.java
index e9e7b8e..d3270d0 100644
--- a/src/main/java/org/apache/sling/scripting/bundle/tracker/internal/BundledScriptServlet.java
+++ b/src/main/java/org/apache/sling/scripting/bundle/tracker/internal/BundledScriptServlet.java
@@ -32,11 +32,11 @@ import javax.servlet.ServletResponse;
 import org.apache.sling.api.SlingConstants;
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.scripting.bundle.tracker.internal.request.RequestWrapper;
 import org.jetbrains.annotations.NotNull;
 
 class BundledScriptServlet extends GenericServlet {
 
-
     private final ScriptContextProvider scriptContextProvider;
     private final LinkedHashSet<TypeProvider> wiredTypeProviders;
     private final Executable executable;
diff --git a/src/main/java/org/apache/sling/scripting/bundle/tracker/internal/request/OnDemandReader.java b/src/main/java/org/apache/sling/scripting/bundle/tracker/internal/request/OnDemandReader.java
new file mode 100644
index 0000000..3df25cf
--- /dev/null
+++ b/src/main/java/org/apache/sling/scripting/bundle/tracker/internal/request/OnDemandReader.java
@@ -0,0 +1,97 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements.  See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership.  The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License.  You may obtain a copy of the License at
+ ~
+ ~   http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied.  See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+package org.apache.sling.scripting.bundle.tracker.internal.request;
+
+import java.io.IOException;
+import java.io.Reader;
+import java.nio.CharBuffer;
+
+import javax.servlet.ServletRequest;
+
+class OnDemandReader extends Reader {
+
+    private final ServletRequest request;
+
+    private Reader delegatee;
+
+    OnDemandReader(ServletRequest request) {
+        this.request = request;
+    }
+
+    @Override
+    public void close() throws IOException {
+        if (delegatee != null) {
+            delegatee.close();
+        }
+    }
+
+    @Override
+    public void mark(int readAheadLimit) throws IOException {
+        getReader().mark(readAheadLimit);
+    }
+
+    @Override
+    public boolean markSupported() {
+        return (delegatee != null) ? delegatee.markSupported() : false;
+    }
+
+    @Override
+    public int read() throws IOException {
+        return getReader().read();
+    }
+
+    @Override
+    public int read(char[] cbuf, int off, int len) throws IOException {
+        return getReader().read(cbuf, off, len);
+    }
+
+    @Override
+    public int read(char[] cbuf) throws IOException {
+        return getReader().read(cbuf);
+    }
+
+    @Override
+    public int read(CharBuffer target) throws IOException {
+        return getReader().read(target);
+    }
+
+    @Override
+    public boolean ready() throws IOException {
+        return getReader().ready();
+    }
+
+    @Override
+    public void reset() throws IOException {
+        if (delegatee != null) {
+            delegatee.reset();
+        }
+    }
+
+    @Override
+    public long skip(long n) throws IOException {
+        return getReader().skip(n);
+    }
+
+    private Reader getReader() throws IOException {
+        if (delegatee == null) {
+            delegatee = request.getReader();
+        }
+        return delegatee;
+    }
+}
diff --git a/src/main/java/org/apache/sling/scripting/bundle/tracker/internal/RequestWrapper.java b/src/main/java/org/apache/sling/scripting/bundle/tracker/internal/request/RequestWrapper.java
similarity index 86%
rename from src/main/java/org/apache/sling/scripting/bundle/tracker/internal/RequestWrapper.java
rename to src/main/java/org/apache/sling/scripting/bundle/tracker/internal/request/RequestWrapper.java
index 57ebfea..1039eec 100644
--- a/src/main/java/org/apache/sling/scripting/bundle/tracker/internal/RequestWrapper.java
+++ b/src/main/java/org/apache/sling/scripting/bundle/tracker/internal/request/RequestWrapper.java
@@ -16,8 +16,10 @@
  ~ specific language governing permissions and limitations
  ~ under the License.
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
-package org.apache.sling.scripting.bundle.tracker.internal;
+package org.apache.sling.scripting.bundle.tracker.internal.request;
 
+import java.io.BufferedReader;
+import java.io.IOException;
 import java.util.Set;
 
 import javax.servlet.RequestDispatcher;
@@ -29,11 +31,12 @@ import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.wrappers.SlingHttpServletRequestWrapper;
 import org.apache.sling.scripting.bundle.tracker.ResourceType;
 
-class RequestWrapper extends SlingHttpServletRequestWrapper {
+public class RequestWrapper extends SlingHttpServletRequestWrapper {
 
     private final Set<ResourceType> wiredResourceTypes;
+    private BufferedReader reader;
 
-    RequestWrapper(SlingHttpServletRequest wrappedRequest, Set<ResourceType> wiredResourceTypes) {
+    public RequestWrapper(SlingHttpServletRequest wrappedRequest, Set<ResourceType> wiredResourceTypes) {
         super(wrappedRequest);
         this.wiredResourceTypes = wiredResourceTypes;
     }
@@ -59,6 +62,14 @@ class RequestWrapper extends SlingHttpServletRequestWrapper {
         return super.getRequestDispatcher(path, processedOptions);
     }
 
+    @Override
+    public BufferedReader getReader() {
+        if (reader == null) {
+            reader = new BufferedReader(new OnDemandReader(getRequest()));
+        }
+        return reader;
+    }
+
     private RequestDispatcherOptions processOptions(RequestDispatcherOptions options) {
         if (options != null) {
             RequestDispatcherOptions requestDispatcherOptions = new RequestDispatcherOptions();