Wrapped requests to provide correct mapping to wired resource types
diff --git a/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptServlet.java b/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptServlet.java
index 24a433a..0bd91a6 100644
--- a/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptServlet.java
+++ b/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptServlet.java
@@ -20,14 +20,15 @@
 
 import java.io.IOException;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Map;
+import java.util.Set;
 import java.util.concurrent.locks.ReadWriteLock;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 
 import javax.script.ScriptContext;
 import javax.script.ScriptException;
 import javax.servlet.GenericServlet;
-import javax.servlet.RequestDispatcher;
 import javax.servlet.ServletException;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
@@ -37,15 +38,16 @@
 import org.apache.sling.api.SlingConstants;
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.SlingHttpServletResponse;
-import org.apache.sling.api.request.RequestDispatcherOptions;
 import org.apache.sling.api.request.RequestPathInfo;
 import org.apache.sling.api.scripting.ScriptEvaluationException;
 import org.osgi.framework.Bundle;
-import org.osgi.framework.wiring.BundleCapability;
+import org.osgi.framework.Version;
 import org.osgi.framework.wiring.BundleWiring;
 
 class BundledScriptServlet extends GenericServlet {
 
+
+
     private final Bundle m_bundle;
     private final BundledScriptFinder m_bundledScriptFinder;
     private final ScriptContextProvider m_scriptContextProvider;
@@ -110,7 +112,8 @@
                 lock.readLock().unlock();
             }
             if (script != null) {
-                ScriptContext scriptContext = m_scriptContextProvider.prepareScriptContext(request, response, script);
+                RequestWrapper requestWrapper = new RequestWrapper(request, getWiredResourceTypes());
+                ScriptContext scriptContext = m_scriptContextProvider.prepareScriptContext(requestWrapper, response, script);
                 try {
                     script.eval(scriptContext);
                 } catch (ScriptException se) {
@@ -132,4 +135,28 @@
         return request.getResource().getResourceType() + (StringUtils.isNotEmpty(selectorString) ? ":" + selectorString : "") +
                 (StringUtils.isNotEmpty(requestExtension) ? ":" + requestExtension : "");
     }
+
+    private Set<String> getWiredResourceTypes() {
+        Set<String> wiredResourceTypes = new HashSet<>();
+        BundleWiring bundleWiring = m_bundle.adapt(BundleWiring.class);
+        bundleWiring.getProvidedWires(BundledScriptTracker.NS_SLING_RESOURCE_TYPE).forEach(
+                bundleWire -> {
+                    String resourceType = (String) bundleWire.getCapability().getAttributes().get(BundledScriptTracker
+                            .NS_SLING_RESOURCE_TYPE);
+                    Version version = (Version) bundleWire.getCapability().getAttributes().get(BundledScriptTracker
+                            .AT_VERSION);
+                    wiredResourceTypes.add(resourceType + (version == null ? "" : "/" + version.toString()));
+                }
+        );
+        bundleWiring.getRequiredWires(BundledScriptTracker.NS_SLING_RESOURCE_TYPE).forEach(
+                bundleWire -> {
+                    String resourceType = (String) bundleWire.getCapability().getAttributes().get(BundledScriptTracker
+                            .NS_SLING_RESOURCE_TYPE);
+                    Version version = (Version) bundleWire.getCapability().getAttributes().get(BundledScriptTracker
+                            .AT_VERSION);
+                    wiredResourceTypes.add(resourceType + (version == null ? "" : "/" + version.toString()));
+                }
+        );
+        return wiredResourceTypes;
+    }
 }
diff --git a/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptTracker.java b/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptTracker.java
index d9fead3..406a225 100644
--- a/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptTracker.java
+++ b/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptTracker.java
@@ -61,11 +61,10 @@
     static final String NS_SLING_SCRIPTING_EXTENDER = "sling.scripting";
     static final String NS_SLING_RESOURCE_TYPE = "sling.resourceType";
     private static final Logger LOGGER = LoggerFactory.getLogger(BundledScriptTracker.class);
-    private static final String AT_SLING_RESOURCE_TYPE_ONLY = "sling.resourceType.standalone";
     private static final String AT_SLING_SELECTORS = "sling.resourceType.selectors";
     private static final String AT_SLING_EXTENSIONS = "sling.resourceType.extensions";
 
-    private static final String AT_VERSION = "version";
+    static final String AT_VERSION = "version";
     private static final String AT_EXTENDS = "extends";
 
     @Reference
diff --git a/main/java/org/apache/sling/scripting/resolver/internal/RequestWrapper.java b/main/java/org/apache/sling/scripting/resolver/internal/RequestWrapper.java
new file mode 100644
index 0000000..f1ad7c0
--- /dev/null
+++ b/main/java/org/apache/sling/scripting/resolver/internal/RequestWrapper.java
@@ -0,0 +1,78 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ 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.resolver.internal;
+
+import java.util.Set;
+
+import javax.servlet.RequestDispatcher;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.request.RequestDispatcherOptions;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.wrappers.SlingHttpServletRequestWrapper;
+
+class RequestWrapper extends SlingHttpServletRequestWrapper {
+
+    private final Set<String> wiredResourceTypes;
+
+    RequestWrapper(SlingHttpServletRequest wrappedRequest, Set<String> wiredResourceTypes) {
+        super(wrappedRequest);
+        this.wiredResourceTypes = wiredResourceTypes;
+    }
+
+    @Override
+    public RequestDispatcher getRequestDispatcher(Resource resource, RequestDispatcherOptions options) {
+        if (resource == null) {
+            return null;
+        }
+        RequestDispatcherOptions processedOptions = processOptions(options);
+        return super.getRequestDispatcher(resource, processedOptions);
+    }
+
+    @Override
+    public RequestDispatcher getRequestDispatcher(String path, RequestDispatcherOptions options) {
+        if (path == null) {
+            return null;
+        }
+        RequestDispatcherOptions processedOptions = processOptions(options);
+        return super.getRequestDispatcher(path, processedOptions);
+    }
+
+    private RequestDispatcherOptions processOptions(RequestDispatcherOptions options) {
+        if (options != null) {
+            RequestDispatcherOptions requestDispatcherOptions = new RequestDispatcherOptions();
+            requestDispatcherOptions.setForceResourceType(options.getForceResourceType());
+            requestDispatcherOptions.setAddSelectors(options.getAddSelectors());
+            requestDispatcherOptions.setReplaceSelectors(options.getReplaceSelectors());
+            requestDispatcherOptions.setReplaceSuffix(options.getReplaceSuffix());
+            String forcedResourceType = options.getForceResourceType();
+            if (StringUtils.isNotEmpty(forcedResourceType)) {
+                for (String wiredResourceType : wiredResourceTypes) {
+                    if (wiredResourceType.startsWith(forcedResourceType + "/")) {
+                        requestDispatcherOptions.setForceResourceType(wiredResourceType);
+                        break;
+                    }
+                }
+            }
+            return requestDispatcherOptions;
+        }
+        return null;
+    }
+}