incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r822627 - /sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverWebConsolePlugin.java
Date Wed, 07 Oct 2009 09:10:16 GMT
Author: fmeschbe
Date: Wed Oct  7 09:10:16 2009
New Revision: 822627

URL: http://svn.apache.org/viewvc?rev=822627&view=rev
Log:
SLING-1138 ConfigurationPrinter for resource resolver mappings

Modified:
    sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverWebConsolePlugin.java

Modified: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverWebConsolePlugin.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverWebConsolePlugin.java?rev=822627&r1=822626&r2=822627&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverWebConsolePlugin.java
(original)
+++ sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverWebConsolePlugin.java
Wed Oct  7 09:10:16 2009
@@ -22,18 +22,20 @@
 import java.io.PrintWriter;
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Dictionary;
 import java.util.Hashtable;
+import java.util.List;
 
 import javax.jcr.Session;
-import javax.servlet.Servlet;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletRequestWrapper;
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.felix.webconsole.AbstractWebConsolePlugin;
+import org.apache.felix.webconsole.ConfigurationPrinter;
 import org.apache.felix.webconsole.WebConsoleConstants;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.jcr.resource.internal.helper.MapEntries;
@@ -42,10 +44,11 @@
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceRegistration;
 
-public class JcrResourceResolverWebConsolePlugin extends AbstractWebConsolePlugin {
-    
+public class JcrResourceResolverWebConsolePlugin extends
+        AbstractWebConsolePlugin implements ConfigurationPrinter {
+
     private static final long serialVersionUID = 0;
-    
+
     private static final String ATTR_TEST = "plugin.test";
 
     private static final String ATTR_SUBMIT = "plugin.submit";
@@ -69,7 +72,9 @@
         props.put(Constants.SERVICE_PID, getClass().getName());
         props.put(WebConsoleConstants.PLUGIN_LABEL, getLabel());
 
-        service = context.registerService(Servlet.class.getName(), this, props);
+        service = context.registerService(new String[] {
+            WebConsoleConstants.SERVICE_NAME, ConfigurationPrinter.SERVICE },
+            this, props);
     }
 
     void dispose() {
@@ -80,6 +85,8 @@
         }
     }
 
