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