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
 11
 12	//get rid of that BS and set/get directly
 13    Content content = null;
 14    Reason reason = null;
 15    Element checksum = null;
 16    Element jingle = new Element("jingle");
 17
 18    //get rid of what ever that is; maybe throw illegal state to ensure we are only calling setContent etc
 19    @Override
 20    public Element addChild(Element child) {
 21        if ("jingle".equals(child.getName())) {
 22            Element contentElement = child.findChild("content");
 23            if (contentElement != null) {
 24                this.content = new Content();
 25                this.content.setChildren(contentElement.getChildren());
 26                this.content.setAttributes(contentElement.getAttributes());
 27            }
 28            Element reasonElement = child.findChild("reason");
 29            if (reasonElement != null) {
 30                this.reason = new Reason();
 31                this.reason.setChildren(reasonElement.getChildren());
 32                this.reason.setAttributes(reasonElement.getAttributes());
 33            }
 34            this.checksum = child.findChild("checksum");
 35            this.jingle.setAttributes(child.getAttributes());
 36        }
 37        return child;
 38    }
 39
 40    public JinglePacket setContent(Content content) { //take content interface
 41        this.content = content;
 42        return this;
 43    }
 44
 45    public Content getJingleContent() {
 46        if (this.content == null) {
 47            this.content = new Content();
 48        }
 49        return this.content;
 50    }
 51
 52    public Reason getReason() {
 53        return this.reason;
 54    }
 55
 56    public JinglePacket setReason(Reason reason) {
 57        this.reason = reason;
 58        return this;
 59    }
 60
 61    public Element getChecksum() {
 62        return this.checksum;
 63    }
 64
 65    //should be unnecessary if we set and get directly
 66    private void build() {
 67        this.children.clear();
 68        this.jingle.clearChildren();
 69        this.jingle.setAttribute("xmlns", "urn:xmpp:jingle:1");
 70        if (this.content != null) {
 71            jingle.addChild(this.content);
 72        }
 73        if (this.reason != null) {
 74            jingle.addChild(this.reason);
 75        }
 76        if (this.checksum != null) {
 77            jingle.addChild(checksum);
 78        }
 79        this.children.add(jingle);
 80        this.setAttribute("type", "set");
 81    }
 82
 83    public String getSessionId() {
 84        return this.jingle.getAttribute("sid");
 85    }
 86
 87    public void setSessionId(String sid) {
 88        this.jingle.setAttribute("sid", sid);
 89    }
 90
 91    @Override
 92    public String toString() {
 93        this.build();
 94        return super.toString();
 95    }
 96
 97    //use enum for action
 98    public String getAction() {
 99        return this.jingle.getAttribute("action");
100    }
101
102    public void setAction(String action) {
103        this.jingle.setAttribute("action", action);
104    }
105
106    public void setInitiator(final Jid initiator) {
107        this.jingle.setAttribute("initiator", initiator.toString());
108    }
109
110    public boolean isAction(String action) {
111        return action.equalsIgnoreCase(this.getAction());
112    }
113
114    public void addChecksum(byte[] sha1Sum, String namespace) {
115        this.checksum = new Element("checksum", namespace);
116        checksum.setAttribute("creator", "initiator");
117        checksum.setAttribute("name", "a-file-offer");
118        Element hash = checksum.addChild("file").addChild("hash", "urn:xmpp:hashes:2");
119        hash.setAttribute("algo", "sha-1").setContent(Base64.encodeToString(sha1Sum, Base64.NO_WRAP));
120    }
121}