aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/xmpp/jingle/stanzas/JinglePacket.java
blob: 8d16a1c6e639f15a1f1b04b329b4be55c0ff84e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package de.pixart.messenger.xmpp.jingle.stanzas;

import android.util.Base64;

import de.pixart.messenger.xml.Element;
import de.pixart.messenger.xmpp.stanzas.IqPacket;
import rocks.xmpp.addr.Jid;

public class JinglePacket extends IqPacket {
    Content content = null;
    Reason reason = null;
    Element checksum = null;
    Element jingle = new Element("jingle");

    @Override
    public Element addChild(Element child) {
        if ("jingle".equals(child.getName())) {
            Element contentElement = child.findChild("content");
            if (contentElement != null) {
                this.content = new Content();
                this.content.setChildren(contentElement.getChildren());
                this.content.setAttributes(contentElement.getAttributes());
            }
            Element reasonElement = child.findChild("reason");
            if (reasonElement != null) {
                this.reason = new Reason();
                this.reason.setChildren(reasonElement.getChildren());
                this.reason.setAttributes(reasonElement.getAttributes());
            }
            this.checksum = child.findChild("checksum");
            this.jingle.setAttributes(child.getAttributes());
        }
        return child;
    }

    public JinglePacket setContent(Content content) {
        this.content = content;
        return this;
    }

    public Content getJingleContent() {
        if (this.content == null) {
            this.content = new Content();
        }
        return this.content;
    }

    public JinglePacket setReason(Reason reason) {
        this.reason = reason;
        return this;
    }

    public Reason getReason() {
        return this.reason;
    }

    public Element getChecksum() {
        return this.checksum;
    }

    private void build() {
        this.children.clear();
        this.jingle.clearChildren();
        this.jingle.setAttribute("xmlns", "urn:xmpp:jingle:1");
        if (this.content != null) {
            jingle.addChild(this.content);
        }
        if (this.reason != null) {
            jingle.addChild(this.reason);
        }
        if (this.checksum != null) {
            jingle.addChild(checksum);
        }
        this.children.add(jingle);
        this.setAttribute("type", "set");
    }

    public String getSessionId() {
        return this.jingle.getAttribute("sid");
    }

    public void setSessionId(String sid) {
        this.jingle.setAttribute("sid", sid);
    }

    @Override
    public String toString() {
        this.build();
        return super.toString();
    }

    public void setAction(String action) {
        this.jingle.setAttribute("action", action);
    }

    public String getAction() {
        return this.jingle.getAttribute("action");
    }

    public void setInitiator(final Jid initiator) {
        this.jingle.setAttribute("initiator", initiator.toString());
    }

    public boolean isAction(String action) {
        return action.equalsIgnoreCase(this.getAction());
    }

    public void addChecksum(byte[] sha1Sum, String namespace) {
        this.checksum = new Element("checksum", namespace);
        checksum.setAttribute("creator", "initiator");
        checksum.setAttribute("name", "a-file-offer");
        Element hash = checksum.addChild("file").addChild("hash", "urn:xmpp:hashes:2");
        hash.setAttribute("algo", "sha-1").setContent(Base64.encodeToString(sha1Sum, Base64.NO_WRAP));
    }
}