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