/**
* $RCSfile$
* $Revision: 2495 $
* $Date: 2005-05-30 10:14:25 -0500 (Mon, 30 May 2005) $
*
* Copyright 2003-2004 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.
*/
package org.jivesoftware.smackx.filetransfer;
import org.jivesoftware.smack.filter.*;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smackx.packet.*;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.*;
import java.util.*;
import java.io.*;
import org.jivesoftware.smackx.packet.IBBOpen;
/**
* Provides an abstraction between IBB and Socks5.
*
*@author Adam Olsen
*/
public class FileReceive extends FileTransfer {
private StreamInitiation si;
private FileTransferManager manager;
private ArrayList progress = new ArrayList();
private FileTransfer transfer;
private File file;
/**
* Creates an instance of the receive class. Used internally by the
* FileTransferManager class.
*
*@param manager the filetransfer class that created this instance
*@param si the streaminitiation that caused this file transfer
*/
protected FileReceive(FileTransferManager manager, StreamInitiation si) {
this.si = si;
this.manager = manager;
}
/**
* Returns the containing FileTransferManager
*
* @return the containing FileTransferManager
*/
FileTransferManager getManager() { return manager; }
/**
* Adds a progress listener to this file receive
*
*@param listener The listener interested in receiving progress updates
*/
public synchronized void addProgressListener(FileProgressListener listener) {
progress.add(listener);
}
/**
* Removes a progress listener
*
*@param listener The listener to remove
*/
public synchronized void removeProgressListener(FileProgressListener listener) {
int index = progress.indexOf(listener);
if(index != -1) progress.remove(index);
}
/**
* Gets the JID of the user sending the file
*
*@return The JID of the user sending the file
*/
public String getFrom() {
return si.getFrom();
}
public String getTo() { return si.getTo(); }
/**
* Cancels the transfer
*/
public void cancel() {
FileTransferManager.update(FileTransferManager.Event.CANCELLED, progress, 0, 0);
if(transfer != null) transfer.cancel();
}
public void setFile(File file) { this.file = file; }
/**
* Gets the size of the file being sent
*
*@return The size of the file being sent
*/
public long getSize() {
return si.getFileDetails().getFileSize();
}
/**
* Gets the description of the file being sent
*
*@return The description of the file being sent
*/
public String getDescription() {
return si.getFileDetails().getDescription();
}
public String getFileLocation()
{
if(file == null) return "";
return file.getPath();
}
/**
* Gets the StreamInitiation that triggered this file receive
*
*@return The sI value
*/
public StreamInitiation getSI() {
return si;
}
/**
* Gets the name of the file being sent
*
*@return The name of the file being sent
*/
public String getName() {
return si.getFileDetails().getFileName();
}
public void reject() throws XMPPException {
StreamInitiation si = this.si.createConfirmationMessage(manager.getPreferredType());
si.setType(IQ.Type.ERROR);
si.setError(new XMPPError(503, "File Rejected"));
manager.getConnection().sendPacket(si);
}
/**
* Saves the file to an OutputStream
*
*@param stream The output stream to save the file to
*@param type The preferred method for receiving
*@throws XMPPException If an error receiving the file occurs
*@throws IOException Thrown if there is an IO error saving the file
*/
public void save(OutputStream stream, int type) throws XMPPException, IOException {
FileTransferManager.update(FileTransferManager.Event.CONNECTING, progress, 0, 0);
PacketCollector collector =
manager.getConnection().createPacketCollector(
new OrFilter(new PacketTypeFilter(IBBOpen.class),
new PacketTypeFilter(StreamHost.class)));
manager.getConnection().sendPacket(si.createConfirmationMessage(manager.getPreferredType()));
IQ result = (IQ) collector.nextResult(manager.getTimeout());
collector.cancel();
if (result == null || !(result instanceof IBBOpen) &&
!(result instanceof StreamHost)) {
if(result != null && result.getError() != null ) {
throw new XMPPException(result.getError());
}
throw new XMPPException("No response from server.");
}
if(result instanceof StreamHost)
{
StreamHost host = (StreamHost)result;
if(!host.getSid().equals(si.getSid())) {
return;
}
Socks5Receive rec = new Socks5Receive(this, stream, (StreamHost)result);
rec.setProgressListeners(progress);
transfer = rec;
rec.save();
}
else if(result instanceof IBBOpen)
{
IBBOpen req = (IBBOpen) result;
if (!req.getSid().equals(si.getSid())) {
return;
}
IBBReceive rec = new IBBReceive(this, stream, (IBBOpen) result);
transfer = rec;
rec.setProgressListeners(progress);
rec.save();
}
}
/**
* Saves the file to a <tt>File</tt>
*@param file The file to save to
"@param type The preferred method for receiving
*@throws XMPPException If an error receiving the file occurs
*@throws IOException If there is an error saving the file
*/
public void save(File file, int type) throws XMPPException, IOException {
this.file = file;
OutputStream stream = new FileOutputStream(file);
save(stream, type);
}
/**
* Saves the file to a <tt>File</tt>
*@param file The file to save to
*@throws XMPPException If an error receiving the file occurs
*@throws IOException If there is an error saving the file
*/
public void save(File file) throws XMPPException, IOException {
save(file, FileTransferManager.TYPE_SOCKS5);
}
/**
* Saves the file to an OutputStream
*
*@param stream The output stream to save the file to
*@throws XMPPException If an error receiving the file occurs
*@throws IOException Thrown if there is an IO error saving the file
*/
public void save(OutputStream stream) throws XMPPException, IOException {
save(stream, FileTransferManager.TYPE_SOCKS5);
}
}