CFMUtils.java
9.76 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.dam.cfm.VersionDef
* com.adobe.cq.dam.cfm.VersionedContent
* com.day.cq.dam.api.Asset
* com.day.cq.dam.api.Rendition
* com.day.cq.dam.api.Revision
* javax.jcr.Node
* javax.jcr.RepositoryException
* javax.jcr.version.Version
* org.apache.commons.io.IOUtils
* org.apache.commons.io.input.ReaderInputStream
* org.apache.sling.api.resource.PersistenceException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.resource.collection.ResourceCollection
* org.apache.sling.resource.collection.ResourceCollectionManager
*/
package com.adobe.cq.dam.cfm.impl;
import com.adobe.cq.dam.cfm.VersionDef;
import com.adobe.cq.dam.cfm.VersionedContent;
import com.adobe.cq.dam.cfm.impl.FragmentWriteException;
import com.adobe.cq.dam.cfm.impl.VersionDefImpl;
import com.adobe.cq.dam.cfm.impl.VersionedContentImpl;
import com.adobe.cq.dam.cfm.impl.VersioningException;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.Rendition;
import com.day.cq.dam.api.Revision;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.version.Version;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.ReaderInputStream;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.resource.collection.ResourceCollection;
import org.apache.sling.resource.collection.ResourceCollectionManager;
public class CFMUtils {
private CFMUtils() {
}
public static String getContentAsString(Rendition rendition) {
return CFMUtils.getContentAsString(rendition.getStream());
}
public static String getContentAsString(Resource renditionContent) {
ValueMap map = (ValueMap)renditionContent.adaptTo(ValueMap.class);
InputStream is = (InputStream)map.get("jcr:data", InputStream.class);
return is != null ? CFMUtils.getContentAsString(is) : null;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
* Loose catch block
* Enabled aggressive block sorting
* Enabled unnecessary exception pruning
* Enabled aggressive exception aggregation
* Lifted jumps to return sites
*/
private static String getContentAsString(InputStream is) {
int curChar;
String content = null;
Reader reader = null;
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuilder contentBuf = new StringBuilder();
do {
if ((curChar = reader.read()) == -1) continue;
contentBuf.append((char)curChar);
} while (curChar != -1);
content = contentBuf.toString();
try {
if (reader == null) return content;
reader.close();
return content;
}
catch (IOException ioe) {
return content;
}
catch (IOException ioe) {
try {
if (reader == null) return content;
reader.close();
return content;
}
catch (IOException ioe) {
return content;
}
catch (Throwable throwable) {
try {
if (reader == null) throw throwable;
reader.close();
throw throwable;
}
catch (IOException ioe) {
// empty catch block
}
throw throwable;
}
}
}
public static String getContentType(Rendition rendition) {
return rendition.getMimeType();
}
public static String getContentType(Resource renditionContent) {
ValueMap map = (ValueMap)renditionContent.adaptTo(ValueMap.class);
return (String)map.get("jcr:mimeType", String.class);
}
public static String removeExtension(String name) {
if (name == null) {
return null;
}
int extPos = name.lastIndexOf(".");
if (extPos > 0) {
name = name.substring(0, extPos);
}
return name;
}
public static Calendar getRenditionLastModified(Rendition rendition) {
ValueMap props = rendition.getProperties();
Calendar lastModified = (Calendar)props.get("jcr:lastModified", Calendar.class);
if (lastModified == null) {
lastModified = (Calendar)props.get("jcr:created", Calendar.class);
}
return lastModified;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public static Rendition setContent(Rendition rendition, String content, String contentType) throws FragmentWriteException {
Asset asset = rendition.getAsset();
boolean oldBatchMode = asset.isBatchMode();
asset.setBatchMode(true);
String renditionName = rendition.getName();
ReaderInputStream is = new ReaderInputStream((Reader)new StringReader(content), "UTF-8");
try {
Rendition newRendition = asset.addRendition(renditionName, (InputStream)is, contentType);
if (newRendition == null) {
throw new FragmentWriteException("Could not change content of rendition '" + renditionName);
}
Rendition rendition2 = newRendition;
return rendition2;
}
finally {
IOUtils.closeQuietly((InputStream)is);
asset.setBatchMode(oldBatchMode);
}
}
public static String getRevPath(Revision rev, String rendition, String subPath) throws RepositoryException {
return rev.getVersion().getFrozenNode().getPath() + (subPath != null ? new StringBuilder().append("/").append(subPath).toString() : "") + "/jcr:content/renditions/" + rendition + "/jcr:content";
}
public static long getRevTc(ResourceResolver resolver, Revision rev, String rendition, String subPath) throws RepositoryException {
String versionedPath = CFMUtils.getRevPath(rev, rendition, subPath);
Resource versioned = resolver.getResource(versionedPath);
ValueMap revProps = (ValueMap)versioned.adaptTo(ValueMap.class);
Calendar revTime = revProps.containsKey((Object)"jcr:lastModified") ? (Calendar)revProps.get("jcr:lastModified", Calendar.class) : (Calendar)revProps.get("jcr:created", Calendar.class);
return revTime != null ? revTime.getTimeInMillis() : -1;
}
public static Iterator<VersionDef> listVersions(Asset mainAsset, long lastModified, String renditionName, String subPath) throws VersioningException {
ArrayList<VersionDefImpl> versions = new ArrayList<VersionDefImpl>();
try {
Collection revisions = mainAsset.getRevisions(null);
ResourceResolver resolver = ((Resource)mainAsset.adaptTo(Resource.class)).getResourceResolver();
long changeTc = lastModified;
for (Revision rev : revisions) {
long revTc = CFMUtils.getRevTc(resolver, rev, renditionName, subPath);
if (revTc == changeTc) continue;
changeTc = revTc;
versions.add(new VersionDefImpl(rev));
}
}
catch (Exception e) {
throw new VersioningException("Could not retrieve list of revisions.", e);
}
return versions.iterator();
}
public static VersionedContent getVersionedContent(VersionDef version, Asset mainAsset, String renditionName, String subPath) throws VersioningException {
String versionName = null;
String content = null;
String contentType = null;
try {
String identifier = version.getIdentifier();
Collection revisions = mainAsset.getRevisions(null);
ResourceResolver resolver = ((Resource)mainAsset.adaptTo(Resource.class)).getResourceResolver();
for (Revision rev : revisions) {
if (!rev.getVersion().getIdentifier().equals(identifier)) continue;
String path = CFMUtils.getRevPath(rev, renditionName, subPath);
Resource versionedRendition = resolver.getResource(path);
if (versionedRendition == null) {
throw new VersioningException("No versioned rendition found at '" + path + "'.");
}
content = CFMUtils.getContentAsString(versionedRendition);
contentType = CFMUtils.getContentType(versionedRendition);
}
}
catch (Exception e) {
if (!(e instanceof VersioningException)) {
if (versionName != null) {
throw new VersioningException("Could get content of revision '" + versionName + "'.", e);
}
throw new VersioningException("Could not access version definition.", e);
}
throw (VersioningException)((Object)e);
}
return new VersionedContentImpl(content, contentType);
}
public static ResourceCollection createEmptyAssocContent(Resource assetResource, ResourceCollectionManager collectionManager) throws PersistenceException {
HashMap<String, String> collProps = new HashMap<String, String>();
collProps.put("jcr:primaryType", "nt:unstructured");
return collectionManager.createCollection(assetResource, "associated", collProps);
}
}