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