1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-23 20:42:06 +01:00

Add JingleInBandByteStream classes

This commit is contained in:
vanitasvitae 2017-05-31 17:26:51 +02:00
parent de3c016e3d
commit 2cbbfd1048
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
9 changed files with 313 additions and 6 deletions

View file

@ -51,6 +51,10 @@ public class HashElementTest extends SmackTestSuite {
assertFalse(parsed.equals(null)); assertFalse(parsed.equals(null));
assertEquals(element, parsed); assertEquals(element, parsed);
assertTrue(element.equals(parsed)); assertTrue(element.equals(parsed));
HashElement other = new HashElement(HashManager.ALGORITHM.SHA_512,
"861844d6704e8573fec34d967e20bcfef3d424cf48be04e6dc08f2bd58c729743371015ead891cc3cf1c9d34b49264b510751b1ff9e537937bc46b5d6ff4ecc8".getBytes(StringUtils.UTF8));
assertFalse(element.equals(other));
} }
} }

View file

@ -16,12 +16,12 @@
*/ */
package org.jivesoftware.smackx.jingle.element; package org.jivesoftware.smackx.jingle.element;
import java.util.Collections;
import java.util.List;
import org.jivesoftware.smack.packet.ExtensionElement; import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
import java.util.Collections;
import java.util.List;
/** /**
* A jingle transport extension. * A jingle transport extension.
* *
@ -58,11 +58,17 @@ public abstract class JingleContentTransport implements ExtensionElement {
public final XmlStringBuilder toXML() { public final XmlStringBuilder toXML() {
XmlStringBuilder xml = new XmlStringBuilder(this); XmlStringBuilder xml = new XmlStringBuilder(this);
addExtraAttributes(xml); 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; return xml;
} }

View file

@ -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<XMPPConnection, JingleInBandByteStreamManager> 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());
}
}

View file

@ -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();
}
}

View file

@ -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 <a href="https://xmpp.org/extensions/xep-0261.html">XEP-0261: Jingle In-Band Bytestreams</a>.
* Element classes.
*/
package org.jivesoftware.smackx.jingle_ibb.element;

View file

@ -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 <a href="https://xmpp.org/extensions/xep-0261.html">XEP-0261: Jingle In-Band Bytestreams</a>.
*/
package org.jivesoftware.smackx.jingle_ibb;

View file

@ -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<JingleInBandByteStreamTransport> {
@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);
}
}

View file

@ -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 <a href="https://xmpp.org/extensions/xep-0261.html">XEP-0261: Jingle In-Band Bytestreams</a>.
* Provider classes.
*/
package org.jivesoftware.smackx.jingle_ibb.provider;

View file

@ -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 = "<transport xmlns='urn:xmpp:jingle:transports:ibb:1' block-size='8192' sid='" + sid + "'/>";
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());
}
}