VideoHandler.java
8.06 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.dam.api.Asset
* com.day.cq.dam.api.Rendition
* com.day.cq.dam.commons.util.DamUtil
* org.apache.commons.codec.DecoderException
* org.apache.commons.codec.net.URLCodec
* org.apache.commons.lang.StringUtils
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.commons.osgi.OsgiUtil
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.commerce.impl.asset;
import com.adobe.cq.commerce.api.asset.ProductAssetHandler;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.Rendition;
import com.day.cq.dam.commons.util.DamUtil;
import java.util.Arrays;
import java.util.Collections;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.net.URLCodec;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
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.commons.osgi.OsgiUtil;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(immediate=1, metatype=1, label="Adobe CQ Commerce Product Asset Handler for Videos", description="Manages product assets of type: videos.")
@Service(value={ProductAssetHandler.class})
@Property(name="service.description", value={"Manages product assets of type: static videos"})
public class VideoHandler
implements ProductAssetHandler {
private static final Logger log = LoggerFactory.getLogger(VideoHandler.class);
static final boolean DEFAULT_ACTIVE = true;
@Property(label="%Active", description="%Make this handler active.", boolValue={1})
protected static final String CONFIG_PROPERTY_ACTIVE = "cq.commerce.asset.handler.active";
static final String DEFAULT_HANDLER_NAME = "video";
@Property(label="%Handler Name", description="%Name of the handler", value={"video"})
private static final String CONFIG_PROPERTY_HANDLER_NAME = "cq.commerce.asset.handler.name";
private static final String[] VID_MIME_TYPES = new String[]{"video/m4v", "video/flv", "video/avi", "video/mov", "video/3gpp", "application/x-troff-msvideo", "video/vnd", "model/vnd.mts", "video/ts", "video/vnd", "video/dvd", "video/x-ms-wmv", "video/msvideo", "video/x-msvideo", "video/x-flv", "video/mpeg", "video/x-mpeg", "video/x-m4v", "video/mpg", "video/x-mpg", "video/mpeg2", "video/x-mpeg2a", "video/mts", "video/x-ms-asf", "video/3gpp2", "video/x-f4v", "video/f4v", "video/m2p", "video/mp2t", "video/avchd-stream", "video/m2ts", "video/mp2t", "video/vnd.dlna.mpeg-tts", "video/m2v", "video/quicktime", "video/x-quicktime", "video/mp4", "video/ogg", "video/x-mxf", "application/mxf", "video/x-matroska", "video/mj2", "video/vnd.rn-realvideo", "application/vnd.rn-realmedia", "video/webm"};
private static final List<String> MIME_TYPES_LIST = Arrays.asList(VID_MIME_TYPES);
private static final String RES_TYPE = "foundation/components/video";
private static final String PN_ASSET_REFERENCE = "asset";
private static final int DEFAULT_THUMBNAIL_WIDTH = 319;
private static final Map<String, Object> PRODUCT_ASSET_RENDERING_PROPERTIES;
private boolean isActive;
private String handlerName;
@Activate
protected void activate(ComponentContext context) throws Exception {
this.isActive = OsgiUtil.toBoolean(context.getProperties().get("cq.commerce.asset.handler.active"), (boolean)true);
this.handlerName = OsgiUtil.toString(context.getProperties().get("cq.commerce.asset.handler.name"), (String)"video");
}
@Override
public boolean isActive() {
return this.isActive;
}
@Override
public String getHandlerName() {
return this.handlerName;
}
@Override
public Map<String, Object> getAssetProperties(ResourceResolver resolver, String assetReference) {
HashMap<String, Object> props = new HashMap<String, Object>();
props.put("asset", assetReference);
props.putAll(PRODUCT_ASSET_RENDERING_PROPERTIES);
return props;
}
@Override
public String getReferencedAsset(Resource productAssetResource) {
Asset asset = this.getAsset(productAssetResource);
if (asset != null) {
return asset.getPath();
}
return null;
}
@Override
public boolean isSupportedReferencedAsset(ResourceResolver resolver, String assetReference) {
Asset asset = this.getAsset(resolver, assetReference);
return this.supports(asset);
}
@Override
public boolean isSupportedAsset(Resource productAssetResource) {
Asset asset = this.getAsset(productAssetResource);
return this.supports(asset);
}
private boolean supports(Asset asset) {
if (asset == null) {
log.debug("The video asset is not defined.");
return false;
}
String dcFormat = asset.getMetadataValue("dc:format");
if (StringUtils.isEmpty((String)dcFormat)) {
log.debug("The dc:format of the video asset is not defined.");
return false;
}
return MIME_TYPES_LIST.contains(dcFormat);
}
@Override
public String getThumbnailUrl(Resource productAssetResource, String selectorString) {
String[] arrstring;
Rendition bestFitRendition;
List renditions;
Asset asset = this.getAsset(productAssetResource);
if (asset == null) {
return null;
}
if (selectorString != null) {
arrstring = selectorString.split("\\.");
} else {
String[] arrstring2 = new String[1];
arrstring = arrstring2;
arrstring2[0] = "";
}
String[] selectors = arrstring;
int width = 319;
for (String selector : selectors) {
try {
width = Integer.parseInt(selector);
break;
}
catch (NumberFormatException nfe) {
continue;
}
}
if ((bestFitRendition = DamUtil.getBestFitRendition((int)width, (List)(renditions = asset.getRenditions()))) == null) {
return null;
}
return bestFitRendition.getPath();
}
private Asset getAsset(Resource productAssetResource) {
if (productAssetResource == null) {
return null;
}
ValueMap props = (ValueMap)productAssetResource.adaptTo(ValueMap.class);
String assetReference = (String)props.get("asset", (Object)"");
return this.getAsset(productAssetResource.getResourceResolver(), assetReference);
}
private Asset getAsset(ResourceResolver resolver, String assetReference) {
if (StringUtils.isEmpty((String)assetReference)) {
log.debug("The video property {} is not defined", (Object)"asset");
return null;
}
try {
assetReference = new URLCodec().decode(assetReference);
}
catch (DecoderException e) {
log.error("Error while decoding fileReference: {}", (Object)assetReference);
}
Resource assetRes = resolver.getResource(assetReference);
if (assetRes == null) {
return null;
}
return DamUtil.resolveToAsset((Resource)assetRes);
}
static {
HashMap<String, String> tmp = new HashMap<String, String>();
tmp.put("sling:resourceType", "foundation/components/video");
PRODUCT_ASSET_RENDERING_PROPERTIES = Collections.unmodifiableMap(tmp);
}
}