DownloadAssetProcess.java
8.58 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.Externalizer
* com.day.cq.dam.api.jobs.AssetDownloadService
* com.day.cq.dam.commons.process.AbstractAssetWorkflowProcess
* com.day.cq.workflow.WorkflowException
* com.day.cq.workflow.WorkflowSession
* com.day.cq.workflow.exec.WorkItem
* com.day.cq.workflow.exec.WorkflowData
* com.day.cq.workflow.metadata.MetaDataMap
* javax.jcr.Session
* org.apache.commons.lang.StringUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.ReferenceCardinality
* org.apache.felix.scr.annotations.ReferencePolicy
* org.apache.felix.scr.annotations.Service
* org.apache.jackrabbit.util.Text
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.dam.core.process;
import com.day.cq.commons.Externalizer;
import com.day.cq.dam.api.jobs.AssetDownloadService;
import com.day.cq.dam.commons.process.AbstractAssetWorkflowProcess;
import com.day.cq.workflow.WorkflowException;
import com.day.cq.workflow.WorkflowSession;
import com.day.cq.workflow.exec.WorkItem;
import com.day.cq.workflow.exec.WorkflowData;
import com.day.cq.workflow.metadata.MetaDataMap;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.zip.ZipOutputStream;
import javax.jcr.Session;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.util.Text;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0)
@Service
@Properties(value={@Property(name="service.description", value={"Download selected asset(s)."}), @Property(name="process.label", value={"Download Dam Asset"})})
public class DownloadAssetProcess
extends AbstractAssetWorkflowProcess {
private static final Logger log = LoggerFactory.getLogger(DownloadAssetProcess.class);
private static final String DOWNLOADASSETS = "downloadAssets";
private static final String DOWNLOADRENDITIONS = "downloadRenditions";
private static final String DOWNLOADSUBASSETS = "downloadSubassets";
private static final String S7EXPORTSETTINGS = "s7exportsettings";
private static final String EMAILTO = "emailTo";
private static final String CONTEXTPATH = "contextPath";
@Reference(cardinality=ReferenceCardinality.MANDATORY_UNARY, policy=ReferencePolicy.STATIC)
public AssetDownloadService assetDownloadService;
public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaData) throws WorkflowException {
try {
int idx;
Session session = workflowSession.getSession();
ResourceResolver rr = this.getResourceResolver(session);
String payload = workItem.getWorkflowData().getPayload().toString();
String srcPath = payload.split(".assetdownload.zip")[0];
srcPath = URLDecoder.decode(srcPath, "UTF-8");
Resource resource = rr.getResource(srcPath);
boolean downloadAssets = Boolean.parseBoolean(this.getValueFromPayload("downloadAssets", payload));
boolean downloadRenditions = Boolean.parseBoolean(this.getValueFromPayload("downloadRenditions", payload));
boolean downloadSubassets = Boolean.parseBoolean(this.getValueFromPayload("downloadSubassets", payload));
String s7exportsettings = this.getValueFromPayload("s7exportsettings", payload);
if (StringUtils.isNotBlank((String)s7exportsettings)) {
s7exportsettings = Text.unescape((String)s7exportsettings);
}
String downloadName = (idx = payload.indexOf("?")) == -1 ? "" : payload.substring(0, idx);
downloadName = this.getDownloadName(Text.unescape((String)downloadName));
ArrayList<String> paths = this.getAssetPath(payload);
HashSet<Resource> downloadSet = new HashSet<Resource>();
if (paths != null && paths.size() > 0) {
for (String path : paths) {
Resource res = rr.getResource(path = URLDecoder.decode(path, "UTF-8"));
if (res == null) continue;
downloadSet.add(res);
}
} else {
downloadSet.add(resource);
}
MetaDataMap wfMetaDataMap = workItem.getWorkflowData().getMetaDataMap();
String emailRecipients = (String)wfMetaDataMap.get("emailTo", (Object)new String());
String downloadUrl = this.assetDownloadService.assetDownload(resource, downloadSet, downloadAssets, downloadRenditions, downloadSubassets, s7exportsettings, null, null, downloadName, emailRecipients);
if (!StringUtils.isEmpty((String)downloadUrl)) {
String contextPath = (String)wfMetaDataMap.get("contextPath", (Object)new String());
downloadUrl = System.getProperty("launchpad.http.server.url", this.getHostPrefix(rr)) + contextPath + downloadUrl;
wfMetaDataMap.put((Object)"downloadUrl", (Object)downloadUrl);
} else {
log.debug("downloadUrl is null or empty.");
}
}
catch (Exception e) {
log.error("execute: error while processing download asset; work item [{}]: ", (Object)workItem.getId(), (Object)e);
}
}
private ArrayList<String> getAssetPath(String arg) {
ArrayList<String> paths = new ArrayList<String>();
try {
int beginIdx = arg.indexOf("path=");
while (beginIdx != -1) {
int endIdx = arg.indexOf("&", beginIdx + 1);
endIdx = endIdx == -1 ? arg.length() : endIdx;
String path = arg.substring(beginIdx + "path+".length(), endIdx);
paths.add(URLDecoder.decode(path, "UTF-8"));
beginIdx = arg.indexOf("path=", endIdx + 1);
}
}
catch (Exception e) {
log.error("unable to parse asset paths for download: " + arg, (Throwable)e);
}
return paths;
}
private String getValueFromPayload(String key, String argment) {
String value = null;
int beginIdx = argment.indexOf(key);
if (beginIdx != -1 && this.validKeyParameter(key, argment.substring(beginIdx - 1, beginIdx + key.length() + 1))) {
int endIdx = argment.indexOf("&", beginIdx);
endIdx = endIdx == -1 ? argment.length() : endIdx;
value = beginIdx == -1 ? "" : argment.substring(beginIdx + (key + "=").length(), endIdx);
}
return value;
}
private boolean validKeyParameter(String key, String argment) {
boolean valid = false;
List<String> params = Arrays.asList(argment.split("&|\\?|="));
for (String param : params) {
if (param.length() == 0) continue;
valid = param.equals(key);
}
return valid;
}
private String getDownloadName(String pathInfo) {
String name = pathInfo.substring(pathInfo.lastIndexOf("/") + 1, pathInfo.length());
name = (name = name.replaceAll("[ :()*|]", "")).endsWith(".zip") ? name : name + ".zip";
return name;
}
private String getHostPrefix(ResourceResolver resolver) {
Externalizer externalizer = (Externalizer)resolver.adaptTo(Externalizer.class);
String externalizerHost = externalizer.externalLink(resolver, "local", "");
if (externalizerHost != null && externalizerHost.endsWith("/")) {
return externalizerHost.substring(0, externalizerHost.length() - 1);
}
return externalizerHost;
}
protected void bindAssetDownloadService(AssetDownloadService assetDownloadService) {
this.assetDownloadService = assetDownloadService;
}
protected void unbindAssetDownloadService(AssetDownloadService assetDownloadService) {
if (this.assetDownloadService == assetDownloadService) {
this.assetDownloadService = null;
}
}
}