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 org.jivesoftware.smack.packet.IQ;
  20. import org.jivesoftware.smack.packet.ExtensionElement;
  21. import org.jivesoftware.smack.packet.IQ.IQChildElementXmlStringBuilder;
  22. import org.jivesoftware.smack.provider.ExtensionElementProvider;
  23. import org.jivesoftware.smack.util.XmlStringBuilder;
  24. import org.xmlpull.v1.XmlPullParser;
  25. import org.xmlpull.v1.XmlPullParserException;

  26. /**
  27.  * Packet extension for {@link org.jivesoftware.smackx.workgroup.agent.TransferRequest}.
  28.  *
  29.  * @author Gaston Dombiak
  30.  */
  31. public class RoomTransfer implements ExtensionElement {

  32.     /**
  33.      * Element name of the packet extension.
  34.      */
  35.     public static final String ELEMENT_NAME = "transfer";

  36.     /**
  37.      * Namespace of the packet extension.
  38.      */
  39.     public static final String NAMESPACE = "http://jabber.org/protocol/workgroup";

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

  65.     public RoomTransfer(RoomTransfer.Type type, String invitee, String sessionID, String reason) {
  66.         this.type = type;
  67.         this.invitee = invitee;
  68.         this.sessionID = sessionID;
  69.         this.reason = reason;
  70.     }

  71.     private RoomTransfer() {
  72.     }

  73.     public String getElementName() {
  74.         return ELEMENT_NAME;
  75.     }

  76.     public String getNamespace() {
  77.         return NAMESPACE;
  78.     }

  79.     public String getInviter() {
  80.         return inviter;
  81.     }

  82.     public String getRoom() {
  83.         return room;
  84.     }

  85.     public String getReason() {
  86.         return reason;
  87.     }

  88.     public String getSessionID() {
  89.         return sessionID;
  90.     }

  91.     @Override
  92.     public XmlStringBuilder toXML() {
  93.         XmlStringBuilder xml = getIQChildElementBuilder(new IQChildElementXmlStringBuilder(this));
  94.         xml.closeElement(this);
  95.         return xml;
  96.     }

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

  109.         return buf;
  110.     }

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

  129.     public static class RoomTransferIQ extends IQ {
  130.         private final RoomTransfer roomTransfer;
  131.         public RoomTransferIQ(RoomTransfer roomTransfer) {
  132.             super(ELEMENT_NAME, NAMESPACE);
  133.             this.roomTransfer = roomTransfer;
  134.         }
  135.         @Override
  136.         protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
  137.             return roomTransfer.getIQChildElementBuilder(xml);
  138.         }
  139.     }

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

  141.         @Override
  142.         public RoomTransfer parse(XmlPullParser parser,
  143.                         int initialDepth) throws XmlPullParserException,
  144.                         IOException {
  145.             final RoomTransfer invitation = new RoomTransfer();
  146.             invitation.type = RoomTransfer.Type.valueOf(parser.getAttributeValue("", "type"));

  147.             boolean done = false;
  148.             while (!done) {
  149.                 parser.next();
  150.                 String elementName = parser.getName();
  151.                 if (parser.getEventType() == XmlPullParser.START_TAG) {
  152.                     if ("session".equals(elementName)) {
  153.                         invitation.sessionID = parser.getAttributeValue("", "id");
  154.                     }
  155.                     else if ("invitee".equals(elementName)) {
  156.                         invitation.invitee = parser.nextText();
  157.                     }
  158.                     else if ("inviter".equals(elementName)) {
  159.                         invitation.inviter = parser.nextText();
  160.                     }
  161.                     else if ("reason".equals(elementName)) {
  162.                         invitation.reason = parser.nextText();
  163.                     }
  164.                     else if ("room".equals(elementName)) {
  165.                         invitation.room = parser.nextText();
  166.                     }
  167.                 }
  168.                 else if (parser.getEventType() == XmlPullParser.END_TAG && ELEMENT_NAME.equals(elementName)) {
  169.                     done = true;
  170.                 }
  171.             }
  172.             return invitation;
  173.         }
  174.     }
  175. }