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.jingleold;
018
019import java.util.logging.Level;
020import java.util.logging.Logger;
021
022import org.jivesoftware.smack.SmackException;
023import org.jivesoftware.smack.XMPPException;
024
025import org.jivesoftware.smackx.jingleold.packet.Jingle;
026
027import org.jxmpp.jid.Jid;
028
029/**
030 * A Jingle session request.
031 *
032 * This class is a facade of a received Jingle request. The user can have direct
033 * access to the Jingle stanza (<i>JingleSessionRequest.getJingle() </i>) of
034 * the request or can use the convenience methods provided by this class.
035 *
036 * @author Alvaro Saurin
037 */
038public class JingleSessionRequest {
039
040    private static final Logger LOGGER = Logger.getLogger(JingleSessionRequest.class.getName());
041
042    private final Jingle jingle; // The Jingle packet
043
044    private final JingleManager manager; // The manager associated to this
045
046    // request
047
048    /**
049     * A receive request is constructed from the Jingle Initiation request
050     * received from the initiator.
051     *
052     * @param manager The manager handling this request
053     * @param jingle  The jingle IQ received from the initiator.
054     */
055    public JingleSessionRequest(JingleManager manager, Jingle jingle) {
056        this.manager = manager;
057        this.jingle = jingle;
058    }
059
060    /**
061     * Returns the fully-qualified jabber ID of the user that requested this
062     * session.
063     *
064     * @return Returns the fully-qualified jabber ID of the user that requested
065     *         this session.
066     */
067    public Jid getFrom() {
068        return jingle.getFrom();
069    }
070
071    /**
072     * Returns the session ID that uniquely identifies this session.
073     *
074     * @return Returns the session ID that uniquely identifies this session
075     */
076    public String getSessionID() {
077        return jingle.getSid();
078    }
079
080    /**
081     * Returns the Jingle stanza that was sent by the requester which contains
082     * the parameters of the session.
083     *
084     * @return the jingle stanza.
085     */
086    public Jingle getJingle() {
087        return jingle;
088    }
089
090    /**
091     * Accepts this request and creates the incoming Jingle session.
092     *
093     * @param pts list of supported Payload Types
094     * @return Returns the <b><i>IncomingJingleSession</b></i> on which the
095     *         negotiation can be carried out.
096     */
097//    public synchronized JingleSession accept(List<PayloadType> pts) throws XMPPException {
098//        JingleSession session = null;
099//        synchronized (manager) {
100//            session = manager.createIncomingJingleSession(this, pts);
101//            // Acknowledge the IQ reception
102//            session.setSid(this.getSessionID());
103//            //session.sendAck(this.getJingle());
104//            //session.respond(this.getJingle());
105//        }
106//        return session;
107//    }
108
109    /**
110     * Accepts this request and creates the incoming Jingle session.
111     *
112     * @return Returns the IncomingJingleSession on which the
113     *         negotiation can be carried out.
114     * @throws XMPPException if an XMPP protocol error was received.
115     * @throws SmackException if Smack detected an exceptional situation.
116     * @throws InterruptedException if the calling thread was interrupted.
117     */
118    public synchronized JingleSession accept() throws XMPPException, SmackException, InterruptedException {
119        JingleSession session;
120        synchronized (manager) {
121            session = manager.createIncomingJingleSession(this);
122            // Acknowledge the IQ reception
123            session.setSid(this.getSessionID());
124            // session.sendAck(this.getJingle());
125            session.updatePacketListener();
126            session.receivePacketAndRespond(this.getJingle());
127        }
128        return session;
129    }
130
131    /**
132     * Rejects the session request.
133     */
134    public synchronized void reject() {
135        JingleSession session;
136        synchronized (manager) {
137            try {
138                session = manager.createIncomingJingleSession(this);
139                // Acknowledge the IQ reception
140                session.setSid(this.getSessionID());
141                // session.sendAck(this.getJingle());
142                session.updatePacketListener();
143                session.terminate("Declined");
144            } catch (Exception e) {
145                LOGGER.log(Level.SEVERE, "Exception in reject", e);
146            }
147        }
148     }
149}