ConfWebConsolePlugin.java 9.97 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.servlet.Servlet
 *  javax.servlet.ServletException
 *  javax.servlet.http.HttpServletRequest
 *  javax.servlet.http.HttpServletResponse
 *  org.apache.commons.lang.StringUtils
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.felix.webconsole.AbstractWebConsolePlugin
 *  org.apache.sling.api.resource.LoginException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceResolverFactory
 */
package com.adobe.granite.confmgr.impl;

import com.adobe.granite.confmgr.Conf;
import com.adobe.granite.confmgr.ConfMgr;
import com.adobe.granite.confmgr.impl.ConfImpl;
import com.adobe.granite.confmgr.impl.ConfMgrImpl;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.felix.webconsole.AbstractWebConsolePlugin;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;

@Component
@Service(value={Servlet.class})
@Properties(value={@Property(name="service.description", value={"Apache Sling Web Console Plugin for /conf Manager"}), @Property(name="felix.webconsole.label", value={"conf"}), @Property(name="felix.webconsole.title", value={"ConfMgr"})})
public class ConfWebConsolePlugin
extends AbstractWebConsolePlugin {
    public static final String LABEL = "conf";
    public static final String TITLE = "ConfMgr";
    @Reference
    private ResourceResolverFactory resolverFactory;
    @Reference
    private ConfMgr confMgr;

    public String getLabel() {
        return "conf";
    }

    public String getTitle() {
        return "ConfMgr";
    }

    protected void renderContent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter pw = response.getWriter();
        this.info(pw, "ConfMgr manages configurations in the JCR repository. Use this tool to test config resolutions.");
        this.printConfiguration(pw);
        pw.println("<br/>");
        this.printResolutionTestTool(request, pw);
    }

    private void printConfiguration(PrintWriter pw) {
        ConfMgrImpl confMgrImpl = (ConfMgrImpl)this.confMgr;
        this.tableStart(pw, "Configuration", 2);
        pw.println("<tr>");
        pw.println("<td style='width:20%'>Allowed paths</td>");
        pw.println("<td>" + StringUtils.join((Object[])confMgrImpl.getAllowedPaths(), (String)", ") + "</td>");
        pw.println("</tr>");
        pw.println("<tr>");
        pw.println("<td style='width:20%'>Fallback paths</td>");
        pw.println("<td>" + StringUtils.join((Object[])confMgrImpl.getFallbackPaths(), (String)", ") + "</td>");
        pw.println("</tr>");
        pw.println("<tr>");
        pw.println("<td></td>");
        pw.println("<td>");
        pw.println("<form method='get' action='${appRoot}/configMgr/com.adobe.granite.confmgr.impl.ConfMgrImpl'>");
        pw.println("<input type='submit' value='Configure'/>");
        pw.println("</form>");
        pw.println("</td>");
        pw.println("</tr>");
        this.tableEnd(pw);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private void printResolutionTestTool(HttpServletRequest request, PrintWriter pw) {
        String item;
        String user;
        String path = request.getParameter("path");
        if (StringUtils.isEmpty((String)path)) {
            path = "/content";
        }
        if (StringUtils.isEmpty((String)(item = request.getParameter("item")))) {
            item = ".";
        }
        if (StringUtils.isEmpty((String)(user = request.getParameter("user")))) {
            user = request.getRemoteUser();
        }
        boolean runTest = request.getParameter("path") != null;
        ResourceResolver resolver = null;
        try {
            Resource content = null;
            if (runTest) {
                resolver = this.getResolver(user);
            }
            if (resolver != null) {
                content = resolver.getResource(path);
            }
            pw.println("<form method='get'>");
            this.tableStart(pw, "Test ConfMgr Resolution", 2);
            pw.println("<td style='width:20%'>Content Path</td>");
            pw.println("<td><input name='path' value='" + path + "' style='width:100%'/>");
            if (resolver != null && content == null) {
                pw.println("<div>");
                pw.println("<span class='ui-icon ui-icon-alert' style='float:left'></span>");
                pw.println("<span style='float:left'>Path does not exist.</span>");
                pw.println("</div>");
            }
            pw.println("</td>");
            this.tableRows(pw);
            pw.println("<td>Item</td>");
            pw.println("<td><input name='item' value='" + item + "' style='width:100%'/></td>");
            this.tableRows(pw);
            pw.println("<td>User</td>");
            pw.println("<td><input name='user' value='" + user + "' style='width:50%'/>");
            if (runTest && resolver == null) {
                pw.println("<div>");
                pw.println("<span class='ui-icon ui-icon-alert' style='float:left'></span>");
                pw.println("<span style='float:left'>User does not exist.</span>");
                pw.println("</div>");
            }
            pw.println("</td>");
            this.tableRows(pw);
            pw.println("<td></td>");
            pw.println("<td><input type='submit' value='Resolve'/></td>");
            this.tableEnd(pw);
            pw.println("</form>");
            pw.println("<br/>");
            if (runTest && content != null) {
                ConfImpl conf = (ConfImpl)this.confMgr.getConf(content);
                Resource itemRes = conf.getItemResource(item);
                String itemResPath = null;
                if (itemRes != null) {
                    itemResPath = itemRes.getPath();
                }
                this.tableStart(pw, "Resolved", 2);
                pw.println("<td style='width:20%'>Code</td>");
                pw.println("<td>");
                pw.println("<code>getResource(\"" + content.getPath() + "\").adaptTo(Conf.class).getItem(\"" + item + "\")</code>");
                pw.println("<br/>&nbsp;");
                pw.println("</td>");
                this.tableRows(pw);
                pw.println("<td style='width:20%'>Item</td>");
                if (itemResPath != null) {
                    pw.println("<td>" + itemResPath + "<br/>&nbsp;</td>");
                } else {
                    pw.println("<td>");
                    pw.println("<div>");
                    pw.println("<span class='ui-icon ui-icon-alert' style='float:left'></span>");
                    pw.println("<span style='float:left'>No matching item found.</span>");
                    pw.println("</div>");
                    pw.println("<br/>&nbsp;</td>");
                }
                this.tableRows(pw);
                pw.println("<td>Config paths</td>");
                pw.println("<td>");
                for (String p : conf.getPaths()) {
                    if (itemResPath != null && itemResPath.startsWith(p + "/" + "settings")) {
                        pw.println("<b>" + p + "</b>");
                    } else {
                        pw.println(p);
                    }
                    pw.println("<br/>");
                }
                pw.println("</td>");
                this.tableEnd(pw);
            }
        }
        finally {
            if (resolver != null) {
                resolver.close();
            }
        }
    }

    private void info(PrintWriter pw, String text) {
        pw.println("<p class='statline ui-state-highlight'>" + text + "</p>");
    }

    private void tableStart(PrintWriter pw, String title, int colspan) {
        pw.println("<table class='nicetable ui-widget'>");
        pw.println("<thead class='ui-widget-header'>");
        pw.println("<tr>");
        pw.println("<th colspan=" + colspan + ">" + title + "</th>");
        pw.println("</tr>");
        pw.println("</thead>");
        pw.println("<tbody class='ui-widget-content'>");
        pw.println("<tr>");
    }

    private void tableEnd(PrintWriter pw) {
        pw.println("</tr>");
        pw.println("</tbody>");
        pw.println("</table>");
    }

    private void tableRows(PrintWriter pw) {
        pw.println("</tr>");
        pw.println("<tr>");
    }

    private ResourceResolver getResolver(final String user) {
        try {
            return this.resolverFactory.getAdministrativeResourceResolver((Map)new HashMap<String, Object>(){});
        }
        catch (LoginException e) {
            return null;
        }
    }

    protected void bindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
        this.resolverFactory = resourceResolverFactory;
    }

    protected void unbindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
        if (this.resolverFactory == resourceResolverFactory) {
            this.resolverFactory = null;
        }
    }

    protected void bindConfMgr(ConfMgr confMgr) {
        this.confMgr = confMgr;
    }

    protected void unbindConfMgr(ConfMgr confMgr) {
        if (this.confMgr == confMgr) {
            this.confMgr = null;
        }
    }

}