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;
021
022/**
023 * A request to send a file recieved from another user.
024 * 
025 * @author Alexander Wenckus
026 * 
027 */
028public class FileTransferRequest {
029        private final StreamInitiation streamInitiation;
030
031        private final FileTransferManager manager;
032
033        /**
034         * A recieve request is constructed from the Stream Initiation request
035         * received from the initator.
036         * 
037         * @param manager
038         *            The manager handling this file transfer
039         * 
040         * @param si
041         *            The Stream initiaton recieved from the initiator.
042         */
043        public FileTransferRequest(FileTransferManager manager, StreamInitiation si) {
044                this.streamInitiation = si;
045                this.manager = manager;
046        }
047
048        /**
049         * Returns the name of the file.
050         * 
051         * @return Returns the name of the file.
052         */
053        public String getFileName() {
054                return streamInitiation.getFile().getName();
055        }
056
057        /**
058         * Returns the size in bytes of the file.
059         * 
060         * @return Returns the size in bytes of the file.
061         */
062        public long getFileSize() {
063                return streamInitiation.getFile().getSize();
064        }
065
066        /**
067         * Returns the description of the file provided by the requestor.
068         * 
069         * @return Returns the description of the file provided by the requestor.
070         */
071        public String getDescription() {
072                return streamInitiation.getFile().getDesc();
073        }
074
075        /**
076         * Returns the mime-type of the file.
077         * 
078         * @return Returns the mime-type of the file.
079         */
080        public String getMimeType() {
081                return streamInitiation.getMimeType();
082        }
083
084        /**
085         * Returns the fully-qualified jabber ID of the user that requested this
086         * file transfer.
087         * 
088         * @return Returns the fully-qualified jabber ID of the user that requested
089         *         this file transfer.
090         */
091        public String getRequestor() {
092                return streamInitiation.getFrom();
093        }
094
095        /**
096         * Returns the stream ID that uniquely identifies this file transfer.
097         * 
098         * @return Returns the stream ID that uniquely identifies this file
099         *         transfer.
100         */
101        public String getStreamID() {
102                return streamInitiation.getSessionID();
103        }
104
105        /**
106         * Returns the stream initiation packet that was sent by the requestor which
107         * contains the parameters of the file transfer being transfer and also the
108         * methods available to transfer the file.
109         * 
110         * @return Returns the stream initiation packet that was sent by the
111         *         requestor which contains the parameters of the file transfer
112         *         being transfer and also the methods available to transfer the
113         *         file.
114         */
115        protected StreamInitiation getStreamInitiation() {
116                return streamInitiation;
117        }
118
119        /**
120         * Accepts this file transfer and creates the incoming file transfer.
121         * 
122         * @return Returns the <b><i>IncomingFileTransfer</b></i> on which the
123         *         file transfer can be carried out.
124         */
125        public IncomingFileTransfer accept() {
126                return manager.createIncomingFileTransfer(this);
127        }
128
129        /**
130         * Rejects the file transfer request.
131         * @throws NotConnectedException 
132         */
133        public void reject() throws NotConnectedException {
134                manager.rejectIncomingFileTransfer(this);
135        }
136
137}