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    public Jingle getJingle() {
085        return jingle;
086    }
087
088    /**
089     * Accepts this request and creates the incoming Jingle session.
090     *
091     * @param pts list of supported Payload Types
092     * @return Returns the <b><i>IncomingJingleSession</b></i> on which the
093     *         negotiation can be carried out.
094     */
095//    public synchronized JingleSession accept(List<PayloadType> pts) throws XMPPException {
096//        JingleSession session = null;
097//        synchronized (manager) {
098//            session = manager.createIncomingJingleSession(this, pts);
099//            // Acknowledge the IQ reception
100//            session.setSid(this.getSessionID());
101//            //session.sendAck(this.getJingle());
102//            //session.respond(this.getJingle());
103//        }
104//        return session;
105//    }
106
107    /**
108     * Accepts this request and creates the incoming Jingle session.
109     *
110     * @return Returns the IncomingJingleSession on which the
111     *         negotiation can be carried out.
112     * @throws SmackException
113     * @throws InterruptedException
114     */
115    public synchronized JingleSession accept() throws XMPPException, SmackException, InterruptedException {
116        JingleSession session;
117        synchronized (manager) {
118            session = manager.createIncomingJingleSession(this);
119            // Acknowledge the IQ reception
120            session.setSid(this.getSessionID());
121            // session.sendAck(this.getJingle());
122            session.updatePacketListener();
123            session.receivePacketAndRespond(this.getJingle());
124        }
125        return session;
126    }
127
128    /**
129     * Rejects the session request.
130     */
131    public synchronized void reject() {
132        JingleSession session;
133        synchronized (manager) {
134            try {
135                session = manager.createIncomingJingleSession(this);
136                // Acknowledge the IQ reception
137                session.setSid(this.getSessionID());
138                // session.sendAck(this.getJingle());
139                session.updatePacketListener();
140                session.terminate("Declined");
141            } catch (Exception e) {
142                LOGGER.log(Level.SEVERE, "Exception in reject", e);
143            }
144        }
145     }
146}