From 7319998bb9a4723e580560846f47ae55ca7992fd Mon Sep 17 00:00:00 2001 From: vanitasvitae Date: Tue, 30 May 2017 23:42:31 +0200 Subject: [PATCH] Add and test RangeElement --- .../JingleFileTransferManager.java | 2 +- .../element/FileTransfer.java | 96 ++++++++++++++++ .../JingleContentDescriptionFileTransfer.java | 14 ++- .../jingle_filetransfer/element/Range.java | 105 ++++++++++++++++++ .../element/package-info.java | 0 .../jingle_filetransfer/package-info.java | 0 .../provider/package-info.java | 0 .../jingle_filetransfer/FileTransferTest.java | 53 +++++++++ .../element/JingleContentDescription.java | 6 +- .../element/JingleContentFile.java | 43 ------- 10 files changed, 268 insertions(+), 51 deletions(-) rename {smack-extensions => smack-experimental}/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/JingleFileTransferManager.java (95%) create mode 100644 smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/element/FileTransfer.java rename smack-extensions/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/element/JingleFileTransferContentDescription.java => smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/element/JingleContentDescriptionFileTransfer.java (66%) create mode 100644 smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/element/Range.java rename {smack-extensions => smack-experimental}/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/element/package-info.java (100%) rename {smack-extensions => smack-experimental}/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/package-info.java (100%) rename {smack-extensions => smack-experimental}/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/provider/package-info.java (100%) create mode 100644 smack-experimental/src/test/java/org/jivesoftware/smackx/jingle_filetransfer/FileTransferTest.java delete mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/element/JingleContentFile.java diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/JingleFileTransferManager.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/JingleFileTransferManager.java similarity index 95% rename from smack-extensions/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/JingleFileTransferManager.java rename to smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/JingleFileTransferManager.java index 7cdbbf13e..d0e3ed57e 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/JingleFileTransferManager.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/JingleFileTransferManager.java @@ -28,7 +28,7 @@ import java.util.WeakHashMap; */ public final class JingleFileTransferManager extends Manager { - public static final String NAMESPACE = "urn:xmpp:jingle:apps:file-transfer:5"; + public static final String NAMESPACE_V5 = "urn:xmpp:jingle:apps:file-transfer:5"; private static final WeakHashMap INSTANCES = new WeakHashMap<>(); diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/element/FileTransfer.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/element/FileTransfer.java new file mode 100644 index 000000000..a72858330 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/element/FileTransfer.java @@ -0,0 +1,96 @@ +/** + * + * 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_filetransfer.element; + +import org.jivesoftware.smack.packet.NamedElement; +import org.jivesoftware.smack.util.XmlStringBuilder; +import org.jivesoftware.smackx.hash.element.HashElement; + +import java.util.Date; + +/** + * Content of type File. + */ +public class FileTransfer implements NamedElement { + public static final String ELEMENT = "file"; + public static final String ELEM_DATE = "date"; + public static final String ELEM_DESC = "desc"; + public static final String ELEM_MEDIA_TYPE = "media-type"; + public static final String ELEM_NAME = "name"; + public static final String ELEM_SIZE = "size"; + + private final Date date; + private final String desc; + private final HashElement hash; + private final String mediaType; + private final String name; + private final int size; + private final Range range; + + public FileTransfer(Date date, String desc, HashElement hash, String mediaType, String name, int size, Range range) { + this.date = date; + this.desc = desc; + this.hash = hash; + this.mediaType = mediaType; + this.name = name; + this.size = size; + this.range = range; + } + + @Override + public String getElementName() { + return ELEMENT; + } + + @Override + public CharSequence toXML() { + XmlStringBuilder sb = new XmlStringBuilder(this); + sb.rightAngleBracket(); + + if (date != null) { + sb.element(ELEM_DATE, date); + } + + if (desc != null) { + sb.element(ELEM_DESC, desc); + } + + if (mediaType != null) { + sb.element(ELEM_MEDIA_TYPE, mediaType); + } + + if (name != null) { + sb.element(ELEM_NAME, name); + } + + if (range != null) { + sb.element(range); + } + + if (size > 0) { + sb.element(ELEM_SIZE, Integer.toString(size)); + } + + if (hash != null) { + sb.element(hash); + } + + sb.closeElement(this); + return sb; + } + +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/element/JingleFileTransferContentDescription.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/element/JingleContentDescriptionFileTransfer.java similarity index 66% rename from smack-extensions/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/element/JingleFileTransferContentDescription.java rename to smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/element/JingleContentDescriptionFileTransfer.java index 0e9b61729..3255204a2 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/element/JingleFileTransferContentDescription.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/element/JingleContentDescriptionFileTransfer.java @@ -17,19 +17,25 @@ package org.jivesoftware.smackx.jingle_filetransfer.element; import org.jivesoftware.smackx.jingle.element.JingleContentDescription; +import org.jivesoftware.smackx.jingle.element.JingleContentDescriptionPayloadType; import org.jivesoftware.smackx.jingle_filetransfer.JingleFileTransferManager; +import java.util.List; + /** * Description. */ -public class JingleFileTransferContentDescription extends JingleContentDescription { +public class JingleContentDescriptionFileTransfer extends JingleContentDescription { - protected JingleFileTransferContentDescription() { - super(null); + private FileTransfer fileTransfer; + + protected JingleContentDescriptionFileTransfer(List payloadTypes, FileTransfer fileTransfer) { + super(payloadTypes); + this.fileTransfer = fileTransfer; } @Override public String getNamespace() { - return JingleFileTransferManager.NAMESPACE; + return JingleFileTransferManager.NAMESPACE_V5; } } diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/element/Range.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/element/Range.java new file mode 100644 index 000000000..f7e0e10cf --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/element/Range.java @@ -0,0 +1,105 @@ +package org.jivesoftware.smackx.jingle_filetransfer.element; + +import org.jivesoftware.smack.packet.NamedElement; +import org.jivesoftware.smack.util.XmlStringBuilder; +import org.jivesoftware.smackx.hash.element.HashElement; + +/** + * RangeElement which specifies, which range of a file shall be transferred. + */ +public class Range implements NamedElement { + + public static final String ELEMENT = "range"; + public static final String ATTR_OFFSET = "offset"; + public static final String ATTR_LENGTH = "length"; + + private final int offset, length; + private final HashElement hash; + + /** + * Create a Range element with default values. + */ + public Range() { + this(0, -1, null); + } + + /** + * Create a Range element with specified length. + * @param length length of the transmitted data in bytes. + */ + public Range(int length) { + this(0, length, null); + } + + /** + * Create a Range element with specified offset and length. + * @param offset offset in bytes from the beginning of the transmitted data. + * @param length number of bytes that shall be transferred. + */ + public Range(int offset, int length) { + this(offset, length, null); + } + + /** + * Create a Range element with specified offset, length and hash. + * @param offset offset in bytes from the beginning of the transmitted data. + * @param length number of bytes that shall be transferred. + * @param hash hash of the bytes in the specified range. + */ + public Range(int offset, int length, HashElement hash) { + this.offset = offset; + this.length = length; + this.hash = hash; + } + + /** + * Return the index of the offset. + * This marks the begin of the specified range. + * @return offset + */ + public int getOffset() { + return offset; + } + + /** + * Return the length of the range. + * @return length + */ + public int getLength() { + return length; + } + + /** + * Return the hash element that contains a checksum of the bytes specified in the range. + * @return hash element + */ + public HashElement getHash() { + return hash; + } + + @Override + public String getElementName() { + return ELEMENT; + } + + @Override + public CharSequence toXML() { + XmlStringBuilder sb = new XmlStringBuilder(this); + + if (offset > 0) { + sb.attribute(ATTR_OFFSET, offset); + } + if (length > 0) { + sb.attribute(ATTR_LENGTH, length); + } + + if (hash != null) { + sb.rightAngleBracket(); + sb.element(hash); + sb.closeElement(this); + } else { + sb.closeEmptyElement(); + } + return sb; + } +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/element/package-info.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/element/package-info.java similarity index 100% rename from smack-extensions/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/element/package-info.java rename to smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/element/package-info.java diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/package-info.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/package-info.java similarity index 100% rename from smack-extensions/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/package-info.java rename to smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/package-info.java diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/provider/package-info.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/provider/package-info.java similarity index 100% rename from smack-extensions/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/provider/package-info.java rename to smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/provider/package-info.java diff --git a/smack-experimental/src/test/java/org/jivesoftware/smackx/jingle_filetransfer/FileTransferTest.java b/smack-experimental/src/test/java/org/jivesoftware/smackx/jingle_filetransfer/FileTransferTest.java new file mode 100644 index 000000000..ee2d47e53 --- /dev/null +++ b/smack-experimental/src/test/java/org/jivesoftware/smackx/jingle_filetransfer/FileTransferTest.java @@ -0,0 +1,53 @@ +package org.jivesoftware.smackx.jingle_filetransfer; + +import org.jivesoftware.smack.test.util.SmackTestSuite; +import org.jivesoftware.smackx.hash.HashManager; +import org.jivesoftware.smackx.hash.element.HashElement; +import org.jivesoftware.smackx.jingle_filetransfer.element.Range; +import org.junit.Test; + +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertNotNull; +import static junit.framework.TestCase.assertNull; + +/** + * Test the JingleContentFile class. + */ +public class FileTransferTest extends SmackTestSuite { + + @Test + public void rangeTest() throws Exception { + Range range = new Range(); + String xml = ""; + assertEquals(0, range.getOffset()); + assertEquals(-1, range.getLength()); + assertNull(range.getHash()); + assertEquals(xml, range.toXML().toString()); + + range = new Range(4096); + xml = ""; + assertEquals(4096, range.getLength()); + assertEquals(0, range.getOffset()); + assertNull(range.getHash()); + assertEquals(xml, range.toXML().toString()); + + range = new Range(256, 1024); + xml = ""; + assertEquals(256, range.getOffset()); + assertEquals(1024, range.getLength()); + assertNull(range.getHash()); + assertEquals(xml, range.toXML().toString()); + + String hashB64 = "f4OxZX/x/FO5LcGBSKHWXfwtSx+j1ncoSt3SABJtkGk="; + HashElement hashElement = new HashElement(HashManager.ALGORITHM.SHA_256, hashB64); + range = new Range(0, 35, hashElement); + xml = "" + + "f4OxZX/x/FO5LcGBSKHWXfwtSx+j1ncoSt3SABJtkGk=" + + ""; + assertEquals(0, range.getOffset()); + assertEquals(35, range.getLength()); + assertNotNull(range.getHash()); + assertEquals(range.getHash().toXML().toString(), hashElement.toXML().toString()); + assertEquals(xml, range.toXML().toString()); + } +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/JingleContentDescription.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/JingleContentDescription.java index 7a1e6463f..71168303c 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/JingleContentDescription.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/JingleContentDescription.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; + /** * Jingle content description. * diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/element/JingleContentFile.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/element/JingleContentFile.java deleted file mode 100644 index 92ef88852..000000000 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/element/JingleContentFile.java +++ /dev/null @@ -1,43 +0,0 @@ -/** - * - * 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_filetransfer.element; - -import org.jivesoftware.smack.packet.ExtensionElement; - -/** - * Content of type File. - */ -public class JingleContentFile implements ExtensionElement { - public static final String ELEMENT = "file"; - - - - @Override - public String getElementName() { - return ELEMENT; - } - - @Override - public CharSequence toXML() { - return null; - } - - @Override - public String getNamespace() { - return null; - } -}