+    // ---------- AbstractWebConsolePlugin
+
     @Override
     public String getLabel() {
         return "jcrresolver";
@@ -104,7 +111,7 @@
 
         MapEntries mapEntries = resolverFactory.getMapEntries();
 
-        title(
+        titleHtml(
             pw,
             "Configuration Test",
             "To test the configuration, enter an URL or a resource path into the field and
click 'Resolve' to resolve the URL or click 'Map' to map the resource path");
@@ -132,7 +139,7 @@
 
         separator(pw);
 
-        dumpMap(
+        dumpMapHtml(
             pw,
             "Resolver Map Entries",
             "Lists the entries used by the ResourceResolver.resolve methods to map URLs to
Resources",
@@ -140,7 +147,7 @@
 
         separator(pw);
 
-        dumpMap(
+        dumpMapHtml(
             pw,
             "Mapping Map Entries",
             "Lists the entries used by the ResourceResolver.map methods to map Resource Paths
to URLs",
@@ -150,10 +157,78 @@
 
     }
 
-    private void dumpMap(PrintWriter pw, String title, String description,
+    @Override
+    protected void doPost(HttpServletRequest request,
+            HttpServletResponse response) throws ServletException, IOException {
+
+        String test = request.getParameter(ATTR_TEST);
+        if (test != null && test.length() > 0) {
+
+            // set test value for the re-rendering of the form
+            request.setAttribute(ATTR_TEST, test);
+
+            Session session = null;
+            try {
+                // prepare the request for the resource resolver
+                HttpServletRequest helper = new ResolverRequest(request, test);
+
+                // get the resource resolver with an administrative session
+                session = resolverFactory.getRepository().loginAdministrative(
+                    null);
+                ResourceResolver resolver = resolverFactory.getResourceResolver(session);
+
+                // map or resolve as instructed
+                Object result;
+                if ("Map".equals(request.getParameter(ATTR_SUBMIT))) {
+                    result = resolver.map(helper, helper.getPathInfo());
+                } else {
+                    result = resolver.resolve(helper, helper.getPathInfo());
+                }
+
+                // set the result to render the result
+                request.setAttribute(ATTR_RESULT, result.toString());
+
+            } catch (Throwable t) {
+
+                // some error occurred, report it as a result
+                request.setAttribute(ATTR_RESULT, "Test Failure: " + t);
+
+            } finally {
+                if (session != null) {
+                    session.logout();
+                }
+            }
+
+        }
+
+        // finally render the result
+        doGet(request, response);
+    }
+
+    // ---------- ConfigurationPrinter
+
+    public void printConfiguration(PrintWriter pw) {
+        final MapEntries mapEntries = resolverFactory.getMapEntries();
+
+        dumpMapText(
+            pw,
+            "Resolver Map Entries",
+            mapEntries.getResolveMaps());
+
+        separator(pw);
+
+        dumpMapText(
+            pw,
+            "Mapping Map Entries",
+            mapEntries.getMapMaps());
+    }
+
+    // ---------- internal
+
+    private void dumpMapHtml(PrintWriter pw, String title, String description,
             Collection<MapEntry> list) {
 
-        title(pw, title, description);
+        titleHtml(pw, title, description);
 
         pw.println("<tr class='content'>");
         pw.println("<th class='content'>Pattern</th>");
@@ -184,7 +259,7 @@
         }
     }
 
-    private void title(PrintWriter pw, String title, String description) {
+    private void titleHtml(PrintWriter pw, String title, String description) {
         pw.println("<tr class='content'>");
         pw.println("<th colspan='3'class='content container'>" + title
             + "</th>");
@@ -198,58 +273,27 @@
         }
     }
 
-    private void separator(PrintWriter pw) {
-        pw.println("<tr class='content'>");
-        pw.println("<td class='content' colspan='3'>&nbsp;</td>");
-        pw.println("</tr>");
-    }
-
-    @Override
-    protected void doPost(HttpServletRequest request,
-            HttpServletResponse response) throws ServletException, IOException {
-
-        String test = request.getParameter(ATTR_TEST);
-        if (test != null && test.length() > 0) {
-
-            // set test value for the re-rendering of the form
-            request.setAttribute(ATTR_TEST, test);
-
-            Session session = null;
-            try {
-                // prepare the request for the resource resolver
-                HttpServletRequest helper = new ResolverRequest(request, test);
-
-                // get the resource resolver with an administrative session
-                session = resolverFactory.getRepository().loginAdministrative(
-                    null);
-                ResourceResolver resolver = resolverFactory.getResourceResolver(session);
-
-                // map or resolve as instructed
-                Object result;
-                if ("Map".equals(request.getParameter(ATTR_SUBMIT))) {
-                    result = resolver.map(helper, helper.getPathInfo());
-                } else {
-                    result = resolver.resolve(helper, helper.getPathInfo());
-                }
+    private void dumpMapText(PrintWriter pw, String title,
+            Collection<MapEntry> list) {
 
-                // set the result to render the result
-                request.setAttribute(ATTR_RESULT, result.toString());
+        pw.println(title);
 
-            } catch (Throwable t) {
-            
-                // some error occurred, report it as a result
-                request.setAttribute(ATTR_RESULT, "Test Failure: " + t);
-
-            } finally {
-                if (session != null) {
-                    session.logout();
-                }
-            }
+        final String format = "%25s%25s%15s\r\n";
+        pw.printf(format, "Pattern", "Replacement", "Redirect");
 
+        for (MapEntry entry : list) {
+            final List<String> redir = Arrays.asList(entry.getRedirect());
+            final String status = entry.isInternal()
+                    ? "internal"
+                    : "external: " + entry.getStatus();
+            pw.printf(format, entry.getPattern(), redir, status);
         }
+    }
 
-        // finally render the result
-        doGet(request, response);
+    private void separator(PrintWriter pw) {
+        pw.println("<tr class='content'>");
+        pw.println("<td class='content' colspan='3'>&nbsp;</td>");
+        pw.println("</tr>");
     }
 
     private static class ResolverRequest extends HttpServletRequestWrapper {



Mime
View raw message