OccupantsInfo.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 java.text.ParseException;
  20. import java.text.SimpleDateFormat;
  21. import java.util.Collections;
  22. import java.util.Date;
  23. import java.util.HashSet;
  24. import java.util.Set;
  25. import java.util.TimeZone;

  26. import org.jivesoftware.smack.packet.IQ;
  27. import org.jivesoftware.smack.packet.IqData;
  28. import org.jivesoftware.smack.packet.XmlEnvironment;
  29. import org.jivesoftware.smack.provider.IqProvider;
  30. import org.jivesoftware.smack.xml.XmlPullParser;
  31. import org.jivesoftware.smack.xml.XmlPullParserException;

  32. /**
  33.  * Stanza used for requesting information about occupants of a room or for retrieving information
  34.  * such information.
  35.  *
  36.  * @author Gaston Dombiak
  37.  */
  38. public class OccupantsInfo extends IQ {

  39.     @SuppressWarnings("DateFormatConstant")
  40.     private static final SimpleDateFormat UTC_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HH:mm:ss");

  41.     static {
  42.         UTC_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT+0"));
  43.     }

  44.     /**
  45.      * Element name of the stanza extension.
  46.      */
  47.     public static final String ELEMENT_NAME = "occupants-info";

  48.     /**
  49.      * Namespace of the stanza extension.
  50.      */
  51.     public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";

  52.     private final String roomID;
  53.     private final Set<OccupantInfo> occupants;

  54.     public OccupantsInfo(String roomID) {
  55.         super(ELEMENT_NAME, NAMESPACE);
  56.         this.roomID = roomID;
  57.         this.occupants = new HashSet<>();
  58.     }

  59.     public String getRoomID() {
  60.         return roomID;
  61.     }

  62.     public int getOccupantsCount() {
  63.         return occupants.size();
  64.     }

  65.     public Set<OccupantInfo> getOccupants() {
  66.         return Collections.unmodifiableSet(occupants);
  67.     }

  68.     @Override
  69.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
  70.         buf.append(" roomID=\"").append(roomID).append("\">");
  71.         synchronized (occupants) {
  72.             for (OccupantInfo occupant : occupants) {
  73.                 buf.append("<occupant>");
  74.                 // Add the occupant jid
  75.                 buf.append("<jid>");
  76.                 buf.append(occupant.getJID());
  77.                 buf.append("</jid>");
  78.                 // Add the occupant nickname
  79.                 buf.append("<name>");
  80.                 buf.append(occupant.getNickname());
  81.                 buf.append("</name>");
  82.                 // Add the date when the occupant joined the room
  83.                 buf.append("<joined>");
  84.                 synchronized (UTC_FORMAT) {
  85.                     buf.append(UTC_FORMAT.format(occupant.getJoined()));
  86.                 }
  87.                 buf.append("</joined>");
  88.                 buf.append("</occupant>");
  89.             }
  90.         }
  91.         return buf;
  92.     }

  93.     public static class OccupantInfo {

  94.         private final String jid;
  95.         private final String nickname;
  96.         private final Date joined;

  97.         public OccupantInfo(String jid, String nickname, Date joined) {
  98.             this.jid = jid;
  99.             this.nickname = nickname;
  100.             this.joined = joined;
  101.         }

  102.         public String getJID() {
  103.             return jid;
  104.         }

  105.         public String getNickname() {
  106.             return nickname;
  107.         }

  108.         public Date getJoined() {
  109.             return joined;
  110.         }
  111.     }

  112.     /**
  113.      * Stanza extension provider for AgentStatusRequest packets.
  114.      */
  115.     public static class Provider extends IqProvider<OccupantsInfo> {

  116.         @Override
  117.         public OccupantsInfo parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment)
  118.                         throws XmlPullParserException, IOException, ParseException {
  119.             OccupantsInfo occupantsInfo = new OccupantsInfo(parser.getAttributeValue("", "roomID"));

  120.             boolean done = false;
  121.             while (!done) {
  122.                 XmlPullParser.Event eventType = parser.next();
  123.                 if (eventType == XmlPullParser.Event.START_ELEMENT &&
  124.                         "occupant".equals(parser.getName())) {
  125.                     occupantsInfo.occupants.add(parseOccupantInfo(parser));
  126.                 } else if (eventType == XmlPullParser.Event.END_ELEMENT &&
  127.                         ELEMENT_NAME.equals(parser.getName())) {
  128.                     done = true;
  129.                 }
  130.             }
  131.             return occupantsInfo;
  132.         }

  133.         private static OccupantInfo parseOccupantInfo(XmlPullParser parser)
  134.                         throws XmlPullParserException, IOException, ParseException {
  135.             boolean done = false;
  136.             String jid = null;
  137.             String nickname = null;
  138.             Date joined = null;
  139.             while (!done) {
  140.                 XmlPullParser.Event eventType = parser.next();
  141.                 if (eventType == XmlPullParser.Event.START_ELEMENT && "jid".equals(parser.getName())) {
  142.                     jid = parser.nextText();
  143.                 } else if (eventType == XmlPullParser.Event.START_ELEMENT &&
  144.                         "nickname".equals(parser.getName())) {
  145.                     nickname = parser.nextText();
  146.                 } else if (eventType == XmlPullParser.Event.START_ELEMENT &&
  147.                         "joined".equals(parser.getName())) {
  148.                         synchronized (UTC_FORMAT) {
  149.                             joined = UTC_FORMAT.parse(parser.nextText());
  150.                         }
  151.                 } else if (eventType == XmlPullParser.Event.END_ELEMENT &&
  152.                         "occupant".equals(parser.getName())) {
  153.                     done = true;
  154.                 }
  155.             }
  156.             return new OccupantInfo(jid, nickname, joined);
  157.         }
  158.     }
  159. }