RoomTransfer.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. /**
  29.  * Stanza extension for {@link org.jivesoftware.smackx.workgroup.agent.TransferRequest}.
  30.  *
  31.  * @author Gaston Dombiak
  32.  */
  33. public class RoomTransfer implements ExtensionElement {

  34.     /**
  35.      * Element name of the stanza extension.
  36.      */
  37.     public static final String ELEMENT_NAME = "transfer";

  38.     /**
  39.      * Namespace of the stanza extension.
  40.      */
  41.     public static final String NAMESPACE = "http://jabber.org/protocol/workgroup";

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

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

  68.     public RoomTransfer(RoomTransfer.Type type, String invitee, String sessionID, String reason) {
  69.         this.type = type;
  70.         this.invitee = invitee;
  71.         this.sessionID = sessionID;
  72.         this.reason = reason;
  73.     }

  74.     private RoomTransfer() {
  75.     }

  76.     @Override
  77.     public String getElementName() {
  78.         return ELEMENT_NAME;
  79.     }

  80.     @Override
  81.     public String getNamespace() {
  82.         return NAMESPACE;
  83.     }

  84.     public String getInviter() {
  85.         return inviter;
  86.     }

  87.     public String getRoom() {
  88.         return room;
  89.     }

  90.     public String getReason() {
  91.         return reason;
  92.     }

  93.     public String getSessionID() {
  94.         return sessionID;
  95.     }

  96.     @Override
  97.     public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  98.         XmlStringBuilder xml = getIQChildElementBuilder(new IQChildElementXmlStringBuilder(this, enclosingNamespace));
  99.         xml.closeElement(this);
  100.         return xml;
  101.     }

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

  114.         return buf;
  115.     }

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

  134.     public static class RoomTransferIQ extends IQ {
  135.         private final RoomTransfer roomTransfer;
  136.         public RoomTransferIQ(RoomTransfer roomTransfer) {
  137.             super(ELEMENT_NAME, NAMESPACE);
  138.             this.roomTransfer = roomTransfer;
  139.         }
  140.         @Override
  141.         protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
  142.             return roomTransfer.getIQChildElementBuilder(xml);
  143.         }
  144.     }

  145.     public static class Provider extends ExtensionElementProvider<RoomTransfer> {

  146.         @Override
  147.         public RoomTransfer parse(XmlPullParser parser,
  148.                         int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException,
  149.                         IOException {
  150.             final RoomTransfer invitation = new RoomTransfer();
  151.             invitation.type = RoomTransfer.Type.valueOf(parser.getAttributeValue("", "type"));

  152.             outerloop: while (true) {
  153.                 parser.next();
  154.                 if (parser.getEventType() == XmlPullParser.Event.START_ELEMENT) {
  155.                     String elementName = parser.getName();
  156.                     if ("session".equals(elementName)) {
  157.                         invitation.sessionID = parser.getAttributeValue("", "id");
  158.                     }
  159.                     else if ("invitee".equals(elementName)) {
  160.                         invitation.invitee = parser.nextText();
  161.                     }
  162.                     else if ("inviter".equals(elementName)) {
  163.                         invitation.inviter = parser.nextText();
  164.                     }
  165.                     else if ("reason".equals(elementName)) {
  166.                         invitation.reason = parser.nextText();
  167.                     }
  168.                     else if ("room".equals(elementName)) {
  169.                         invitation.room = parser.nextText();
  170.                     }
  171.                 }
  172.                 else if (parser.getEventType() == XmlPullParser.Event.END_ELEMENT && parser.getDepth() == initialDepth) {
  173.                     break outerloop;
  174.                 }
  175.             }
  176.             return invitation;
  177.         }
  178.     }
  179. }