1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.opencastproject.mediapackage.elementbuilder;
24
25 import org.opencastproject.mediapackage.Catalog;
26 import org.opencastproject.mediapackage.CatalogImpl;
27 import org.opencastproject.mediapackage.MediaPackageElement;
28 import org.opencastproject.mediapackage.MediaPackageElement.Type;
29 import org.opencastproject.mediapackage.MediaPackageElementFlavor;
30 import org.opencastproject.mediapackage.MediaPackageReferenceImpl;
31 import org.opencastproject.mediapackage.MediaPackageSerializer;
32 import org.opencastproject.mediapackage.UnsupportedElementException;
33 import org.opencastproject.util.Checksum;
34 import org.opencastproject.util.MimeType;
35 import org.opencastproject.util.MimeTypes;
36
37 import org.apache.commons.lang3.StringUtils;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.w3c.dom.Node;
41 import org.w3c.dom.NodeList;
42
43 import java.net.URI;
44 import java.net.URISyntaxException;
45 import java.security.NoSuchAlgorithmException;
46
47 import javax.xml.xpath.XPath;
48 import javax.xml.xpath.XPathConstants;
49 import javax.xml.xpath.XPathExpressionException;
50 import javax.xml.xpath.XPathFactory;
51
52
53
54
55
56 public class CatalogBuilderPlugin implements MediaPackageElementBuilderPlugin {
57
58
59 protected XPath xpath = XPathFactory.newInstance().newXPath();
60
61
62
63
64 private static final Logger logger = LoggerFactory.getLogger(CatalogBuilderPlugin.class);
65
66
67
68
69
70 @Override
71 public boolean accept(MediaPackageElement.Type type, MediaPackageElementFlavor flavor) {
72 return type.equals(MediaPackageElement.Type.Catalog);
73 }
74
75
76
77
78 @Override
79 public boolean accept(Node elementNode) {
80 String name = elementNode.getNodeName();
81 if (name.contains(":")) {
82 name = name.substring(name.indexOf(":") + 1);
83 }
84 return name.equalsIgnoreCase(MediaPackageElement.Type.Catalog.toString());
85 }
86
87
88
89
90
91
92 @Override
93 public boolean accept(URI uri, MediaPackageElement.Type type, MediaPackageElementFlavor flavor) {
94 return MediaPackageElement.Type.Catalog.equals(type);
95 }
96
97
98
99
100 @Override
101 public MediaPackageElement elementFromURI(URI uri) throws UnsupportedElementException {
102 logger.trace("Creating video track from " + uri);
103 Catalog track = CatalogImpl.fromURI(uri);
104 return track;
105 }
106
107 @Override
108 public String toString() {
109 return "Indefinite Catalog Builder Plugin";
110 }
111
112
113
114
115
116
117 @Override
118 public void destroy() {
119 }
120
121
122
123
124
125
126
127 @Override
128 public MediaPackageElement elementFromManifest(Node elementNode, MediaPackageSerializer serializer)
129 throws UnsupportedElementException {
130 String id = null;
131 String flavor = null;
132 URI url = null;
133 long size = -1;
134 Checksum checksum = null;
135 MimeType mimeType = null;
136 String reference = null;
137
138 try {
139
140 id = (String) xpath.evaluate("@id", elementNode, XPathConstants.STRING);
141
142
143 url = serializer.decodeURI(new URI(xpath.evaluate("url/text()", elementNode).trim()));
144
145
146 flavor = (String) xpath.evaluate("@type", elementNode, XPathConstants.STRING);
147
148
149 reference = (String) xpath.evaluate("@ref", elementNode, XPathConstants.STRING);
150
151
152 String documentSize = xpath.evaluate("size/text()", elementNode).trim();
153 if (!"".equals(documentSize))
154 size = Long.parseLong(documentSize);
155
156
157 String checksumValue = (String) xpath.evaluate("checksum/text()", elementNode, XPathConstants.STRING);
158 String checksumType = (String) xpath.evaluate("checksum/@type", elementNode, XPathConstants.STRING);
159 if (StringUtils.isNotEmpty(checksumValue) && checksumType != null)
160 checksum = Checksum.create(checksumType.trim(), checksumValue.trim());
161
162
163 String mimeTypeValue = (String) xpath.evaluate("mimetype/text()", elementNode, XPathConstants.STRING);
164 if (StringUtils.isNotEmpty(mimeTypeValue))
165 mimeType = MimeTypes.parseMimeType(mimeTypeValue);
166
167
168 Catalog dc = CatalogImpl.fromURI(url);
169 if (StringUtils.isNotEmpty(id))
170 dc.setIdentifier(id);
171
172
173 dc.setURI(url);
174
175
176 if (flavor != null)
177 dc.setFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
178
179
180 if (StringUtils.isNotEmpty(reference))
181 dc.referTo(MediaPackageReferenceImpl.fromString(reference));
182
183
184 if (size > 0)
185 dc.setSize(size);
186
187
188 if (checksum != null)
189 dc.setChecksum(checksum);
190
191
192 if (mimeType != null)
193 dc.setMimeType(mimeType);
194
195
196 NodeList tagNodes = (NodeList) xpath.evaluate("tags/tag", elementNode, XPathConstants.NODESET);
197 for (int i = 0; i < tagNodes.getLength(); i++) {
198 dc.addTag(tagNodes.item(i).getTextContent());
199 }
200
201 return dc;
202 } catch (XPathExpressionException e) {
203 throw new UnsupportedElementException("Error while reading catalog information from manifest: " + e.getMessage());
204 } catch (NoSuchAlgorithmException e) {
205 throw new UnsupportedElementException("Unsupported digest algorithm: " + e.getMessage());
206 } catch (URISyntaxException e) {
207 throw new UnsupportedElementException("Error while reading dublin core catalog " + url + ": " + e.getMessage());
208 }
209 }
210
211
212
213
214
215
216
217 @Override
218 public MediaPackageElement newElement(Type type, MediaPackageElementFlavor flavor) {
219 Catalog cat = CatalogImpl.newInstance();
220 cat.setFlavor(flavor);
221 return cat;
222 }
223
224
225
226
227
228
229 @Override
230 public void init() throws Exception {
231 }
232
233 }