001/**
002 *
003 * Copyright 2003-2006 Jive Software.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.filetransfer;
018
019import org.jivesoftware.smack.SmackException.NotConnectedException;
020import org.jivesoftware.smackx.si.packet.StreamInitiation;
021import org.jxmpp.jid.Jid;
022
023/**
024 * A request to send a file recieved from another user.
025 * 
026 * @author Alexander Wenckus
027 * 
028 */
029public class FileTransferRequest {
030    private final StreamInitiation streamInitiation;
031
032    private final FileTransferManager manager;
033
034    /**
035     * A recieve request is constructed from the Stream Initiation request
036     * received from the initator.
037     * 
038     * @param manager
039     *            The manager handling this file transfer
040     * 
041     * @param si
042     *            The Stream initiaton recieved from the initiator.
043     */
044    public FileTransferRequest(FileTransferManager manager, StreamInitiation si) {
045        this.streamInitiation = si;
046        this.manager = manager;
047    }
048
049    /**
050     * Returns the name of the file.
051     * 
052     * @return Returns the name of the file.
053     */
054    public String getFileName() {
055        return streamInitiation.getFile().getName();
056    }
057
058    /**
059     * Returns the size in bytes of the file.
060     * 
061     * @return Returns the size in bytes of the file.
062     */
063    public long getFileSize() {
064        return streamInitiation.getFile().getSize();
065    }
066
067    /**
068     * Returns the description of the file provided by the requestor.
069     * 
070     * @return Returns the description of the file provided by the requestor.
071     */
072    public String getDescription() {
073        return streamInitiation.getFile().getDesc();
074    }
075
076    /**
077     * Returns the mime-type of the file.
078     * 
079     * @return Returns the mime-type of the file.
080     */
081    public String getMimeType() {
082        return streamInitiation.getMimeType();
083    }
084
085    /**
086     * Returns the fully-qualified jabber ID of the user that requested this
087     * file transfer.
088     * 
089     * @return Returns the fully-qualified jabber ID of the user that requested
090     *         this file transfer.
091     */
092    public Jid getRequestor() {
093        return streamInitiation.getFrom();
094    }
095
096    /**
097     * Returns the stream ID that uniquely identifies this file transfer.
098     * 
099     * @return Returns the stream ID that uniquely identifies this file
100     *         transfer.
101     */
102    public String getStreamID() {
103        return streamInitiation.getSessionID();
104    }
105
106    /**
107     * Returns the stream initiation stanza(/packet) that was sent by the requestor which
108     * contains the parameters of the file transfer being transfer and also the
109     * methods available to transfer the file.
110     * 
111     * @return Returns the stream initiation stanza(/packet) that was sent by the
112     *         requestor which contains the parameters of the file transfer
113     *         being transfer and also the methods available to transfer the
114     *         file.
115     */
116    protected StreamInitiation getStreamInitiation() {
117        return streamInitiation;
118    }
119
120    /**
121     * Accepts this file transfer and creates the incoming file transfer.
122     * 
123     * @return Returns the <b><i>IncomingFileTransfer</b></i> on which the
124     *         file transfer can be carried out.
125     */
126    public IncomingFileTransfer accept() {
127        return manager.createIncomingFileTransfer(this);
128    }
129
130    /**
131     * Rejects the file transfer request.
132     * @throws NotConnectedException 
133     * @throws InterruptedException 
134     */
135    public void reject() throws NotConnectedException, InterruptedException {
136        manager.rejectIncomingFileTransfer(this);
137    }
138
139}