2006-02-03 19:44:22 +01:00
|
|
|
/**
|
2006-02-03 20:13:23 +01:00
|
|
|
* $RCSfile$
|
|
|
|
* $Revision: $
|
|
|
|
* $Date: $
|
|
|
|
*
|
|
|
|
* Copyright 2003-2006 Jive Software.
|
|
|
|
*
|
|
|
|
* All rights reserved. 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.
|
2006-02-03 19:44:22 +01:00
|
|
|
*/
|
|
|
|
package org.jivesoftware.smackx.filetransfer;
|
|
|
|
|
2007-03-21 05:09:52 +01:00
|
|
|
import org.jivesoftware.smack.*;
|
2006-05-03 23:13:25 +02:00
|
|
|
import org.jivesoftware.smack.util.StringUtils;
|
2006-02-03 19:44:22 +01:00
|
|
|
import org.jivesoftware.smack.filter.*;
|
|
|
|
import org.jivesoftware.smack.packet.IQ;
|
|
|
|
import org.jivesoftware.smack.packet.Message;
|
|
|
|
import org.jivesoftware.smack.packet.Packet;
|
2006-02-09 02:39:04 +01:00
|
|
|
import org.jivesoftware.smack.packet.XMPPError;
|
2006-02-03 19:44:22 +01:00
|
|
|
import org.jivesoftware.smackx.packet.IBBExtensions;
|
|
|
|
import org.jivesoftware.smackx.packet.IBBExtensions.Open;
|
|
|
|
import org.jivesoftware.smackx.packet.StreamInitiation;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The in-band bytestream file transfer method, or IBB for short, transfers the
|
|
|
|
* file over the same XML Stream used by XMPP. It is the fall-back mechanism in
|
|
|
|
* case the SOCKS5 bytestream method of transfering files is not available.
|
|
|
|
*
|
|
|
|
* @author Alexander Wenckus
|
|
|
|
* @see <a href="http://www.jabber.org/jeps/jep-0047.html">JEP-0047: In-Band
|
|
|
|
* Bytestreams (IBB)</a>
|
|
|
|
*/
|
|
|
|
public class IBBTransferNegotiator extends StreamNegotiator {
|
|
|
|
|
|
|
|
protected static final String NAMESPACE = "http://jabber.org/protocol/ibb";
|
|
|
|
|
|
|
|
public static final int DEFAULT_BLOCK_SIZE = 4096;
|
|
|
|
|
|
|
|
private XMPPConnection connection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The default constructor for the In-Band Bystream Negotiator.
|
|
|
|
*
|
|
|
|
* @param connection The connection which this negotiator works on.
|
|
|
|
*/
|
|
|
|
protected IBBTransferNegotiator(XMPPConnection connection) {
|
|
|
|
this.connection = connection;
|
|
|
|
}
|
|
|
|
|
2006-02-08 01:31:17 +01:00
|
|
|
public PacketFilter getInitiationPacketFilter(String from, String streamID) {
|
|
|
|
return new AndFilter(new FromContainsFilter(
|
|
|
|
from), new IBBOpenSidFilter(streamID));
|
|
|
|
}
|
|
|
|
|
|
|
|
InputStream negotiateIncomingStream(Packet streamInitiation) throws XMPPException {
|
|
|
|
Open openRequest = (Open) streamInitiation;
|
|
|
|
|
|
|
|
if (openRequest.getType().equals(IQ.Type.ERROR)) {
|
2006-02-03 19:44:22 +01:00
|
|
|
throw new XMPPException(openRequest.getError());
|
|
|
|
}
|
|
|
|
|
2006-02-08 01:31:17 +01:00
|
|
|
PacketFilter dataFilter = new IBBMessageSidFilter(openRequest.getFrom(),
|
|
|
|
openRequest.getSessionID());
|
2006-02-03 19:44:22 +01:00
|
|
|
PacketFilter closeFilter = new AndFilter(new PacketTypeFilter(
|
2006-02-08 01:31:17 +01:00
|
|
|
IBBExtensions.Close.class), new FromMatchesFilter(openRequest
|
2006-02-03 19:44:22 +01:00
|
|
|
.getFrom()));
|
|
|
|
|
|
|
|
InputStream stream = new IBBInputStream(openRequest.getSessionID(),
|
|
|
|
dataFilter, closeFilter);
|
|
|
|
|
|
|
|
initInBandTransfer(openRequest);
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2006-02-08 01:31:17 +01:00
|
|
|
public InputStream createIncomingStream(StreamInitiation initiation) throws XMPPException {
|
|
|
|
Packet openRequest = initiateIncomingStream(connection, initiation);
|
|
|
|
return negotiateIncomingStream(openRequest);
|
|
|
|
}
|
|
|
|
|
2006-02-03 19:44:22 +01:00
|
|
|
/**
|
|
|
|
* Creates and sends the response for the open request.
|
|
|
|
*
|
|
|
|
* @param openRequest The open request recieved from the peer.
|
|
|
|
*/
|
|
|
|
private void initInBandTransfer(final Open openRequest) {
|
|
|
|
connection.sendPacket(FileTransferNegotiator.createIQ(openRequest
|
|
|
|
.getPacketID(), openRequest.getFrom(), openRequest.getTo(),
|
|
|
|
IQ.Type.RESULT));
|
|
|
|
}
|
|
|
|
|
2006-02-08 01:31:17 +01:00
|
|
|
public OutputStream createOutgoingStream(String streamID, String initiator,
|
2006-02-03 19:44:22 +01:00
|
|
|
String target) throws XMPPException {
|
|
|
|
Open openIQ = new Open(streamID, DEFAULT_BLOCK_SIZE);
|
|
|
|
openIQ.setTo(target);
|
|
|
|
openIQ.setType(IQ.Type.SET);
|
|
|
|
|
|
|
|
// wait for the result from the peer
|
|
|
|
PacketCollector collector = connection
|
|
|
|
.createPacketCollector(new PacketIDFilter(openIQ.getPacketID()));
|
|
|
|
connection.sendPacket(openIQ);
|
2007-03-21 05:09:52 +01:00
|
|
|
// We don't want to wait forever for the result
|
|
|
|
IQ openResponse = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
|
2006-02-03 19:44:22 +01:00
|
|
|
collector.cancel();
|
|
|
|
|
|
|
|
if (openResponse == null) {
|
2007-03-21 05:09:52 +01:00
|
|
|
throw new XMPPException("No response from peer on IBB open");
|
2006-02-03 19:44:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
IQ.Type type = openResponse.getType();
|
|
|
|
if (!type.equals(IQ.Type.RESULT)) {
|
|
|
|
if (type.equals(IQ.Type.ERROR)) {
|
|
|
|
throw new XMPPException("Target returned an error",
|
|
|
|
openResponse.getError());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new XMPPException("Target returned unknown response");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new IBBOutputStream(target, streamID, DEFAULT_BLOCK_SIZE);
|
|
|
|
}
|
|
|
|
|
2006-02-08 01:31:17 +01:00
|
|
|
public String[] getNamespaces() {
|
|
|
|
return new String[]{NAMESPACE};
|
|
|
|
}
|
|
|
|
|
|
|
|
public void cleanup() {
|
2006-02-03 19:44:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private class IBBOutputStream extends OutputStream {
|
|
|
|
|
|
|
|
protected byte[] buffer;
|
|
|
|
|
|
|
|
protected int count = 0;
|
|
|
|
|
|
|
|
protected int seq = 0;
|
|
|
|
|
2006-02-08 01:31:17 +01:00
|
|
|
final String userID;
|
2006-02-03 19:44:22 +01:00
|
|
|
|
2006-02-08 01:31:17 +01:00
|
|
|
final private IQ closePacket;
|
2006-02-03 19:44:22 +01:00
|
|
|
|
|
|
|
private String messageID;
|
2006-02-08 01:31:17 +01:00
|
|
|
private String sid;
|
2006-02-03 19:44:22 +01:00
|
|
|
|
|
|
|
IBBOutputStream(String userID, String sid, int blockSize) {
|
|
|
|
if (blockSize <= 0) {
|
|
|
|
throw new IllegalArgumentException("Buffer size <= 0");
|
|
|
|
}
|
|
|
|
buffer = new byte[blockSize];
|
2006-02-08 01:31:17 +01:00
|
|
|
this.userID = userID;
|
|
|
|
|
|
|
|
Message template = new Message(userID);
|
2006-02-03 19:44:22 +01:00
|
|
|
messageID = template.getPacketID();
|
2006-02-08 01:31:17 +01:00
|
|
|
this.sid = sid;
|
2006-02-03 19:44:22 +01:00
|
|
|
closePacket = createClosePacket(userID, sid);
|
|
|
|
}
|
|
|
|
|
|
|
|
private IQ createClosePacket(String userID, String sid) {
|
|
|
|
IQ packet = new IBBExtensions.Close(sid);
|
|
|
|
packet.setTo(userID);
|
|
|
|
packet.setType(IQ.Type.SET);
|
|
|
|
return packet;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void write(int b) throws IOException {
|
|
|
|
if (count >= buffer.length) {
|
|
|
|
flushBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer[count++] = (byte) b;
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized void write(byte b[], int off, int len)
|
|
|
|
throws IOException {
|
|
|
|
if (len >= buffer.length) {
|
2006-04-13 22:18:26 +02:00
|
|
|
// "byte" off the first chunck to write out
|
|
|
|
writeOut(b, off, buffer.length);
|
|
|
|
// recursivly call this method again with the lesser amount subtracted.
|
|
|
|
write(b, off + buffer.length, len - buffer.length);
|
|
|
|
} else {
|
|
|
|
writeOut(b, off, len);
|
2006-02-03 19:44:22 +01:00
|
|
|
}
|
2006-04-13 22:18:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void writeOut(byte b[], int off, int len) {
|
2006-02-03 19:44:22 +01:00
|
|
|
if (len > buffer.length - count) {
|
|
|
|
flushBuffer();
|
|
|
|
}
|
|
|
|
System.arraycopy(b, off, buffer, count, len);
|
|
|
|
count += len;
|
|
|
|
}
|
|
|
|
|
2007-06-11 23:42:50 +02:00
|
|
|
private synchronized void flushBuffer() {
|
2006-02-03 19:44:22 +01:00
|
|
|
writeToXML(buffer, 0, count);
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
|
2006-02-09 02:39:04 +01:00
|
|
|
private synchronized void writeToXML(byte[] buffer, int offset, int len) {
|
2006-02-08 01:31:17 +01:00
|
|
|
Message template = createTemplate(messageID + "_" + seq);
|
|
|
|
IBBExtensions.Data ext = new IBBExtensions.Data(sid);
|
|
|
|
template.addExtension(ext);
|
2006-02-03 19:44:22 +01:00
|
|
|
|
2006-05-03 23:13:25 +02:00
|
|
|
String enc = StringUtils.encodeBase64(buffer, offset, len, false);
|
2006-02-03 19:44:22 +01:00
|
|
|
|
|
|
|
ext.setData(enc);
|
|
|
|
ext.setSeq(seq);
|
2006-04-13 22:18:26 +02:00
|
|
|
synchronized (this) {
|
2006-02-09 02:39:04 +01:00
|
|
|
try {
|
|
|
|
this.wait(100);
|
|
|
|
}
|
|
|
|
catch (InterruptedException e) {
|
2006-04-13 22:18:26 +02:00
|
|
|
/* Do Nothing */
|
2006-02-09 02:39:04 +01:00
|
|
|
}
|
|
|
|
}
|
2006-02-03 19:44:22 +01:00
|
|
|
|
|
|
|
connection.sendPacket(template);
|
|
|
|
|
|
|
|
seq = (seq + 1 == 65535 ? 0 : seq + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void close() throws IOException {
|
2007-06-11 23:42:50 +02:00
|
|
|
this.flush();
|
2006-02-03 19:44:22 +01:00
|
|
|
connection.sendPacket(closePacket);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void flush() throws IOException {
|
|
|
|
flushBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void write(byte[] b) throws IOException {
|
|
|
|
write(b, 0, b.length);
|
|
|
|
}
|
|
|
|
|
2006-02-08 01:31:17 +01:00
|
|
|
public Message createTemplate(String messageID) {
|
|
|
|
Message template = new Message(userID);
|
|
|
|
template.setPacketID(messageID);
|
|
|
|
return template;
|
|
|
|
}
|
2006-02-03 19:44:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private class IBBInputStream extends InputStream implements PacketListener {
|
|
|
|
|
|
|
|
private String streamID;
|
|
|
|
|
|
|
|
private PacketCollector dataCollector;
|
|
|
|
|
|
|
|
private byte[] buffer;
|
|
|
|
|
|
|
|
private int bufferPointer;
|
|
|
|
|
|
|
|
private int seq = -1;
|
|
|
|
|
|
|
|
private boolean isDone;
|
|
|
|
|
|
|
|
private boolean isEOF;
|
|
|
|
|
|
|
|
private boolean isClosed;
|
|
|
|
|
|
|
|
private IQ closeConfirmation;
|
|
|
|
|
2006-02-09 02:39:04 +01:00
|
|
|
private Message lastMess;
|
|
|
|
|
2006-02-03 19:44:22 +01:00
|
|
|
private IBBInputStream(String streamID, PacketFilter dataFilter,
|
|
|
|
PacketFilter closeFilter) {
|
|
|
|
this.streamID = streamID;
|
|
|
|
this.dataCollector = connection.createPacketCollector(dataFilter);
|
|
|
|
connection.addPacketListener(this, closeFilter);
|
|
|
|
this.bufferPointer = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized int read() throws IOException {
|
|
|
|
if (isEOF || isClosed) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (bufferPointer == -1 || bufferPointer >= buffer.length) {
|
|
|
|
loadBufferWait();
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int) buffer[bufferPointer++];
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized int read(byte[] b) throws IOException {
|
|
|
|
return read(b, 0, b.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized int read(byte[] b, int off, int len)
|
|
|
|
throws IOException {
|
|
|
|
if (isEOF || isClosed) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (bufferPointer == -1 || bufferPointer >= buffer.length) {
|
|
|
|
if (!loadBufferWait()) {
|
|
|
|
isEOF = true;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len - off > buffer.length - bufferPointer) {
|
|
|
|
len = buffer.length - bufferPointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
System.arraycopy(buffer, bufferPointer, b, off, len);
|
|
|
|
bufferPointer += len;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean loadBufferWait() throws IOException {
|
|
|
|
IBBExtensions.Data data;
|
2006-02-08 01:31:17 +01:00
|
|
|
|
|
|
|
Message mess = null;
|
|
|
|
while (mess == null) {
|
|
|
|
if (isDone) {
|
|
|
|
mess = (Message) dataCollector.pollResult();
|
|
|
|
if (mess == null) {
|
|
|
|
return false;
|
2006-02-03 19:44:22 +01:00
|
|
|
}
|
|
|
|
}
|
2006-02-08 01:31:17 +01:00
|
|
|
else {
|
|
|
|
mess = (Message) dataCollector.nextResult(1000);
|
|
|
|
}
|
2006-02-03 19:44:22 +01:00
|
|
|
}
|
2006-02-09 02:39:04 +01:00
|
|
|
lastMess = mess;
|
2006-02-08 01:31:17 +01:00
|
|
|
data = (IBBExtensions.Data) mess.getExtension(
|
|
|
|
IBBExtensions.Data.ELEMENT_NAME,
|
|
|
|
IBBExtensions.NAMESPACE);
|
2006-04-13 22:18:26 +02:00
|
|
|
|
2006-02-09 02:39:04 +01:00
|
|
|
checkSequence(mess, (int) data.getSeq());
|
2006-05-03 23:13:25 +02:00
|
|
|
buffer = StringUtils.decodeBase64(data.getData());
|
2006-02-03 19:44:22 +01:00
|
|
|
bufferPointer = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-02-09 02:39:04 +01:00
|
|
|
private void checkSequence(Message mess, int seq) throws IOException {
|
2006-02-03 19:44:22 +01:00
|
|
|
if (this.seq == 65535) {
|
|
|
|
this.seq = -1;
|
|
|
|
}
|
|
|
|
if (seq - 1 != this.seq) {
|
2006-02-09 02:39:04 +01:00
|
|
|
cancelTransfer(mess);
|
2006-02-03 19:44:22 +01:00
|
|
|
throw new IOException("Packets out of sequence");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.seq = seq;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-09 02:39:04 +01:00
|
|
|
private void cancelTransfer(Message mess) {
|
2006-02-03 19:44:22 +01:00
|
|
|
cleanup();
|
|
|
|
|
2006-02-09 02:39:04 +01:00
|
|
|
sendCancelMessage(mess);
|
2006-02-03 19:44:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void cleanup() {
|
|
|
|
dataCollector.cancel();
|
|
|
|
connection.removePacketListener(this);
|
|
|
|
}
|
|
|
|
|
2006-02-09 02:39:04 +01:00
|
|
|
private void sendCancelMessage(Message message) {
|
|
|
|
IQ error = FileTransferNegotiator.createIQ(message.getPacketID(), message.getFrom(), message.getTo(),
|
|
|
|
IQ.Type.ERROR);
|
2006-07-19 21:24:00 +02:00
|
|
|
error.setError(new XMPPError(XMPPError.Condition.remote_server_timeout, "Cancel Message Transfer"));
|
2006-02-09 02:39:04 +01:00
|
|
|
connection.sendPacket(error);
|
2006-02-03 19:44:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean markSupported() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void processPacket(Packet packet) {
|
|
|
|
IBBExtensions.Close close = (IBBExtensions.Close) packet;
|
|
|
|
if (close.getSessionID().equals(streamID)) {
|
|
|
|
isDone = true;
|
|
|
|
closeConfirmation = FileTransferNegotiator.createIQ(packet
|
|
|
|
.getPacketID(), packet.getFrom(), packet.getTo(),
|
|
|
|
IQ.Type.RESULT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized void close() throws IOException {
|
|
|
|
if (isClosed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cleanup();
|
|
|
|
|
|
|
|
if (isEOF) {
|
|
|
|
sendCloseConfirmation();
|
|
|
|
}
|
2006-04-13 22:18:26 +02:00
|
|
|
else if (lastMess != null) {
|
2006-02-09 02:39:04 +01:00
|
|
|
sendCancelMessage(lastMess);
|
2006-02-03 19:44:22 +01:00
|
|
|
}
|
|
|
|
isClosed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void sendCloseConfirmation() {
|
|
|
|
connection.sendPacket(closeConfirmation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-08 01:31:17 +01:00
|
|
|
private static class IBBOpenSidFilter implements PacketFilter {
|
2006-02-03 19:44:22 +01:00
|
|
|
|
|
|
|
private String sessionID;
|
|
|
|
|
2006-02-08 01:31:17 +01:00
|
|
|
public IBBOpenSidFilter(String sessionID) {
|
2006-02-03 19:44:22 +01:00
|
|
|
if (sessionID == null) {
|
|
|
|
throw new IllegalArgumentException("StreamID cannot be null");
|
|
|
|
}
|
|
|
|
this.sessionID = sessionID;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean accept(Packet packet) {
|
|
|
|
if (!IBBExtensions.Open.class.isInstance(packet)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
IBBExtensions.Open open = (IBBExtensions.Open) packet;
|
|
|
|
String sessionID = open.getSessionID();
|
|
|
|
|
|
|
|
return (sessionID != null && sessionID.equals(this.sessionID));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-08 01:31:17 +01:00
|
|
|
private static class IBBMessageSidFilter implements PacketFilter {
|
|
|
|
|
|
|
|
private final String sessionID;
|
|
|
|
private String from;
|
|
|
|
|
|
|
|
public IBBMessageSidFilter(String from, String sessionID) {
|
|
|
|
this.from = from;
|
|
|
|
this.sessionID = sessionID;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean accept(Packet packet) {
|
|
|
|
if (!(packet instanceof Message)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!packet.getFrom().equalsIgnoreCase(from)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
IBBExtensions.Data data = (IBBExtensions.Data) packet.
|
|
|
|
getExtension(IBBExtensions.Data.ELEMENT_NAME, IBBExtensions.NAMESPACE);
|
2007-03-21 05:09:52 +01:00
|
|
|
return data != null && data.getSessionID() != null
|
|
|
|
&& data.getSessionID().equalsIgnoreCase(sessionID);
|
2006-02-08 01:31:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-03 19:44:22 +01:00
|
|
|
}
|