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