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

Add and test RangeElement

This commit is contained in:
vanitasvitae 2017-05-30 23:42:31 +02:00
parent 47f800be3b
commit 7319998bb9
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
10 changed files with 268 additions and 51 deletions

View file

@ -28,7 +28,7 @@ import java.util.WeakHashMap;
*/ */
public final class JingleFileTransferManager extends Manager { 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<XMPPConnection, JingleFileTransferManager> INSTANCES = new WeakHashMap<>(); private static final WeakHashMap<XMPPConnection, JingleFileTransferManager> INSTANCES = new WeakHashMap<>();

View file

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

View file

@ -17,19 +17,25 @@
package org.jivesoftware.smackx.jingle_filetransfer.element; package org.jivesoftware.smackx.jingle_filetransfer.element;
import org.jivesoftware.smackx.jingle.element.JingleContentDescription; import org.jivesoftware.smackx.jingle.element.JingleContentDescription;
import org.jivesoftware.smackx.jingle.element.JingleContentDescriptionPayloadType;
import org.jivesoftware.smackx.jingle_filetransfer.JingleFileTransferManager; import org.jivesoftware.smackx.jingle_filetransfer.JingleFileTransferManager;
import java.util.List;
/** /**
* Description. * Description.
*/ */
public class JingleFileTransferContentDescription extends JingleContentDescription { public class JingleContentDescriptionFileTransfer extends JingleContentDescription {
protected JingleFileTransferContentDescription() { private FileTransfer fileTransfer;
super(null);
protected JingleContentDescriptionFileTransfer(List<JingleContentDescriptionPayloadType> payloadTypes, FileTransfer fileTransfer) {
super(payloadTypes);
this.fileTransfer = fileTransfer;
} }
@Override @Override
public String getNamespace() { public String getNamespace() {
return JingleFileTransferManager.NAMESPACE; return JingleFileTransferManager.NAMESPACE_V5;
} }
} }

View file

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

View file

@ -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 = "<range/>";
assertEquals(0, range.getOffset());
assertEquals(-1, range.getLength());
assertNull(range.getHash());
assertEquals(xml, range.toXML().toString());
range = new Range(4096);
xml = "<range length='4096'/>";
assertEquals(4096, range.getLength());
assertEquals(0, range.getOffset());
assertNull(range.getHash());
assertEquals(xml, range.toXML().toString());
range = new Range(256, 1024);
xml = "<range offset='256' length='1024'/>";
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 = "<range length='35'>" +
"<hash xmlns='urn:xmpp:hashes:2' algo='sha-256'>f4OxZX/x/FO5LcGBSKHWXfwtSx+j1ncoSt3SABJtkGk=</hash>" +
"</range>";
assertEquals(0, range.getOffset());
assertEquals(35, range.getLength());
assertNotNull(range.getHash());
assertEquals(range.getHash().toXML().toString(), hashElement.toXML().toString());
assertEquals(xml, range.toXML().toString());
}
}

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;
/** /**
* Jingle content description. * Jingle content description.
* *

View file

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