HybridAppPathRewriterTransformer.java 4.17 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.rewriter.linkchecker.LinkChecker
 *  com.day.text.Text
 *  org.apache.cocoon.xml.sax.AbstractSAXPipe
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.rewriter.ProcessingComponentConfiguration
 *  org.apache.sling.rewriter.ProcessingContext
 *  org.apache.sling.rewriter.Transformer
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.mobile.platform.impl.rewriter;

import com.adobe.cq.mobile.platform.impl.rewriter.HybridAppPathRewriterOptions;
import com.adobe.cq.mobile.platform.impl.rewriter.HybridAppPathRewriterTransformerConfig;
import com.day.cq.rewriter.linkchecker.LinkChecker;
import com.day.text.Text;
import java.io.IOException;
import java.util.Map;
import org.apache.cocoon.xml.sax.AbstractSAXPipe;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.rewriter.ProcessingComponentConfiguration;
import org.apache.sling.rewriter.ProcessingContext;
import org.apache.sling.rewriter.Transformer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

public class HybridAppPathRewriterTransformer
extends AbstractSAXPipe
implements Transformer {
    private static final Logger log = LoggerFactory.getLogger(HybridAppPathRewriterTransformer.class);
    private final LinkChecker checker;
    private SlingHttpServletRequest request;
    private HybridAppPathRewriterOptions options;
    private boolean enabled = false;
    private HybridAppPathRewriterTransformerConfig config;

    public HybridAppPathRewriterTransformer(LinkChecker checker, HybridAppPathRewriterTransformerConfig config) {
        this.checker = checker;
        this.config = config;
    }

    public void init(ProcessingContext context, ProcessingComponentConfiguration config) throws IOException {
        this.request = context.getRequest();
        this.options = (HybridAppPathRewriterOptions)this.request.getAttribute("hybridAppPathRewritingOptions");
        boolean bl = this.enabled = this.options != null;
        if (this.enabled) {
            log.debug("Rewriting paths for hybrid app");
        }
    }

    public void dispose() {
    }

    public void startElement(String uri, String name, String raw, Attributes a) throws SAXException {
        if (this.enabled) {
            AttributesImpl attrs = new AttributesImpl(a);
            boolean result = false;
            if (result |= this.hybridRewrite(this.config.assetsMapping, name, attrs)) {
                super.startElement(uri, name, raw, (Attributes)attrs);
                return;
            }
        }
        super.startElement(uri, name, raw, a);
    }

    private boolean hybridRewrite(Map<String, String> mapping, String name, AttributesImpl attrs) throws SAXException {
        if (mapping.containsKey(name)) {
            String[] attributesOfInterest = Text.explode((String)mapping.get(name), (int)58);
            boolean targetFound = false;
            for (String attrName : attributesOfInterest) {
                String value = attrs.getValue(attrName);
                if (!this.isTargetOfInterest(value)) continue;
                targetFound = true;
                int index = attrs.getIndex(attrName);
                boolean replace = true;
                if ("src".equals(attrName)) {
                    boolean bl = replace = !value.startsWith("http");
                }
                if (replace) {
                    value = this.replaceJcrContent(value);
                }
                attrs.setAttribute(index, "", attrName, attrName, "String", value);
                log.debug("Attribute \"{}\" rewritten to: {}", (Object)name, (Object)value);
            }
            return targetFound;
        }
        return false;
    }

    private boolean isTargetOfInterest(String target) {
        return target != null && !this.checker.isSpecial(target);
    }

    private String replaceJcrContent(String path) {
        if (path == null) {
            return null;
        }
        return path.replaceAll("/jcr:content/", "/jcr_content/").replaceAll("/_jcr_content/", "/jcr_content/");
    }
}