001/** 002 * 003 * Copyright 2003-2007 Jive Software. 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.jivesoftware.smackx.workgroup.packet; 019 020import java.io.IOException; 021import java.text.ParseException; 022import java.text.SimpleDateFormat; 023import java.util.Collections; 024import java.util.Date; 025import java.util.HashSet; 026import java.util.Set; 027import java.util.TimeZone; 028 029import org.jivesoftware.smack.SmackException; 030import org.jivesoftware.smack.packet.IQ; 031import org.jivesoftware.smack.provider.IQProvider; 032 033import org.xmlpull.v1.XmlPullParser; 034import org.xmlpull.v1.XmlPullParserException; 035 036/** 037 * Stanza(/Packet) used for requesting information about occupants of a room or for retrieving information 038 * such information. 039 * 040 * @author Gaston Dombiak 041 */ 042public class OccupantsInfo extends IQ { 043 044 private static final SimpleDateFormat UTC_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HH:mm:ss"); 045 046 static { 047 UTC_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT+0")); 048 } 049 050 /** 051 * Element name of the stanza(/packet) extension. 052 */ 053 public static final String ELEMENT_NAME = "occupants-info"; 054 055 /** 056 * Namespace of the stanza(/packet) extension. 057 */ 058 public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup"; 059 060 private final String roomID; 061 private final Set<OccupantInfo> occupants; 062 063 public OccupantsInfo(String roomID) { 064 super(ELEMENT_NAME, NAMESPACE); 065 this.roomID = roomID; 066 this.occupants = new HashSet<>(); 067 } 068 069 public String getRoomID() { 070 return roomID; 071 } 072 073 public int getOccupantsCount() { 074 return occupants.size(); 075 } 076 077 public Set<OccupantInfo> getOccupants() { 078 return Collections.unmodifiableSet(occupants); 079 } 080 081 @Override 082 protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) { 083 buf.append(" roomID=\"").append(roomID).append("\">"); 084 synchronized (occupants) { 085 for (OccupantInfo occupant : occupants) { 086 buf.append("<occupant>"); 087 // Add the occupant jid 088 buf.append("<jid>"); 089 buf.append(occupant.getJID()); 090 buf.append("</jid>"); 091 // Add the occupant nickname 092 buf.append("<name>"); 093 buf.append(occupant.getNickname()); 094 buf.append("</name>"); 095 // Add the date when the occupant joined the room 096 buf.append("<joined>"); 097 buf.append(UTC_FORMAT.format(occupant.getJoined())); 098 buf.append("</joined>"); 099 buf.append("</occupant>"); 100 } 101 } 102 return buf; 103 } 104 105 public static class OccupantInfo { 106 107 private final String jid; 108 private final String nickname; 109 private final Date joined; 110 111 public OccupantInfo(String jid, String nickname, Date joined) { 112 this.jid = jid; 113 this.nickname = nickname; 114 this.joined = joined; 115 } 116 117 public String getJID() { 118 return jid; 119 } 120 121 public String getNickname() { 122 return nickname; 123 } 124 125 public Date getJoined() { 126 return joined; 127 } 128 } 129 130 /** 131 * Stanza(/Packet) extension provider for AgentStatusRequest packets. 132 */ 133 public static class Provider extends IQProvider<OccupantsInfo> { 134 135 @Override 136 public OccupantsInfo parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException, SmackException { 137 OccupantsInfo occupantsInfo = new OccupantsInfo(parser.getAttributeValue("", "roomID")); 138 139 boolean done = false; 140 while (!done) { 141 int eventType = parser.next(); 142 if ((eventType == XmlPullParser.START_TAG) && 143 ("occupant".equals(parser.getName()))) { 144 occupantsInfo.occupants.add(parseOccupantInfo(parser)); 145 } else if (eventType == XmlPullParser.END_TAG && 146 ELEMENT_NAME.equals(parser.getName())) { 147 done = true; 148 } 149 } 150 return occupantsInfo; 151 } 152 153 private OccupantInfo parseOccupantInfo(XmlPullParser parser) throws XmlPullParserException, IOException, SmackException { 154 155 boolean done = false; 156 String jid = null; 157 String nickname = null; 158 Date joined = null; 159 while (!done) { 160 int eventType = parser.next(); 161 if ((eventType == XmlPullParser.START_TAG) && ("jid".equals(parser.getName()))) { 162 jid = parser.nextText(); 163 } else if ((eventType == XmlPullParser.START_TAG) && 164 ("nickname".equals(parser.getName()))) { 165 nickname = parser.nextText(); 166 } else if ((eventType == XmlPullParser.START_TAG) && 167 ("joined".equals(parser.getName()))) { 168 try { 169 joined = UTC_FORMAT.parse(parser.nextText()); 170 } catch (ParseException e) { 171 throw new SmackException(e); 172 } 173 } else if (eventType == XmlPullParser.END_TAG && 174 "occupant".equals(parser.getName())) { 175 done = true; 176 } 177 } 178 return new OccupantInfo(jid, nickname, joined); 179 } 180 } 181}