1package eu.siacs.conversations.xmpp.jingle;
2
3import com.google.common.collect.Collections2;
4import com.google.common.collect.ImmutableList;
5import com.google.common.collect.ImmutableSet;
6
7import eu.siacs.conversations.xmpp.jingle.stanzas.Content;
8import eu.siacs.conversations.xmpp.jingle.stanzas.GenericDescription;
9import eu.siacs.conversations.xmpp.jingle.stanzas.GenericTransportInfo;
10import eu.siacs.conversations.xmpp.jingle.stanzas.Group;
11import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
12
13import java.util.List;
14import java.util.Map;
15import java.util.Set;
16
17public abstract class AbstractContentMap<
18 D extends GenericDescription, T extends GenericTransportInfo> {
19
20 public final Group group;
21
22 public final Map<String, DescriptionTransport<D, T>> contents;
23
24 protected AbstractContentMap(
25 final Group group, final Map<String, DescriptionTransport<D, T>> contents) {
26 this.group = group;
27 this.contents = contents;
28 }
29
30 public static class UnsupportedApplicationException extends IllegalArgumentException {
31 UnsupportedApplicationException(String message) {
32 super(message);
33 }
34 }
35
36 public static class UnsupportedTransportException extends IllegalArgumentException {
37 UnsupportedTransportException(String message) {
38 super(message);
39 }
40 }
41
42 public Set<Content.Senders> getSenders() {
43 return ImmutableSet.copyOf(Collections2.transform(contents.values(), dt -> dt.senders));
44 }
45
46 public List<String> getNames() {
47 return ImmutableList.copyOf(contents.keySet());
48 }
49
50 JinglePacket toJinglePacket(final JinglePacket.Action action, final String sessionId) {
51 final JinglePacket jinglePacket = new JinglePacket(action, sessionId);
52 for (final Map.Entry<String, DescriptionTransport<D, T>> entry : this.contents.entrySet()) {
53 final DescriptionTransport<D, T> descriptionTransport = entry.getValue();
54 final Content content =
55 new Content(
56 Content.Creator.INITIATOR,
57 descriptionTransport.senders,
58 entry.getKey());
59 if (descriptionTransport.description != null) {
60 content.addChild(descriptionTransport.description);
61 }
62 content.addChild(descriptionTransport.transport);
63 jinglePacket.addJingleContent(content);
64 }
65 if (this.group != null) {
66 jinglePacket.addGroup(this.group);
67 }
68 return jinglePacket;
69 }
70
71 void requireContentDescriptions() {
72 if (this.contents.size() == 0) {
73 throw new IllegalStateException("No contents available");
74 }
75 for (final Map.Entry<String, DescriptionTransport<D, T>> entry : this.contents.entrySet()) {
76 if (entry.getValue().description == null) {
77 throw new IllegalStateException(
78 String.format("%s is lacking content description", entry.getKey()));
79 }
80 }
81 }
82}