Merge pull request #1 from JEBailey/master

SLING-7169 consolidate configuration to DefaultGetServlet
diff --git a/src/main/java/org/apache/sling/servlets/get/impl/DefaultGetServlet.java b/src/main/java/org/apache/sling/servlets/get/impl/DefaultGetServlet.java
index db71b65..e045a4b 100644
--- a/src/main/java/org/apache/sling/servlets/get/impl/DefaultGetServlet.java
+++ b/src/main/java/org/apache/sling/servlets/get/impl/DefaultGetServlet.java
@@ -133,8 +133,6 @@
 
     private Map<String, Servlet> rendererMap = new HashMap<>();
 
-    private Servlet streamerServlet;
-
     private int jsonMaximumResults;
 
     /** Additional aliases. */
@@ -156,6 +154,16 @@
 
     @Reference(policyOption = ReferencePolicyOption.GREEDY)
     private XSSAPI xssApi;
+    
+    public static final String EXT_HTML = "html";
+
+    public static final String EXT_TXT = "txt";
+
+    public static final String EXT_JSON = "json";
+
+    public static final String EXT_XML = "xml";
+
+    public static final String EXT_RES = "res";
 
     @Activate
     protected void activate(Config cfg) {
@@ -182,15 +190,15 @@
 
     private Servlet getDefaultRendererServlet(final String type) {
         Servlet servlet = null;
-        if ( StreamRendererServlet.EXT_RES.equals(type) ) {
+        if ( EXT_RES.equals(type) ) {
             servlet = new StreamRendererServlet(index, indexFiles);
-        } else if ( HtmlRendererServlet.EXT_HTML.equals(type) ) {
+        } else if ( EXT_HTML.equals(type) ) {
             servlet = new HtmlRendererServlet(xssApi);
-        } else if ( PlainTextRendererServlet.EXT_TXT.equals(type) ) {
+        } else if ( EXT_TXT.equals(type) ) {
             servlet = new PlainTextRendererServlet();
-        } else if (JsonRendererServlet.EXT_JSON.equals(type) ) {
+        } else if (EXT_JSON.equals(type) ) {
             servlet = new JsonRendererServlet(jsonMaximumResults);
-        } else if ( XMLRendererServlet.EXT_XML.equals(type) ) {
+        } else if ( EXT_XML.equals(type) ) {
             try {
                 servlet = new XMLRendererServlet();
             } catch (Throwable t) {
@@ -215,29 +223,30 @@
 
         // use the servlet for rendering StreamRendererServlet.EXT_RES as the
         // streamer servlet
-        streamerServlet = getDefaultRendererServlet(StreamRendererServlet.EXT_RES);
+        Servlet streamerServlet = getDefaultRendererServlet(EXT_RES);
 
-        // Register renderer servlets
-        rendererMap.put(StreamRendererServlet.EXT_RES, streamerServlet);
-
+        rendererMap.put(null, streamerServlet);
+        
+        rendererMap.put(EXT_RES, streamerServlet);
+        
         if (enableHtml) {
-            rendererMap.put(HtmlRendererServlet.EXT_HTML,
-                    getDefaultRendererServlet(HtmlRendererServlet.EXT_HTML));
+            rendererMap.put(EXT_HTML,
+                    getDefaultRendererServlet(EXT_HTML));
         }
 
         if (enableTxt) {
-            rendererMap.put(PlainTextRendererServlet.EXT_TXT,
-                    getDefaultRendererServlet(PlainTextRendererServlet.EXT_TXT));
+            rendererMap.put(EXT_TXT,
+                    getDefaultRendererServlet(EXT_TXT));
         }
 
         if (enableJson) {
-            rendererMap.put(JsonRendererServlet.EXT_JSON,
-                    getDefaultRendererServlet(JsonRendererServlet.EXT_JSON));
+            rendererMap.put(EXT_JSON,
+                    getDefaultRendererServlet(EXT_JSON));
         }
 
         if (enableXml) {
-            rendererMap.put(XMLRendererServlet.EXT_XML,
-                    getDefaultRendererServlet(XMLRendererServlet.EXT_XML));
+            rendererMap.put(EXT_XML,
+                    getDefaultRendererServlet(EXT_XML));
         }
 
 
@@ -284,11 +293,7 @@
 
         Servlet rendererServlet;
         String ext = request.getRequestPathInfo().getExtension();
-        if (ext == null) {
-            rendererServlet = streamerServlet;
-        } else {
-            rendererServlet = rendererMap.get(ext);
-        }
+        rendererServlet = rendererMap.get(ext);
 
         // fail if we should not just stream or we cannot support the ext.
         if (rendererServlet == null) {
@@ -334,7 +339,6 @@
             }
         }
 
-        streamerServlet = null;
         rendererMap.clear();
 
         super.destroy();
diff --git a/src/main/java/org/apache/sling/servlets/get/impl/RedirectServlet.java b/src/main/java/org/apache/sling/servlets/get/impl/RedirectServlet.java
index b1d612b..bec72dc 100644
--- a/src/main/java/org/apache/sling/servlets/get/impl/RedirectServlet.java
+++ b/src/main/java/org/apache/sling/servlets/get/impl/RedirectServlet.java
@@ -32,12 +32,8 @@
 import org.apache.sling.api.resource.ResourceUtil;
 import org.apache.sling.api.resource.ValueMap;
 import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
-import org.apache.sling.servlets.get.impl.helpers.JsonRendererServlet;
-import org.osgi.service.component.annotations.Activate;
 import org.osgi.service.component.annotations.Component;
-import org.osgi.service.metatype.annotations.AttributeDefinition;
-import org.osgi.service.metatype.annotations.Designate;
-import org.osgi.service.metatype.annotations.ObjectClassDefinition;
+import org.osgi.service.component.annotations.Reference;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -57,6 +53,9 @@
  * status is sent where the target is the relative URL from the current resource
  * to the target resource. Selectors, extension, suffix and query string are
  * also appended to the redirect URL.
+ * <p>
+ * The exception to this process are GET requests that have a JSON extension.
+ * Those requests are handed off to the DefaultGetServlet for handling
  */
 @SuppressWarnings("serial")
 @Component(service = Servlet.class,
@@ -67,21 +66,8 @@
             "sling.servlet.methods=GET",
             "sling.servlet.prefix:Integer=-1"
     })
-@Designate(ocd = RedirectServlet.Config.class)
 public class RedirectServlet extends SlingSafeMethodsServlet {
 
-    @ObjectClassDefinition(name="Apache Sling Redirect Servlet",
-            description="The Sling servlet handling redirect resources.")
-    public @interface Config {
-
-        @AttributeDefinition(name = "JSON Max results",
-                description = "The maximum number of resources that should " +
-                  "be returned when doing a node.5.json or node.infinity.json. In JSON terms " +
-                  "this basically means the number of Objects to return. Default value is " +
-                  "200.")
-        int json_maximumresults() default 200;
-    }
-
     /** The name of the target property */
     public static final String TARGET_PROP = "sling:target";
 
@@ -91,16 +77,8 @@
     /** default log */
     private final Logger log = LoggerFactory.getLogger(getClass());
 
-    private Servlet jsonRendererServlet;
-
-    private int jsonMaximumResults;
-
-    @Activate
-    protected void activate(Config cfg) {
-      this.jsonMaximumResults = cfg.json_maximumresults();
-      // When the maximumResults get updated, we force a reset for the jsonRendererServlet.
-      jsonRendererServlet = getJsonRendererServlet();
-    }
+    @Reference(target="(component.name=org.apache.sling.servlets.get.DefaultGetServlet)")
+    private Servlet defaultRenderServlet;
 
     @Override
     protected void doGet(SlingHttpServletRequest request,
@@ -108,8 +86,8 @@
             IOException {
 
         // handle json export of the redirect node
-        if (JsonRendererServlet.EXT_JSON.equals(request.getRequestPathInfo().getExtension())) {
-            getJsonRendererServlet().service(request, response);
+        if (DefaultGetServlet.EXT_JSON.equals(request.getRequestPathInfo().getExtension())) {
+            defaultRenderServlet.service(request, response);
             return;
         }
 
@@ -308,19 +286,6 @@
         return absUriBuilder.toString();
     }
 
-    private Servlet getJsonRendererServlet() {
-        if (jsonRendererServlet == null) {
-            Servlet jrs = new JsonRendererServlet(jsonMaximumResults);
-            try {
-                jrs.init(getServletConfig());
-            } catch (Exception e) {
-                // don't care too much here
-            }
-            jsonRendererServlet = jrs;
-        }
-        return jsonRendererServlet;
-    }
-
     /**
      * Returns <code>true</code> if the path is potentially an URL. This
      * checks whether the path starts with a scheme followed by a colon
diff --git a/src/main/java/org/apache/sling/servlets/get/impl/helpers/HtmlRendererServlet.java b/src/main/java/org/apache/sling/servlets/get/impl/helpers/HtmlRendererServlet.java
index c018427..8168e13 100644
--- a/src/main/java/org/apache/sling/servlets/get/impl/helpers/HtmlRendererServlet.java
+++ b/src/main/java/org/apache/sling/servlets/get/impl/helpers/HtmlRendererServlet.java
@@ -40,7 +40,7 @@
 
     private static final long serialVersionUID = -5815904221043005085L;
 
-    public static final String EXT_HTML = "html";
+
 
     private final XSSAPI xssApi;
 
diff --git a/src/main/java/org/apache/sling/servlets/get/impl/helpers/JsonRendererServlet.java b/src/main/java/org/apache/sling/servlets/get/impl/helpers/JsonRendererServlet.java
index 253df1e..7a436d7 100644
--- a/src/main/java/org/apache/sling/servlets/get/impl/helpers/JsonRendererServlet.java
+++ b/src/main/java/org/apache/sling/servlets/get/impl/helpers/JsonRendererServlet.java
@@ -46,8 +46,6 @@
 
     private static final long serialVersionUID = 5577121546674133317L;
 
-    public static final String EXT_JSON = "json";
-
     /** Recursion level selector that means "all levels" */
     public static final String INFINITY = "infinity";
 
diff --git a/src/main/java/org/apache/sling/servlets/get/impl/helpers/PlainTextRendererServlet.java b/src/main/java/org/apache/sling/servlets/get/impl/helpers/PlainTextRendererServlet.java
index 6a919a1..d6a7e28 100644
--- a/src/main/java/org/apache/sling/servlets/get/impl/helpers/PlainTextRendererServlet.java
+++ b/src/main/java/org/apache/sling/servlets/get/impl/helpers/PlainTextRendererServlet.java
@@ -40,8 +40,6 @@
 
     private static final long serialVersionUID = -5815904221043005085L;
 
-    public static final String EXT_TXT = "txt";
-
     @Override
     protected void doGet(SlingHttpServletRequest req,
             SlingHttpServletResponse resp) throws ServletException, IOException {
diff --git a/src/main/java/org/apache/sling/servlets/get/impl/helpers/StreamRendererServlet.java b/src/main/java/org/apache/sling/servlets/get/impl/helpers/StreamRendererServlet.java
index 17a4469..08e04e2 100644
--- a/src/main/java/org/apache/sling/servlets/get/impl/helpers/StreamRendererServlet.java
+++ b/src/main/java/org/apache/sling/servlets/get/impl/helpers/StreamRendererServlet.java
@@ -51,6 +51,7 @@
 import org.apache.sling.api.resource.external.ExternalizableInputStream;
 import org.apache.sling.api.servlets.HttpConstants;
 import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
+import org.apache.sling.servlets.get.impl.DefaultGetServlet;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -63,8 +64,6 @@
  */
 public class StreamRendererServlet extends SlingSafeMethodsServlet {
 
-    public static final String EXT_RES = "res";
-
     private static final long serialVersionUID = -1L;
 
     /**
@@ -125,7 +124,7 @@
 
         // ensure no extension or "res"
         String ext = request.getRequestPathInfo().getExtension();
-        if (ext != null && !ext.equals(EXT_RES)) {
+        if (ext != null && !ext.equals(DefaultGetServlet.EXT_RES)) {
             request.getRequestProgressTracker().log(
                 "StreamRendererServlet does not support for extension " + ext);
             if (included || response.isCommitted()) {
diff --git a/src/main/java/org/apache/sling/servlets/get/impl/helpers/XMLRendererServlet.java b/src/main/java/org/apache/sling/servlets/get/impl/helpers/XMLRendererServlet.java
index bd4e808..546dc50 100644
--- a/src/main/java/org/apache/sling/servlets/get/impl/helpers/XMLRendererServlet.java
+++ b/src/main/java/org/apache/sling/servlets/get/impl/helpers/XMLRendererServlet.java
@@ -43,8 +43,6 @@
 
     private static final long serialVersionUID = -3520278283206430415L;
 
-    public static final String EXT_XML = "xml";
-
     private static final String SYSVIEW = "sysview";
     private static final String DOCVIEW = "docview";
 
diff --git a/src/test/java/org/apache/sling/servlets/get/impl/DefaultGetServletTest.java b/src/test/java/org/apache/sling/servlets/get/impl/DefaultGetServletTest.java
index e012048..acfbb28 100644
--- a/src/test/java/org/apache/sling/servlets/get/impl/DefaultGetServletTest.java
+++ b/src/test/java/org/apache/sling/servlets/get/impl/DefaultGetServletTest.java
@@ -48,10 +48,11 @@
 
         @SuppressWarnings("unchecked")
         final Map<String, Servlet> map = (Map<String, Servlet>) field.get(servlet);
-        assertEquals(4, map.size());
-        assertNotNull(map.get("res"));
-        assertNotNull(map.get("html"));
-        assertNotNull(map.get("json"));
+        assertEquals(5, map.size());
+        assertNotNull(map.get(DefaultGetServlet.EXT_RES));
+        assertNotNull(map.get(DefaultGetServlet.EXT_HTML));
+        assertNotNull(map.get(DefaultGetServlet.EXT_JSON));
         assertNotNull(map.get("pdf"));
+        assertNotNull(map.get(null));
     }
 }