RoomInvitation.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.jivesoftware.smackx.workgroup.packet;

  18. import java.io.IOException;

  19. import javax.xml.namespace.QName;

  20. import org.jivesoftware.smack.packet.ExtensionElement;
  21. import org.jivesoftware.smack.packet.IQ;
  22. import org.jivesoftware.smack.packet.IQ.IQChildElementXmlStringBuilder;
  23. import org.jivesoftware.smack.packet.XmlEnvironment;
  24. import org.jivesoftware.smack.provider.ExtensionElementProvider;
  25. import org.jivesoftware.smack.util.XmlStringBuilder;
  26. import org.jivesoftware.smack.xml.XmlPullParser;
  27. import org.jivesoftware.smack.xml.XmlPullParserException;

  28. import org.jxmpp.jid.EntityBareJid;
  29. import org.jxmpp.jid.EntityJid;
  30. import org.jxmpp.jid.Jid;
  31. import org.jxmpp.jid.impl.JidCreate;

  32. /**
  33.  * Stanza extension for {@link org.jivesoftware.smackx.workgroup.agent.InvitationRequest}.
  34.  *
  35.  * @author Gaston Dombiak
  36.  */
  37. public class RoomInvitation implements ExtensionElement {

  38.     /**
  39.      * Element name of the stanza extension.
  40.      */
  41.     public static final String ELEMENT_NAME = "invite";

  42.     /**
  43.      * Namespace of the stanza extension.
  44.      */
  45.     public static final String NAMESPACE = "http://jabber.org/protocol/workgroup";

  46.     public static final QName QNAME = new QName(NAMESPACE, ELEMENT_NAME);

  47.     /**
  48.      * Type of entity being invited to a groupchat support session.
  49.      */
  50.     private Type type;
  51.     /**
  52.      * JID of the entity being invited. The entity could be another agent, user , a queue or a workgroup. In
  53.      * the case of a queue or a workgroup the server will select the best agent to invite.
  54.      */
  55.     private Jid invitee;
  56.     /**
  57.      * Full JID of the user that sent the invitation.
  58.      */
  59.     private EntityJid inviter;
  60.     /**
  61.      * ID of the session that originated the initial user request.
  62.      */
  63.     private String sessionID;
  64.     /**
  65.      * JID of the room to join if offer is accepted.
  66.      */
  67.     private EntityBareJid room;
  68.     /**
  69.      * Text provided by the inviter explaining the reason why the invitee is invited.
  70.      */
  71.     private String reason;

  72.     public RoomInvitation(Type type, Jid invitee, String sessionID, String reason) {
  73.         this.type = type;
  74.         this.invitee = invitee;
  75.         this.sessionID = sessionID;
  76.         this.reason = reason;
  77.     }

  78.     private RoomInvitation() {
  79.     }

  80.     @Override
  81.     public String getElementName() {
  82.         return ELEMENT_NAME;
  83.     }

  84.     @Override
  85.     public String getNamespace() {
  86.         return NAMESPACE;
  87.     }

  88.     public EntityJid getInviter() {
  89.         return inviter;
  90.     }

  91.     public EntityBareJid getRoom() {
  92.         return room;
  93.     }

  94.     public String getReason() {
  95.         return reason;
  96.     }

  97.     public String getSessionID() {
  98.         return sessionID;
  99.     }

  100.     @Override
  101.     public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  102.         XmlStringBuilder xml = getIQChildElementBuilder(new IQChildElementXmlStringBuilder(this, enclosingNamespace));
  103.         xml.closeElement(this);
  104.         return xml;
  105.     }

  106.     public IQ.IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
  107.         buf.append(" type=\"").append(type.name()).append("\">");
  108.         buf.append("<session xmlns=\"http://jivesoftware.com/protocol/workgroup\" id=\"").append(sessionID).append("\"></session>");
  109.         if (invitee != null) {
  110.             buf.append("<invitee>").append(invitee).append("</invitee>");
  111.         }
  112.         if (inviter != null) {
  113.             buf.append("<inviter>").append(inviter).append("</inviter>");
  114.         }
  115.         if (reason != null) {
  116.             buf.append("<reason>").append(reason).append("</reason>");
  117.         }

  118.         return buf;
  119.     }

  120.     /**
  121.      * Type of entity being invited to a groupchat support session.
  122.      */
  123.     public enum Type {
  124.         /**
  125.          * A user is being invited to a groupchat support session. The user could be another agent
  126.          * or just a regular XMPP user.
  127.          */
  128.         user,
  129.         /**
  130.          * Some agent of the specified queue will be invited to the groupchat support session.
  131.          */
  132.         queue,
  133.         /**
  134.          * Some agent of the specified workgroup will be invited to the groupchat support session.
  135.          */
  136.         workgroup
  137.     }

  138.     public static class RoomInvitationIQ extends IQ {
  139.         private final RoomInvitation roomInvitation;
  140.         public RoomInvitationIQ(RoomInvitation roomInvitation) {
  141.             super(ELEMENT_NAME, NAMESPACE);
  142.             this.roomInvitation = roomInvitation;
  143.         }
  144.         @Override
  145.         protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
  146.             return roomInvitation.getIQChildElementBuilder(xml);
  147.         }
  148.     }

  149.     public static class Provider extends ExtensionElementProvider<RoomInvitation> {

  150.         @Override
  151.         public RoomInvitation parse(XmlPullParser parser,
  152.                         int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException,
  153.                         IOException {
  154.             final RoomInvitation invitation = new RoomInvitation();
  155.             invitation.type = Type.valueOf(parser.getAttributeValue("", "type"));

  156.             outerloop: while (true) {
  157.                 parser.next();
  158.                 if (parser.getEventType() == XmlPullParser.Event.START_ELEMENT) {
  159.                     String elementName = parser.getName();
  160.                     if ("session".equals(elementName)) {
  161.                         invitation.sessionID = parser.getAttributeValue("", "id");
  162.                     }
  163.                     else if ("invitee".equals(elementName)) {
  164.                         String inviteeString = parser.nextText();
  165.                         invitation.invitee = JidCreate.from(inviteeString);
  166.                     }
  167.                     else if ("inviter".equals(elementName)) {
  168.                         String inviterString = parser.nextText();
  169.                         invitation.inviter = JidCreate.entityFrom(inviterString);
  170.                     }
  171.                     else if ("reason".equals(elementName)) {
  172.                         invitation.reason = parser.nextText();
  173.                     }
  174.                     else if ("room".equals(elementName)) {
  175.                         String roomString = parser.nextText();
  176.                         invitation.room = JidCreate.entityBareFrom(roomString);
  177.                     }
  178.                 }
  179.                 else if (parser.getEventType() == XmlPullParser.Event.END_ELEMENT && parser.getDepth() == initialDepth) {
  180.                     break outerloop;
  181.                 }
  182.             }
  183.             return invitation;
  184.         }
  185.     }
  186. }