EntryPreprocessorImpl.java 5.91 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.Externalizer
 *  javax.jcr.Node
 *  javax.jcr.RepositoryException
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.commons.osgi.OsgiUtil
 *  org.apache.tika.io.CloseShieldInputStream
 *  org.apache.tika.io.IOUtils
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.designimporter.impl;

import com.day.cq.commons.Externalizer;
import com.day.cq.wcm.designimporter.DesignImporterContext;
import com.day.cq.wcm.designimporter.api.EntryPreprocessor;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Dictionary;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.commons.osgi.OsgiUtil;
import org.apache.tika.io.CloseShieldInputStream;
import org.apache.tika.io.IOUtils;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(label="%entry.preprocessor.name", description="%entry.preprocessor.description", metatype=1)
@Service
public class EntryPreprocessorImpl
implements EntryPreprocessor {
    private static final String TOKEN_DESIGNPATH = "{designPath}";
    private static final String DEFAULT_SEARCH_PATTERN = "/\\* *CQ_DESIGN_PATH *\\*/ *(['\"])";
    @Property(value={"/\\* *CQ_DESIGN_PATH *\\*/ *(['\"])"})
    private static final String PN_SEARCH_PATTERN = "search.pattern";
    @Property(value={"$1{designPath}/"})
    private static final String PN_REPLACE_PATTERN = "replace.pattern";
    @Deprecated
    @Reference
    Externalizer externalizer;
    private Pattern searchPattern = Pattern.compile("/\\* *CQ_DESIGN_PATH *\\*/ *(['\"])");
    private String replacePattern = "\"{designPath}";
    private Logger logger = LoggerFactory.getLogger(EntryPreprocessorImpl.class);

    @Override
    public InputStream getProcessedStream(String entry, InputStream inputStream, DesignImporterContext designImporterContext) {
        if (entry.matches("(?i)[^.]*\\.js")) {
            return this.getProcessedScriptStream(entry, inputStream, designImporterContext);
        }
        if (entry.matches("(?i)[^.]*\\.css")) {
            return this.getProcessedCssStream(entry, inputStream, designImporterContext);
        }
        if (entry.matches("(?i)[^.]*\\.html")) {
            return this.getProcessedHtmlStream(entry, inputStream, designImporterContext);
        }
        return new CloseShieldInputStream(inputStream);
    }

    @Activate
    protected void activate(ComponentContext context) {
        try {
            this.searchPattern = Pattern.compile(OsgiUtil.toString(context.getProperties().get("search.pattern"), (String)"/\\* *CQ_DESIGN_PATH *\\*/ *(['\"])"));
        }
        catch (PatternSyntaxException e) {
            this.logger.error("Invalid pattern encountered", (Throwable)e);
        }
        this.replacePattern = OsgiUtil.toString(context.getProperties().get("replace.pattern"), (String)"");
    }

    protected InputStream getProcessedCssStream(String entry, InputStream inputStream, DesignImporterContext designImporterContext) {
        return this.getProcessedStream(inputStream, designImporterContext);
    }

    protected InputStream getProcessedHtmlStream(String entry, InputStream inputStream, DesignImporterContext designImporterContext) {
        return this.getProcessedStream(inputStream, designImporterContext);
    }

    protected InputStream getProcessedScriptStream(String entry, InputStream inputStream, DesignImporterContext designImporterContext) {
        return this.getProcessedStream(inputStream, designImporterContext);
    }

    private String findReplace(String input, DesignImporterContext designImporterContext) {
        Matcher m = this.searchPattern.matcher(input);
        String designPath = this.getDesignPath(designImporterContext);
        String replacement = this.replacePattern.replace("{designPath}", designPath);
        return m.replaceAll(replacement);
    }

    private String getDesignPath(DesignImporterContext designImporterContext) {
        try {
            String designPath = designImporterContext.designNode.getPath();
            return designPath;
        }
        catch (RepositoryException e) {
            this.logger.error("Error occurred while trying to access the path of the design node", (Throwable)e);
            return "";
        }
    }

    private InputStream getProcessedStream(InputStream inputStream, DesignImporterContext designImporterContext) {
        StringWriter stringWriter = new StringWriter();
        String encoding = "UTF-8";
        try {
            IOUtils.copy((InputStream)inputStream, (Writer)stringWriter, (String)encoding);
            String contents = stringWriter.toString();
            String processedContents = this.findReplace(contents, designImporterContext);
            return new ByteArrayInputStream(processedContents.getBytes(encoding));
        }
        catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    protected void bindExternalizer(Externalizer externalizer) {
        this.externalizer = externalizer;
    }

    protected void unbindExternalizer(Externalizer externalizer) {
        if (this.externalizer == externalizer) {
            this.externalizer = null;
        }
    }
}