AbstractAssetWorkflowProcess.java
8.66 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.dam.api.Asset
* com.day.cq.dam.api.AssetManager
* com.day.cq.dam.api.handler.AssetHandler
* com.day.cq.dam.api.handler.store.AssetStore
* com.day.cq.workflow.exec.WorkItem
* com.day.cq.workflow.exec.Workflow
* com.day.cq.workflow.exec.WorkflowData
* com.day.cq.workflow.exec.WorkflowProcess
* javax.jcr.Item
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.UnsupportedRepositoryOperationException
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.ReferencePolicy
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.commons.mime.MimeTypeService
* org.apache.sling.jcr.resource.JcrResourceResolverFactory
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.dam.commons.process;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.AssetManager;
import com.day.cq.dam.api.handler.AssetHandler;
import com.day.cq.dam.api.handler.store.AssetStore;
import com.day.cq.dam.commons.util.DamUtil;
import com.day.cq.workflow.exec.WorkItem;
import com.day.cq.workflow.exec.Workflow;
import com.day.cq.workflow.exec.WorkflowData;
import com.day.cq.workflow.exec.WorkflowProcess;
import java.util.LinkedList;
import java.util.List;
import javax.jcr.Item;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.UnsupportedRepositoryOperationException;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.mime.MimeTypeService;
import org.apache.sling.jcr.resource.JcrResourceResolverFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(componentAbstract=1, metatype=0)
public abstract class AbstractAssetWorkflowProcess
implements WorkflowProcess {
private static final Logger log = LoggerFactory.getLogger(AbstractAssetWorkflowProcess.class);
public static final String TYPE_JCR_PATH = "JCR_PATH";
protected static final String APPLICATION_OCTET_STREAM_MIMETYPE = "application/octet-stream";
@Reference(policy=ReferencePolicy.STATIC)
protected MimeTypeService mimeTypeService;
@Reference(policy=ReferencePolicy.STATIC)
private AssetStore store;
@Reference(policy=ReferencePolicy.STATIC)
private JcrResourceResolverFactory jcrResolverFactory;
protected Asset getAssetFromPayload(WorkItem item, Session session) {
Asset asset = null;
if (item.getWorkflowData().getPayloadType().equals("JCR_PATH")) {
String path = item.getWorkflowData().getPayload().toString();
Resource resource = this.getResourceResolver(session).getResource(path);
if (null != resource) {
asset = DamUtil.resolveToAsset(resource);
} else {
log.error("getAssetFromPaylod: asset [{}] in payload of workflow [{}] does not exist.", (Object)path, (Object)item.getWorkflow().getId());
}
}
return asset;
}
protected Node getNodeFromPayload(WorkItem item, Session session) {
Node asset = null;
if (item.getWorkflowData().getPayloadType().equals("JCR_PATH")) {
String path = item.getWorkflowData().getPayload().toString();
try {
if (session.itemExists(path)) {
asset = (Node)session.getItem(path);
} else {
log.warn("getNodeFromPayload: payload node [{}] for work item [" + item.getId() + "] does not exist anymore", (Object)path);
}
}
catch (RepositoryException e) {
log.error("getNodeFromPayload: error while getting payload node [{}] for work item [" + item.getId() + "]: ", (Object)path, (Object)e);
}
}
return asset;
}
protected String getMimetype(Node file) {
String mimetype = null;
try {
if (file.isNodeType("nt:file") && file.hasNode("jcr:content") && file.hasProperty("jcr:content/jcr:mimeType")) {
mimetype = file.getProperty("jcr:content/jcr:mimeType").getString();
} else if (file.isNodeType("dam:Asset") && file.hasProperty("jcr:content/renditions/original/jcr:content/jcr:mimeType")) {
mimetype = file.getProperty("jcr:content/renditions/original/jcr:content/jcr:mimeType").getString();
}
mimetype = this.recheck(mimetype, file);
}
catch (RepositoryException e) {
log.error("getMimetype: error while getting mime type for file [{}]: ", (Object)this.safeGetPath(file), (Object)e);
}
return mimetype;
}
protected boolean isNotReadyForProcessing(Node asset) throws RepositoryException {
if (asset.hasProperty("jcr:content/jcr:data")) {
Property data = asset.getProperty("jcr:content/jcr:data");
if (data.getLength() == 0) {
log.info("asset not ready for processing: is 0 bytes: {}", (Object)asset.getPath());
return true;
}
try {
int numRetries = 1;
while (asset.isLocked()) {
log.info("asset not ready for processing {}: is locked...retry {}/4", (Object)asset.getPath(), (Object)numRetries);
try {
Thread.sleep(500);
}
catch (InterruptedException e) {
// empty catch block
}
if (++numRetries <= 4) continue;
log.info("asset not ready for processing: is locked: {}", (Object)asset.getPath());
return true;
}
}
catch (UnsupportedRepositoryOperationException e) {
log.info("isNotReadyForProcessing: repository does not support locking; asset: [{}]: ", (Object)this.safeGetPath(asset), (Object)e);
}
return false;
}
return true;
}
protected List<String> getValuesFromArgs(String key, String[] arguments) {
LinkedList<String> values = new LinkedList<String>();
for (String str : arguments) {
if (!str.startsWith(key + ":")) continue;
String mt = str.substring((key + ":").length()).trim();
values.add(mt);
}
return values;
}
protected String recheck(String mimeType, Node file) throws RepositoryException {
String name;
String mType = mimeType;
if ((mimeType == null || mimeType.equals("application/octet-stream")) && this.mimeTypeService.getMimeType(name = file.getName().toLowerCase()) != null) {
mType = this.mimeTypeService.getMimeType(name);
}
return mType;
}
protected String safeGetPath(Node node) {
try {
return node.getPath();
}
catch (RepositoryException e) {
log.warn("safeGetPath: error while getting path from node: ", (Throwable)e);
return "(unknown)";
}
}
protected ResourceResolver getResourceResolver(Session session) {
return this.jcrResolverFactory.getResourceResolver(session);
}
protected AssetManager getAssetManager(Session session) {
return (AssetManager)this.getResourceResolver(session).adaptTo(AssetManager.class);
}
protected AssetHandler getAssetHandler(String mimeType) {
return this.store.getAssetHandler(mimeType);
}
protected void bindMimeTypeService(MimeTypeService mimeTypeService) {
this.mimeTypeService = mimeTypeService;
}
protected void unbindMimeTypeService(MimeTypeService mimeTypeService) {
if (this.mimeTypeService == mimeTypeService) {
this.mimeTypeService = null;
}
}
protected void bindStore(AssetStore assetStore) {
this.store = assetStore;
}
protected void unbindStore(AssetStore assetStore) {
if (this.store == assetStore) {
this.store = null;
}
}
protected void bindJcrResolverFactory(JcrResourceResolverFactory jcrResourceResolverFactory) {
this.jcrResolverFactory = jcrResourceResolverFactory;
}
protected void unbindJcrResolverFactory(JcrResourceResolverFactory jcrResourceResolverFactory) {
if (this.jcrResolverFactory == jcrResourceResolverFactory) {
this.jcrResolverFactory = null;
}
}
}