View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
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   * This implementation of the {@link MediaPackageElementBuilderPlugin} recognizes metadata catalogs and provides the
54   * functionality of reading it on behalf of the media package.
55   */
56  public class CatalogBuilderPlugin implements MediaPackageElementBuilderPlugin {
57  
58    /** The xpath facility */
59    protected XPath xpath = XPathFactory.newInstance().newXPath();
60  
61    /**
62     * the logging facility provided by log4j
63     */
64    private static final Logger logger = LoggerFactory.getLogger(CatalogBuilderPlugin.class);
65  
66    /**
67     * @see org.opencastproject.mediapackage.elementbuilder.MediaPackageElementBuilderPlugin#accept(org.opencastproject.mediapackage.MediaPackageElement.Type,
68     *      org.opencastproject.mediapackage.MediaPackageElementFlavor)
69     */
70    @Override
71    public boolean accept(MediaPackageElement.Type type, MediaPackageElementFlavor flavor) {
72      return type.equals(MediaPackageElement.Type.Catalog);
73    }
74  
75    /**
76     * @see org.opencastproject.mediapackage.elementbuilder.MediaPackageElementBuilderPlugin#accept(org.w3c.dom.Node)
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     * @see org.opencastproject.mediapackage.elementbuilder.MediaPackageElementBuilderPlugin#accept(URI,
89     *      org.opencastproject.mediapackage.MediaPackageElement.Type,
90     *      org.opencastproject.mediapackage.MediaPackageElementFlavor)
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     * @see org.opencastproject.mediapackage.elementbuilder.MediaPackageElementBuilderPlugin#elementFromURI(URI)
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    * {@inheritDoc}
114    *
115    * @see org.opencastproject.mediapackage.elementbuilder.MediaPackageElementBuilderPlugin#destroy()
116    */
117   @Override
118   public void destroy() {
119   }
120 
121   /**
122    * {@inheritDoc}
123    *
124    * @see org.opencastproject.mediapackage.elementbuilder.MediaPackageElementBuilderPlugin#elementFromManifest(org.w3c.dom.Node,
125    *      org.opencastproject.mediapackage.MediaPackageSerializer)
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       // id
140       id = (String) xpath.evaluate("@id", elementNode, XPathConstants.STRING);
141 
142       // url
143       url = serializer.decodeURI(new URI(xpath.evaluate("url/text()", elementNode).trim()));
144 
145       // flavor
146       flavor = (String) xpath.evaluate("@type", elementNode, XPathConstants.STRING);
147 
148       // reference
149       reference = (String) xpath.evaluate("@ref", elementNode, XPathConstants.STRING);
150 
151       // size
152       String documentSize = xpath.evaluate("size/text()", elementNode).trim();
153       if (!"".equals(documentSize))
154         size = Long.parseLong(documentSize);
155 
156       // checksum
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       // mimetype
163       String mimeTypeValue = (String) xpath.evaluate("mimetype/text()", elementNode, XPathConstants.STRING);
164       if (StringUtils.isNotEmpty(mimeTypeValue))
165         mimeType = MimeTypes.parseMimeType(mimeTypeValue);
166 
167       // create the catalog
168       Catalog dc = CatalogImpl.fromURI(url);
169       if (StringUtils.isNotEmpty(id))
170         dc.setIdentifier(id);
171 
172       // Add url
173       dc.setURI(url);
174 
175       // Add flavor
176       if (flavor != null)
177         dc.setFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
178 
179       // Add reference
180       if (StringUtils.isNotEmpty(reference))
181         dc.referTo(MediaPackageReferenceImpl.fromString(reference));
182 
183       // Set size
184       if (size > 0)
185         dc.setSize(size);
186 
187       // Set checksum
188       if (checksum != null)
189         dc.setChecksum(checksum);
190 
191       // Set Mimetype
192       if (mimeType != null)
193         dc.setMimeType(mimeType);
194 
195       // Tags
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    * {@inheritDoc}
213    *
214    * @see org.opencastproject.mediapackage.elementbuilder.MediaPackageElementBuilderPlugin#newElement(org.opencastproject.mediapackage.MediaPackageElement.Type,
215    *      org.opencastproject.mediapackage.MediaPackageElementFlavor)
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    * {@inheritDoc}
226    *
227    * @see org.opencastproject.mediapackage.elementbuilder.MediaPackageElementBuilderPlugin#init()
228    */
229   @Override
230   public void init() throws Exception {
231   }
232 
233 }