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  
96    /**
97     * Creates an attachment.
98     *
99     * @param uri
100    *          the attachments location
101    */
102   protected AttachmentImpl(URI uri) {
103     this(null, null, uri, null, null, null);
104   }
105 
106   /**
107    * Creates a new attachment from the url.
108    *
109    * @param uri
110    *          the attachment location
111    * @return the attachment
112    */
113   public static Attachment fromURI(URI uri) {
114     return new AttachmentImpl(uri);
115   }
116 
117   @Override
118   public Map<String, String> getProperties() {
119     if (properties == null) {
120       properties = new HashMap<String, String>();
121     }
122 
123     return properties;
124   }
125 
126   /**
127    * JAXB properties xml adapter class.
128    */
129   private static class PropertiesXmlAdapter extends XmlAdapter<PropertiesAdapter, Map<String, String>> {
130 
131     @Override
132     public Map<String, String> unmarshal(PropertiesAdapter pa) throws Exception {
133       Map<String, String> properties = new HashMap<>();
134       if (pa != null) {
135         for (Property p : pa.propertiesList) {
136           properties.put(p.key, p.value);
137         }
138       }
139       return properties;
140     }
141 
142     @Override
143     public PropertiesAdapter marshal(Map<String, String> p) throws Exception {
144       if (p == null || p.size() == 0) {
145         return null;
146       }
147 
148       PropertiesAdapter pa = new PropertiesAdapter();
149       for (String key : p.keySet()) {
150         pa.propertiesList.add(new Property(key, p.get(key)));
151       }
152       return pa;
153     }
154   }
155 
156   /**
157    * Properties map to list of entries adapter class.
158    */
159   private static class PropertiesAdapter {
160     @XmlElement(name = "property")
161     private List<Property> propertiesList;
162 
163     PropertiesAdapter() {
164       this(new LinkedList<Property>());
165     }
166 
167     PropertiesAdapter(List<Property> propertiesList) {
168       this.propertiesList = propertiesList;
169     }
170   }
171 
172   /**
173    * Properties entry adapter class.
174    */
175   private static class Property {
176     @XmlAttribute(name = "key")
177     private String key;
178     @XmlValue
179     private String value;
180 
181     Property() {
182       // Default constructor
183     }
184 
185     Property(String key, String value) {
186       this.key = key;
187       this.value = value;
188     }
189   }
190 
191   public static class Adapter extends XmlAdapter<AttachmentImpl, Attachment> {
192     public AttachmentImpl marshal(Attachment mp) throws Exception {
193       return (AttachmentImpl) mp;
194     }
195 
196     public Attachment unmarshal(AttachmentImpl mp) throws Exception {
197       return mp;
198     }
199   }
200 }