001/*
002 *
003 * Copyright 2003-2007 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 */
017
018package org.jivesoftware.smackx.workgroup.packet;
019
020import java.io.IOException;
021
022import javax.xml.namespace.QName;
023
024import org.jivesoftware.smack.packet.ExtensionElement;
025import org.jivesoftware.smack.packet.IQ;
026import org.jivesoftware.smack.packet.IQ.IQChildElementXmlStringBuilder;
027import org.jivesoftware.smack.packet.XmlEnvironment;
028import org.jivesoftware.smack.provider.ExtensionElementProvider;
029import org.jivesoftware.smack.util.XmlStringBuilder;
030import org.jivesoftware.smack.xml.XmlPullParser;
031import org.jivesoftware.smack.xml.XmlPullParserException;
032
033import org.jxmpp.JxmppContext;
034import org.jxmpp.jid.EntityBareJid;
035import org.jxmpp.jid.EntityJid;
036import org.jxmpp.jid.Jid;
037import org.jxmpp.jid.impl.JidCreate;
038
039/**
040 * Stanza extension for {@link org.jivesoftware.smackx.workgroup.agent.InvitationRequest}.
041 *
042 * @author Gaston Dombiak
043 */
044public class RoomInvitation implements ExtensionElement {
045
046    /**
047     * Element name of the stanza extension.
048     */
049    public static final String ELEMENT_NAME = "invite";
050
051    /**
052     * Namespace of the stanza extension.
053     */
054    public static final String NAMESPACE = "http://jabber.org/protocol/workgroup";
055
056    public static final QName QNAME = new QName(NAMESPACE, ELEMENT_NAME);
057
058    /**
059     * Type of entity being invited to a groupchat support session.
060     */
061    private Type type;
062    /**
063     * JID of the entity being invited. The entity could be another agent, user , a queue or a workgroup. In
064     * the case of a queue or a workgroup the server will select the best agent to invite.
065     */
066    private Jid invitee;
067    /**
068     * Full JID of the user that sent the invitation.
069     */
070    private EntityJid inviter;
071    /**
072     * ID of the session that originated the initial user request.
073     */
074    private String sessionID;
075    /**
076     * JID of the room to join if offer is accepted.
077     */
078    private EntityBareJid room;
079    /**
080     * Text provided by the inviter explaining the reason why the invitee is invited.
081     */
082    private String reason;
083
084    public RoomInvitation(Type type, Jid invitee, String sessionID, String reason) {
085        this.type = type;
086        this.invitee = invitee;
087        this.sessionID = sessionID;
088        this.reason = reason;
089    }
090
091    private RoomInvitation() {
092    }
093
094    @Override
095    public String getElementName() {
096        return ELEMENT_NAME;
097    }
098
099    @Override
100    public String getNamespace() {
101        return NAMESPACE;
102    }
103
104    public EntityJid getInviter() {
105        return inviter;
106    }
107
108    public EntityBareJid getRoom() {
109        return room;
110    }
111
112    public String getReason() {
113        return reason;
114    }
115
116    public String getSessionID() {
117        return sessionID;
118    }
119
120    @Override
121    public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
122        XmlStringBuilder xml = getIQChildElementBuilder(new IQChildElementXmlStringBuilder(this, enclosingNamespace));
123        xml.closeElement(this);
124        return xml;
125    }
126
127    public IQ.IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
128        buf.append(" type=\"").append(type.name()).append("\">");
129        buf.append("<session xmlns=\"http://jivesoftware.com/protocol/workgroup\" id=\"").append(sessionID).append("\"></session>");
130        if (invitee != null) {
131            buf.append("<invitee>").append(invitee).append("</invitee>");
132        }
133        if (inviter != null) {
134            buf.append("<inviter>").append(inviter).append("</inviter>");
135        }
136        if (reason != null) {
137            buf.append("<reason>").append(reason).append("</reason>");
138        }
139
140        return buf;
141    }
142
143    /**
144     * Type of entity being invited to a groupchat support session.
145     */
146    public enum Type {
147        /**
148         * A user is being invited to a groupchat support session. The user could be another agent
149         * or just a regular XMPP user.
150         */
151        user,
152        /**
153         * Some agent of the specified queue will be invited to the groupchat support session.
154         */
155        queue,
156        /**
157         * Some agent of the specified workgroup will be invited to the groupchat support session.
158         */
159        workgroup
160    }
161
162    public static class RoomInvitationIQ extends IQ {
163        private final RoomInvitation roomInvitation;
164        public RoomInvitationIQ(RoomInvitation roomInvitation) {
165            super(ELEMENT_NAME, NAMESPACE);
166            this.roomInvitation = roomInvitation;
167        }
168        @Override
169        protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
170            return roomInvitation.getIQChildElementBuilder(xml);
171        }
172    }
173
174    public static class Provider extends ExtensionElementProvider<RoomInvitation> {
175
176        @Override
177        public RoomInvitation parse(XmlPullParser parser,
178                        int initialDepth, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) throws XmlPullParserException,
179                        IOException {
180            final RoomInvitation invitation = new RoomInvitation();
181            invitation.type = Type.valueOf(parser.getAttributeValue("", "type"));
182
183            outerloop: while (true) {
184                parser.next();
185                if (parser.getEventType() == XmlPullParser.Event.START_ELEMENT) {
186                    String elementName = parser.getName();
187                    if ("session".equals(elementName)) {
188                        invitation.sessionID = parser.getAttributeValue("", "id");
189                    }
190                    else if ("invitee".equals(elementName)) {
191                        String inviteeString = parser.nextText();
192                        invitation.invitee = JidCreate.from(inviteeString);
193                    }
194                    else if ("inviter".equals(elementName)) {
195                        String inviterString = parser.nextText();
196                        invitation.inviter = JidCreate.entityFrom(inviterString);
197                    }
198                    else if ("reason".equals(elementName)) {
199                        invitation.reason = parser.nextText();
200                    }
201                    else if ("room".equals(elementName)) {
202                        String roomString = parser.nextText();
203                        invitation.room = JidCreate.entityBareFrom(roomString);
204                    }
205                }
206                else if (parser.getEventType() == XmlPullParser.Event.END_ELEMENT && parser.getDepth() == initialDepth) {
207                    break outerloop;
208                }
209            }
210            return invitation;
211        }
212    }
213}