aboutsummaryrefslogtreecommitdiff
path: root/src/org/jivesoftware/smackx/bytestreams/ibb/packet/DataPacketExtension.java
blob: 80ed1e1fc1df1d877491e92791f00e89b1def964 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jivesoftware.smackx.bytestreams.ibb.packet;

import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager;

/**
 * Represents a chunk of data of an In-Band Bytestream within an IQ stanza or a
 * message stanza
 * 
 * @author Henning Staib
 */
public class DataPacketExtension implements PacketExtension {

    /**
     * The element name of the data packet extension.
     */
    public final static String ELEMENT_NAME = "data";

    /* unique session ID identifying this In-Band Bytestream */
    private final String sessionID;

    /* sequence of this packet in regard to the other data packets */
    private final long seq;

    /* the data contained in this packet */
    private final String data;

    private byte[] decodedData;

    /**
     * Creates a new In-Band Bytestream data packet.
     * 
     * @param sessionID unique session ID identifying this In-Band Bytestream
     * @param seq sequence of this packet in regard to the other data packets
     * @param data the base64 encoded data contained in this packet
     */
    public DataPacketExtension(String sessionID, long seq, String data) {
        if (sessionID == null || "".equals(sessionID)) {
            throw new IllegalArgumentException("Session ID must not be null or empty");
        }
        if (seq < 0 || seq > 65535) {
            throw new IllegalArgumentException("Sequence must not be between 0 and 65535");
        }
        if (data == null) {
            throw new IllegalArgumentException("Data must not be null");
        }
        this.sessionID = sessionID;
        this.seq = seq;
        this.data = data;
    }

    /**
     * Returns the unique session ID identifying this In-Band Bytestream.
     * 
     * @return the unique session ID identifying this In-Band Bytestream
     */
    public String getSessionID() {
        return sessionID;
    }

    /**
     * Returns the sequence of this packet in regard to the other data packets.
     * 
     * @return the sequence of this packet in regard to the other data packets.
     */
    public long getSeq() {
        return seq;
    }

    /**
     * Returns the data contained in this packet.
     * 
     * @return the data contained in this packet.
     */
    public String getData() {
        return data;
    }

    /**
     * Returns the decoded data or null if data could not be decoded.
     * <p>
     * The encoded data is invalid if it contains bad Base64 input characters or
     * if it contains the pad ('=') character on a position other than the last
     * character(s) of the data. See <a
     * href="http://xmpp.org/extensions/xep-0047.html#sec">XEP-0047</a> Section
     * 6.
     * 
     * @return the decoded data
     */
    public byte[] getDecodedData() {
        // return cached decoded data
        if (this.decodedData != null) {
            return this.decodedData;
        }

        // data must not contain the pad (=) other than end of data
        if (data.matches(".*={1,2}+.+")) {
            return null;
        }

        // decodeBase64 will return null if bad characters are included
        this.decodedData = StringUtils.decodeBase64(data);
        return this.decodedData;
    }

    public String getElementName() {
        return ELEMENT_NAME;
    }

    public String getNamespace() {
        return InBandBytestreamManager.NAMESPACE;
    }

    public String toXML() {
        StringBuilder buf = new StringBuilder();
        buf.append("<");
        buf.append(getElementName());
        buf.append(" ");
        buf.append("xmlns=\"");
        buf.append(InBandBytestreamManager.NAMESPACE);
        buf.append("\" ");
        buf.append("seq=\"");
        buf.append(seq);
        buf.append("\" ");
        buf.append("sid=\"");
        buf.append(sessionID);
        buf.append("\">");
        buf.append(data);
        buf.append("</");
        buf.append(getElementName());
        buf.append(">");
        return buf.toString();
    }

}