DefaultDataProvider.java 6.86 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.resourceresolverhelper.ResourceResolverHelper
 *  javax.jcr.Binary
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.PropertyUnbounded
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.NonExistingResource
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceNotFoundException
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.commons.osgi.PropertiesUtil
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.forms.common.service.impl;

import com.adobe.forms.common.service.AbstractDataProvider;
import com.adobe.forms.common.service.ContentType;
import com.adobe.forms.common.service.DataOptions;
import com.adobe.forms.common.service.DataProvider;
import com.adobe.forms.common.service.FormsException;
import com.adobe.forms.common.service.PrefillData;
import com.adobe.granite.resourceresolverhelper.ResourceResolverHelper;
import java.io.File;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.jcr.Binary;
import javax.jcr.Node;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.PropertyUnbounded;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.NonExistingResource;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceNotFoundException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(immediate=1, metatype=1, label="Default Prefill Service Configuration")
@Service(value={DataProvider.class})
public class DefaultDataProvider
extends AbstractDataProvider {
    private Logger logger = LoggerFactory.getLogger(DefaultDataProvider.class);
    private static final String ALLOWED_DATA_FILE_LOCATIONS = "alloweddataFileLocations";
    @Reference
    ResourceResolverHelper resourceResolverHelper;
    @Property(name="alloweddataFileLocations", label="Data files locations", description="The list of paths allowed for prefill of Adaptive Forms. Paths can either be a string or a regex.", unbounded=PropertyUnbounded.ARRAY, value={"crx://.*"})
    private List<Pattern> allowedLocationRegex;
    private static String URL_REGEX = "(https?|ftp|file)://.+";
    @Property(intValue={2000})
    static final String SERVICE_RANKING = "service.ranking";

    protected void activate(ComponentContext context) {
        Dictionary props = context.getProperties();
        String[] allowedPrefillPaths = PropertiesUtil.toStringArray(props.get("alloweddataFileLocations"));
        this.allowedLocationRegex = new ArrayList<Pattern>();
        for (String allowedPrefillPath : allowedPrefillPaths) {
            try {
                this.allowedLocationRegex.add(Pattern.compile(allowedPrefillPath));
                continue;
            }
            catch (Exception e) {
                this.logger.error("Unable to compile the regex: " + allowedPrefillPath, (Throwable)e);
            }
        }
    }

    @Override
    public PrefillData getPrefillData(DataOptions options) throws FormsException {
        String dataRef = options.getDataRef();
        Resource formResource = options.getFormResource();
        Resource aemFormContainer = options.getAemFormContainer();
        ResourceResolver resolver = null;
        InputStream inputStream = null;
        PrefillData prefillData = null;
        resolver = formResource != null ? formResource.getResourceResolver() : (aemFormContainer != null ? aemFormContainer.getResourceResolver() : this.resourceResolverHelper.getResourceResolver());
        if (dataRef != null && this.isPrefillAllowedForPath(dataRef)) {
            try {
                if (dataRef.startsWith("crx://")) {
                    Binary data;
                    if (resolver == null) {
                        throw new Exception("resource resolver is null while reading resource : " + dataRef);
                    }
                    Resource fileResource = resolver.resolve(dataRef.substring("crx://".length()));
                    if (fileResource instanceof NonExistingResource) {
                        throw new ResourceNotFoundException("dataRef passed as CRX Resource does not exist at: " + dataRef);
                    }
                    Node jcrNode = (Node)fileResource.adaptTo(Node.class);
                    if (jcrNode.isNodeType("nt:file")) {
                        jcrNode = jcrNode.getNode("jcr:content");
                    }
                    if ((data = jcrNode.getProperty("jcr:data").getBinary()).getSize() < 1) {
                        throw new IllegalArgumentException("dataRef passed as CRX Resource is Empty at: " + dataRef);
                    }
                    inputStream = data.getStream();
                } else {
                    URL url;
                    if (dataRef.matches(URL_REGEX)) {
                        url = new URL(dataRef);
                    } else {
                        File file = new File(dataRef);
                        url = file.toURI().toURL();
                    }
                    inputStream = url.openStream();
                }
            }
            catch (Exception e) {
                this.logger.error("Unable to read data for the dataRef: " + dataRef, (Throwable)e);
            }
        }
        if (inputStream != null) {
            prefillData = new PrefillData(inputStream, options.getContentType());
        }
        return prefillData;
    }

    @Override
    public String getServiceName() {
        return null;
    }

    @Override
    public String getServiceDescription() {
        return null;
    }

    public boolean isPrefillAllowedForPath(String path) {
        for (Pattern regex : this.allowedLocationRegex) {
            Matcher matcher = regex.matcher(path);
            if (!matcher.matches()) continue;
            return true;
        }
        return false;
    }

    protected void bindResourceResolverHelper(ResourceResolverHelper resourceResolverHelper) {
        this.resourceResolverHelper = resourceResolverHelper;
    }

    protected void unbindResourceResolverHelper(ResourceResolverHelper resourceResolverHelper) {
        if (this.resourceResolverHelper == resourceResolverHelper) {
            this.resourceResolverHelper = null;
        }
    }
}