1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-15 03:59:38 +02:00
This commit is contained in:
vanitasvitae 2017-07-17 20:18:08 +02:00
parent 59e79ef668
commit 7658369d63
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
26 changed files with 268 additions and 356 deletions

View file

@ -33,8 +33,8 @@ import javax.crypto.spec.SecretKeySpec;
import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Element;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smackx.jet.element.JetSecurityElement;
import org.jivesoftware.smackx.jingle.JingleManager;
import org.jivesoftware.smackx.jingle.JingleUtil;
import org.jivesoftware.smackx.jingle.element.Jingle;
@ -140,14 +140,25 @@ public final class JetManager extends Manager {
return null;
}
String contentName = JingleManager.randomId();
ExtensionElement encryptionExtension = encryptionMethod.encryptJingleTransfer(recipient, keyAndIv);
JetSecurityElement securityElement = new JetSecurityElement(contentName, encryptionExtension);
OutgoingJingleFileOffer offer = new OutgoingJingleFileOffer(connection(), recipient);
JingleFileTransferChild fileTransferChild = JingleFileTransferChild.getBuilder().setFile(file).build();
JingleFileTransfer fileTransfer = new JingleFileTransfer(Collections.<JingleContentDescriptionChildElement>singletonList(fileTransferChild));
Jingle initiate = jutil.createSessionInitiateFileOffer(recipient, JingleManager.randomId(), JingleContent.Creator.initiator,
JingleManager.randomId(), fileTransfer, offer.getTransportSession().createTransport(), Collections.<Element>singletonList(encryptionExtension));
JingleContent content = JingleContent.getBuilder()
.setCreator(JingleContent.Creator.initiator)
.setName(contentName)
.setTransport(offer.getTransportSession().createTransport())
.setSecurity(securityElement)
.setDescription(fileTransfer)
.build();
Jingle initiate = jutil.createSessionInitiate(recipient, JingleManager.randomId(), content);
return offer;
}

View file

