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  package org.opencastproject.mediapackage.attachment;
23  
24  import org.opencastproject.mediapackage.AbstractMediaPackageElement;
25  import org.opencastproject.mediapackage.Attachment;
26  import org.opencastproject.mediapackage.MediaPackageElementFlavor;
27  import org.opencastproject.util.Checksum;
28  import org.opencastproject.util.MimeType;
29  import org.opencastproject.util.MimeTypes;
30  import org.opencastproject.util.UnknownFileTypeException;
31  
32  import java.net.URI;
33  import java.util.HashMap;
34  import java.util.LinkedList;
35  import java.util.List;
36  import java.util.Map;
37  
38  import javax.xml.bind.annotation.XmlAttribute;
39  import javax.xml.bind.annotation.XmlElement;
40  import javax.xml.bind.annotation.XmlRootElement;
41  import javax.xml.bind.annotation.XmlType;
42  import javax.xml.bind.annotation.XmlValue;
43  import javax.xml.bind.annotation.adapters.XmlAdapter;
44  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
45  
46  /**
47   * Basic implementation of an attachment.
48   */
49  @XmlType(name = "attachment", namespace = "http://mediapackage.opencastproject.org")
50  @XmlRootElement(name = "attachment", namespace = "http://mediapackage.opencastproject.org")
51  public class AttachmentImpl extends AbstractMediaPackageElement implements Attachment {
52  
53    /** Serial version UID */
54    private static final long serialVersionUID = 6626531251856698138L;
55  
56    /** The object properties */
57    @XmlElement(name = "additionalProperties")
58    @XmlJavaTypeAdapter(PropertiesXmlAdapter.class)
59    protected Map<String, String> properties = null;
60  
61    /**
62     * Needed by JAXB
63     */
64    public AttachmentImpl() {
65      super(Type.Attachment, null, null);
66      properties = new HashMap<>();
67    }
68  
69    /**
70     * Creates an attachment.
71     *
72     * @param identifier
73     *          the attachment identifier
74     * @param flavor
75     *          the attachment type
76     * @param uri
77     *          the attachments location
78     * @param size
79     *          the attachments size
80     * @param checksum
81     *          the attachments checksum
82     * @param mimeType
83     *          the attachments mime type
84     */
85    protected AttachmentImpl(String identifier, MediaPackageElementFlavor flavor, URI uri, Long size, Checksum checksum,
86            MimeType mimeType) {
87      super(identifier, Type.Attachment, flavor, uri, size, checksum, mimeType);
88      if (uri != null)
89        try {
90          this.setMimeType(MimeTypes.fromURI(uri));
91        } catch (UnknownFileTypeException e) { }
92    }
93  
94    /**
95     * Creates an attachment.
96     *
97     * @param uri
98     *          the attachments location
99     */
100   protected AttachmentImpl(URI uri) {
101     this(null, null, uri, null, null, null);
102   }
103 
104   /**
105    * Creates a new attachment from the url.
106    *
107    * @param uri
108    *          the attachment location
109    * @return the attachment
110    */
111   public static Attachment fromURI(URI uri) {
112     return new AttachmentImpl(uri);
113   }
114 
115   @Override
116   public Map<String, String> getProperties() {
117     if (properties == null)
118       properties = new HashMap<String, String>();
119 
120     return properties;
121   }
122 
123   /**
124    * JAXB properties xml adapter class.
125    */
126   private static class PropertiesXmlAdapter extends XmlAdapter<PropertiesAdapter, Map<String, String>> {
127 
128     @Override
129     public Map<String, String> unmarshal(PropertiesAdapter pa) throws Exception {
130       Map<String, String> properties = new HashMap<>();
131       if (pa != null) {
132         for (Property p : pa.propertiesList) {
133           properties.put(p.key, p.value);
134         }
135       }
136       return properties;
137     }
138 
139     @Override
140     public PropertiesAdapter marshal(Map<String, String> p) throws Exception {
141       if (p == null || p.size() == 0) return null;
142 
143       PropertiesAdapter pa = new PropertiesAdapter();
144         for (String key : p.keySet()) {
145           pa.propertiesList.add(new Property(key, p.get(key)));
146         }
147       return pa;
148     }
149   }
150 
151   /**
152    * Properties map to list of entries adapter class.
153    */
154   private static class PropertiesAdapter {
155     @XmlElement(name = "property")
156     private List<Property> propertiesList;
157 
158     PropertiesAdapter() {
159       this(new LinkedList<Property>());
160     }
161 
162     PropertiesAdapter(List<Property> propertiesList) {
163       this.propertiesList = propertiesList;
164     }
165   }
166 
167   /**
168    * Properties entry adapter class.
169    */
170   private static class Property {
171     @XmlAttribute(name = "key")
172     private String key;
173     @XmlValue
174     private String value;
175 
176     Property() {
177       // Default constructor
178     }
179 
180     Property(String key, String value) {
181       this.key = key;
182       this.value = value;
183     }
184   }
185 
186   public static class Adapter extends XmlAdapter<AttachmentImpl, Attachment> {
187     public AttachmentImpl marshal(Attachment mp) throws Exception {
188       return (AttachmentImpl) mp;
189     }
190 
191     public Attachment unmarshal(AttachmentImpl mp) throws Exception {
192       return mp;
193     }
194   }
195 }