mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2025-12-13 16:41:08 +01:00
Introduce packet.Element
Re-work filetransfer/bytestream stanza toXML() method to use XmlStringBuilder. Move the ELEMENT and NAMESPACE definitions in the right place, ie. the stanza class.
This commit is contained in:
parent
f05b208120
commit
8526f8ab29
20 changed files with 203 additions and 199 deletions
|
|
@ -131,11 +131,6 @@ public class InBandBytestreamManager implements BytestreamManager {
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* The XMPP namespace of the In-Band Bytestream
|
||||
*/
|
||||
public static final String NAMESPACE = "http://jabber.org/protocol/ibb";
|
||||
|
||||
/**
|
||||
* Maximum block size that is allowed for In-Band Bytestreams
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -451,8 +451,8 @@ public class InBandBytestreamSession implements BytestreamSession {
|
|||
public void processPacket(Packet packet) throws NotConnectedException {
|
||||
// get data packet extension
|
||||
DataPacketExtension data = (DataPacketExtension) packet.getExtension(
|
||||
DataPacketExtension.ELEMENT_NAME,
|
||||
InBandBytestreamManager.NAMESPACE);
|
||||
DataPacketExtension.ELEMENT,
|
||||
DataPacketExtension.NAMESPACE);
|
||||
|
||||
/*
|
||||
* check if sequence was not used already (see XEP-0047 Section 2.2)
|
||||
|
|
@ -514,8 +514,8 @@ public class InBandBytestreamSession implements BytestreamSession {
|
|||
public void processPacket(Packet packet) {
|
||||
// get data packet extension
|
||||
DataPacketExtension data = (DataPacketExtension) packet.getExtension(
|
||||
DataPacketExtension.ELEMENT_NAME,
|
||||
InBandBytestreamManager.NAMESPACE);
|
||||
DataPacketExtension.ELEMENT,
|
||||
DataPacketExtension.NAMESPACE);
|
||||
|
||||
// check if encoded data is valid
|
||||
if (data.getDecodedData() == null) {
|
||||
|
|
@ -563,8 +563,8 @@ public class InBandBytestreamSession implements BytestreamSession {
|
|||
}
|
||||
|
||||
// stanza contains data packet extension
|
||||
PacketExtension packetExtension = packet.getExtension(DataPacketExtension.ELEMENT_NAME,
|
||||
InBandBytestreamManager.NAMESPACE);
|
||||
PacketExtension packetExtension = packet.getExtension(DataPacketExtension.ELEMENT,
|
||||
DataPacketExtension.NAMESPACE);
|
||||
if (packetExtension == null || !(packetExtension instanceof DataPacketExtension)) {
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@
|
|||
package org.jivesoftware.smackx.bytestreams.ibb.packet;
|
||||
|
||||
import org.jivesoftware.smack.packet.IQ;
|
||||
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager;
|
||||
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||
|
||||
/**
|
||||
* Represents a request to close an In-Band Bytestream.
|
||||
|
|
@ -26,6 +26,8 @@ import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager;
|
|||
*/
|
||||
public class Close extends IQ {
|
||||
|
||||
public static final String ELEMENT = "close";
|
||||
|
||||
/* unique session ID identifying this In-Band Bytestream */
|
||||
private final String sessionID;
|
||||
|
||||
|
|
@ -52,17 +54,13 @@ public class Close extends IQ {
|
|||
}
|
||||
|
||||
@Override
|
||||
public String getChildElementXML() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append("<close ");
|
||||
buf.append("xmlns=\"");
|
||||
buf.append(InBandBytestreamManager.NAMESPACE);
|
||||
buf.append("\" ");
|
||||
buf.append("sid=\"");
|
||||
buf.append(sessionID);
|
||||
buf.append("\"");
|
||||
buf.append("/>");
|
||||
return buf.toString();
|
||||
public XmlStringBuilder getChildElementXML() {
|
||||
XmlStringBuilder xml = new XmlStringBuilder();
|
||||
xml.halfOpenElement(ELEMENT);
|
||||
xml.xmlnsAttribute(DataPacketExtension.NAMESPACE);
|
||||
xml.attribute("sid", sessionID);
|
||||
xml.closeEmptyElement();
|
||||
return xml;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@
|
|||
package org.jivesoftware.smackx.bytestreams.ibb.packet;
|
||||
|
||||
import org.jivesoftware.smack.packet.IQ;
|
||||
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||
|
||||
/**
|
||||
* Represents a chunk of data sent over an In-Band Bytestream encapsulated in an
|
||||
|
|
@ -60,8 +61,9 @@ public class Data extends IQ {
|
|||
return this.dataPacketExtension;
|
||||
}
|
||||
|
||||
public String getChildElementXML() {
|
||||
return this.dataPacketExtension.toXML();
|
||||
@Override
|
||||
public XmlStringBuilder getChildElementXML() {
|
||||
return dataPacketExtension.toXML();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ 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;
|
||||
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||
|
||||
/**
|
||||
* Represents a chunk of data of an In-Band Bytestream within an IQ stanza or a
|
||||
|
|
@ -31,7 +31,12 @@ public class DataPacketExtension implements PacketExtension {
|
|||
/**
|
||||
* The element name of the data packet extension.
|
||||
*/
|
||||
public final static String ELEMENT_NAME = "data";
|
||||
public final static String ELEMENT = "data";
|
||||
|
||||
/**
|
||||
* The XMPP namespace of the In-Band Bytestream
|
||||
*/
|
||||
public static final String NAMESPACE = "http://jabber.org/protocol/ibb";
|
||||
|
||||
/* unique session ID identifying this In-Band Bytestream */
|
||||
private final String sessionID;
|
||||
|
|
@ -121,32 +126,22 @@ public class DataPacketExtension implements PacketExtension {
|
|||
}
|
||||
|
||||
public String getElementName() {
|
||||
return ELEMENT_NAME;
|
||||
return ELEMENT;
|
||||
}
|
||||
|
||||
public String getNamespace() {
|
||||
return InBandBytestreamManager.NAMESPACE;
|
||||
return 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();
|
||||
@Override
|
||||
public XmlStringBuilder toXML() {
|
||||
XmlStringBuilder xml = new XmlStringBuilder(this);
|
||||
xml.attribute("seq", Long.toString(seq));
|
||||
xml.attribute("sid", sessionID);
|
||||
xml.rightAngelBracket();
|
||||
xml.append(data);
|
||||
xml.closeElement(this);
|
||||
return xml;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ package org.jivesoftware.smackx.bytestreams.ibb.packet;
|
|||
import java.util.Locale;
|
||||
|
||||
import org.jivesoftware.smack.packet.IQ;
|
||||
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager;
|
||||
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager.StanzaType;
|
||||
|
||||
/**
|
||||
|
|
@ -29,6 +29,8 @@ import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager.StanzaTyp
|
|||
*/
|
||||
public class Open extends IQ {
|
||||
|
||||
public static final String ELEMENT = "open";
|
||||
|
||||
/* unique session ID identifying this In-Band Bytestream */
|
||||
private final String sessionID;
|
||||
|
||||
|
|
@ -109,23 +111,15 @@ public class Open extends IQ {
|
|||
}
|
||||
|
||||
@Override
|
||||
public String getChildElementXML() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append("<open ");
|
||||
buf.append("xmlns=\"");
|
||||
buf.append(InBandBytestreamManager.NAMESPACE);
|
||||
buf.append("\" ");
|
||||
buf.append("block-size=\"");
|
||||
buf.append(blockSize);
|
||||
buf.append("\" ");
|
||||
buf.append("sid=\"");
|
||||
buf.append(sessionID);
|
||||
buf.append("\" ");
|
||||
buf.append("stanza=\"");
|
||||
buf.append(stanza.toString().toLowerCase(Locale.US));
|
||||
buf.append("\"");
|
||||
buf.append("/>");
|
||||
return buf.toString();
|
||||
public XmlStringBuilder getChildElementXML() {
|
||||
XmlStringBuilder xml = new XmlStringBuilder();
|
||||
xml.halfOpenElement(ELEMENT);
|
||||
xml.xmlnsAttribute(DataPacketExtension.NAMESPACE);
|
||||
xml.attribute("block-size", Integer.toString(blockSize));
|
||||
xml.attribute("sid", sessionID);
|
||||
xml.attribute("stanza", stanza.toString().toLowerCase(Locale.US));
|
||||
xml.closeEmptyElement();
|
||||
return xml;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -125,11 +125,6 @@ public final class Socks5BytestreamManager implements BytestreamManager {
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* The XMPP namespace of the SOCKS5 Bytestream
|
||||
*/
|
||||
public static final String NAMESPACE = "http://jabber.org/protocol/bytestreams";
|
||||
|
||||
/* prefix used to generate session IDs */
|
||||
private static final String SESSION_ID_PREFIX = "js5_";
|
||||
|
||||
|
|
@ -322,7 +317,7 @@ public final class Socks5BytestreamManager implements BytestreamManager {
|
|||
|
||||
// check if service discovery is not already disposed by connection shutdown
|
||||
if (serviceDiscoveryManager != null) {
|
||||
serviceDiscoveryManager.removeFeature(NAMESPACE);
|
||||
serviceDiscoveryManager.removeFeature(Bytestream.NAMESPACE);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -540,7 +535,7 @@ public final class Socks5BytestreamManager implements BytestreamManager {
|
|||
* @throws NotConnectedException
|
||||
*/
|
||||
private boolean supportsSocks5(String targetJID) throws NoResponseException, XMPPErrorException, NotConnectedException {
|
||||
return ServiceDiscoveryManager.getInstanceFor(connection).supportsFeature(targetJID, NAMESPACE);
|
||||
return ServiceDiscoveryManager.getInstanceFor(connection).supportsFeature(targetJID, Bytestream.NAMESPACE);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -732,9 +727,7 @@ public final class Socks5BytestreamManager implements BytestreamManager {
|
|||
*/
|
||||
private void enableService() {
|
||||
ServiceDiscoveryManager manager = ServiceDiscoveryManager.getInstanceFor(this.connection);
|
||||
if (!manager.includesFeature(NAMESPACE)) {
|
||||
manager.addFeature(NAMESPACE);
|
||||
}
|
||||
manager.addFeature(Bytestream.NAMESPACE);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -21,8 +21,9 @@ import java.util.Collection;
|
|||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.jivesoftware.smack.packet.Element;
|
||||
import org.jivesoftware.smack.packet.IQ;
|
||||
import org.jivesoftware.smack.packet.PacketExtension;
|
||||
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||
|
||||
/**
|
||||
* A packet representing part of a SOCKS5 Bytestream negotiation.
|
||||
|
|
@ -30,6 +31,10 @@ import org.jivesoftware.smack.packet.PacketExtension;
|
|||
* @author Alexander Wenckus
|
||||
*/
|
||||
public class Bytestream extends IQ {
|
||||
/**
|
||||
* The XMPP namespace of the SOCKS5 Bytestream
|
||||
*/
|
||||
public static final String NAMESPACE = "http://jabber.org/protocol/bytestreams";
|
||||
|
||||
private String sessionID;
|
||||
|
||||
|
|
@ -213,48 +218,51 @@ public class Bytestream extends IQ {
|
|||
this.toActivate = new Activate(targetID);
|
||||
}
|
||||
|
||||
public String getChildElementXML() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
@Override
|
||||
public XmlStringBuilder getChildElementXML() {
|
||||
XmlStringBuilder xml = new XmlStringBuilder();
|
||||
xml.openElement(IQ.QUERY_ELEMENT);
|
||||
xml.xmlnsAttribute(NAMESPACE);
|
||||
|
||||
buf.append("<query xmlns=\"http://jabber.org/protocol/bytestreams\"");
|
||||
if (this.getType().equals(IQ.Type.set)) {
|
||||
switch(getType()) {
|
||||
case set:
|
||||
if (getSessionID() != null) {
|
||||
buf.append(" sid=\"").append(getSessionID()).append("\"");
|
||||
xml.attribute("sid", getSessionID());
|
||||
}
|
||||
if (getMode() != null) {
|
||||
buf.append(" mode = \"").append(getMode()).append("\"");
|
||||
xml.attribute("mode", getMode());
|
||||
}
|
||||
buf.append(">");
|
||||
xml.rightAngelBracket();
|
||||
if (getToActivate() == null) {
|
||||
for (StreamHost streamHost : getStreamHosts()) {
|
||||
buf.append(streamHost.toXML());
|
||||
xml.append(streamHost.toXML());
|
||||
}
|
||||
}
|
||||
else {
|
||||
buf.append(getToActivate().toXML());
|
||||
xml.append(getToActivate().toXML());
|
||||
}
|
||||
}
|
||||
else if (this.getType().equals(IQ.Type.result)) {
|
||||
buf.append(">");
|
||||
break;
|
||||
case result:
|
||||
xml.rightAngelBracket();
|
||||
if (getUsedHost() != null) {
|
||||
buf.append(getUsedHost().toXML());
|
||||
xml.append(getUsedHost().toXML());
|
||||
}
|
||||
// A result from the server can also contain stream hosts
|
||||
else if (countStreamHosts() > 0) {
|
||||
for (StreamHost host : streamHosts) {
|
||||
buf.append(host.toXML());
|
||||
xml.append(host.toXML());
|
||||
}
|
||||
}
|
||||
break;
|
||||
case get:
|
||||
xml.closeEmptyElement();
|
||||
return xml;
|
||||
default:
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
else if (this.getType().equals(IQ.Type.get)) {
|
||||
return buf.append("/>").toString();
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
buf.append("</query>");
|
||||
xml.closeElement(IQ.QUERY_ELEMENT);
|
||||
|
||||
return buf.toString();
|
||||
return xml;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -263,9 +271,7 @@ public class Bytestream extends IQ {
|
|||
*
|
||||
* @author Alexander Wenckus
|
||||
*/
|
||||
public static class StreamHost implements PacketExtension {
|
||||
|
||||
public static String NAMESPACE = "";
|
||||
public static class StreamHost implements Element {
|
||||
|
||||
public static String ELEMENTNAME = "streamhost";
|
||||
|
||||
|
|
@ -322,29 +328,22 @@ public class Bytestream extends IQ {
|
|||
return port;
|
||||
}
|
||||
|
||||
public String getNamespace() {
|
||||
return NAMESPACE;
|
||||
}
|
||||
|
||||
public String getElementName() {
|
||||
return ELEMENTNAME;
|
||||
}
|
||||
|
||||
public String toXML() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
|
||||
buf.append("<").append(getElementName()).append(" ");
|
||||
buf.append("jid=\"").append(getJID()).append("\" ");
|
||||
buf.append("host=\"").append(getAddress()).append("\" ");
|
||||
@Override
|
||||
public XmlStringBuilder toXML() {
|
||||
XmlStringBuilder xml = new XmlStringBuilder(this);
|
||||
xml.attribute("jid", getJID());
|
||||
xml.attribute("host", getAddress());
|
||||
if (getPort() != 0) {
|
||||
buf.append("port=\"").append(getPort()).append("\"");
|
||||
xml.attribute("port", Integer.toString(getPort()));
|
||||
} else {
|
||||
xml.attribute("zeroconf", "_jabber.bytestreams");
|
||||
}
|
||||
else {
|
||||
buf.append("zeroconf=\"_jabber.bytestreams\"");
|
||||
}
|
||||
buf.append("/>");
|
||||
|
||||
return buf.toString();
|
||||
xml.closeEmptyElement();
|
||||
return xml;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -354,9 +353,7 @@ public class Bytestream extends IQ {
|
|||
*
|
||||
* @author Alexander Wenckus
|
||||
*/
|
||||
public static class StreamHostUsed implements PacketExtension {
|
||||
|
||||
public String NAMESPACE = "";
|
||||
public static class StreamHostUsed implements Element {
|
||||
|
||||
public static String ELEMENTNAME = "streamhost-used";
|
||||
|
||||
|
|
@ -380,20 +377,16 @@ public class Bytestream extends IQ {
|
|||
return JID;
|
||||
}
|
||||
|
||||
public String getNamespace() {
|
||||
return NAMESPACE;
|
||||
}
|
||||
|
||||
public String getElementName() {
|
||||
return ELEMENTNAME;
|
||||
}
|
||||
|
||||
public String toXML() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append("<").append(getElementName()).append(" ");
|
||||
buf.append("jid=\"").append(getJID()).append("\" ");
|
||||
buf.append("/>");
|
||||
return buf.toString();
|
||||
@Override
|
||||
public XmlStringBuilder toXML() {
|
||||
XmlStringBuilder xml = new XmlStringBuilder(this);
|
||||
xml.attribute("jid", getJID());
|
||||
xml.closeEmptyElement();
|
||||
return xml;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -402,9 +395,7 @@ public class Bytestream extends IQ {
|
|||
*
|
||||
* @author Alexander Wenckus
|
||||
*/
|
||||
public static class Activate implements PacketExtension {
|
||||
|
||||
public String NAMESPACE = "";
|
||||
public static class Activate implements Element {
|
||||
|
||||
public static String ELEMENTNAME = "activate";
|
||||
|
||||
|
|
@ -428,20 +419,17 @@ public class Bytestream extends IQ {
|
|||
return target;
|
||||
}
|
||||
|
||||
public String getNamespace() {
|
||||
return NAMESPACE;
|
||||
}
|
||||
|
||||
public String getElementName() {
|
||||
return ELEMENTNAME;
|
||||
}
|
||||
|
||||
public String toXML() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append("<").append(getElementName()).append(">");
|
||||
buf.append(getTarget());
|
||||
buf.append("</").append(getElementName()).append(">");
|
||||
return buf.toString();
|
||||
@Override
|
||||
public XmlStringBuilder toXML() {
|
||||
XmlStringBuilder xml = new XmlStringBuilder(this);
|
||||
xml.rightAngelBracket();
|
||||
xml.escape(getTarget());
|
||||
xml.closeElement(this);
|
||||
return xml;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue