GroupChatInvitation.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.muc.packet;

  18. import java.io.IOException;

  19. import org.jivesoftware.smack.packet.Stanza;
  20. import org.jivesoftware.smack.packet.ExtensionElement;
  21. import org.jivesoftware.smack.provider.ExtensionElementProvider;
  22. import org.jivesoftware.smack.util.XmlStringBuilder;
  23. import org.xmlpull.v1.XmlPullParser;
  24. import org.xmlpull.v1.XmlPullParserException;

  25. /**
  26.  * A group chat invitation packet extension, which is used to invite other
  27.  * users to a group chat room. To invite a user to a group chat room, address
  28.  * a new message to the user and set the room name appropriately, as in the
  29.  * following code example:
  30.  *
  31.  * <pre>
  32.  * Message message = new Message("user@chat.example.com");
  33.  * message.setBody("Join me for a group chat!");
  34.  * message.addExtension(new GroupChatInvitation("room@chat.example.com"););
  35.  * con.sendStanza(message);
  36.  * </pre>
  37.  *
  38.  * To listen for group chat invitations, use a StanzaExtensionFilter for the
  39.  * <tt>x</tt> element name and <tt>jabber:x:conference</tt> namespace, as in the
  40.  * following code example:
  41.  *
  42.  * <pre>
  43.  * PacketFilter filter = new StanzaExtensionFilter("x", "jabber:x:conference");
  44.  * // Create a packet collector or packet listeners using the filter...
  45.  * </pre>
  46.  *
  47.  * <b>Note</b>: this protocol is outdated now that the Multi-User Chat (MUC) XEP is available
  48.  * (<a href="http://www.xmpp.org/extensions/jep-0045.html">XEP-45</a>). However, most
  49.  * existing clients still use this older protocol. Once MUC support becomes more
  50.  * widespread, this API may be deprecated.
  51.  *
  52.  * @author Matt Tucker
  53.  */
  54. public class GroupChatInvitation implements ExtensionElement {

  55.     /**
  56.      * Element name of the packet extension.
  57.      */
  58.     public static final String ELEMENT = "x";

  59.     /**
  60.      * Namespace of the packet extension.
  61.      */
  62.     public static final String NAMESPACE = "jabber:x:conference";

  63.     private final String roomAddress;

  64.     /**
  65.      * Creates a new group chat invitation to the specified room address.
  66.      * GroupChat room addresses are in the form <tt>room@service</tt>,
  67.      * where <tt>service</tt> is the name of groupchat server, such as
  68.      * <tt>chat.example.com</tt>.
  69.      *
  70.      * @param roomAddress the address of the group chat room.
  71.      */
  72.     public GroupChatInvitation(String roomAddress) {
  73.         this.roomAddress = roomAddress;
  74.     }

  75.     /**
  76.      * Returns the address of the group chat room. GroupChat room addresses
  77.      * are in the form <tt>room@service</tt>, where <tt>service</tt> is
  78.      * the name of groupchat server, such as <tt>chat.example.com</tt>.
  79.      *
  80.      * @return the address of the group chat room.
  81.      */
  82.     public String getRoomAddress() {
  83.         return roomAddress;
  84.     }

  85.     public String getElementName() {
  86.         return ELEMENT;
  87.     }

  88.     public String getNamespace() {
  89.         return NAMESPACE;
  90.     }

  91.     @Override
  92.     public XmlStringBuilder toXML() {
  93.         XmlStringBuilder xml = new XmlStringBuilder(this);
  94.         xml.attribute("jid", getRoomAddress());
  95.         xml.closeEmptyElement();
  96.         return xml;
  97.     }

  98.     /**
  99.      *
  100.      * @param packet
  101.      * @return the GroupChatInvitation or null
  102.      * @deprecated use {@link #from(Stanza)} instead
  103.      */
  104.     @Deprecated
  105.     public static GroupChatInvitation getFrom(Stanza packet) {
  106.         return from(packet);
  107.     }

  108.     /**
  109.      *
  110.      * @param packet
  111.      * @return the GroupChatInvitation or null
  112.      */
  113.     public static GroupChatInvitation from(Stanza packet) {
  114.         return packet.getExtension(ELEMENT, NAMESPACE);
  115.     }

  116.     public static class Provider extends ExtensionElementProvider<GroupChatInvitation> {

  117.         @Override
  118.         public GroupChatInvitation parse(XmlPullParser parser,
  119.                         int initialDepth) throws XmlPullParserException,
  120.                         IOException {
  121.             String roomAddress = parser.getAttributeValue("", "jid");
  122.             // Advance to end of extension.
  123.             parser.next();
  124.             return new GroupChatInvitation(roomAddress);
  125.         }
  126.     }
  127. }