From 8526f8ab29ecb7f94c3d82eeaaca158e6d3f0662 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Sat, 5 Jul 2014 11:58:13 +0200 Subject: [PATCH] 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. --- .../jivesoftware/smack/packet/Element.java | 39 ++++++ .../org/jivesoftware/smack/packet/IQ.java | 2 + .../smack/packet/PacketExtension.java | 15 +-- .../smack/util/XmlStringBuilder.java | 10 +- .../ibb/InBandBytestreamManager.java | 5 - .../ibb/InBandBytestreamSession.java | 12 +- .../smackx/bytestreams/ibb/packet/Close.java | 22 ++- .../smackx/bytestreams/ibb/packet/Data.java | 6 +- .../ibb/packet/DataPacketExtension.java | 41 +++--- .../smackx/bytestreams/ibb/packet/Open.java | 30 ++--- .../socks5/Socks5BytestreamManager.java | 13 +- .../bytestreams/socks5/packet/Bytestream.java | 126 ++++++++---------- .../smackx/disco/ServiceDiscoveryManager.java | 6 +- .../filetransfer/FileTransferNegotiator.java | 28 ++-- .../filetransfer/IBBTransferNegotiator.java | 3 +- .../Socks5TransferNegotiator.java | 2 +- .../InBandBytestreamSessionMessageTest.java | 2 +- .../ibb/packet/DataPacketExtensionTest.java | 2 +- .../bytestreams/ibb/packet/DataTest.java | 16 ++- .../socks5/Socks5ByteStreamManagerTest.java | 22 +-- 20 files changed, 203 insertions(+), 199 deletions(-) create mode 100644 smack-core/src/main/java/org/jivesoftware/smack/packet/Element.java diff --git a/smack-core/src/main/java/org/jivesoftware/smack/packet/Element.java b/smack-core/src/main/java/org/jivesoftware/smack/packet/Element.java new file mode 100644 index 000000000..6e5292570 --- /dev/null +++ b/smack-core/src/main/java/org/jivesoftware/smack/packet/Element.java @@ -0,0 +1,39 @@ +/** + * + * Copyright © 2014 Florian Schmaus + * + * 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.smack.packet; + +/** + * Interface to represent a XML element. This is similar to {@link PacketExtension}, but does not + * carry a namespace and is usually included as child element of an packet extension. + */ +public interface Element { + + /** + * Returns the root element name. + * + * @return the element name. + */ + public String getElementName(); + + /** + * Returns the XML representation of the PacketExtension. + * + * @return the packet extension as XML. + */ + public CharSequence toXML(); +} diff --git a/smack-core/src/main/java/org/jivesoftware/smack/packet/IQ.java b/smack-core/src/main/java/org/jivesoftware/smack/packet/IQ.java index 4236a2101..9d619f368 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/packet/IQ.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/packet/IQ.java @@ -40,6 +40,8 @@ import org.jivesoftware.smack.util.XmlStringBuilder; */ public abstract class IQ extends Packet { + public static final String QUERY_ELEMENT = "query"; + private Type type = Type.get; public IQ() { diff --git a/smack-core/src/main/java/org/jivesoftware/smack/packet/PacketExtension.java b/smack-core/src/main/java/org/jivesoftware/smack/packet/PacketExtension.java index 3a20444e2..a7827ae82 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/packet/PacketExtension.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/packet/PacketExtension.java @@ -28,14 +28,7 @@ package org.jivesoftware.smack.packet; * @see org.jivesoftware.smack.provider.PacketExtensionProvider * @author Matt Tucker */ -public interface PacketExtension { - - /** - * Returns the root element name. - * - * @return the element name. - */ - public String getElementName(); +public interface PacketExtension extends Element { /** * Returns the root element XML namespace. @@ -44,10 +37,4 @@ public interface PacketExtension { */ public String getNamespace(); - /** - * Returns the XML representation of the PacketExtension. - * - * @return the packet extension as XML. - */ - public CharSequence toXML(); } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/XmlStringBuilder.java b/smack-core/src/main/java/org/jivesoftware/smack/util/XmlStringBuilder.java index 109187ced..7c6bf6c3c 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/XmlStringBuilder.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/XmlStringBuilder.java @@ -16,6 +16,7 @@ */ package org.jivesoftware.smack.util; +import org.jivesoftware.smack.packet.Element; import org.jivesoftware.smack.packet.PacketExtension; public class XmlStringBuilder implements Appendable, CharSequence { @@ -32,6 +33,11 @@ public class XmlStringBuilder implements Appendable, CharSequence { prelude(pe); } + public XmlStringBuilder(Element e) { + this(); + halfOpenElement(e.getElementName()); + } + /** * Does nothing if content is null. * @@ -83,8 +89,8 @@ public class XmlStringBuilder implements Appendable, CharSequence { return this; } - public XmlStringBuilder closeElement(PacketExtension pe) { - closeElement(pe.getElementName()); + public XmlStringBuilder closeElement(Element e) { + closeElement(e.getElementName()); return this; } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/ibb/InBandBytestreamManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/ibb/InBandBytestreamManager.java index cf9cdb2d3..f5f605548 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/ibb/InBandBytestreamManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/ibb/InBandBytestreamManager.java @@ -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 */ diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/ibb/InBandBytestreamSession.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/ibb/InBandBytestreamSession.java index c19ecaa7b..150bba2d9 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/ibb/InBandBytestreamSession.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/ibb/InBandBytestreamSession.java @@ -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; } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/ibb/packet/Close.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/ibb/packet/Close.java index 6593cfbef..9b6412eba 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/ibb/packet/Close.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/ibb/packet/Close.java @@ -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(""); - 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; } } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/ibb/packet/Data.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/ibb/packet/Data.java index d566cdcfd..7c950771e 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/ibb/packet/Data.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/ibb/packet/Data.java @@ -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(); } } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/ibb/packet/DataPacketExtension.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/ibb/packet/DataPacketExtension.java index ee3c4e49b..c02ef7364 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/ibb/packet/DataPacketExtension.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/ibb/packet/DataPacketExtension.java @@ -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(""); - 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; } } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/ibb/packet/Open.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/ibb/packet/Open.java index 5489dd459..3f1f915bc 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/ibb/packet/Open.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/ibb/packet/Open.java @@ -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(""); - 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; } } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5BytestreamManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5BytestreamManager.java index 7c25b674e..849eeb96e 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5BytestreamManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5BytestreamManager.java @@ -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); } /** diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/packet/Bytestream.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/packet/Bytestream.java index ea43c48f6..20004b0e0 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/packet/Bytestream.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/packet/Bytestream.java @@ -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(""); + 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(""); + 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(""); - return buf.toString(); + @Override + public XmlStringBuilder toXML() { + XmlStringBuilder xml = new XmlStringBuilder(this); + xml.rightAngelBracket(); + xml.escape(getTarget()); + xml.closeElement(this); + return xml; } } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/disco/ServiceDiscoveryManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/disco/ServiceDiscoveryManager.java index 6212d501d..ca4e981b3 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/disco/ServiceDiscoveryManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/disco/ServiceDiscoveryManager.java @@ -405,8 +405,10 @@ public class ServiceDiscoveryManager extends Manager { */ public void addFeature(String feature) { synchronized (features) { - features.add(feature); - renewEntityCapsVersion(); + if (!features.contains(feature)) { + features.add(feature); + renewEntityCapsVersion(); + } } } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/FileTransferNegotiator.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/FileTransferNegotiator.java index 5ca2ceb01..f14e7a438 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/FileTransferNegotiator.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/FileTransferNegotiator.java @@ -34,8 +34,8 @@ import org.jivesoftware.smack.XMPPException.XMPPErrorException; import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.packet.XMPPError; -import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager; -import org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamManager; +import org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension; +import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream; import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; import org.jivesoftware.smackx.si.packet.StreamInitiation; import org.jivesoftware.smackx.xdata.Form; @@ -103,9 +103,9 @@ public class FileTransferNegotiator extends Manager { List namespaces = new ArrayList(); namespaces.addAll(Arrays.asList(NAMESPACE)); - namespaces.add(InBandBytestreamManager.NAMESPACE); + namespaces.add(DataPacketExtension.NAMESPACE); if (!IBB_ONLY) { - namespaces.add(Socks5BytestreamManager.NAMESPACE); + namespaces.add(Bytestream.NAMESPACE); } for (String namespace : namespaces) { @@ -130,9 +130,9 @@ public class FileTransferNegotiator extends Manager { List namespaces = new ArrayList(); namespaces.addAll(Arrays.asList(NAMESPACE)); - namespaces.add(InBandBytestreamManager.NAMESPACE); + namespaces.add(DataPacketExtension.NAMESPACE); if (!IBB_ONLY) { - namespaces.add(Socks5BytestreamManager.NAMESPACE); + namespaces.add(Bytestream.NAMESPACE); } for (String namespace : namespaces) { @@ -150,9 +150,9 @@ public class FileTransferNegotiator extends Manager { */ public static Collection getSupportedProtocols() { List protocols = new ArrayList(); - protocols.add(InBandBytestreamManager.NAMESPACE); + protocols.add(DataPacketExtension.NAMESPACE); if (!IBB_ONLY) { - protocols.add(Socks5BytestreamManager.NAMESPACE); + protocols.add(Bytestream.NAMESPACE); } return Collections.unmodifiableList(protocols); } @@ -227,10 +227,10 @@ public class FileTransferNegotiator extends Manager { boolean isIBB = false; for (FormField.Option option : field.getOptions()) { variable = option.getValue(); - if (variable.equals(Socks5BytestreamManager.NAMESPACE) && !IBB_ONLY) { + if (variable.equals(Bytestream.NAMESPACE) && !IBB_ONLY) { isByteStream = true; } - else if (variable.equals(InBandBytestreamManager.NAMESPACE)) { + else if (variable.equals(DataPacketExtension.NAMESPACE)) { isIBB = true; } } @@ -354,10 +354,10 @@ public class FileTransferNegotiator extends Manager { boolean isByteStream = false; boolean isIBB = false; for (String variable : field.getValues()) { - if (variable.equals(Socks5BytestreamManager.NAMESPACE) && !IBB_ONLY) { + if (variable.equals(Bytestream.NAMESPACE) && !IBB_ONLY) { isByteStream = true; } - else if (variable.equals(InBandBytestreamManager.NAMESPACE)) { + else if (variable.equals(DataPacketExtension.NAMESPACE)) { isIBB = true; } } @@ -385,9 +385,9 @@ public class FileTransferNegotiator extends Manager { FormField field = new FormField(STREAM_DATA_FIELD_NAME); field.setType(FormField.TYPE_LIST_SINGLE); if (!IBB_ONLY) { - field.addOption(new FormField.Option(Socks5BytestreamManager.NAMESPACE)); + field.addOption(new FormField.Option(Bytestream.NAMESPACE)); } - field.addOption(new FormField.Option(InBandBytestreamManager.NAMESPACE)); + field.addOption(new FormField.Option(DataPacketExtension.NAMESPACE)); form.addField(field); return form; } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/IBBTransferNegotiator.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/IBBTransferNegotiator.java index 22fde9fb4..563954a41 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/IBBTransferNegotiator.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/IBBTransferNegotiator.java @@ -32,6 +32,7 @@ import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager; import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamRequest; import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamSession; +import org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension; import org.jivesoftware.smackx.bytestreams.ibb.packet.Open; import org.jivesoftware.smackx.si.packet.StreamInitiation; @@ -92,7 +93,7 @@ public class IBBTransferNegotiator extends StreamNegotiator { } public String[] getNamespaces() { - return new String[] { InBandBytestreamManager.NAMESPACE }; + return new String[] { DataPacketExtension.NAMESPACE }; } InputStream negotiateIncomingStream(Packet streamInitiation) throws NotConnectedException { diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/Socks5TransferNegotiator.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/Socks5TransferNegotiator.java index c57923db6..8ae8df29b 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/Socks5TransferNegotiator.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/Socks5TransferNegotiator.java @@ -97,7 +97,7 @@ public class Socks5TransferNegotiator extends StreamNegotiator { @Override public String[] getNamespaces() { - return new String[] { Socks5BytestreamManager.NAMESPACE }; + return new String[] { Bytestream.NAMESPACE }; } @Override diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/ibb/InBandBytestreamSessionMessageTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/ibb/InBandBytestreamSessionMessageTest.java index e701f88c3..dbaa7a5a2 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/ibb/InBandBytestreamSessionMessageTest.java +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/ibb/InBandBytestreamSessionMessageTest.java @@ -99,7 +99,7 @@ public class InBandBytestreamSessionMessageTest { public void verify(Message request, IQ response) { DataPacketExtension dpe = (DataPacketExtension) request.getExtension( - DataPacketExtension.ELEMENT_NAME, InBandBytestreamManager.NAMESPACE); + DataPacketExtension.ELEMENT, DataPacketExtension.NAMESPACE); assertEquals(lastSeq++, dpe.getSeq()); } diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/ibb/packet/DataPacketExtensionTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/ibb/packet/DataPacketExtensionTest.java index f6abd7abf..8d7184754 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/ibb/packet/DataPacketExtensionTest.java +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/ibb/packet/DataPacketExtensionTest.java @@ -93,7 +93,7 @@ public class DataPacketExtensionTest { .asString(outputProperties); DataPacketExtension data = new DataPacketExtension("i781hf64", 0, "DATA"); - assertXMLEqual(control, data.toXML()); + assertXMLEqual(control, data.toXML().toString()); } } diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/ibb/packet/DataTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/ibb/packet/DataTest.java index bca527ef7..9cc349896 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/ibb/packet/DataTest.java +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/ibb/packet/DataTest.java @@ -18,13 +18,13 @@ package org.jivesoftware.smackx.bytestreams.ibb.packet; import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; import static org.junit.Assert.assertEquals; - import static org.mockito.Mockito.*; import java.util.Properties; import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.util.StringUtils; +import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smackx.bytestreams.ibb.packet.Data; import org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension; import org.junit.Test; @@ -72,12 +72,14 @@ public class DataTest { .asString(outputProperties); DataPacketExtension dpe = mock(DataPacketExtension.class); - String dataTag = XMLBuilder.create("data") - .a("xmlns", "http://jabber.org/protocol/ibb") - .a("seq", "0") - .a("sid", "i781hf64") - .t(encodedData) - .asString(outputProperties); + XmlStringBuilder dataTag = new XmlStringBuilder(); + dataTag.halfOpenElement(DataPacketExtension.ELEMENT); + dataTag.xmlnsAttribute(DataPacketExtension.NAMESPACE); + dataTag.attribute("seq", "0"); + dataTag.attribute("sid", "i781hf64"); + dataTag.rightAngelBracket(); + dataTag.escape(encodedData); + dataTag.closeElement(DataPacketExtension.ELEMENT); when(dpe.toXML()).thenReturn(dataTag); Data data = new Data(dpe); data.setFrom("romeo@montague.lit/orchard"); diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5ByteStreamManagerTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5ByteStreamManagerTest.java index 5b3ccf63d..39540dbdf 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5ByteStreamManagerTest.java +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5ByteStreamManagerTest.java @@ -127,11 +127,11 @@ public class Socks5ByteStreamManagerTest { Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection); ServiceDiscoveryManager discoveryManager = ServiceDiscoveryManager.getInstanceFor(connection); - assertTrue(discoveryManager.includesFeature(Socks5BytestreamManager.NAMESPACE)); + assertTrue(discoveryManager.includesFeature(Bytestream.NAMESPACE)); byteStreamManager.disableService(); - assertFalse(discoveryManager.includesFeature(Socks5BytestreamManager.NAMESPACE)); + assertFalse(discoveryManager.includesFeature(Bytestream.NAMESPACE)); } /** @@ -182,7 +182,7 @@ public class Socks5ByteStreamManagerTest { // build discover info that supports the SOCKS5 feature DiscoverInfo discoverInfo = Socks5PacketUtils.createDiscoverInfo(targetJID, initiatorJID); - discoverInfo.addFeature(Socks5BytestreamManager.NAMESPACE); + discoverInfo.addFeature(Bytestream.NAMESPACE); // return that SOCKS5 is supported if target is queried protocol.addResponse(discoverInfo, Verification.correspondingSenderReceiver, @@ -233,7 +233,7 @@ public class Socks5ByteStreamManagerTest { // build discover info that supports the SOCKS5 feature DiscoverInfo discoverInfo = Socks5PacketUtils.createDiscoverInfo(targetJID, initiatorJID); - discoverInfo.addFeature(Socks5BytestreamManager.NAMESPACE); + discoverInfo.addFeature(Bytestream.NAMESPACE); // return that SOCKS5 is supported if target is queried protocol.addResponse(discoverInfo, Verification.correspondingSenderReceiver, @@ -297,7 +297,7 @@ public class Socks5ByteStreamManagerTest { // build discover info that supports the SOCKS5 feature DiscoverInfo discoverInfo = Socks5PacketUtils.createDiscoverInfo(targetJID, initiatorJID); - discoverInfo.addFeature(Socks5BytestreamManager.NAMESPACE); + discoverInfo.addFeature(Bytestream.NAMESPACE); // return that SOCKS5 is supported if target is queried protocol.addResponse(discoverInfo, Verification.correspondingSenderReceiver, @@ -388,7 +388,7 @@ public class Socks5ByteStreamManagerTest { // build discover info that supports the SOCKS5 feature DiscoverInfo discoverInfo = Socks5PacketUtils.createDiscoverInfo(targetJID, initiatorJID); - discoverInfo.addFeature(Socks5BytestreamManager.NAMESPACE); + discoverInfo.addFeature(Bytestream.NAMESPACE); // return that SOCKS5 is supported if target is queried protocol.addResponse(discoverInfo, Verification.correspondingSenderReceiver, @@ -478,7 +478,7 @@ public class Socks5ByteStreamManagerTest { // build discover info that supports the SOCKS5 feature DiscoverInfo discoverInfo = Socks5PacketUtils.createDiscoverInfo(targetJID, initiatorJID); - discoverInfo.addFeature(Socks5BytestreamManager.NAMESPACE); + discoverInfo.addFeature(Bytestream.NAMESPACE); // return that SOCKS5 is supported if target is queried protocol.addResponse(discoverInfo, Verification.correspondingSenderReceiver, @@ -560,7 +560,7 @@ public class Socks5ByteStreamManagerTest { // build discover info that supports the SOCKS5 feature DiscoverInfo discoverInfo = Socks5PacketUtils.createDiscoverInfo(targetJID, initiatorJID); - discoverInfo.addFeature(Socks5BytestreamManager.NAMESPACE); + discoverInfo.addFeature(Bytestream.NAMESPACE); // return that SOCKS5 is supported if target is queried protocol.addResponse(discoverInfo, Verification.correspondingSenderReceiver, @@ -654,7 +654,7 @@ public class Socks5ByteStreamManagerTest { // build discover info that supports the SOCKS5 feature DiscoverInfo discoverInfo = Socks5PacketUtils.createDiscoverInfo(targetJID, initiatorJID); - discoverInfo.addFeature(Socks5BytestreamManager.NAMESPACE); + discoverInfo.addFeature(Bytestream.NAMESPACE); // return that SOCKS5 is supported if target is queried protocol.addResponse(discoverInfo, Verification.correspondingSenderReceiver, @@ -773,7 +773,7 @@ public class Socks5ByteStreamManagerTest { // build discover info that supports the SOCKS5 feature DiscoverInfo discoverInfo = Socks5PacketUtils.createDiscoverInfo(targetJID, initiatorJID); - discoverInfo.addFeature(Socks5BytestreamManager.NAMESPACE); + discoverInfo.addFeature(Bytestream.NAMESPACE); // return that SOCKS5 is supported if target is queried protocol.addResponse(discoverInfo, Verification.correspondingSenderReceiver, @@ -1003,7 +1003,7 @@ public class Socks5ByteStreamManagerTest { private void createResponses(Verification streamHostUsedVerification) { // build discover info that supports the SOCKS5 feature DiscoverInfo discoverInfo = Socks5PacketUtils.createDiscoverInfo(targetJID, initiatorJID); - discoverInfo.addFeature(Socks5BytestreamManager.NAMESPACE); + discoverInfo.addFeature(Bytestream.NAMESPACE); // return that SOCKS5 is supported if target is queried protocol.addResponse(discoverInfo, Verification.correspondingSenderReceiver,