diff --git a/smack-experimental/src/test/java/org/jivesoftware/smackx/hash/HashElementTest.java b/smack-experimental/src/test/java/org/jivesoftware/smackx/hash/HashElementTest.java index 10ac10c9b..5872263ca 100644 --- a/smack-experimental/src/test/java/org/jivesoftware/smackx/hash/HashElementTest.java +++ b/smack-experimental/src/test/java/org/jivesoftware/smackx/hash/HashElementTest.java @@ -51,6 +51,10 @@ public class HashElementTest extends SmackTestSuite { assertFalse(parsed.equals(null)); assertEquals(element, parsed); assertTrue(element.equals(parsed)); + + HashElement other = new HashElement(HashManager.ALGORITHM.SHA_512, + "861844d6704e8573fec34d967e20bcfef3d424cf48be04e6dc08f2bd58c729743371015ead891cc3cf1c9d34b49264b510751b1ff9e537937bc46b5d6ff4ecc8".getBytes(StringUtils.UTF8)); + assertFalse(element.equals(other)); } } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/JingleContentTransport.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/JingleContentTransport.java index 5885c5ad8..c05ec2416 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/JingleContentTransport.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/JingleContentTransport.java @@ -16,12 +16,12 @@ */ package org.jivesoftware.smackx.jingle.element; -import java.util.Collections; -import java.util.List; - import org.jivesoftware.smack.packet.ExtensionElement; import org.jivesoftware.smack.util.XmlStringBuilder; +import java.util.Collections; +import java.util.List; + /** * A jingle transport extension. * @@ -58,11 +58,17 @@ public abstract class JingleContentTransport implements ExtensionElement { public final XmlStringBuilder toXML() { XmlStringBuilder xml = new XmlStringBuilder(this); addExtraAttributes(xml); - xml.rightAngleBracket(); - xml.append(candidates); + if (candidates.isEmpty()) { + xml.closeEmptyElement(); - xml.closeElement(this); + } else { + xml.rightAngleBracket(); + + xml.append(candidates); + + xml.closeElement(this); + } return xml; } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle_ibb/JingleInBandByteStreamManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle_ibb/JingleInBandByteStreamManager.java new file mode 100644 index 000000000..0f137b1c2 --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle_ibb/JingleInBandByteStreamManager.java @@ -0,0 +1,57 @@ +/** + * + * Copyright 2017 Paul Schaub + * + * 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.jingle_ibb; + +import org.jivesoftware.smack.Manager; +import org.jivesoftware.smack.XMPPConnection; +import org.jivesoftware.smackx.jingle.provider.JingleContentProviderManager; +import org.jivesoftware.smackx.jingle_ibb.provider.JingleInBandByteStreamTransportProvider; + +import java.util.Random; +import java.util.WeakHashMap; + +/** + * Manager for Jingle In-Band-ByteStreams. + */ +public final class JingleInBandByteStreamManager extends Manager { + + public static final String NAMESPACE_V1 = "urn:xmpp:jingle:transports:ibb:1"; + + private static final WeakHashMap INSTANCES = new WeakHashMap<>(); + + private JingleInBandByteStreamManager(XMPPConnection connection) { + super(connection); + JingleContentProviderManager.addJingleContentTransportProvider(NAMESPACE_V1, new JingleInBandByteStreamTransportProvider()); + } + + public static JingleInBandByteStreamManager getInstanceFor(XMPPConnection connection) { + JingleInBandByteStreamManager manager = INSTANCES.get(connection); + if (manager == null) { + manager = new JingleInBandByteStreamManager(connection); + INSTANCES.put(connection, manager); + } + return manager; + } + + /** + * Generate a random session id. + * @return + */ + public static String generateSessionId() { + return Integer.toString(64,new Random().nextInt()); + } +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle_ibb/element/JingleInBandByteStreamTransport.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle_ibb/element/JingleInBandByteStreamTransport.java new file mode 100644 index 000000000..6117ca3e7 --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle_ibb/element/JingleInBandByteStreamTransport.java @@ -0,0 +1,85 @@ +/** + * + * Copyright 2017 Paul Schaub + * + * 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.jingle_ibb.element; + +import org.jivesoftware.smack.util.XmlStringBuilder; +import org.jivesoftware.smackx.jingle.element.JingleContentTransport; +import org.jivesoftware.smackx.jingle_ibb.JingleInBandByteStreamManager; + +/** + * Jingle In-Band-ByteStream transport. + */ +public class JingleInBandByteStreamTransport extends JingleContentTransport { + + public static final String ATTR_BLOCK_SIZE = "block-size"; + public static final String ATTR_SID = "sid"; + public static final short DEFAULT_BLOCK_SIZE = 4096; + + private final short blockSize; + private final String sid; + + public JingleInBandByteStreamTransport() { + this(DEFAULT_BLOCK_SIZE); + } + + public JingleInBandByteStreamTransport(short blockSize) { + this(blockSize, JingleInBandByteStreamManager.generateSessionId()); + } + + public JingleInBandByteStreamTransport(short blockSize, String sid) { + super(null); + if (blockSize > 0) { + this.blockSize = blockSize; + } else { + this.blockSize = DEFAULT_BLOCK_SIZE; + } + this.sid = sid; + } + + public String getSessionId() { + return sid; + } + + public short getBlockSize() { + return blockSize; + } + + @Override + protected void addExtraAttributes(XmlStringBuilder xml) { + xml.attribute(ATTR_BLOCK_SIZE, blockSize); + xml.attribute(ATTR_SID, sid); + } + + @Override + public String getNamespace() { + return JingleInBandByteStreamManager.NAMESPACE_V1; + } + + @Override + public boolean equals(Object other) { + if (other == null || !(other instanceof JingleInBandByteStreamTransport)) { + return false; + } + + return this.hashCode() == other.hashCode(); + } + + @Override + public int hashCode() { + return this.toXML().toString().hashCode(); + } +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle_ibb/element/package-info.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle_ibb/element/package-info.java new file mode 100644 index 000000000..d8011dd0c --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle_ibb/element/package-info.java @@ -0,0 +1,22 @@ +/** + * + * Copyright 2017 Paul Schaub + * + * 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. + */ + +/** + * Smack's API for XEP-0261: Jingle In-Band Bytestreams. + * Element classes. + */ +package org.jivesoftware.smackx.jingle_ibb.element; diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle_ibb/package-info.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle_ibb/package-info.java new file mode 100644 index 000000000..ad99c4d18 --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle_ibb/package-info.java @@ -0,0 +1,21 @@ +/** + * + * Copyright 2017 Paul Schaub + * + * 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. + */ + +/** + * Smack's API for XEP-0261: Jingle In-Band Bytestreams. + */ +package org.jivesoftware.smackx.jingle_ibb; diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle_ibb/provider/JingleInBandByteStreamTransportProvider.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle_ibb/provider/JingleInBandByteStreamTransportProvider.java new file mode 100644 index 000000000..cbfbe680e --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle_ibb/provider/JingleInBandByteStreamTransportProvider.java @@ -0,0 +1,39 @@ +/** + * + * Copyright 2017 Paul Schaub + * + * 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.jingle_ibb.provider; + +import org.jivesoftware.smackx.jingle.provider.JingleContentTransportProvider; +import org.jivesoftware.smackx.jingle_ibb.element.JingleInBandByteStreamTransport; +import org.xmlpull.v1.XmlPullParser; + +/** + * Parse JingleByteStreamTransport elements. + */ +public class JingleInBandByteStreamTransportProvider extends JingleContentTransportProvider { + @Override + public JingleInBandByteStreamTransport parse(XmlPullParser parser, int initialDepth) throws Exception { + String blockSizeString = parser.getAttributeValue(null, JingleInBandByteStreamTransport.ATTR_BLOCK_SIZE); + String sid = parser.getAttributeValue(null, JingleInBandByteStreamTransport.ATTR_SID); + + short blockSize = -1; + if (blockSizeString != null) { + blockSize = Short.valueOf(blockSizeString); + } + + return new JingleInBandByteStreamTransport(blockSize, sid); + } +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle_ibb/provider/package-info.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle_ibb/provider/package-info.java new file mode 100644 index 000000000..737556547 --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle_ibb/provider/package-info.java @@ -0,0 +1,22 @@ +/** + * + * Copyright 2017 Paul Schaub + * + * 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. + */ + +/** + * Smack's API for XEP-0261: Jingle In-Band Bytestreams. + * Provider classes. + */ +package org.jivesoftware.smackx.jingle_ibb.provider; diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/jingle_ibb/JingleInBandByteStreamTransportTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/jingle_ibb/JingleInBandByteStreamTransportTest.java new file mode 100644 index 000000000..c58a4f5ee --- /dev/null +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/jingle_ibb/JingleInBandByteStreamTransportTest.java @@ -0,0 +1,51 @@ +package org.jivesoftware.smackx.jingle_ibb; + +import org.jivesoftware.smack.test.util.SmackTestSuite; +import org.jivesoftware.smack.test.util.TestUtils; +import org.jivesoftware.smackx.jingle_ibb.element.JingleInBandByteStreamTransport; +import org.jivesoftware.smackx.jingle_ibb.provider.JingleInBandByteStreamTransportProvider; +import org.junit.Test; + +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertFalse; +import static junit.framework.TestCase.assertNotSame; +import static junit.framework.TestCase.assertTrue; + +/** + * Test JingleInBandByteStreamTransport provider and element. + */ +public class JingleInBandByteStreamTransportTest extends SmackTestSuite { + + @Test + public void parserTest() throws Exception { + String sid = JingleInBandByteStreamManager.generateSessionId(); + short size = 8192; + + String xml = ""; + + JingleInBandByteStreamTransport transport = new JingleInBandByteStreamTransport(size, sid); + assertEquals(xml, transport.toXML().toString()); + assertEquals(size, transport.getBlockSize()); + assertEquals(sid, transport.getSessionId()); + + JingleInBandByteStreamTransport parsed = new JingleInBandByteStreamTransportProvider() + .parse(TestUtils.getParser(xml)); + assertEquals(transport, parsed); + assertTrue(transport.equals(parsed)); + assertEquals(xml, parsed.toXML().toString()); + + JingleInBandByteStreamTransport transport1 = new JingleInBandByteStreamTransport((short) 1024); + assertEquals((short) 1024, transport1.getBlockSize()); + assertNotSame(transport, transport1); + assertNotSame(transport.getSessionId(), transport1.getSessionId()); + + assertFalse(transport.equals(null)); + + JingleInBandByteStreamTransport transport2 = new JingleInBandByteStreamTransport(); + assertEquals(JingleInBandByteStreamTransport.DEFAULT_BLOCK_SIZE, transport2.getBlockSize()); + assertFalse(transport1.equals(transport2)); + + JingleInBandByteStreamTransport transport3 = new JingleInBandByteStreamTransport((short) -1024); + assertEquals(JingleInBandByteStreamTransport.DEFAULT_BLOCK_SIZE, transport3.getBlockSize()); + } +}