EchoSignUtils.java 5.84 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.aem.adobesign.recipient.ParticipantInfo
 *  com.adobe.aem.adobesign.recipient.ParticipantSetInfo
 *  com.adobe.aem.adobesign.service.AdobeSignServiceException
 *  com.adobe.aem.adobesign.service.Attachment
 *  com.adobe.aem.adobesign.service.WidgetCreationOptions
 *  org.apache.commons.io.IOUtils
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.commons.json.JSONArray
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.aemds.guide.addon.signing.echosign;

import com.adobe.aem.adobesign.recipient.ParticipantInfo;
import com.adobe.aem.adobesign.recipient.ParticipantSetInfo;
import com.adobe.aem.adobesign.service.AdobeSignServiceException;
import com.adobe.aem.adobesign.service.Attachment;
import com.adobe.aem.adobesign.service.WidgetCreationOptions;
import com.adobe.aemds.guide.addon.signing.echosign.EchoSignConfiguration;
import com.adobe.aemds.guide.addon.signing.echosign.EchoSignConstants;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class EchoSignUtils {
    protected static final Logger log = LoggerFactory.getLogger(EchoSignUtils.class);

    private static List<Attachment> prepareAttachmentList(ResourceResolver resolver, EchoSignConfiguration config) {
        ArrayList<Attachment> attachments = null;
        JSONArray attachmentInfo = null;
        if (config.shouldSendAttachments()) {
            attachments = new ArrayList<Attachment>();
            try {
                attachmentInfo = new JSONArray(config.getAttachmentJsonString());
                for (int i = 0; i < attachmentInfo.length(); ++i) {
                    JSONObject attachment = attachmentInfo.getJSONObject(i);
                    Attachment aObject = EchoSignUtils.createAttachment(resolver, attachment.getString("name"), attachment.getString("path"));
                    if (aObject == null) continue;
                    attachments.add(aObject);
                }
            }
            catch (JSONException jsonException) {
                log.error("Failed to parse the attachment json array String", (Throwable)jsonException);
                return null;
            }
            catch (IOException e) {
                log.error("Failed to process attachments", (Throwable)e);
                return null;
            }
        }
        return attachments;
    }

    public static WidgetCreationOptions createWidgetOptions(EchoSignConfiguration config, ResourceResolver resolver) {
        WidgetCreationOptions widgetOptions = new WidgetCreationOptions();
        widgetOptions.setLocale(config.getAgreementLocale());
        widgetOptions.setName(config.getAgreementName());
        widgetOptions.setCallbackUrl(config.getSuccessURL());
        widgetOptions.setRecipientEmail(config.getRecipientEmail());
        widgetOptions.setAttachments(EchoSignUtils.prepareAttachmentList(resolver, config));
        return widgetOptions;
    }

    private static Attachment createAttachment(ResourceResolver resolver, String fileName, String filePath) throws IOException {
        Attachment attachment = null;
        Resource fileResource = resolver.getResource(filePath);
        if (!ResourceUtil.isNonExistingResource((Resource)fileResource)) {
            attachment = new Attachment(fileName, IOUtils.toByteArray((InputStream)((InputStream)fileResource.adaptTo(InputStream.class))));
        } else {
            log.error("Non existant resource at path:" + filePath);
        }
        return attachment;
    }

    public static String getLocaleCode(String localeString) {
        String[] localeParts = null;
        if (localeString.indexOf("-") != -1) {
            localeParts = localeString.split("-");
        } else if (localeString.indexOf("_") != -1) {
            localeParts = localeString.split("_");
        }
        if (localeParts != null) {
            String enumString = localeParts[0].toLowerCase() + "_" + localeParts[1].toUpperCase();
            try {
                EchoSignConstants.LOCALES locale = EchoSignConstants.LOCALES.valueOf(enumString);
                log.debug("Deduced locale code " + locale.getCode() + " for locale String:" + localeString);
                return locale.getCode();
            }
            catch (IllegalArgumentException illegalArgumentException) {
                log.error("Unrecognized locale: " + localeString, (Throwable)illegalArgumentException);
            }
        } else {
            log.warn("Locale string format <language code>-/_<country code> not adhered by locale string:" + localeString);
        }
        return null;
    }

    public static String[] getNextSigners(List<ParticipantSetInfo> participantSets) throws AdobeSignServiceException {
        ArrayList<String> nextSigners = null;
        if (participantSets != null && !participantSets.isEmpty()) {
            nextSigners = new ArrayList<String>();
            for (ParticipantSetInfo setInfo : participantSets) {
                for (ParticipantInfo participantInfo : setInfo.getMembers()) {
                    nextSigners.add(participantInfo.getEmail());
                }
            }
        }
        if (nextSigners != null) {
            return nextSigners.toArray(new String[10]);
        }
        return null;
    }
}