External.java 4.81 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.xss.XSSAPI
 *  com.day.cq.wcm.api.Page
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.SlingHttpServletResponse
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ValueMap
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.foundation;

import com.adobe.granite.xss.XSSAPI;
import com.day.cq.wcm.api.Page;
import java.io.IOException;
import java.io.PrintWriter;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class External {
    private static final Logger log = LoggerFactory.getLogger(External.class);
    public static final String PN_TARGET = "target";
    public static final String PN_INCLUSION = "inclusion";
    public static final String PN_LIMIT = "limit";
    public static final String PN_PASSPARAMS = "passparams";
    public static final String PN_WIDTH = "width";
    public static final String PN_HEIGHT = "height";
    public static final String PV_FIXED = "fixed";
    protected final Resource resource;
    protected final ValueMap properties;
    protected final Node node;
    protected final String pagePath;
    protected final String resourcePath;
    protected final String postSelector;
    protected final String targetParam;
    protected String target;

    public External(Resource resource, Page page, String spoolSelector, String postSelector, String targetParam) {
        if (null == resource) {
            throw new IllegalArgumentException("resource may not be null");
        }
        this.resource = resource;
        this.properties = (ValueMap)resource.adaptTo(ValueMap.class);
        this.node = (Node)resource.adaptTo(Node.class);
        this.pagePath = page.getPath();
        this.resourcePath = resource.getPath() + "." + spoolSelector;
        this.postSelector = postSelector;
        this.targetParam = targetParam;
    }

    public Resource getResource() {
        return this.resource;
    }

    public boolean hasContent() {
        return this.getTarget() != null;
    }

    public String getTarget() {
        if (this.target != null) {
            return this.target;
        }
        return this.getStringProperty("target");
    }

    public void setTarget(String target) {
        this.target = target;
    }

    public boolean passParameters() {
        return "true".equals(this.getStringProperty("passparams"));
    }

    public boolean isFixed() {
        return "fixed".equals(this.getStringProperty("inclusion"));
    }

    public String getWidth() {
        return this.getStringProperty("width", "100%");
    }

    public String getHeight() {
        return this.getStringProperty("height", "100%");
    }

    public Limit getLimit() {
        String s = this.getStringProperty("limit");
        if (s != null) {
            try {
                return Limit.valueOf(s.toUpperCase());
            }
            catch (IllegalArgumentException e) {
                log.warn("Value of limit illegal: " + s);
            }
        }
        return Limit.NO;
    }

    private String getStringProperty(String name) {
        return this.getStringProperty(name, null);
    }

    private String getStringProperty(String name, String defaultValue) {
        try {
            if (this.node.hasProperty(name)) {
                return this.node.getProperty(name).getString();
            }
        }
        catch (RepositoryException e) {
            log.warn("Unable to retrieve property " + name, (Throwable)e);
        }
        return defaultValue;
    }

    public void draw(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
        String target = this.getTarget();
        if (target == null) {
            return;
        }
        XSSAPI xssapi = (XSSAPI)request.adaptTo(XSSAPI.class);
        PrintWriter writer = response.getWriter();
        writer.println("<iframe src=\"");
        writer.println(xssapi.getValidHref(target));
        writer.println("\" width=\"");
        writer.println(this.getWidth());
        writer.println("\" height=\"");
        writer.println(this.getHeight());
        writer.println("\"></iframe>");
    }

    public static enum Limit {
        NO("no"),
        HOST("host"),
        OFF("off");
        
        private final String value;

        private Limit(String value) {
            this.value = value;
        }

        public String toString() {
            return this.value;
        }
    }

}