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