DownloadResource.java
13.7 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.text.Text
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.commons.lang3.StringEscapeUtils
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.resource.ResourceWrapper
* org.apache.sling.api.resource.ValueMap
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.commons;
import com.day.cq.commons.DiffInfo;
import com.day.text.Text;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang3.StringEscapeUtils;
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.api.resource.ResourceWrapper;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DownloadResource
extends ResourceWrapper {
private static final Logger log = LoggerFactory.getLogger(DownloadResource.class);
public static final String PN_REFERENCE = "fileReference";
public static final String NN_FILE = "file";
public static final String PN_FILE_NAME = "fileName";
public static final String PN_TITLE = "jcr:title";
public static final String PN_DESCRIPTION = "jcr:description";
protected final ValueMap properties;
protected final Node node;
private final Map<String, String> overlaid = new HashMap<String, String>();
private String fileNodePath = null;
private String source = null;
private String extension = ".res";
private String selector = "";
private String suffix = null;
private String query = null;
protected Map<String, String> attrs;
private Object innerHtml;
private Property data;
private Map<String, String> itemNames = new HashMap<String, String>();
private String diffVersionLabel;
private boolean isTouchAuthoringUIMode = false;
public DownloadResource(Resource resource) {
super(resource);
this.node = (Node)resource.adaptTo(Node.class);
ValueMap props = null;
try {
props = ResourceUtil.getValueMap((Resource)resource);
}
catch (Exception e) {
// empty catch block
}
this.properties = props == null ? ValueMap.EMPTY : props;
DiffInfo info = (DiffInfo)resource.adaptTo(DiffInfo.class);
if (info != null) {
String path;
int idx;
Resource dr = info.getContent();
if (dr != null && (idx = (path = dr.getPath()).indexOf("/jcr:frozenNode/")) > 0) {
this.diffVersionLabel = Text.getName((String)path.substring(0, idx), (char)'/');
this.addQueryParam("cq_diffTo", this.diffVersionLabel);
}
if (info.getType() != DiffInfo.TYPE.SAME) {
this.addQueryParam("cq_diffType", info.getType().name());
}
}
}
public boolean isTouchAuthoringUIMode() {
return this.isTouchAuthoringUIMode;
}
public void setIsInUITouchMode(boolean inUITouchMode) {
this.isTouchAuthoringUIMode = inUITouchMode;
}
public String get(String name) {
String value = this.overlaid.get(name);
if (value == null) {
value = (String)this.properties.get(name, (Object)"");
}
return value;
}
public int get(String name, int defaultValue) {
try {
return Integer.parseInt(this.get(name));
}
catch (Exception e) {
return defaultValue;
}
}
public void set(String name, String value) {
this.overlaid.put(name, value);
}
public void addAttribute(String name, String value) {
if (this.attrs == null) {
this.attrs = new HashMap<String, String>();
}
this.attrs.put(name, value);
}
public void addCssClass(String name) {
if (this.attrs == null || !this.attrs.containsKey("class")) {
this.addAttribute("class", name);
} else {
String prev = this.attrs.get("class");
if (prev.length() == 0) {
prev = name;
} else if (!(prev.equals(name) || prev.contains(" " + name) || prev.contains(name + " "))) {
prev = prev + " " + name;
}
this.attrs.put("class", prev);
}
}
public void init() {
if (this.source == null) {
String fileName;
if (this.fileNodePath == null) {
this.fileNodePath = this.getItemName("file");
}
if (this.fileNodePath.length() > 0 && this.fileNodePath.charAt(0) != '/') {
try {
this.fileNodePath = this.node != null && this.node.hasNode(this.fileNodePath) ? this.node.getNode(this.fileNodePath).getPath() : "";
}
catch (RepositoryException e) {
this.fileNodePath = "";
log.warn("Error while accessing the repository.", (Throwable)e);
}
}
if ((fileName = this.get(this.getItemName("fileName"))).length() == 0) {
String fileRef = this.getFileReference();
fileName = fileRef != null && fileRef.length() > 0 ? Text.getName((String)fileRef) : "";
this.set(this.getItemName("fileName"), fileName);
}
if (this.suffix == null) {
this.setSuffix(fileName);
}
if (this.selector.length() > 0 && !ResourceUtil.isNonExistingResource((Resource)this)) {
this.source = this.getPath() + this.getSelector() + this.getExtension() + this.getSuffix();
} else if (this.fileNodePath.length() > 0) {
this.source = this.fileNodePath + this.getExtension() + this.getSuffix();
} else if (this.getFileReference().length() > 0) {
this.source = this.getFileReference();
}
}
}
public String getItemName(String name) {
return this.itemNames.containsKey(name) ? this.itemNames.get(name) : name;
}
public void setItemName(String key, String name) {
this.itemNames.put(key, name);
}
public String getFileReference() {
return this.get(this.getItemName("fileReference"));
}
public void setFileReference(String fileReference) {
this.set(this.getItemName("fileReference"), fileReference);
}
public Object getInnerHtml() {
return this.innerHtml;
}
public void setInnerHtml(Object innerHtml) {
this.innerHtml = innerHtml;
}
public String getFileNodePath() {
this.init();
return this.fileNodePath != null ? this.fileNodePath : "";
}
public void setFileNodePath(String fileNodePath) {
this.fileNodePath = fileNodePath;
}
public String getFileName() {
this.init();
return this.get(this.getItemName("fileName"));
}
public void setFileName(String fileName) {
this.set(this.getItemName("fileName"), fileName);
}
public String getTitle() {
return this.getTitle(false);
}
public String getTitle(boolean escape) {
return escape ? StringEscapeUtils.escapeHtml4((String)this.get(this.getItemName("jcr:title"))) : this.get(this.getItemName("jcr:title"));
}
public void setTitle(String title) {
this.set(this.getItemName("jcr:title"), title);
}
public String getDescription() {
return this.getDescription(false);
}
public String getDescription(boolean escape) {
return escape ? StringEscapeUtils.escapeHtml4((String)this.get(this.getItemName("jcr:description"))) : this.get(this.getItemName("jcr:description"));
}
public void setDescription(String description) {
this.set(this.getItemName("jcr:description"), description);
}
public String getHref() {
this.init();
return this.source;
}
public void setHref(String href) {
this.source = href;
}
public String getQuery() {
return this.query;
}
public void setQuery(String query) {
this.query = query;
}
public void addQueryParam(String name, String value) {
this.query = this.query == null || this.query.length() == 0 ? "?" : this.query + "&";
this.query = this.query + Text.escape((String)name) + "=" + Text.escape((String)value);
}
public String getExtension() {
return this.extension;
}
public void setExtension(String extension) {
this.extension = extension == null ? "" : (!extension.startsWith(".") ? "." + extension : extension);
}
public String getIconType() {
String fileName = this.getFileName();
if (fileName.lastIndexOf(46) > 0) {
return fileName.substring(fileName.lastIndexOf(46) + 1).toLowerCase();
}
return "dat";
}
@Deprecated
public String getIconPath() {
return null;
}
public String getSuffix() {
return this.suffix;
}
public void setSuffix(String suffix) {
if (this.source != null) {
log.warn("Illegal call to setSuffix() after source already calculated.");
}
this.suffix = suffix == null || suffix.length() == 0 ? "" : (!suffix.startsWith("/") ? "/" + suffix : suffix);
}
public String getSelector() {
return this.selector;
}
public void setSelector(String selector) {
if (this.source != null) {
log.warn("Illegal call to setSelector() after source already calculated.");
}
this.selector = selector == null ? "" : (!selector.startsWith(".") ? "." + selector : selector);
}
public boolean hasContent() {
try {
return this.getData() != null;
}
catch (RepositoryException e) {
return false;
}
}
public void draw(Writer w) throws IOException {
if (!this.hasContent()) {
return;
}
PrintWriter out = new PrintWriter(w);
Object[] arrobject = new Object[2];
arrobject[0] = Text.escape((String)this.getHref(), (char)'%', (boolean)true);
arrobject[1] = this.query == null ? "" : this.query;
out.printf("<a href=\"%s%s\" ", arrobject);
out.printf("title=\"%s\" ", this.getTitle(true));
if (this.attrs != null) {
for (Map.Entry<String, String> e : this.attrs.entrySet()) {
out.printf("%s=\"%s\" ", StringEscapeUtils.escapeHtml4((String)e.getKey().toString()), StringEscapeUtils.escapeHtml4((String)e.getValue().toString()));
}
}
out.print(">");
out.print((Object)(this.innerHtml == null ? this.getFileName() : this.innerHtml));
out.print("</a>");
}
public String getString() {
StringWriter w = new StringWriter();
try {
this.draw(w);
}
catch (IOException e) {
// empty catch block
}
return w.getBuffer().toString();
}
public String getMimeType() throws RepositoryException {
Property data = this.getData();
if (data == null) {
return null;
}
return data.getParent().getProperty("jcr:mimeType").getString();
}
public Calendar getLastModified() throws RepositoryException {
Property data = this.getData();
if (data == null) {
return null;
}
return data.getParent().getProperty("jcr:lastModified").getDate();
}
/*
* Enabled force condition propagation
* Lifted jumps to return sites
*/
public Property getData() throws RepositoryException {
Node fileNode;
if (this.data != null) {
return this.data;
}
String ref = this.getFileReference();
if (ref.length() > 0) {
if (ref.charAt(0) != '/') {
Session s = this.node == null ? (Session)this.getResourceResolver().adaptTo(Session.class) : this.node.getSession();
fileNode = s.getNodeByUUID(ref);
} else {
Resource res = this.getReferencedResource(ref);
if (res == null) return null;
this.data = (Property)res.adaptTo(Property.class);
if (this.data != null) {
return this.data;
}
fileNode = (Node)res.adaptTo(Node.class);
if (fileNode == null) {
return null;
}
}
} else if (this.node != null) {
if (!this.node.hasNode(this.getItemName("file"))) return null;
fileNode = this.node.getNode(this.getItemName("file"));
} else {
Resource fileResource = this.getResource().getChild(this.getItemName("file"));
if (fileResource == null) return null;
fileNode = (Node)fileResource.adaptTo(Node.class);
if (fileNode == null) {
return null;
}
}
if (fileNode.hasNode("jcr:content")) {
fileNode = fileNode.getNode("jcr:content");
}
if (!fileNode.hasProperty("jcr:data")) return null;
this.data = fileNode.getProperty("jcr:data");
return this.data;
}
protected Resource getReferencedResource(String path) {
return this.getResourceResolver().getResource(path);
}
public Map<String, String> getAttributes() {
return this.attrs;
}
}