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 package info.magnolia.module.templatingkit.dam.handlers;
35
36 import info.magnolia.cms.core.Content;
37 import info.magnolia.cms.core.NodeData;
38 import info.magnolia.cms.i18n.I18nContentSupport;
39 import info.magnolia.module.templatingkit.dam.Asset;
40 import info.magnolia.module.templatingkit.dam.AssetNotFoundException;
41 import info.magnolia.module.templatingkit.dam.DAMException;
42 import info.magnolia.module.templatingkit.dam.assets.InternalAsset;
43 import info.magnolia.objectfactory.Components;
44 import info.magnolia.templating.functions.TemplatingFunctions;
45
46 import javax.inject.Inject;
47 import javax.jcr.PathNotFoundException;
48 import javax.jcr.RepositoryException;
49
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53
54
55
56
57
58 public class InternalContentDAMHandler extends AbstractInternalContentDAMHandler {
59
60 private final TemplatingFunctions templatingFunctions;
61 private static Logger log = LoggerFactory.getLogger(InternalContentDAMHandler.class);
62
63 @Inject
64 public InternalContentDAMHandler(TemplatingFunctions templatingFunctions){
65 this.templatingFunctions = templatingFunctions;
66 }
67
68
69 @Override
70 public Asset getAsset(final Content node, final String nodeDataPrefix) throws DAMException {
71 try {
72 return new InternalAsset(this, node, nodeDataPrefix, getBinaryNodeData(node, nodeDataPrefix), templatingFunctions);
73 }
74 catch (RepositoryException e) {
75 throw new DAMException("Can't find asset " + nodeDataPrefix + " for content [" + node.getHandle() + "]", e);
76 }
77 }
78
79 @Override
80 public Asset getAssetByKey(String key) {
81 throw new UnsupportedOperationException();
82 }
83
84 public NodeData getBinaryNodeData(final Content node, final String nodeDataPrefix) throws AssetNotFoundException {
85 final NodeData nd = node.getNodeData(getNodeDataName(node, nodeDataPrefix));
86 if (!nd.isExist()) {
87 throw new AssetNotFoundException(node.getHandle() + "/" + nodeDataPrefix + " has no binary stored");
88 }
89 return nd;
90 }
91
92 @Override
93 protected String getNodeDataName(Content node, String nodeDataPrefix) {
94 String nodeDataName = nodeDataPrefix + getNodeDataSuffix();
95 I18nContentSupport i18n = Components.getComponent(I18nContentSupport.class);
96 try {
97 nodeDataName = i18n.getNode(node.getJCRNode(), nodeDataName).getName();
98 } catch (PathNotFoundException e) {
99
100 } catch (Exception e) {
101 log.warn("Failed to determine locale of nodeData '{}' for '{}'.", new Object[] { nodeDataName, node, e });
102 }
103 return nodeDataName;
104 }
105
106 }
107