@ -1,3 +1,19 @@
/**
*
* 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.jet;
import java.io.File;

View file

@ -19,28 +19,30 @@ package org.jivesoftware.smackx.jet.element;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smackx.jet.JetManager;
import org.jivesoftware.smackx.jingle.element.JingleContentSecurity;
/**
* Implementation of the Jingle security element as specified in XEP-XXXX (Jingle Encrypted Transfers).
* <jingle>
* <content>
* <description/>
* <transport/>
* <security/> <- You are here.
* </content>
* </jingle>
*/
public class SecurityElement implements ExtensionElement {
public static final String ELEMENT = "security";
public class JetSecurityElement extends JingleContentSecurity {
public static final String ATTR_NAME = "name";
public static final String ATTR_TYPE = "type";
private final ExtensionElement child;
private final String name;
public SecurityElement(String name, ExtensionElement child) {
public JetSecurityElement(String name, ExtensionElement child) {
this.name = name;
this.child = child;
}
@Override
public String getElementName() {
return ELEMENT;
}
@Override
public CharSequence toXML() {
XmlStringBuilder xml = new XmlStringBuilder(this);

View file

@ -24,20 +24,20 @@ import org.jivesoftware.smack.provider.ExtensionElementProvider;
import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smackx.jet.JetManager;
import org.jivesoftware.smackx.jet.JingleEncryptionMethodManager;
import org.jivesoftware.smackx.jet.element.SecurityElement;
import org.jivesoftware.smackx.jet.element.JetSecurityElement;
import org.xmlpull.v1.XmlPullParser;
/**
* Provider for the Jingle security element for XEP-XXXX (Jingle Encrypted Transfers).
*/
public class SecurityProvider extends ExtensionElementProvider<SecurityElement> {
private static final Logger LOGGER = Logger.getLogger(SecurityProvider.class.getName());
public class JetSecurityProvider extends ExtensionElementProvider<JetSecurityElement> {
private static final Logger LOGGER = Logger.getLogger(JetSecurityProvider.class.getName());
@Override
public SecurityElement parse(XmlPullParser parser, int initialDepth) throws Exception {
String name = parser.getAttributeValue(JetManager.NAMESPACE, SecurityElement.ATTR_NAME);
String type = parser.getAttributeValue(JetManager.NAMESPACE, SecurityElement.ATTR_TYPE);
public JetSecurityElement parse(XmlPullParser parser, int initialDepth) throws Exception {
String name = parser.getAttributeValue(JetManager.NAMESPACE, JetSecurityElement.ATTR_NAME);
String type = parser.getAttributeValue(JetManager.NAMESPACE, JetSecurityElement.ATTR_TYPE);
ExtensionElement child;
Objects.requireNonNull(type);
@ -48,10 +48,10 @@ public class SecurityProvider extends ExtensionElementProvider<SecurityElement>
if (encryptionElementProvider != null) {
child = encryptionElementProvider.parse(parser);
} else {
LOGGER.log(Level.WARNING, "Unknown child element in SecurityElement: " + type);
LOGGER.log(Level.WARNING, "Unknown child element in JetSecurityElement: " + type);
return null;
}
return new SecurityElement(name, child);
return new JetSecurityElement(name, child);
}
}

View file

@ -105,7 +105,7 @@ public class IncomingJingleFileOffer extends JingleFileTransferSession implement
if (transportManager == null) {
//No usable transports.
LOGGER.log(Level.WARNING, "No usable transports.");
jutil.sendSessionTerminateUnsupportedTransports(getInitiator(), getSessionId());
connection.createStanzaCollectorAndSend(jutil.createSessionTerminateUnsupportedTransports(getInitiator(), getSessionId()));
state = State.terminated;
return jutil.createAck(initiate);
}
@ -167,7 +167,7 @@ public class IncomingJingleFileOffer extends JingleFileTransferSession implement
LOGGER.log(Level.INFO, "Unsupported transport. Reject transport-replace.");
jutil.sendTransportReject(transportReplace.getFrom().asFullJidOrThrow(), transportReplace.getInitiator(),
transportReplace.getSid(), getContents().get(0).getCreator(),
getContents().get(0).getName(), transportReplace.getContents().get(0).getJingleTransport());
getContents().get(0).getName(), transportReplace.getContents().get(0).getTransport());
}
} catch (InterruptedException | XMPPException.XMPPErrorException | SmackException.NotConnectedException | SmackException.NoResponseException e) {
LOGGER.log(Level.SEVERE, "Help me please!", e);

View file

@ -184,7 +184,7 @@ public class OutgoingJingleFileOffer extends JingleFileTransferSession {
LOGGER.log(Level.INFO, "Unsupported transport. Reject transport-replace.");
jutil.sendTransportReject(transportReplace.getFrom().asFullJidOrThrow(), transportReplace.getInitiator(),
transportReplace.getSid(), getContents().get(0).getCreator(),
getContents().get(0).getName(), transportReplace.getContents().get(0).getJingleTransport());
getContents().get(0).getName(), transportReplace.getContents().get(0).getTransport());
}
} catch (InterruptedException | XMPPException.XMPPErrorException | SmackException.NotConnectedException | SmackException.NoResponseException e) {
LOGGER.log(Level.SEVERE, "Help me please!", e);

View file

@ -29,22 +29,22 @@ public class Range implements NamedElement {
public static final String ATTR_OFFSET = "offset";
public static final String ATTR_LENGTH = "length";
private final int offset, length;
private final Long offset, length;
private final HashElement hash;
/**
* Create a Range element with default values.
*/
public Range() {
this(0, -1, null);
this(null, null, 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);
public Range(Long length) {
this(null, length, null);
}
/**
@ -52,7 +52,7 @@ public class Range implements NamedElement {
* @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) {
public Range(Long offset, Long length) {
this(offset, length, null);
}
@ -62,7 +62,7 @@ public class Range implements NamedElement {
* @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) {
public Range(Long offset, Long length, HashElement hash) {
this.offset = offset;
this.length = length;
this.hash = hash;
@ -73,7 +73,7 @@ public class Range implements NamedElement {
* This marks the begin of the specified range.
* @return offset
*/
public int getOffset() {
public Long getOffset() {
return offset;
}
@ -81,7 +81,7 @@ public class Range implements NamedElement {
* Return the length of the range.
* @return length
*/
public int getLength() {
public Long getLength() {
return length;
}
@ -102,12 +102,8 @@ public class Range implements NamedElement {
public CharSequence toXML() {
XmlStringBuilder sb = new XmlStringBuilder(this);
if (offset > 0) {
sb.attribute(ATTR_OFFSET, offset);
}
if (length > 0) {
sb.attribute(ATTR_LENGTH, length);
}
sb.optAttribute(ATTR_OFFSET, offset);
sb.optAttribute(ATTR_LENGTH, length);
if (hash != null) {
sb.rightAngleBracket();

View file

@ -20,6 +20,7 @@ import static org.xmlpull.v1.XmlPullParser.END_TAG;
import static org.xmlpull.v1.XmlPullParser.START_TAG;
import org.jivesoftware.smack.provider.ExtensionElementProvider;
import org.jivesoftware.smack.util.ParserUtils;
import org.jivesoftware.smackx.hashes.element.HashElement;
import org.jivesoftware.smackx.hashes.provider.HashElementProvider;
import org.jivesoftware.smackx.jingle.element.JingleContent;
@ -59,11 +60,9 @@ public class ChecksumProvider extends ExtensionElementProvider<Checksum> {
break;
case Range.ELEMENT:
String offset = parser.getAttributeValue(null, Range.ATTR_OFFSET);
String length = parser.getAttributeValue(null, Range.ATTR_LENGTH);
int o = offset == null ? 0 : Integer.parseInt(offset);
int l = length == null ? -1 : Integer.parseInt(length);
range = new Range(o, l);
Long offset = ParserUtils.getLongAttribute(parser, Range.ATTR_OFFSET);
Long length = ParserUtils.getLongAttribute(parser, Range.ATTR_LENGTH);
range = new Range(offset, length);
}
} else if (tag == END_TAG) {
switch (n) {

View file

@ -21,6 +21,7 @@ import static org.xmlpull.v1.XmlPullParser.START_TAG;
import java.util.ArrayList;
import org.jivesoftware.smack.util.ParserUtils;
import org.jivesoftware.smackx.hashes.element.HashElement;
import org.jivesoftware.smackx.hashes.provider.HashElementProvider;
import org.jivesoftware.smackx.jingle.element.JingleContentDescriptionChildElement;
@ -44,10 +45,7 @@ public class JingleFileTransferProvider
boolean inRange = false;
JingleFileTransferChild.Builder builder = JingleFileTransferChild.getBuilder();
HashElement inRangeHash = null;
int offset = 0;
int length = -1;
Long length = null, offset = null;
while (true) {
int tag = parser.nextTag();
@ -77,10 +75,8 @@ public class JingleFileTransferProvider
case Range.ELEMENT:
inRange = true;
String offsetString = parser.getAttributeValue(null, Range.ATTR_OFFSET);
String lengthString = parser.getAttributeValue(null, Range.ATTR_LENGTH);
offset = (offsetString != null ? Integer.parseInt(offsetString) : 0);
length = (lengthString != null ? Integer.parseInt(lengthString) : -1);
offset = ParserUtils.getLongAttribute(parser, Range.ATTR_OFFSET);
length = ParserUtils.getLongAttribute(parser, Range.ATTR_LENGTH);
if (parser.isEmptyElementTag()) {
inRange = false;

View file

@ -50,7 +50,7 @@ public class ChecksumTest extends SmackTestSuite {
assertXMLEqual(xml, checksum.toXML().toString());
assertXMLEqual(xml, new ChecksumProvider().parse(TestUtils.getParser(xml)).toXML().toString());
Range range = new Range(12,34);
Range range = new Range(12L,34L);
file = new JingleFileTransferChild(null, null, hash, null, null, -1, range);
checksum = new Checksum(JingleContent.Creator.initiator, "name", file);

View file

@ -118,7 +118,7 @@ public class JingleUtilFileTransferTest extends SmackTestSuite {
assertEquals(1337, file.getSize());
assertNull(file.getRange());
assertEquals(transport, content.getJingleTransport());
assertEquals(transport, content.getTransport());
assertEquals("transid", transport.getSessionId());
assertEquals(JingleIBBTransport.DEFAULT_BLOCK_SIZE, transport.getBlockSize());