1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
54 private static final long serialVersionUID = 6626531251856698138L;
55
56
57 @XmlElement(name = "additionalProperties")
58 @XmlJavaTypeAdapter(PropertiesXmlAdapter.class)
59 protected Map<String, String> properties = null;
60
61
62
63
64 public AttachmentImpl() {
65 super(Type.Attachment, null, null);
66 properties = new HashMap<>();
67 }
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
98
99
100 protected AttachmentImpl(URI uri) {
101 this(null, null, uri, null, null, null);
102 }
103
104
105
106
107
108
109
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
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
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
169
170 private static class Property {
171 @XmlAttribute(name = "key")
172 private String key;
173 @XmlValue
174 private String value;
175
176 Property() {
177
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 }