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;
23
24 import org.opencastproject.util.Checksum;
25 import org.opencastproject.util.IoSupport;
26 import org.opencastproject.util.MimeType;
27
28 import org.w3c.dom.Document;
29 import org.w3c.dom.Element;
30 import org.w3c.dom.Node;
31
32 import java.io.ByteArrayInputStream;
33 import java.io.ByteArrayOutputStream;
34 import java.io.Serializable;
35 import java.net.URI;
36 import java.net.URISyntaxException;
37 import java.util.Arrays;
38 import java.util.Collection;
39 import java.util.SortedSet;
40 import java.util.TreeSet;
41 import java.util.UUID;
42
43 import javax.xml.bind.JAXBException;
44 import javax.xml.bind.Marshaller;
45 import javax.xml.bind.Unmarshaller;
46 import javax.xml.bind.annotation.XmlAccessType;
47 import javax.xml.bind.annotation.XmlAccessorType;
48 import javax.xml.bind.annotation.XmlAttribute;
49 import javax.xml.bind.annotation.XmlElement;
50 import javax.xml.bind.annotation.XmlElementWrapper;
51 import javax.xml.bind.annotation.XmlID;
52 import javax.xml.bind.annotation.XmlTransient;
53
54
55
56
57 @XmlTransient
58 @XmlAccessorType(XmlAccessType.NONE)
59 public abstract class AbstractMediaPackageElement implements MediaPackageElement, Serializable {
60
61
62 private static final long serialVersionUID = 1L;
63
64
65 @XmlID
66 @XmlAttribute(name = "id")
67 protected String id = null;
68
69
70 protected Type elementType = null;
71
72
73 protected String description = null;
74
75
76 @XmlElement(name = "mimetype")
77 protected MimeType mimeType = null;
78
79
80 @XmlAttribute(name = "type")
81 protected MediaPackageElementFlavor flavor = null;
82
83
84 @XmlElementWrapper(name = "tags")
85 @XmlElement(name = "tag")
86 protected SortedSet<String> tags = new TreeSet<String>();
87
88
89 @XmlElement(name = "url")
90 protected URI uri = null;
91
92
93 @XmlElement(name = "size")
94 protected Long size = null;
95
96
97 @XmlElement(name = "checksum")
98 protected Checksum checksum = null;
99
100
101 protected MediaPackage mediaPackage = null;
102
103
104 @XmlAttribute(name = "ref")
105 protected MediaPackageReference reference = null;
106
107
108 protected AbstractMediaPackageElement() {
109 }
110
111
112
113
114
115
116
117
118
119
120
121 protected AbstractMediaPackageElement(Type elementType, MediaPackageElementFlavor flavor, URI uri) {
122 this(null, elementType, flavor, uri, null, null, null);
123 }
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141 protected AbstractMediaPackageElement(Type elementType, MediaPackageElementFlavor flavor, URI uri, Long size,
142 Checksum checksum, MimeType mimeType) {
143 this(null, elementType, flavor, uri, size, checksum, mimeType);
144 }
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164 protected AbstractMediaPackageElement(String id, Type elementType, MediaPackageElementFlavor flavor, URI uri,
165 Long size, Checksum checksum, MimeType mimeType) {
166 if (elementType == null) {
167 throw new IllegalArgumentException("Argument 'elementType' is null");
168 }
169 this.id = id;
170 this.elementType = elementType;
171 this.flavor = flavor;
172 this.mimeType = mimeType;
173 this.uri = uri;
174 this.size = size;
175 this.checksum = checksum;
176 this.tags = new TreeSet<String>();
177 }
178
179
180
181
182 @Override
183 public void setIdentifier(String id) {
184 this.id = id;
185 }
186
187 @Override
188 public String generateIdentifier() {
189 id = UUID.randomUUID().toString();
190 return id;
191 }
192
193
194
195
196 @Override
197 public String getIdentifier() {
198 return id;
199 }
200
201
202
203
204 @Override
205 public void setTags(String[] tags) {
206 this.tags = new TreeSet<String>(Arrays.asList(tags));
207 }
208
209
210
211
212 @Override
213 public void addTag(String tag) {
214 if (tag == null) {
215 throw new IllegalArgumentException("Tag must not be null");
216 }
217 tags.add(tag);
218 }
219
220
221
222
223 @Override
224 public void removeTag(String tag) {
225 if (tag == null) {
226 return;
227 }
228 tags.remove(tag);
229 }
230
231
232
233
234 @Override
235 public boolean containsTag(String tag) {
236 if (tag == null || tags == null) {
237 return false;
238 }
239 return tags.contains(tag);
240 }
241
242
243
244
245 @Override
246 public boolean containsTag(Collection<String> tags) {
247 if (tags == null || tags.size() == 0) {
248 return true;
249 }
250 for (String tag : tags) {
251 if (containsTag(tag)) {
252 return true;
253 }
254 }
255 return false;
256 }
257
258
259
260
261 @Override
262 public String[] getTags() {
263 return tags.toArray(new String[tags.size()]);
264 }
265
266
267
268
269 @Override
270 public void clearTags() {
271 if (tags != null) {
272 tags.clear();
273 }
274 }
275
276
277
278
279 @Override
280 public MediaPackage getMediaPackage() {
281 return mediaPackage;
282 }
283
284
285
286
287 @Override
288 public Type getElementType() {
289 return elementType;
290 }
291
292
293
294
295 @Override
296 public String getElementDescription() {
297 return (description != null) ? description : uri.toString();
298 }
299
300
301
302
303 @Override
304 public void setElementDescription(String name) {
305 this.description = name;
306 }
307
308
309
310
311 @Override
312 public MediaPackageReference getReference() {
313 return reference;
314 }
315
316
317
318
319 @Override
320 public void setReference(MediaPackageReference reference) {
321 this.reference = reference;
322 }
323
324
325
326
327 @Override
328 public URI getURI() {
329 return uri;
330 }
331
332
333
334
335 @Override
336 public void setURI(URI uri) {
337 this.uri = uri;
338 }
339
340
341
342
343 @Override
344 public Checksum getChecksum() {
345 return checksum;
346 }
347
348
349
350
351 @Override
352 public void setChecksum(Checksum checksum) {
353 this.checksum = checksum;
354 }
355
356
357
358
359 @Override
360 public MimeType getMimeType() {
361 return mimeType;
362 }
363
364
365
366
367 @Override
368 public void setMimeType(MimeType mimeType) {
369 this.mimeType = mimeType;
370 }
371
372
373
374
375 @Override
376 public void setFlavor(MediaPackageElementFlavor flavor) {
377 this.flavor = flavor;
378 }
379
380
381
382
383 @Override
384 public MediaPackageElementFlavor getFlavor() {
385 return flavor;
386 }
387
388
389
390
391 @Override
392 public long getSize() {
393 return size != null ? size : -1;
394 }
395
396
397
398
399 @Override
400 public void setSize(long size) {
401 this.size = size;
402 }
403
404
405
406
407
408
409
410
411
412 void setMediaPackage(MediaPackage mediaPackage) {
413 this.mediaPackage = mediaPackage;
414 }
415
416
417
418
419
420 @Override
421 public void referTo(MediaPackageElement element) {
422 referTo(new MediaPackageReferenceImpl(element));
423 }
424
425
426
427
428
429 @Override
430 public void referTo(MediaPackageReference reference) {
431
432 this.reference = reference;
433 }
434
435
436
437
438 @Override
439 public void clearReference() {
440 this.reference = null;
441 }
442
443
444
445
446 @Override
447 public void verify() throws MediaPackageException {
448
449
450
451
452
453
454
455 }
456
457
458
459
460 @Override
461 public int compareTo(MediaPackageElement o) {
462 return uri.toString().compareTo(o.getURI().toString());
463 }
464
465
466
467
468 @Override
469 public boolean equals(Object obj) {
470 if (!(obj instanceof MediaPackageElement)) {
471 return false;
472 }
473 MediaPackageElement e = (MediaPackageElement) obj;
474 if (mediaPackage != null && e.getMediaPackage() != null && !mediaPackage.equals(e.getMediaPackage())) {
475 return false;
476 }
477 if (id != null && !id.equals(e.getIdentifier())) {
478 return false;
479 }
480 if (uri != null && !uri.equals(e.getURI())) {
481 return false;
482 }
483 return true;
484 }
485
486
487
488
489 @Override
490 public int hashCode() {
491 final int prime = 31;
492 int result = 1;
493 result = prime * result + ((id == null) ? 0 : id.hashCode());
494 result = prime * result + ((mediaPackage == null) ? 0 : mediaPackage.hashCode());
495 result = prime * result + ((uri == null) ? 0 : uri.hashCode());
496 return result;
497 }
498
499
500
501
502
503 @Override
504 public Node toManifest(Document document, MediaPackageSerializer serializer) throws MediaPackageException {
505 Element node = document.createElement(elementType.toString().toLowerCase());
506 if (id != null) {
507 node.setAttribute("id", id);
508 }
509
510
511 if (flavor != null) {
512 node.setAttribute("type", flavor.toString());
513 }
514
515
516 if (reference != null) {
517 if (mediaPackage == null || !reference.matches(new MediaPackageReferenceImpl(mediaPackage))) {
518 node.setAttribute("ref", reference.toString());
519 }
520 }
521
522
523 if (description != null) {
524 Element descriptionNode = document.createElement("description");
525 descriptionNode.appendChild(document.createTextNode(description));
526 node.appendChild(descriptionNode);
527 }
528
529
530 if (tags.size() > 0) {
531 Element tagsNode = document.createElement("tags");
532 node.appendChild(tagsNode);
533 for (String tag : tags) {
534 Element tagNode = document.createElement("tag");
535 tagsNode.appendChild(tagNode);
536 tagNode.appendChild(document.createTextNode(tag));
537 }
538 }
539
540
541 Element urlNode = document.createElement("url");
542 String urlValue;
543 try {
544 urlValue = (serializer != null) ? serializer.encodeURI(uri).toString() : uri.toString();
545 } catch (URISyntaxException e) {
546 throw new MediaPackageException(e);
547 }
548 urlNode.appendChild(document.createTextNode(urlValue));
549 node.appendChild(urlNode);
550
551
552 if (mimeType != null) {
553 Element mimeNode = document.createElement("mimetype");
554 mimeNode.appendChild(document.createTextNode(mimeType.toString()));
555 node.appendChild(mimeNode);
556 }
557
558
559 if (size != null && size != -1) {
560 Element sizeNode = document.createElement("size");
561 sizeNode.appendChild(document.createTextNode(Long.toString(size)));
562 node.appendChild(sizeNode);
563 }
564
565
566 if (checksum != null) {
567 Element checksumNode = document.createElement("checksum");
568 checksumNode.setAttribute("type", checksum.getType().getName());
569 checksumNode.appendChild(document.createTextNode(checksum.getValue()));
570 node.appendChild(checksumNode);
571 }
572
573 return node;
574 }
575
576
577
578
579 @Override
580 public String toString() {
581 String s = (description != null) ? description : uri.toString();
582 return s.toLowerCase();
583 }
584
585
586
587
588
589 @Override
590 public Object clone() {
591 ByteArrayOutputStream out = new ByteArrayOutputStream();
592 ByteArrayInputStream in = null;
593 try {
594 Marshaller marshaller = MediaPackageImpl.context.createMarshaller();
595 marshaller.marshal(this, out);
596 Unmarshaller unmarshaller = MediaPackageImpl.context.createUnmarshaller();
597 in = new ByteArrayInputStream(out.toByteArray());
598
599
600 return unmarshaller.unmarshal(in);
601
602 } catch (JAXBException e) {
603 throw new RuntimeException(e.getLinkedException() != null ? e.getLinkedException() : e);
604 } finally {
605 IoSupport.closeQuietly(in);
606 }
607 }
608
609 }