From 287976e0e0cd8b8f3f2779a5ffc3e8c7268fb410 Mon Sep 17 00:00:00 2001 From: vanitasvitae Date: Fri, 16 Jun 2017 22:43:50 +0200 Subject: [PATCH] Add Jingle InBandBytestream transports --- .../element/JingleIBBTransport.java | 90 +++++++++++++++++++ .../jingle_ibb/element/package-info.java | 22 +++++ .../transports/jingle_ibb/package-info.java | 21 +++++ .../provider/JingleIBBTransportProvider.java | 39 ++++++++ .../jingle_ibb/provider/package-info.java | 22 +++++ .../jingle_ibb/JingleIBBTransportTest.java | 71 +++++++++++++++ 6 files changed, 265 insertions(+) create mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/transports/jingle_ibb/element/JingleIBBTransport.java create mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/transports/jingle_ibb/element/package-info.java create mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/transports/jingle_ibb/package-info.java create mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/transports/jingle_ibb/provider/JingleIBBTransportProvider.java create mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/transports/jingle_ibb/provider/package-info.java create mode 100644 smack-extensions/src/test/java/org/jivesoftware/smackx/jingle/transports/jingle_ibb/JingleIBBTransportTest.java diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/transports/jingle_ibb/element/JingleIBBTransport.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/transports/jingle_ibb/element/JingleIBBTransport.java new file mode 100644 index 000000000..89b9fef00 --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/transports/jingle_ibb/element/JingleIBBTransport.java @@ -0,0 +1,90 @@ +/** + * + * 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.transports.jingle_ibb.element; + +import org.jivesoftware.smack.util.StringUtils; +import org.jivesoftware.smack.util.XmlStringBuilder; +import org.jivesoftware.smackx.jingle.element.JingleContentTransport; + +/** + * Transport Element for JingleInBandBytestream transports. + */ +public class JingleIBBTransport extends JingleContentTransport { + public static final String NAMESPACE_V1 = "urn:xmpp:jingle:transports:ibb:1"; + 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 JingleIBBTransport() { + this(DEFAULT_BLOCK_SIZE); + } + + public JingleIBBTransport(String sid) { + this(DEFAULT_BLOCK_SIZE, sid); + } + + public JingleIBBTransport(short blockSize) { + this(blockSize, StringUtils.randomString(24)); + } + + public JingleIBBTransport(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 NAMESPACE_V1; + } + + @Override + public boolean equals(Object other) { + if (other == null || !(other instanceof JingleIBBTransport)) { + return false; + } + + return this == other || 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/transports/jingle_ibb/element/package-info.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/transports/jingle_ibb/element/package-info.java new file mode 100644 index 000000000..3cbb5462e --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/transports/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.transports.jingle_ibb.element; diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/transports/jingle_ibb/package-info.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/transports/jingle_ibb/package-info.java new file mode 100644 index 000000000..b42da7b09 --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/transports/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.transports.jingle_ibb; diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/transports/jingle_ibb/provider/JingleIBBTransportProvider.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/transports/jingle_ibb/provider/JingleIBBTransportProvider.java new file mode 100644 index 000000000..e4b2121e6 --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/transports/jingle_ibb/provider/JingleIBBTransportProvider.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.transports.jingle_ibb.provider; + +import org.jivesoftware.smackx.jingle.provider.JingleContentTransportProvider; +import org.jivesoftware.smackx.jingle.transports.jingle_ibb.element.JingleIBBTransport; +import org.xmlpull.v1.XmlPullParser; + +/** + * Parse JingleByteStreamTransport elements. + */ +public class JingleIBBTransportProvider extends JingleContentTransportProvider { + @Override + public JingleIBBTransport parse(XmlPullParser parser, int initialDepth) throws Exception { + String blockSizeString = parser.getAttributeValue(null, JingleIBBTransport.ATTR_BLOCK_SIZE); + String sid = parser.getAttributeValue(null, JingleIBBTransport.ATTR_SID); + + short blockSize = -1; + if (blockSizeString != null) { + blockSize = Short.valueOf(blockSizeString); + } + + return new JingleIBBTransport(blockSize, sid); + } +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/transports/jingle_ibb/provider/package-info.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/transports/jingle_ibb/provider/package-info.java new file mode 100644 index 000000000..3bf479e8e --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/transports/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.transports.jingle_ibb.provider; diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/jingle/transports/jingle_ibb/JingleIBBTransportTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/jingle/transports/jingle_ibb/JingleIBBTransportTest.java new file mode 100644 index 000000000..41021baae --- /dev/null +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/jingle/transports/jingle_ibb/JingleIBBTransportTest.java @@ -0,0 +1,71 @@ +/** + * + * 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.transports.jingle_ibb; + +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertFalse; +import static junit.framework.TestCase.assertNotSame; +import static junit.framework.TestCase.assertTrue; + +import org.jivesoftware.smack.test.util.SmackTestSuite; +import org.jivesoftware.smack.test.util.TestUtils; +import org.jivesoftware.smack.util.StringUtils; +import org.jivesoftware.smackx.jingle.transports.jingle_ibb.element.JingleIBBTransport; +import org.jivesoftware.smackx.jingle.transports.jingle_ibb.provider.JingleIBBTransportProvider; +import org.junit.Test; + +/** + * Test JingleIBBTransport provider and element. + */ +public class JingleIBBTransportTest extends SmackTestSuite { + + @Test + public void parserTest() throws Exception { + String sid = StringUtils.randomString(24); + short size = 8192; + + String xml = ""; + + JingleIBBTransport transport = new JingleIBBTransport(size, sid); + assertEquals(xml, transport.toXML().toString()); + assertEquals(size, transport.getBlockSize()); + assertEquals(sid, transport.getSessionId()); + + JingleIBBTransport parsed = new JingleIBBTransportProvider() + .parse(TestUtils.getParser(xml)); + assertEquals(transport, parsed); + assertTrue(transport.equals(parsed)); + assertEquals(xml, parsed.toXML().toString()); + + JingleIBBTransport transport1 = new JingleIBBTransport((short) 1024); + assertEquals((short) 1024, transport1.getBlockSize()); + assertNotSame(transport, transport1); + assertNotSame(transport.getSessionId(), transport1.getSessionId()); + + assertFalse(transport.equals(null)); + + JingleIBBTransport transport2 = new JingleIBBTransport(); + assertEquals(JingleIBBTransport.DEFAULT_BLOCK_SIZE, transport2.getBlockSize()); + assertFalse(transport1.equals(transport2)); + + JingleIBBTransport transport3 = new JingleIBBTransport((short) -1024); + assertEquals(JingleIBBTransport.DEFAULT_BLOCK_SIZE, transport3.getBlockSize()); + + assertEquals(transport3.getNamespace(), JingleIBBTransport.NAMESPACE_V1); + assertEquals(transport3.getElementName(), "transport"); + } +}