001/**
002 *
003 * Copyright the original author or authors
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.bytestreams.ibb;
018
019import org.jivesoftware.smack.SmackException.NotConnectedException;
020import org.jivesoftware.smack.XMPPConnection;
021import org.jivesoftware.smack.packet.IQ;
022import org.jivesoftware.smackx.bytestreams.BytestreamRequest;
023import org.jivesoftware.smackx.bytestreams.ibb.packet.Open;
024
025/**
026 * InBandBytestreamRequest class handles incoming In-Band Bytestream requests.
027 * 
028 * @author Henning Staib
029 */
030public class InBandBytestreamRequest implements BytestreamRequest {
031
032    /* the bytestream initialization request */
033    private final Open byteStreamRequest;
034
035    /*
036     * In-Band Bytestream manager containing the XMPP connection and helper
037     * methods
038     */
039    private final InBandBytestreamManager manager;
040
041    protected InBandBytestreamRequest(InBandBytestreamManager manager,
042                    Open byteStreamRequest) {
043        this.manager = manager;
044        this.byteStreamRequest = byteStreamRequest;
045    }
046
047    /**
048     * Returns the sender of the In-Band Bytestream open request.
049     * 
050     * @return the sender of the In-Band Bytestream open request
051     */
052    public String getFrom() {
053        return this.byteStreamRequest.getFrom();
054    }
055
056    /**
057     * Returns the session ID of the In-Band Bytestream open request.
058     * 
059     * @return the session ID of the In-Band Bytestream open request
060     */
061    public String getSessionID() {
062        return this.byteStreamRequest.getSessionID();
063    }
064
065    /**
066     * Accepts the In-Band Bytestream open request and returns the session to
067     * send/receive data.
068     * 
069     * @return the session to send/receive data
070     * @throws NotConnectedException 
071     */
072    public InBandBytestreamSession accept() throws NotConnectedException {
073        XMPPConnection connection = this.manager.getConnection();
074
075        // create In-Band Bytestream session and store it
076        InBandBytestreamSession ibbSession = new InBandBytestreamSession(connection,
077                        this.byteStreamRequest, this.byteStreamRequest.getFrom());
078        this.manager.getSessions().put(this.byteStreamRequest.getSessionID(), ibbSession);
079
080        // acknowledge request
081        IQ resultIQ = IQ.createResultIQ(this.byteStreamRequest);
082        connection.sendStanza(resultIQ);
083
084        return ibbSession;
085    }
086
087    /**
088     * Rejects the In-Band Bytestream request by sending a reject error to the
089     * initiator.
090     * @throws NotConnectedException 
091     */
092    public void reject() throws NotConnectedException {
093        this.manager.replyRejectPacket(this.byteStreamRequest);
094    }
095
096}