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 org.jivesoftware.smack.packet.IQ; 021import org.jivesoftware.smack.provider.IQProvider; 022import org.xmlpull.v1.XmlPullParser; 023 024import java.text.SimpleDateFormat; 025import java.util.*; 026 027/** 028 * Packet used for requesting information about occupants of a room or for retrieving information 029 * such information. 030 * 031 * @author Gaston Dombiak 032 */ 033public class OccupantsInfo extends IQ { 034 035 private static final SimpleDateFormat UTC_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HH:mm:ss"); 036 037 static { 038 UTC_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT+0")); 039 } 040 041 /** 042 * Element name of the packet extension. 043 */ 044 public static final String ELEMENT_NAME = "occupants-info"; 045 046 /** 047 * Namespace of the packet extension. 048 */ 049 public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup"; 050 051 private String roomID; 052 private final Set<OccupantInfo> occupants; 053 054 public OccupantsInfo(String roomID) { 055 this.roomID = roomID; 056 this.occupants = new HashSet<OccupantInfo>(); 057 } 058 059 public String getRoomID() { 060 return roomID; 061 } 062 063 public int getOccupantsCount() { 064 return occupants.size(); 065 } 066 067 public Set<OccupantInfo> getOccupants() { 068 return Collections.unmodifiableSet(occupants); 069 } 070 071 public String getChildElementXML() { 072 StringBuilder buf = new StringBuilder(); 073 buf.append("<").append(ELEMENT_NAME).append(" xmlns=\"").append(NAMESPACE); 074 buf.append("\" roomID=\"").append(roomID).append("\">"); 075 synchronized (occupants) { 076 for (OccupantInfo occupant : occupants) { 077 buf.append("<occupant>"); 078 // Add the occupant jid 079 buf.append("<jid>"); 080 buf.append(occupant.getJID()); 081 buf.append("</jid>"); 082 // Add the occupant nickname 083 buf.append("<name>"); 084 buf.append(occupant.getNickname()); 085 buf.append("</name>"); 086 // Add the date when the occupant joined the room 087 buf.append("<joined>"); 088 buf.append(UTC_FORMAT.format(occupant.getJoined())); 089 buf.append("</joined>"); 090 buf.append("</occupant>"); 091 } 092 } 093 buf.append("</").append(ELEMENT_NAME).append("> "); 094 return buf.toString(); 095 } 096 097 public static class OccupantInfo { 098 099 private String jid; 100 private String nickname; 101 private Date joined; 102 103 public OccupantInfo(String jid, String nickname, Date joined) { 104 this.jid = jid; 105 this.nickname = nickname; 106 this.joined = joined; 107 } 108 109 public String getJID() { 110 return jid; 111 } 112 113 public String getNickname() { 114 return nickname; 115 } 116 117 public Date getJoined() { 118 return joined; 119 } 120 } 121 122 /** 123 * Packet extension provider for AgentStatusRequest packets. 124 */ 125 public static class Provider implements IQProvider { 126 127 public IQ parseIQ(XmlPullParser parser) throws Exception { 128 if (parser.getEventType() != XmlPullParser.START_TAG) { 129 throw new IllegalStateException("Parser not in proper position, or bad XML."); 130 } 131 OccupantsInfo occupantsInfo = new OccupantsInfo(parser.getAttributeValue("", "roomID")); 132 133 boolean done = false; 134 while (!done) { 135 int eventType = parser.next(); 136 if ((eventType == XmlPullParser.START_TAG) && 137 ("occupant".equals(parser.getName()))) { 138 occupantsInfo.occupants.add(parseOccupantInfo(parser)); 139 } else if (eventType == XmlPullParser.END_TAG && 140 ELEMENT_NAME.equals(parser.getName())) { 141 done = true; 142 } 143 } 144 return occupantsInfo; 145 } 146 147 private OccupantInfo parseOccupantInfo(XmlPullParser parser) throws Exception { 148 149 boolean done = false; 150 String jid = null; 151 String nickname = null; 152 Date joined = null; 153 while (!done) { 154 int eventType = parser.next(); 155 if ((eventType == XmlPullParser.START_TAG) && ("jid".equals(parser.getName()))) { 156 jid = parser.nextText(); 157 } else if ((eventType == XmlPullParser.START_TAG) && 158 ("nickname".equals(parser.getName()))) { 159 nickname = parser.nextText(); 160 } else if ((eventType == XmlPullParser.START_TAG) && 161 ("joined".equals(parser.getName()))) { 162 joined = UTC_FORMAT.parse(parser.nextText()); 163 } else if (eventType == XmlPullParser.END_TAG && 164 "occupant".equals(parser.getName())) { 165 done = true; 166 } 167 } 168 return new OccupantInfo(jid, nickname, joined); 169 } 170 } 171}