mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-09-10 17:49:38 +02:00
Proper(er) implementation
This commit is contained in:
parent
83cedbe00f
commit
2686fc1ccb
19 changed files with 674 additions and 469 deletions
|
@ -17,48 +17,34 @@
|
|||
package org.jivesoftware.smackx.jingle_filetransfer;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.WeakHashMap;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.jivesoftware.smack.Manager;
|
||||
import org.jivesoftware.smack.SmackException;
|
||||
import org.jivesoftware.smack.XMPPConnection;
|
||||
import org.jivesoftware.smack.XMPPException;
|
||||
import org.jivesoftware.smack.packet.IQ;
|
||||
import org.jivesoftware.smack.packet.XMPPError;
|
||||
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamListener;
|
||||
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager;
|
||||
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamRequest;
|
||||
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamSession;
|
||||
import org.jivesoftware.smack.util.StringUtils;
|
||||
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
||||
import org.jivesoftware.smackx.hash.HashManager;
|
||||
import org.jivesoftware.smackx.hash.element.HashElement;
|
||||
import org.jivesoftware.smackx.jingle.JingleHandler;
|
||||
import org.jivesoftware.smackx.jingle.JingleManager;
|
||||
import org.jivesoftware.smackx.jingle.JingleSession;
|
||||
import org.jivesoftware.smackx.jingle.JingleSessionHandler;
|
||||
import org.jivesoftware.smackx.jingle.element.Jingle;
|
||||
import org.jivesoftware.smackx.jingle.element.JingleAction;
|
||||
import org.jivesoftware.smackx.jingle.element.JingleContent;
|
||||
import org.jivesoftware.smackx.jingle.element.JingleContentDescriptionChildElement;
|
||||
import org.jivesoftware.smackx.jingle.element.JingleError;
|
||||
import org.jivesoftware.smackx.jingle.element.JingleReason;
|
||||
import org.jivesoftware.smackx.jingle.provider.JingleContentProviderManager;
|
||||
import org.jivesoftware.smackx.jingle_filetransfer.callback.IncomingJingleFileTransferCallback;
|
||||
import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransferChildElement;
|
||||
import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransferContentDescription;
|
||||
import org.jivesoftware.smackx.jingle_filetransfer.listener.IncomingJingleFileTransferListener;
|
||||
import org.jivesoftware.smackx.jingle_filetransfer.provider.JingleFileTransferContentDescriptionProvider;
|
||||
import org.jivesoftware.smackx.jingle_ibb.JingleInBandByteStreamManager;
|
||||
import org.jivesoftware.smackx.jingle_ibb.element.JingleInBandByteStreamTransport;
|
||||
import org.jivesoftware.smackx.jingle_ibb.JingleInBandBytestreamTransportManager;
|
||||
import org.jivesoftware.smackx.jingle_ibb.element.JingleInBandBytestreamTransport;
|
||||
import org.jxmpp.jid.FullJid;
|
||||
|
||||
/**
|
||||
|
@ -66,15 +52,15 @@ import org.jxmpp.jid.FullJid;
|
|||
*
|
||||
* @author Paul Schaub
|
||||
*/
|
||||
public final class JingleFileTransferManager extends Manager implements JingleHandler, JingleSessionHandler {
|
||||
public final class JingleFileTransferManager extends Manager implements JingleHandler {
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger(JingleFileTransferManager.class.getName());
|
||||
|
||||
public static final String NAMESPACE_V5 = "urn:xmpp:jingle:apps:file-transfer:5";
|
||||
|
||||
private final JingleManager jingleManager;
|
||||
private static final WeakHashMap<XMPPConnection, JingleFileTransferManager> INSTANCES = new WeakHashMap<>();
|
||||
private final HashSet<IncomingJingleFileTransferListener> incomingJingleFileTransferListeners = new HashSet<>();
|
||||
private final HashMap<String, JingleSession> sessions = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Private constructor. This registers a JingleContentDescriptionFileTransferProvider with the
|
||||
|
@ -85,11 +71,12 @@ public final class JingleFileTransferManager extends Manager implements JingleHa
|
|||
super(connection);
|
||||
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
|
||||
sdm.addFeature(NAMESPACE_V5);
|
||||
JingleManager.getInstanceFor(connection).registerDescriptionHandler(
|
||||
jingleManager = JingleManager.getInstanceFor(connection);
|
||||
jingleManager.registerDescriptionHandler(
|
||||
NAMESPACE_V5, this);
|
||||
JingleContentProviderManager.addJingleContentDescriptionProvider(
|
||||
NAMESPACE_V5, new JingleFileTransferContentDescriptionProvider());
|
||||
|
||||
JingleInBandBytestreamTransportManager.getInstanceFor(connection);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -115,170 +102,10 @@ public final class JingleFileTransferManager extends Manager implements JingleHa
|
|||
incomingJingleFileTransferListeners.remove(listener);
|
||||
}
|
||||
|
||||
public JingleFileTransferChildElement.Builder fileTransferPayloadBuilderFromFile(File file) {
|
||||
JingleFileTransferChildElement.Builder payloadBuilder = JingleFileTransferChildElement.getBuilder();
|
||||
payloadBuilder.setDate(new Date(file.lastModified()));
|
||||
payloadBuilder.setName(file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf(File.pathSeparator) + 1));
|
||||
payloadBuilder.setSize((int) file.length());
|
||||
return payloadBuilder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IQ handleJingleRequest(final Jingle jingle) {
|
||||
LOGGER.log(Level.INFO, "handleJingleRequest");
|
||||
final JingleSession session = new JingleSession(jingle.getInitiator(), jingle.getResponder(), jingle.getSid());
|
||||
sessions.put(jingle.getSid(), session);
|
||||
JingleManager.getInstanceFor(connection()).registerJingleSessionHandler(jingle.getInitiator(), session.getSid(), this);
|
||||
|
||||
void notifyIncomingFileTransferListeners(Jingle jingle, IncomingJingleFileTransferCallback callback) {
|
||||
for (IncomingJingleFileTransferListener l : incomingJingleFileTransferListeners) {
|
||||
l.onIncomingJingleFileTransfer(jingle, new IncomingJingleFileTransferCallback() {
|
||||
@Override
|
||||
public void acceptFileTransfer(final File target) throws SmackException.NotConnectedException, InterruptedException {
|
||||
|
||||
InBandBytestreamManager.getByteStreamManager(connection()).addIncomingBytestreamListener(new InBandBytestreamListener() {
|
||||
@Override
|
||||
public void incomingBytestreamRequest(InBandBytestreamRequest request) {
|
||||
try {
|
||||
if (!target.exists()) {
|
||||
target.createNewFile();
|
||||
}
|
||||
JingleFileTransferChildElement payload = (JingleFileTransferChildElement) jingle.getContents().get(0).getDescription().getJingleContentDescriptionChildren().get(0);
|
||||
JingleInBandByteStreamTransport transport = (JingleInBandByteStreamTransport) jingle.getContents().get(0).getJingleTransports().get(0);
|
||||
int s = payload.getSize();
|
||||
int bs = transport.getBlockSize();
|
||||
byte[] recv = new byte[s];
|
||||
|
||||
FileOutputStream o = new FileOutputStream(target);
|
||||
InBandBytestreamSession ibs = request.accept();
|
||||
InputStream i = ibs.getInputStream();
|
||||
int read = 0;
|
||||
int count = 0;
|
||||
while (read > -1 && read < s) {
|
||||
byte[] buf = new byte[bs];
|
||||
int r = i.read(buf);
|
||||
read += r;
|
||||
|
||||
LOGGER.log(Level.INFO, "Read " + r + " (" + read + ") bytes of " + s + " (" + count + " of size " + bs + ")");
|
||||
System.arraycopy(buf, 0, recv,bs * count, r);
|
||||
count++;
|
||||
}
|
||||
i.close();
|
||||
o.write(recv);
|
||||
o.close();
|
||||
} catch (IOException | SmackException.NotConnectedException | InterruptedException e) {
|
||||
LOGGER.log(Level.SEVERE, e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Jingle.Builder jb = Jingle.getBuilder();
|
||||
jb.setAction(JingleAction.session_accept)
|
||||
.setSessionId(jingle.getSid())
|
||||
.setInitiator(jingle.getInitiator())
|
||||
.setResponder(jingle.getResponder())
|
||||
.addJingleContent(jingle.getContents().get(0));
|
||||
Jingle j = jb.build();
|
||||
j.setTo(jingle.getFrom());
|
||||
j.setType(IQ.Type.set);
|
||||
connection().sendStanza(j);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancelFileTransfer() throws SmackException.NotConnectedException, InterruptedException {
|
||||
Jingle.Builder jb = Jingle.getBuilder();
|
||||
jb.setInitiator(jingle.getInitiator())
|
||||
.setResponder(jingle.getResponder())
|
||||
.setSessionId(jingle.getSid())
|
||||
.setAction(JingleAction.session_terminate)
|
||||
.setReason(JingleReason.Reason.decline);
|
||||
connection().sendStanza(jb.build());
|
||||
}
|
||||
});
|
||||
l.onIncomingJingleFileTransfer(jingle, callback);
|
||||
}
|
||||
|
||||
return IQ.createResultIQ(jingle);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IQ handleJingleSessionRequest(Jingle jingle, String sessionId) {
|
||||
LOGGER.log(Level.INFO, "handleJingleSessionRequest");
|
||||
JingleSession session = sessions.get(sessionId);
|
||||
|
||||
if (session == null) {
|
||||
// Handle unknown session (XEP-0166 §10)
|
||||
XMPPError.Builder errorBuilder = XMPPError.getBuilder();
|
||||
errorBuilder.setCondition(XMPPError.Condition.item_not_found)
|
||||
.addExtension(JingleError.UNKNOWN_SESSION);
|
||||
return IQ.createErrorResponse(jingle, errorBuilder);
|
||||
}
|
||||
|
||||
for (int i = 0; i < jingle.getContents().size() && i < 1; i++) { //TODO: Remove && i<1 later
|
||||
switch (jingle.getAction()) {
|
||||
case session_initiate:
|
||||
throw new AssertionError("Session is already initiated.");
|
||||
case session_accept:
|
||||
try {
|
||||
connection().sendStanza(IQ.createResultIQ(jingle));
|
||||
} catch (SmackException.NotConnectedException | InterruptedException e) {
|
||||
LOGGER.log(Level.SEVERE, e.getMessage(), e);
|
||||
}
|
||||
LOGGER.log(Level.INFO, "Received session-accept");
|
||||
// Remote accepts our session-initiate
|
||||
InBandBytestreamManager ibm = InBandBytestreamManager.getByteStreamManager(connection());
|
||||
ibm.setMaximumBlockSize(4096);
|
||||
InBandBytestreamSession ibs;
|
||||
try {
|
||||
ibs = ibm.establishSession(jingle.getResponder(), sessionId);
|
||||
} catch (SmackException.NoResponseException | InterruptedException | SmackException.NotConnectedException | XMPPException.XMPPErrorException e) {
|
||||
LOGGER.log(Level.SEVERE, "Fail in handle request: " + e, e);
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
LOGGER.log(Level.INFO, "Writing bytes...");
|
||||
OutgoingJingleFileTransferSession outgoing = (OutgoingJingleFileTransferSession) session;
|
||||
ibs.getOutputStream().write(outgoing.getBytes());
|
||||
ibs.close();
|
||||
LOGGER.log(Level.INFO, "Bytes written.");
|
||||
} catch (IOException e) {
|
||||
LOGGER.log(Level.SEVERE, "Fail while writing: " + e, e);
|
||||
}
|
||||
// ACK
|
||||
return IQ.createResultIQ(jingle);
|
||||
case session_info:
|
||||
// Remote sends session-info (eg. hash)
|
||||
case session_terminate:
|
||||
// Remote wants to terminate our current session
|
||||
sessions.remove(sessionId);
|
||||
return IQ.createResultIQ(jingle);
|
||||
|
||||
case content_accept:
|
||||
// Remote accepts our content-add request.
|
||||
case content_add:
|
||||
// Remote wants to add content to the session.
|
||||
case content_modify:
|
||||
// Remote wants to change the directionality of the session
|
||||
case content_reject:
|
||||
// Remote rejects our content-add request
|
||||
case content_remove:
|
||||
// Remote wants to remove a content from the session/abort a single transfer
|
||||
case description_info:
|
||||
// Additional parameters of exchanged media
|
||||
case security_info:
|
||||
// Remote wants to exchange security information
|
||||
case transport_accept:
|
||||
// Remote accepts our transport-replace
|
||||
case transport_info:
|
||||
// Remote exchanges transport methods
|
||||
case transport_reject:
|
||||
// Remote rejects our transport-replace
|
||||
case transport_replace:
|
||||
// Remote wants to replace the transport
|
||||
default:
|
||||
return IQ.createErrorResponse(jingle, XMPPError.Condition.feature_not_implemented);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -286,42 +113,40 @@ public final class JingleFileTransferManager extends Manager implements JingleHa
|
|||
* @param file
|
||||
*/
|
||||
public void sendFile(File file, final FullJid recipient) throws IOException, SmackException.NotConnectedException, InterruptedException {
|
||||
final byte[] bytes = new byte[(int) file.length()];
|
||||
HashElement hashElement = FileAndHashReader.readAndCalculateHash(file, bytes, HashManager.ALGORITHM.SHA_256);
|
||||
Date lastModified = new Date(file.lastModified());
|
||||
JingleFileTransferChildElement payload = new JingleFileTransferChildElement(
|
||||
lastModified, "A file", hashElement,
|
||||
"application/octet-stream", file.getName(), (int) file.length(), null);
|
||||
ArrayList<JingleContentDescriptionChildElement> payloadTypes = new ArrayList<>();
|
||||
payloadTypes.add(payload);
|
||||
JingleFileTransferSession session = new JingleFileTransferSession(connection(), recipient, connection().getUser(), recipient);
|
||||
JingleFileTransferChildElement.Builder b = JingleFileTransferChildElement.getBuilder();
|
||||
b.setFile(file);
|
||||
byte[] buf = new byte[(int) file.length()];
|
||||
HashElement hashElement = FileAndHashReader.readAndCalculateHash(file, buf, HashManager.ALGORITHM.SHA_256);
|
||||
b.setHash(hashElement);
|
||||
b.setDescription("File");
|
||||
b.setMediaType("text/plain");
|
||||
|
||||
JingleFileTransferContentDescription descriptionFileTransfer = new JingleFileTransferContentDescription(payloadTypes);
|
||||
final JingleInBandByteStreamTransport transport = new JingleInBandByteStreamTransport();
|
||||
JingleContent.Builder cb = JingleContent.getBuilder();
|
||||
cb.setDescription(descriptionFileTransfer)
|
||||
.addTransport(transport)
|
||||
session.setBytes(buf);
|
||||
JingleManager.getInstanceFor(connection()).registerJingleSession(session);
|
||||
|
||||
ArrayList<JingleContentDescriptionChildElement> payloads = new ArrayList<>();
|
||||
payloads.add(b.build());
|
||||
|
||||
JingleContent.Builder bb = JingleContent.getBuilder();
|
||||
bb.setDescription(new JingleFileTransferContentDescription(payloads))
|
||||
.setCreator(JingleContent.Creator.initiator)
|
||||
.setSenders(JingleContent.Senders.initiator)
|
||||
.setName("file");
|
||||
JingleContent content = cb.build();
|
||||
.setName(StringUtils.randomString(24))
|
||||
.addTransport(new JingleInBandBytestreamTransport());
|
||||
|
||||
final String sid = JingleInBandByteStreamManager.generateSessionId();
|
||||
|
||||
Jingle.Builder jb = Jingle.getBuilder();
|
||||
jb.setInitiator(connection().getUser())
|
||||
.setResponder(recipient)
|
||||
.setAction(JingleAction.session_initiate)
|
||||
.addJingleContent(content)
|
||||
.setSessionId(sid);
|
||||
Jingle jingle = jb.build();
|
||||
Jingle jingle = (Jingle) session.initiate(Collections.singletonList(bb.build()));
|
||||
jingle.setTo(recipient);
|
||||
jingle.setType(IQ.Type.set);
|
||||
|
||||
OutgoingJingleFileTransferSession session = new OutgoingJingleFileTransferSession(jingle);
|
||||
session.setBytes(bytes);
|
||||
sessions.put(sid, session);
|
||||
JingleManager.getInstanceFor(connection()).registerJingleSessionHandler(jingle.getResponder(), session.getSid(), this);
|
||||
|
||||
connection().sendStanza(jingle);
|
||||
}
|
||||
|
||||
public FullJid ourJid() {
|
||||
return connection().getUser();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IQ handleJingleRequest(Jingle jingle) {
|
||||
JingleFileTransferSession session = new JingleFileTransferSession(connection(), jingle);
|
||||
JingleManager.getInstanceFor(connection()).registerJingleSession(session);
|
||||
return session.handleRequest(jingle);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,145 @@
|
|||
/**
|
||||
*
|
||||
* 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;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.jivesoftware.smack.SmackException;
|
||||
import org.jivesoftware.smack.XMPPConnection;
|
||||
import org.jivesoftware.smackx.jingle.JingleInputStream;
|
||||
import org.jivesoftware.smackx.jingle.JingleSession;
|
||||
import org.jivesoftware.smackx.jingle.JingleTransportInputStreamCallback;
|
||||
import org.jivesoftware.smackx.jingle.element.Jingle;
|
||||
import org.jivesoftware.smackx.jingle_filetransfer.callback.IncomingJingleFileTransferCallback;
|
||||
import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransferChildElement;
|
||||
import org.jivesoftware.smackx.jingle_ibb.JingleInBandBytestreamTransportManager;
|
||||
import org.jxmpp.jid.FullJid;
|
||||
|
||||
/**
|
||||
* Represent a jingle file transfer session.
|
||||
*/
|
||||
public class JingleFileTransferSession extends JingleSession {
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger(JingleFileTransferSession.class.getName());
|
||||
|
||||
private byte[] buffer;
|
||||
|
||||
public JingleFileTransferSession(XMPPConnection connection, FullJid remote, FullJid initiator, FullJid responder, String sid) {
|
||||
super(connection, remote, initiator, responder, sid);
|
||||
}
|
||||
|
||||
public JingleFileTransferSession(XMPPConnection connection, FullJid remote, FullJid initiator, FullJid responder) {
|
||||
super(connection, remote, initiator, responder);
|
||||
}
|
||||
|
||||
public JingleFileTransferSession(XMPPConnection connection, Jingle initiate) {
|
||||
super(connection, initiate);
|
||||
}
|
||||
|
||||
public void setBytes(byte[] bytes) {
|
||||
this.buffer = bytes;
|
||||
}
|
||||
|
||||
public byte[] getBytes() {
|
||||
return buffer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSessionInitiate(final Jingle jingle) {
|
||||
JingleFileTransferManager jfm = JingleFileTransferManager.getInstanceFor(connection);
|
||||
jfm.notifyIncomingFileTransferListeners(jingle, new IncomingJingleFileTransferCallback() {
|
||||
@Override
|
||||
public void acceptFileTransfer(final File target) throws SmackException.NotConnectedException, InterruptedException {
|
||||
connection.sendStanza(accept(jingle));
|
||||
JingleInBandBytestreamTransportManager.getInstanceFor(connection).acceptInputStream(jingle, new JingleTransportInputStreamCallback() {
|
||||
@Override
|
||||
public void onInputStream(JingleInputStream inputStream) {
|
||||
receive(inputStream, target);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancelFileTransfer() throws SmackException.NotConnectedException, InterruptedException {
|
||||
connection.sendStanza(terminateFormally());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAccept(Jingle jingle) {
|
||||
this.contents = jingle.getContents();
|
||||
JingleInBandBytestreamTransportManager jibb = JingleInBandBytestreamTransportManager.getInstanceFor(connection);
|
||||
OutputStream outputStream = jibb.createOutputStream(jingle);
|
||||
|
||||
if (outputStream == null) {
|
||||
LOGGER.log(Level.SEVERE, "OutputStream is null!");
|
||||
return;
|
||||
}
|
||||
send(outputStream);
|
||||
}
|
||||
|
||||
void send(OutputStream outputStream) {
|
||||
try {
|
||||
outputStream.write(buffer);
|
||||
} catch (IOException e) {
|
||||
LOGGER.log(Level.SEVERE, "Caught exception while writing to output stream: " + e, e);
|
||||
} finally {
|
||||
try {
|
||||
outputStream.close();
|
||||
} catch (IOException e) {
|
||||
LOGGER.log(Level.SEVERE, "Could not close output stream: " + e, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void receive(JingleInputStream in, File file) {
|
||||
JingleFileTransferChildElement payload = (JingleFileTransferChildElement) contents.get(0).getDescription().getJingleContentDescriptionChildren().get(0);
|
||||
InputStream inputStream = in.getInputStream();
|
||||
byte[] fileBuffer = new byte[payload.getSize()];
|
||||
byte[] packetBuffer = new byte[in.getBlockSize()];
|
||||
|
||||
try {
|
||||
FileOutputStream outputStream = new FileOutputStream(file);
|
||||
int read = 0, count = 0;
|
||||
while (read > -1 && read < fileBuffer.length) {
|
||||
int r = inputStream.read(packetBuffer);
|
||||
read += r;
|
||||
System.arraycopy(packetBuffer, 0, fileBuffer, packetBuffer.length * count, r);
|
||||
count++;
|
||||
}
|
||||
|
||||
inputStream.close();
|
||||
outputStream.write(fileBuffer);
|
||||
outputStream.close();
|
||||
|
||||
} catch (IOException e) {
|
||||
LOGGER.log(Level.SEVERE, "Caught exception while receiving and writing file: " + e, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTerminate(Jingle jingle) {
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,128 @@
|
|||
/**
|
||||
*
|
||||
* 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 java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.WeakHashMap;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.jivesoftware.smack.Manager;
|
||||
import org.jivesoftware.smack.SmackException;
|
||||
import org.jivesoftware.smack.XMPPConnection;
|
||||
import org.jivesoftware.smack.XMPPException;
|
||||
import org.jivesoftware.smack.util.StringUtils;
|
||||
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamListener;
|
||||
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager;
|
||||
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamRequest;
|
||||
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamSession;
|
||||
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
||||
import org.jivesoftware.smackx.jingle.JingleContentTransportManager;
|
||||
import org.jivesoftware.smackx.jingle.JingleInputStream;
|
||||
import org.jivesoftware.smackx.jingle.JingleTransportInputStreamCallback;
|
||||
import org.jivesoftware.smackx.jingle.element.Jingle;
|
||||
import org.jivesoftware.smackx.jingle.element.JingleContent;
|
||||
import org.jivesoftware.smackx.jingle.element.JingleContentTransport;
|
||||
import org.jivesoftware.smackx.jingle.provider.JingleContentProviderManager;
|
||||
import org.jivesoftware.smackx.jingle_ibb.element.JingleInBandBytestreamTransport;
|
||||
import org.jivesoftware.smackx.jingle_ibb.provider.JingleInBandByteStreamTransportProvider;
|
||||
|
||||
/**
|
||||
* Manager for Jingle In-Band-Bytestreams.
|
||||
*/
|
||||
public final class JingleInBandBytestreamTransportManager extends Manager implements JingleContentTransportManager {
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger(JingleInBandBytestreamTransportManager.class.getName());
|
||||
public static final String NAMESPACE_V1 = "urn:xmpp:jingle:transports:ibb:1";
|
||||
|
||||
private static final WeakHashMap<XMPPConnection, JingleInBandBytestreamTransportManager> INSTANCES = new WeakHashMap<>();
|
||||
|
||||
private JingleInBandBytestreamTransportManager(XMPPConnection connection) {
|
||||
super(connection);
|
||||
JingleContentProviderManager.addJingleContentTransportProvider(NAMESPACE_V1, new JingleInBandByteStreamTransportProvider());
|
||||
ServiceDiscoveryManager.getInstanceFor(connection).addFeature(NAMESPACE_V1);
|
||||
}
|
||||
|
||||
public static JingleInBandBytestreamTransportManager getInstanceFor(XMPPConnection connection) {
|
||||
JingleInBandBytestreamTransportManager manager = INSTANCES.get(connection);
|
||||
if (manager == null) {
|
||||
manager = new JingleInBandBytestreamTransportManager(connection);
|
||||
INSTANCES.put(connection, manager);
|
||||
}
|
||||
return manager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void acceptInputStream(final Jingle jingle, final JingleTransportInputStreamCallback callback) {
|
||||
final int blockSize = ((JingleInBandBytestreamTransport)
|
||||
jingle.getContents().get(0).getJingleTransports().get(0)).getBlockSize();
|
||||
InBandBytestreamListener bytestreamListener = new InBandBytestreamListener() {
|
||||
@Override
|
||||
public void incomingBytestreamRequest(InBandBytestreamRequest request) {
|
||||
if (request.getSessionID().equals(jingle.getSid())) {
|
||||
try {
|
||||
InBandBytestreamSession ibs = request.accept();
|
||||
InputStream inputStream = ibs.getInputStream();
|
||||
callback.onInputStream(new JingleInputStream(inputStream, blockSize));
|
||||
} catch (SmackException.NotConnectedException | InterruptedException e) {
|
||||
LOGGER.log(Level.SEVERE, "Could not accept IBB session: " + e, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
InBandBytestreamManager.getByteStreamManager(connection())
|
||||
.addIncomingBytestreamListener(bytestreamListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public OutputStream createOutputStream(Jingle jingle) {
|
||||
JingleInBandBytestreamTransport transport = null;
|
||||
JingleContent content = jingle.getContents().get(0);
|
||||
for (JingleContentTransport t : content.getJingleTransports()) {
|
||||
if (t.getNamespace().equals(NAMESPACE_V1)) {
|
||||
transport = (JingleInBandBytestreamTransport) t;
|
||||
}
|
||||
}
|
||||
|
||||
if (transport == null) {
|
||||
//TODO: Transport-failed
|
||||
return null;
|
||||
}
|
||||
|
||||
InBandBytestreamManager ibm = InBandBytestreamManager.getByteStreamManager(connection());
|
||||
ibm.setMaximumBlockSize(transport.getBlockSize());
|
||||
InBandBytestreamSession ibs;
|
||||
try {
|
||||
ibs = ibm.establishSession(jingle.getFrom(), jingle.getSid());
|
||||
} catch (SmackException.NoResponseException | InterruptedException | SmackException.NotConnectedException | XMPPException.XMPPErrorException e) {
|
||||
LOGGER.log(Level.SEVERE, "Fail in handle request: " + e, e);
|
||||
return null;
|
||||
}
|
||||
|
||||
return ibs.getOutputStream();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a random session id.
|
||||
* @return
|
||||
*/
|
||||
public static String generateSessionId() {
|
||||
return StringUtils.randomString(24);
|
||||
}
|
||||
}
|
|
@ -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.JingleInBandBytestreamTransportManager;
|
||||
|
||||
/**
|
||||
* 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, JingleInBandBytestreamTransportManager.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 JingleInBandBytestreamTransportManager.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();
|
||||
}
|
||||
}
|
|
@ -14,27 +14,9 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smackx.jingle_filetransfer;
|
||||
|
||||
import org.jivesoftware.smackx.jingle.JingleSession;
|
||||
import org.jivesoftware.smackx.jingle.element.Jingle;
|
||||
|
||||
/**
|
||||
* Created by vanitas on 04.06.17.
|
||||
* Smack's API for <a href="https://xmpp.org/extensions/xep-0261.html">XEP-0261: Jingle In-Band Bytestreams</a>.
|
||||
* Element classes.
|
||||
*/
|
||||
public class OutgoingJingleFileTransferSession extends JingleSession {
|
||||
|
||||
private byte[] bytes;
|
||||
|
||||
public OutgoingJingleFileTransferSession(Jingle jingle) {
|
||||
super(jingle.getInitiator(), jingle.getResponder(), jingle.getSid());
|
||||
}
|
||||
|
||||
public void setBytes(byte[] bytes) {
|
||||
this.bytes = bytes;
|
||||
}
|
||||
|
||||
public byte[] getBytes() {
|
||||
return bytes;
|
||||
}
|
||||
}
|
||||
package org.jivesoftware.smackx.jingle_ibb.element;
|
|
@ -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;
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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;
|
|
@ -0,0 +1,70 @@
|
|||
/**
|
||||
*
|
||||
* 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 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.smackx.jingle_ibb.element.JingleInBandBytestreamTransport;
|
||||
import org.jivesoftware.smackx.jingle_ibb.provider.JingleInBandByteStreamTransportProvider;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Test JingleInBandByteStreamTransport provider and element.
|
||||
*/
|
||||
public class JingleInBandByteStreamTransportTest extends SmackTestSuite {
|
||||
|
||||
@Test
|
||||
public void parserTest() throws Exception {
|
||||
String sid = JingleInBandBytestreamTransportManager.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());
|
||||
|
||||
assertEquals(transport3.getNamespace(), JingleInBandBytestreamTransportManager.NAMESPACE_V1);
|
||||
assertEquals(transport3.getElementName(), "transport");
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue