AbstractSubAssetHandler.java
3.81 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
/*
* 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.commons.util.DamUtil
* javax.jcr.Binary
* javax.jcr.Item
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.ValueFactory
* org.apache.felix.scr.annotations.Component
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.dam.core;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.AssetManager;
import com.day.cq.dam.commons.util.DamUtil;
import com.day.cq.dam.core.AbstractAssetHandler;
import java.io.InputStream;
import java.util.Calendar;
import javax.jcr.Binary;
import javax.jcr.Item;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.ValueFactory;
import org.apache.felix.scr.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(componentAbstract=1, metatype=0)
public abstract class AbstractSubAssetHandler
extends AbstractAssetHandler {
public static final String TYPE_JCR_PATH = "JCR_PATH";
private static final Logger log = LoggerFactory.getLogger(AbstractSubAssetHandler.class);
protected Node initializeSubAsset(String parentFilePath, String assetName, InputStream is, Session session, String mimeType, boolean doSave) throws RepositoryException {
String parentAssetPath = DamUtil.binaryToAssetPath((String)parentFilePath);
String subAssetPath = parentAssetPath + "/" + "subassets" + "/" + assetName;
if (!session.itemExists(parentAssetPath + "/" + "subassets")) {
Node parent = (Node)session.getItem(parentAssetPath);
parent.addNode("subassets", "nt:unstructured");
if (doSave) {
session.save();
}
}
Asset subAsset = this.getAssetManager(session).createAssetForBinary(subAssetPath, doSave);
Node renditionFolder = this.getRenditionFolder(subAsset.getPath(), session);
this.createFileNode(renditionFolder, is, mimeType, doSave);
return (Node)subAsset.adaptTo(Node.class);
}
protected Node getRenditionFolder(String path, Session session) throws RepositoryException {
Asset asset = this.getAssetManager(session).getAssetForBinary(path);
return ((Node)asset.adaptTo(Node.class)).getNode("jcr:content/renditions");
}
protected Node createFileNode(Node parent, InputStream is, String mimeType, boolean doSave) throws RepositoryException {
if (parent.hasNode("original")) {
Node content = parent.getNode("original/jcr:content");
this.setFileContent(content, is, mimeType);
if (doSave) {
content.getSession().save();
}
} else {
Node file = parent.addNode("original", "nt:file");
Node content = file.addNode("jcr:content", "nt:resource");
this.setFileContent(content, is, mimeType);
if (doSave) {
parent.getSession().save();
}
}
return parent.getNode("original");
}
protected void setFileContent(Node content, InputStream is, String mimeType) throws RepositoryException {
content.setProperty("jcr:mimeType", mimeType);
content.setProperty("jcr:data", content.getSession().getValueFactory().createBinary(is));
content.setProperty("jcr:lastModified", Calendar.getInstance());
}
protected boolean isSubasset(Node node) {
try {
return node.getPath().indexOf("/subassets/") > 0;
}
catch (RepositoryException e) {
log.error("isSubAsset: error while determining sub-asset status for asset [{}]: ", (Object)this.safeGetPath(node), (Object)e);
return false;
}
}
}