LCFormsOptionServiceImpl.java 9.04 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.xss.XSSAPI
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.request.RequestParameter
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.commons.json.JSONObject
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.forms.service.impl;

import com.adobe.forms.admin.LCFormsAdminService;
import com.adobe.forms.option.LCFormsOptions;
import com.adobe.forms.service.LCFormsOptionService;
import com.adobe.granite.xss.XSSAPI;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.Property;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(label="LC Forms Option Service", description="LC Forms Option Service")
@Service(value={LCFormsOptionService.class})
public class LCFormsOptionServiceImpl
implements LCFormsOptionService {
    private static Logger logger = LoggerFactory.getLogger(LCFormsOptionServiceImpl.class);
    private static Set<String> SECURE_PROPERTIES = Collections.unmodifiableSet(new HashSet<String>(Arrays.asList("skipTrustedCheck", "template", "templateBytes", "contentRoot")));
    @Reference
    private LCFormsAdminService lcFormsAdminService;

    public static LCFormsOptions create(SlingHttpServletRequest slingRequest) {
        LCFormsOptions options = new LCFormsOptions();
        for (String opts : LCFormsOptions.getSupported()) {
            try {
                String val = LCFormsOptionServiceImpl.getAttributeOrRequestParam(slingRequest, opts);
                LCFormsOptionServiceImpl.processOption(val, opts, options, slingRequest);
            }
            catch (Exception e) {
                logger.debug("Error setting " + opts + " in LCFormsOptions: ", (Throwable)e);
            }
        }
        return options;
    }

    @Override
    public LCFormsOptions createFromRequest(SlingHttpServletRequest slingRequest) {
        LCFormsOptions options = new LCFormsOptions();
        for (String opts : LCFormsOptions.getSupported()) {
            try {
                String val = LCFormsOptionServiceImpl.getAttributeOrRequestParam(slingRequest, opts, this.lcFormsAdminService);
                LCFormsOptionServiceImpl.processOption(val, opts, options, slingRequest);
            }
            catch (Exception e) {
                logger.debug("Error setting " + opts + " in LCFormsOptions: ", (Throwable)e);
            }
        }
        return options;
    }

    private static void processOption(String val, String opts, LCFormsOptions options, SlingHttpServletRequest slingRequest) {
        if (val != null) {
            String setter = LCFormsOptionServiceImpl.formSetterName(opts);
            LCFormsOptionServiceImpl.invokeSetter(options, setter, val);
        }
    }

    private static void invokeSetter(LCFormsOptions options, String setter, String val) {
        try {
            options.getClass().getMethod(setter, String.class).invoke(options, val);
        }
        catch (Exception e) {
            logger.error("Cannot invoke setter for setter" + setter, (Throwable)e);
        }
    }

    private static String formSetterName(String option) {
        return "set" + Character.toUpperCase(option.charAt(0)) + option.substring(1);
    }

    @Override
    public LCFormsOptions create(Map<String, String> formOptions) {
        LCFormsOptions options = new LCFormsOptions();
        for (String opts : LCFormsOptions.getSupported()) {
            try {
                String val = formOptions.get(opts);
                LCFormsOptionServiceImpl.processOption(val, opts, options, null);
            }
            catch (Exception e) {
                logger.debug("Error setting " + opts + " in LCFormsOptions: ", (Throwable)e);
            }
        }
        return options;
    }

    @Override
    public JSONObject getBehaviourConfig(SlingHttpServletRequest slingRequest, XSSAPI xssAPI) {
        String[] behaviourConstants = new String[]{"mfDataDependentFloatingField", "mfDisableHeadRequest", "mfStripInitialFormDom", "mfDisableLeanSubmit", "mfDisableCalendarIcon", "mfRangeTabIndex"};
        List<String> boolVal = Arrays.asList("true", "false");
        HashMap<String, Object> mpBehaviour = new HashMap<String, Object>();
        for (String behaviourConstant : behaviourConstants) {
            String behaviorConfigParam = this.get(slingRequest, behaviourConstant);
            if (xssAPI != null) {
                behaviorConfigParam = xssAPI.encodeForJSString(behaviorConfigParam);
            }
            if (behaviorConfigParam == null) continue;
            if (boolVal.contains(behaviorConfigParam)) {
                mpBehaviour.put(behaviourConstant, Boolean.valueOf(behaviorConfigParam));
                continue;
            }
            mpBehaviour.put(behaviourConstant, behaviorConfigParam);
        }
        return new JSONObject(mpBehaviour);
    }

    @Override
    public JSONObject getPagingConfig(SlingHttpServletRequest slingRequest, XSSAPI xssAPI) {
        String[] pagingConstants = new String[]{"mfPagingDisabled", "mfShrinkPageDisabled"};
        HashMap<String, Boolean> mpPaging = new HashMap<String, Boolean>();
        mpPaging.put("mfPagingDisabled", false);
        mpPaging.put("mfShrinkPageDisabled", false);
        for (int i = 0; i < pagingConstants.length; ++i) {
            String pagingConfig = xssAPI != null ? xssAPI.encodeForJSString(this.get(slingRequest, pagingConstants[i])) : this.get(slingRequest, pagingConstants[i]);
            if (pagingConfig == null) continue;
            mpPaging.put(pagingConstants[i], Boolean.valueOf(pagingConfig));
        }
        return new JSONObject(mpPaging);
    }

    public static Map<String, Object> getAttributeOrRequestParamAsMap(SlingHttpServletRequest slingRequest, String param) {
        Map value = null;
        Object valObj = slingRequest.getAttribute(param);
        if (valObj != null && valObj instanceof Map) {
            value = (Map)valObj;
        }
        return value;
    }

    @Deprecated
    public static String getAttributeOrRequestParam(SlingHttpServletRequest slingRequest, String param) {
        return LCFormsOptionServiceImpl.getAttributeOrRequestParam(slingRequest, param, null);
    }

    public static String getAttributeOrRequestParam(SlingHttpServletRequest slingRequest, String param, LCFormsAdminService lcFormsAdminService) {
        boolean isSkipTrustedCheck;
        RequestParameter reqParam;
        Node node;
        Resource resource;
        if ("debugDir".equals(param) && (lcFormsAdminService == null || !lcFormsAdminService.isAllowDebugParameters())) {
            return null;
        }
        String value = null;
        Object valObj = slingRequest.getAttribute(param);
        if (valObj != null && valObj instanceof String) {
            value = (String)valObj;
        }
        if ((isSkipTrustedCheck = "true".equals(slingRequest.getAttribute("skipTrustedCheck"))) && SECURE_PROPERTIES.contains(param)) {
            return value;
        }
        if (value == null && (resource = slingRequest.getResource()) != null && (node = (Node)resource.adaptTo(Node.class)) != null) {
            try {
                Property property;
                if (node.hasProperty(param) && (property = node.getProperty(param)).getType() == 1) {
                    value = property.getString();
                }
            }
            catch (Exception e) {
                logger.debug("Exception getting value of " + param, (Throwable)e);
            }
        }
        if (value == null && (reqParam = slingRequest.getRequestParameter(param)) != null) {
            value = reqParam.getString();
        }
        return value;
    }

    @Override
    public String get(SlingHttpServletRequest slingRequest, String option) {
        return LCFormsOptionServiceImpl.getAttributeOrRequestParam(slingRequest, option, this.lcFormsAdminService);
    }

    @Override
    public void set(SlingHttpServletRequest slingRequest, String option, String value) {
        slingRequest.setAttribute(option, (Object)value);
    }

    protected void bindLcFormsAdminService(LCFormsAdminService lCFormsAdminService) {
        this.lcFormsAdminService = lCFormsAdminService;
    }

    protected void unbindLcFormsAdminService(LCFormsAdminService lCFormsAdminService) {
        if (this.lcFormsAdminService == lCFormsAdminService) {
            this.lcFormsAdminService = null;
        }
    }
}