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.MediaPackageElement;
26 import org.opencastproject.mediapackage.MediaPackageElement.Type;
27 import org.opencastproject.mediapackage.MediaPackageElementFlavor;
28 import org.opencastproject.mediapackage.MediaPackageReferenceImpl;
29 import org.opencastproject.mediapackage.MediaPackageSerializer;
30 import org.opencastproject.mediapackage.Publication;
31 import org.opencastproject.mediapackage.PublicationImpl;
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.XPathConstants;
48 import javax.xml.xpath.XPathExpressionException;
49
50 public class PublicationBuilderPlugin extends AbstractElementBuilderPlugin {
51
52
53
54
55 private static final Logger logger = LoggerFactory.getLogger(PublicationBuilderPlugin.class);
56
57 @Override
58 public boolean accept(Type type, MediaPackageElementFlavor flavor) {
59 return type.equals(MediaPackageElement.Type.Publication);
60 }
61
62 @Override
63 public boolean accept(URI uri, Type type, MediaPackageElementFlavor flavor) {
64 return MediaPackageElement.Type.Publication.equals(type);
65 }
66
67 @Override
68 public boolean accept(Node elementNode) {
69 String name = elementNode.getNodeName();
70 if (name.contains(":")) {
71 name = name.substring(name.indexOf(":") + 1);
72 }
73 return name.equalsIgnoreCase(MediaPackageElement.Type.Publication.toString());
74 }
75
76 @Override
77 public MediaPackageElement elementFromURI(URI uri)
78 throws UnsupportedElementException {
79
80 logger.trace("Creating publication element from " + uri);
81 Publication publication = new PublicationImpl();
82 publication.setURI(uri);
83 return publication;
84 }
85
86 @Override
87 public MediaPackageElement elementFromManifest(Node elementNode,
88 MediaPackageSerializer serializer)
89 throws UnsupportedElementException {
90
91 String id = null;
92 MimeType mimeType = null;
93 MediaPackageElementFlavor flavor = null;
94 String reference = null;
95 String channel = null;
96 URI url = null;
97 long size = -1;
98 Checksum checksum = null;
99
100 try {
101
102 id = (String) xpath.evaluate("@id", elementNode, XPathConstants.STRING);
103 if (StringUtils.isEmpty(id)) {
104 throw new UnsupportedElementException("Unvalid or missing id argument!");
105 }
106
107
108 url = serializer.decodeURI(new URI(xpath.evaluate("url/text()", elementNode).trim()));
109
110
111 channel = xpath.evaluate("@channel", elementNode).trim();
112 if (StringUtils.isEmpty(channel)) {
113 throw new UnsupportedElementException("Unvalid or missing channel argument!");
114 }
115
116
117 reference = (String) xpath.evaluate("@ref", elementNode, XPathConstants.STRING);
118
119
120 String trackSize = xpath.evaluate("size/text()", elementNode).trim();
121 if (!"".equals(trackSize)) {
122 size = Long.parseLong(trackSize);
123 }
124
125
126 String flavorValue = (String) xpath.evaluate("@type", elementNode, XPathConstants.STRING);
127 if (StringUtils.isNotEmpty(flavorValue)) {
128 flavor = MediaPackageElementFlavor.parseFlavor(flavorValue);
129 }
130
131
132 String checksumValue = (String) xpath.evaluate("checksum/text()", elementNode, XPathConstants.STRING);
133 String checksumType = (String) xpath.evaluate("checksum/@type", elementNode, XPathConstants.STRING);
134 if (StringUtils.isNotEmpty(checksumValue) && checksumType != null) {
135 checksum = Checksum.create(checksumType.trim(), checksumValue.trim());
136 }
137
138
139 String mimeTypeValue = (String) xpath.evaluate("mimetype/text()", elementNode, XPathConstants.STRING);
140 if (StringUtils.isNotEmpty(mimeTypeValue)) {
141 mimeType = MimeTypes.parseMimeType(mimeTypeValue);
142 } else {
143 throw new UnsupportedElementException("Unvalid or missing mimetype argument!");
144 }
145
146
147 PublicationImpl publication = new PublicationImpl(id, channel, url, mimeType);
148
149 if (StringUtils.isNotBlank(id)) {
150 publication.setIdentifier(id);
151 }
152
153
154 publication.setURI(url);
155
156
157 if (StringUtils.isNotEmpty(reference)) {
158 publication.referTo(MediaPackageReferenceImpl.fromString(reference));
159 }
160
161
162 if (size > 0) {
163 publication.setSize(size);
164 }
165
166
167 if (checksum != null) {
168 publication.setChecksum(checksum);
169 }
170
171
172 if (mimeType != null) {
173 publication.setMimeType(mimeType);
174 }
175
176 if (flavor != null) {
177 publication.setFlavor(flavor);
178 }
179
180
181 String description = (String) xpath.evaluate("description/text()", elementNode, XPathConstants.STRING);
182 if (StringUtils.isNotBlank(description)) {
183 publication.setElementDescription(description.trim());
184 }
185
186
187 NodeList tagNodes = (NodeList) xpath.evaluate("tags/tag", elementNode, XPathConstants.NODESET);
188 for (int i = 0; i < tagNodes.getLength(); i++) {
189 publication.addTag(tagNodes.item(i).getTextContent());
190 }
191
192 return publication;
193 } catch (XPathExpressionException e) {
194 throw new UnsupportedElementException("Error while reading track information from manifest: " + e.getMessage());
195 } catch (NoSuchAlgorithmException e) {
196 throw new UnsupportedElementException("Unsupported digest algorithm: " + e.getMessage());
197 } catch (URISyntaxException e) {
198 throw new UnsupportedElementException("Error while reading presenter track " + url + ": " + e.getMessage());
199 }
200 }
201
202 @Override
203 public MediaPackageElement newElement(Type type,
204 MediaPackageElementFlavor flavor) {
205 Publication element = new PublicationImpl();
206 element.setFlavor(flavor);
207 return element;
208 }
209
210 }