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