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.SmackException;
  27. import org.jivesoftware.smack.packet.IQ;
  28. import org.jivesoftware.smack.provider.IQProvider;
  29. import org.xmlpull.v1.XmlPullParser;
  30. import org.xmlpull.v1.XmlPullParserException;

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

  38.     private static final SimpleDateFormat UTC_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HH:mm:ss");

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

  42.     /**
  43.      * Element name of the packet extension.
  44.      */
  45.     public static final String ELEMENT_NAME = "occupants-info";

  46.     /**
  47.      * Namespace of the packet extension.
  48.      */
  49.     public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";

  50.     private String roomID;
  51.     private final Set<OccupantInfo> occupants;

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

  57.     public String getRoomID() {
  58.         return roomID;
  59.     }

  60.     public int getOccupantsCount() {
  61.         return occupants.size();
  62.     }

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

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

  89.     public static class OccupantInfo {

  90.         private String jid;
  91.         private String nickname;
  92.         private Date joined;

  93.         public OccupantInfo(String jid, String nickname, Date joined) {
  94.             this.jid = jid;
  95.             this.nickname = nickname;
  96.             this.joined = joined;
  97.         }

  98.         public String getJID() {
  99.             return jid;
  100.         }

  101.         public String getNickname() {
  102.             return nickname;
  103.         }

  104.         public Date getJoined() {
  105.             return joined;
  106.         }
  107.     }

  108.     /**
  109.      * Packet extension provider for AgentStatusRequest packets.
  110.      */
  111.     public static class Provider extends IQProvider<OccupantsInfo> {

  112.         @Override
  113.         public OccupantsInfo parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
  114.             OccupantsInfo occupantsInfo = new OccupantsInfo(parser.getAttributeValue("", "roomID"));

  115.             boolean done = false;
  116.             while (!done) {
  117.                 int eventType = parser.next();
  118.                 if ((eventType == XmlPullParser.START_TAG) &&
  119.                         ("occupant".equals(parser.getName()))) {
  120.                     occupantsInfo.occupants.add(parseOccupantInfo(parser));
  121.                 } else if (eventType == XmlPullParser.END_TAG &&
  122.                         ELEMENT_NAME.equals(parser.getName())) {
  123.                     done = true;
  124.                 }
  125.             }
  126.             return occupantsInfo;
  127.         }

  128.         private OccupantInfo parseOccupantInfo(XmlPullParser parser) throws XmlPullParserException, IOException {

  129.             boolean done = false;
  130.             String jid = null;
  131.             String nickname = null;
  132.             Date joined = null;
  133.             while (!done) {
  134.                 int eventType = parser.next();
  135.                 if ((eventType == XmlPullParser.START_TAG) && ("jid".equals(parser.getName()))) {
  136.                     jid = parser.nextText();
  137.                 } else if ((eventType == XmlPullParser.START_TAG) &&
  138.                         ("nickname".equals(parser.getName()))) {
  139.                     nickname = parser.nextText();
  140.                 } else if ((eventType == XmlPullParser.START_TAG) &&
  141.                         ("joined".equals(parser.getName()))) {
  142.                     try {
  143.                         joined = UTC_FORMAT.parse(parser.nextText());
  144.                     } catch (ParseException e) {
  145.                         new SmackException(e);
  146.                     }
  147.                 } else if (eventType == XmlPullParser.END_TAG &&
  148.                         "occupant".equals(parser.getName())) {
  149.                     done = true;
  150.                 }
  151.             }
  152.             return new OccupantInfo(jid, nickname, joined);
  153.         }
  154.     }
  155. }