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