EchoSignUtils.java
5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* 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;
}
}