JinglePacket.java

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