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
101
102 protected AttachmentImpl(URI uri) {
103 this(null, null, uri, null, null, null);
104 }
105
106
107
108
109
110
111
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
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
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
174
175 private static class Property {
176 @XmlAttribute(name = "key")
177 private String key;
178 @XmlValue
179 private String value;
180
181 Property() {
182
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 }