CreateWebEnabledImageProcess.java
8.11 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
186
187
188
189
190
191
192
193
194
195
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.dam.api.Asset
* com.day.cq.dam.api.Rendition
* com.day.cq.dam.api.renditions.RenditionMaker
* com.day.cq.dam.api.renditions.RenditionTemplate
* 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.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.commons.lang.StringUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.ValueMap
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.dam.core.process;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.Rendition;
import com.day.cq.dam.api.renditions.RenditionMaker;
import com.day.cq.dam.api.renditions.RenditionTemplate;
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.util.Calendar;
import java.util.List;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0)
@Service
@Property(name="process.label", value={"Create Web Enabled Image"})
public class CreateWebEnabledImageProcess
extends AbstractAssetWorkflowProcess {
private static final Logger log = LoggerFactory.getLogger(CreateWebEnabledImageProcess.class);
@Reference
protected RenditionMaker renditionMaker;
public void createWebEnabledImage(WorkItem workItem, Config config, Asset asset, RenditionMaker renditionMaker) throws RepositoryException {
String lastModified;
if (this.handleAsset(asset, config)) {
asset.setBatchMode(true);
RenditionTemplate template = renditionMaker.createWebRenditionTemplate(asset, config.width, config.height, config.quality, config.mimeType, config.mimeTypesToKeep);
renditionMaker.generateRenditions(asset, new RenditionTemplate[]{template});
}
Node assetNode = (Node)asset.adaptTo(Node.class);
Node content = assetNode.getNode("jcr:content");
String resolvedUser = (String)workItem.getWorkflowData().getMetaDataMap().get("userId", String.class);
Rendition rendition = asset.getRendition("original");
if (rendition != null && StringUtils.isNotBlank((String)(lastModified = (String)rendition.getProperties().get((Object)"jcr:lastModifiedBy")))) {
resolvedUser = lastModified;
}
content.setProperty("jcr:lastModifiedBy", resolvedUser);
content.setProperty("jcr:lastModified", Calendar.getInstance());
}
public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaData) throws WorkflowException {
Asset asset = this.getAssetFromPayload(workItem, workflowSession.getSession());
if (asset == null) {
String wfPayload = workItem.getWorkflowData().getPayload().toString();
String message = "execute: cannot create web enabled image, asset [{" + wfPayload + "}] in payload doesn't exist for workflow [{" + workItem.getId() + "}].";
throw new WorkflowException(message);
}
Config config = this.parseConfig(metaData);
try {
this.createWebEnabledImage(workItem, config, asset, this.renditionMaker);
}
catch (RepositoryException re) {
throw new WorkflowException((Throwable)re);
}
}
public Config parseConfig(MetaDataMap metaData) {
String processArgs = (String)metaData.get(Arguments.PROCESS_ARGS.name(), String.class);
if (StringUtils.isNotEmpty((String)processArgs)) {
return this.parseLegacyConfig(processArgs);
}
Config cfg = new Config();
cfg.width = ((Long)metaData.get(Arguments.WIDTH.name(), (Object)1000)).intValue();
cfg.height = ((Long)metaData.get(Arguments.HEIGHT.name(), (Object)1000)).intValue();
cfg.mimeType = (String)metaData.get(Arguments.MIME_TYPE.name(), (Object)"image/png");
cfg.quality = ((Long)metaData.get(Arguments.QUALITY.name(), (Object)(cfg.mimeType.equals("image/gif") ? 256 : 60))).intValue();
cfg.mimeTypesToKeep = (String[])metaData.get(Arguments.KEEP_FORMAT_LIST.name(), (Object)new String[]{"image/pjpeg", "image/jpeg", "image/jpg", "image/gif", "image/png", "image/x-png"});
cfg.skipMimeTypes = (String[])metaData.get(Arguments.SKIP.name(), (Object)new String[0]);
return cfg;
}
private Config parseLegacyConfig(String processArgs) {
Config cfg = new Config();
String[] args = processArgs.split(",");
String dimension = this.getFirstValueFromArgs(args, Arguments.DIMENSION.legacyName, null);
if (dimension == null) {
cfg.width = 1000;
cfg.height = 1000;
} else {
String[] dim = dimension.split(":");
cfg.width = Integer.valueOf(dim[0]);
cfg.height = Integer.valueOf(dim[1]);
}
cfg.mimeType = this.getFirstValueFromArgs(args, Arguments.MIME_TYPE.legacyName, "image/png");
String keepFormat = this.getFirstValueFromArgs(args, Arguments.KEEP_FORMAT_LIST.legacyName, "image/pjpeg,image/jpeg,image/jpg,image/gif,image/png,image/x-png");
cfg.mimeTypesToKeep = keepFormat.split(",");
String qualityStr = this.getFirstValueFromArgs(args, Arguments.QUALITY.legacyName, cfg.mimeType.equals("image/gif") ? "256" : "60");
cfg.quality = Integer.valueOf(qualityStr);
List values = this.getValuesFromArgs(Arguments.SKIP.legacyName, args);
cfg.skipMimeTypes = values.toArray(new String[values.size()]);
return cfg;
}
private String getFirstValueFromArgs(String[] arguments, String key, String defaultValue) {
List values = this.getValuesFromArgs(key, arguments);
if (values.size() > 0) {
return (String)values.get(0);
}
return defaultValue;
}
protected boolean handleAsset(Asset asset, Config config) {
if (asset == null || config.skipMimeTypes == null) {
return true;
}
String mimeType = asset.getMimeType();
if (mimeType == null) {
return true;
}
for (String val : config.skipMimeTypes) {
if (!mimeType.matches(val)) continue;
log.debug(this.getClass().getName() + " skipped for MIME type: " + mimeType);
return false;
}
return true;
}
protected void bindRenditionMaker(RenditionMaker renditionMaker) {
this.renditionMaker = renditionMaker;
}
protected void unbindRenditionMaker(RenditionMaker renditionMaker) {
if (this.renditionMaker == renditionMaker) {
this.renditionMaker = null;
}
}
public static class Config {
public int width;
public int height;
public int quality;
public String mimeType;
public String[] mimeTypesToKeep;
public String[] skipMimeTypes;
}
public static enum Arguments {
PROCESS_ARGS("PROCESS_ARGS"),
DIMENSION("dimension"),
WIDTH("width"),
HEIGHT("height"),
QUALITY("quality"),
MIME_TYPE("mimetype"),
KEEP_FORMAT_LIST("keepFormatList"),
SKIP("skip");
public final String legacyName;
private Arguments(String legacyName) {
this.legacyName = legacyName;
}
}
}