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     * @return Returns the IncomingJingleSession on which the
094     *         negotiation can be carried out.
095     * @throws XMPPException if an XMPP protocol error was received.
096     * @throws SmackException if Smack detected an exceptional situation.
097     * @throws InterruptedException if the calling thread was interrupted.
098     */
099    public synchronized JingleSession accept() throws XMPPException, SmackException, InterruptedException {
100        JingleSession session;
101        synchronized (manager) {
102            session = manager.createIncomingJingleSession(this);
103            // Acknowledge the IQ reception
104            session.setSid(this.getSessionID());
105            // session.sendAck(this.getJingle());
106            session.updatePacketListener();
107            session.receivePacketAndRespond(this.getJingle());
108        }
109        return session;
110    }
111
112    /**
113     * Rejects the session request.
114     */
115    public synchronized void reject() {
116        JingleSession session;
117        synchronized (manager) {
118            try {
119                session = manager.createIncomingJingleSession(this);
120                // Acknowledge the IQ reception
121                session.setSid(this.getSessionID());
122                // session.sendAck(this.getJingle());
123                session.updatePacketListener();
124                session.terminate("Declined");
125            } catch (Exception e) {
126                LOGGER.log(Level.SEVERE, "Exception in reject", e);
127            }
128        }
129     }
130}