Scene7AssetUtils.java
6.75 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Node
* javax.jcr.RepositoryException
* org.apache.commons.io.FilenameUtils
* org.apache.sling.api.resource.Resource
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.dam.scene7.impl.utils;
import com.day.cq.dam.scene7.api.S7Config;
import com.day.cq.dam.scene7.api.Scene7Service;
import com.day.cq.dam.scene7.api.model.Scene7Asset;
import com.day.cq.dam.scene7.api.model.Scene7AssetImpl;
import com.day.cq.dam.scene7.impl.protocol.NameValue;
import com.day.cq.dam.scene7.impl.protocol.is.ISProtocolParser;
import com.day.cq.dam.scene7.impl.protocol.is.ParamCommand;
import com.day.cq.dam.scene7.impl.protocol.is.ParamUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import org.apache.commons.io.FilenameUtils;
import org.apache.sling.api.resource.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Scene7AssetUtils {
private static final Logger LOG = LoggerFactory.getLogger(Scene7AssetUtils.class);
public static String getAbsoluteTargetPath(String rootPath, String targetPath, String scene7AssetFolderPath) {
String path = null;
if (rootPath != null && scene7AssetFolderPath != null && targetPath != null) {
if (!rootPath.endsWith("/")) {
rootPath = rootPath + "/";
}
if (!targetPath.endsWith("/")) {
targetPath = targetPath + "/";
}
StringBuilder pathSB = new StringBuilder(targetPath);
pathSB.append(scene7AssetFolderPath.replaceFirst(rootPath, ""));
path = pathSB.toString();
}
return path;
}
public static String extractTemplateParams(String urlModifier) {
String templateParams = null;
if (urlModifier != null) {
NameValue root = ISProtocolParser.parse(urlModifier, true);
Vector<NameValue> params = ParamUtils.getParameters(root);
StringBuilder sb = new StringBuilder();
for (int j = 0; j < params.size(); ++j) {
sb.append("&type=").append(((ParamCommand)params.elementAt(j)).getType()).append(params.elementAt(j).nodeString(false, true));
}
if (sb.length() > 0) {
templateParams = sb.toString();
}
if (templateParams != null && templateParams.length() > 0) {
templateParams = templateParams.substring(1);
}
}
return templateParams;
}
public static Scene7Asset extractTemplateSizeInformation(Scene7Asset scene7Asset, Scene7Service scene7Service, S7Config s7Config) {
String urlMod = scene7Asset.getUrlModifier();
if (urlMod != null) {
try {
NameValue root = ISProtocolParser.parse(urlMod, true);
NameValue layer0 = root.getNode("layer", "0");
NameValue size = layer0 != null ? layer0.getNode("size") : null;
NameValue src = layer0 != null ? layer0.getNode("src") : null;
Long width = null;
Long height = null;
if (size != null) {
String[] sizeArr = size.getValue().split(",");
if (sizeArr.length > 1) {
width = Long.parseLong(sizeArr[0]);
height = Long.parseLong(sizeArr[1]);
}
} else if (src != null && (scene7Asset = scene7Service.getAssociatedAssets(scene7Asset, s7Config)).getSubAssets().size() > 0) {
Scene7Asset firstSubAsset = scene7Asset.getSubAssets().get(0);
width = firstSubAsset.getWidth();
height = firstSubAsset.getHeight();
}
HashMap<Object, Object> changedAssetProperties = new HashMap<Object, Object>();
changedAssetProperties.put((Object)Scene7AssetImpl.Scene7AssetProperty.WIDTH, width);
changedAssetProperties.put((Object)Scene7AssetImpl.Scene7AssetProperty.HEIGHT, height);
scene7Asset = new Scene7AssetImpl(scene7Asset, changedAssetProperties);
}
catch (Exception e) {
LOG.warn("Could not extract template size information for asset {}!", (Object)scene7Asset.getName());
}
}
return scene7Asset;
}
public static Integer parseInt(String string) {
Integer value = null;
try {
value = Integer.parseInt(string);
}
catch (NumberFormatException e) {
// empty catch block
}
return value;
}
public static Long parseLong(String string) {
Long value = null;
try {
value = Long.parseLong(string);
}
catch (NumberFormatException e) {
// empty catch block
}
return value;
}
public static String getExtensionForFileReference(String fileReference) {
String file;
int index = fileReference.lastIndexOf("/");
String extension = null;
if (index != -1 && "".equals(extension = FilenameUtils.getExtension((String)(file = fileReference.substring(index + 1))))) {
extension = null;
}
return extension;
}
public static Node getDamMetadataNode(Resource resource) {
if (resource == null) {
return null;
}
Resource metadataResource = resource.getChild("jcr:content/metadata");
if (metadataResource == null) {
return null;
}
Node metadataNode = (Node)metadataResource.adaptTo(Node.class);
try {
if (metadataNode != null && metadataNode.hasProperty("dam:scene7File") && metadataNode.hasProperty("dam:scene7Type")) {
return metadataNode;
}
}
catch (RepositoryException e) {
LOG.error("Failed to get DAM metadata node dam asset " + resource.getPath(), (Throwable)e);
}
return null;
}
public static Node getDamMetadataInProgressNode(Resource resource) {
if (resource == null) {
return null;
}
Resource metadataResource = resource.getChild("jcr:content/metadata");
if (metadataResource == null) {
return null;
}
Node metadataNode = (Node)metadataResource.adaptTo(Node.class);
try {
if (metadataNode != null && metadataNode.hasProperty("dam:scene7FileStatus")) {
return metadataNode;
}
}
catch (RepositoryException e) {
LOG.error("Failed to get DAM metadata node dam asset " + resource.getPath(), (Throwable)e);
}
return null;
}
}