JinglePacket.java

 1package eu.siacs.conversations.xmpp.jingle.stanzas;
 2
 3import eu.siacs.conversations.xml.Element;
 4import eu.siacs.conversations.xmpp.jid.Jid;
 5import eu.siacs.conversations.xmpp.stanzas.IqPacket;
 6
 7public class JinglePacket extends IqPacket {
 8	Content content = null;
 9	Reason reason = null;
10	Element jingle = new Element("jingle");
11
12	@Override
13	public Element addChild(Element child) {
14		if ("jingle".equals(child.getName())) {
15			Element contentElement = child.findChild("content");
16			if (contentElement != null) {
17				this.content = new Content();
18				this.content.setChildren(contentElement.getChildren());
19				this.content.setAttributes(contentElement.getAttributes());
20			}
21			Element reasonElement = child.findChild("reason");
22			if (reasonElement != null) {
23				this.reason = new Reason();
24				this.reason.setChildren(reasonElement.getChildren());
25				this.reason.setAttributes(reasonElement.getAttributes());
26			}
27			this.jingle.setAttributes(child.getAttributes());
28		}
29		return child;
30	}
31
32	public JinglePacket setContent(Content content) {
33		this.content = content;
34		return this;
35	}
36
37	public Content getJingleContent() {
38		if (this.content == null) {
39			this.content = new Content();
40		}
41		return this.content;
42	}
43
44	public JinglePacket setReason(Reason reason) {
45		this.reason = reason;
46		return this;
47	}
48
49	public Reason getReason() {
50		return this.reason;
51	}
52
53	private void build() {
54		this.children.clear();
55		this.jingle.clearChildren();
56		this.jingle.setAttribute("xmlns", "urn:xmpp:jingle:1");
57		if (this.content != null) {
58			jingle.addChild(this.content);
59		}
60		if (this.reason != null) {
61			jingle.addChild(this.reason);
62		}
63		this.children.add(jingle);
64		this.setAttribute("type", "set");
65	}
66
67	public String getSessionId() {
68		return this.jingle.getAttribute("sid");
69	}
70
71	public void setSessionId(String sid) {
72		this.jingle.setAttribute("sid", sid);
73	}
74
75	@Override
76	public String toString() {
77		this.build();
78		return super.toString();
79	}
80
81	public void setAction(String action) {
82		this.jingle.setAttribute("action", action);
83	}
84
85	public String getAction() {
86		return this.jingle.getAttribute("action");
87	}
88
89	public void setInitiator(final Jid initiator) {
90		this.jingle.setAttribute("initiator", initiator.toString());
91	}
92
93	public boolean isAction(String action) {
94		return action.equalsIgnoreCase(this.getAction());
95	}
96}