2014-02-17 23:58:40 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Copyright the original author or authors
|
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
|
2015-02-26 18:41:17 +01:00
|
|
|
import org.jivesoftware.smack.packet.ExtensionElement;
|
2014-11-07 21:12:01 +01:00
|
|
|
import org.jivesoftware.smack.packet.IQ.IQChildElementXmlStringBuilder;
|
2014-07-05 11:58:13 +02:00
|
|
|
import org.jivesoftware.smack.util.XmlStringBuilder;
|
2014-09-04 11:04:51 +02:00
|
|
|
import org.jivesoftware.smack.util.stringencoder.Base64;
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a chunk of data of an In-Band Bytestream within an IQ stanza or a
|
2015-03-29 12:15:32 +02:00
|
|
|
* message stanza.
|
2014-02-17 23:58:40 +01:00
|
|
|
*
|
|
|
|
* @author Henning Staib
|
|
|
|
*/
|
2015-02-26 18:41:17 +01:00
|
|
|
public class DataPacketExtension implements ExtensionElement {
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
/**
|
2015-03-21 09:36:28 +01:00
|
|
|
* The element name of the data stanza(/packet) extension.
|
2014-02-17 23:58:40 +01:00
|
|
|
*/
|
2014-07-05 11:58:13 +02:00
|
|
|
public final static String ELEMENT = "data";
|
|
|
|
|
|
|
|
/**
|
2015-03-29 12:15:32 +02:00
|
|
|
* The XMPP namespace of the In-Band Bytestream.
|
2014-07-05 11:58:13 +02:00
|
|
|
*/
|
|
|
|
public static final String NAMESPACE = "http://jabber.org/protocol/ibb";
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
/* 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
|
2015-03-21 09:36:28 +01:00
|
|
|
* @param seq sequence of this stanza(/packet) in regard to the other data packets
|
2014-02-17 23:58:40 +01:00
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-21 09:36:28 +01:00
|
|
|
* Returns the sequence of this stanza(/packet) in regard to the other data packets.
|
2014-02-17 23:58:40 +01:00
|
|
|
*
|
2015-03-21 09:36:28 +01:00
|
|
|
* @return the sequence of this stanza(/packet) in regard to the other data packets.
|
2014-02-17 23:58:40 +01:00
|
|
|
*/
|
|
|
|
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
|
2014-09-04 11:04:51 +02:00
|
|
|
this.decodedData = Base64.decode(data);
|
2014-02-17 23:58:40 +01:00
|
|
|
return this.decodedData;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getElementName() {
|
2014-07-05 11:58:13 +02:00
|
|
|
return ELEMENT;
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getNamespace() {
|
2014-07-05 11:58:13 +02:00
|
|
|
return NAMESPACE;
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
2014-07-05 11:58:13 +02:00
|
|
|
@Override
|
|
|
|
public XmlStringBuilder toXML() {
|
2014-11-07 21:12:01 +01:00
|
|
|
XmlStringBuilder xml = getIQChildElementBuilder(new IQChildElementXmlStringBuilder(this));
|
|
|
|
xml.closeElement(this);
|
|
|
|
return xml;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
|
2014-07-05 11:58:13 +02:00
|
|
|
xml.attribute("seq", Long.toString(seq));
|
|
|
|
xml.attribute("sid", sessionID);
|
2014-08-20 21:25:14 +02:00
|
|
|
xml.rightAngleBracket();
|
2014-07-05 11:58:13 +02:00
|
|
|
xml.append(data);
|
|
|
|
return xml;
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
}
|