JinglePacket.java

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