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