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 */ 017package org.jivesoftware.smackx.workgroup.packet; 018 019import java.io.IOException; 020import java.text.ParseException; 021import java.text.SimpleDateFormat; 022import java.util.ArrayList; 023import java.util.Collections; 024import java.util.Date; 025import java.util.Iterator; 026import java.util.List; 027import java.util.TimeZone; 028 029import javax.xml.namespace.QName; 030 031import org.jivesoftware.smack.packet.ExtensionElement; 032import org.jivesoftware.smack.packet.XmlEnvironment; 033import org.jivesoftware.smack.provider.ExtensionElementProvider; 034import org.jivesoftware.smack.util.ParserUtils; 035import org.jivesoftware.smack.xml.XmlPullParser; 036import org.jivesoftware.smack.xml.XmlPullParserException; 037 038import org.jxmpp.jid.EntityBareJid; 039 040/** 041 * Agent status packet. 042 * 043 * @author Matt Tucker 044 */ 045public class AgentStatus implements ExtensionElement { 046 047 @SuppressWarnings("DateFormatConstant") 048 private static final SimpleDateFormat UTC_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HH:mm:ss"); 049 050 static { 051 UTC_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT+0")); 052 } 053 054 /** 055 * Element name of the stanza extension. 056 */ 057 public static final String ELEMENT_NAME = "agent-status"; 058 059 /** 060 * Namespace of the stanza extension. 061 */ 062 public static final String NAMESPACE = "http://jabber.org/protocol/workgroup"; 063 064 public static final QName QNAME = new QName(NAMESPACE, ELEMENT_NAME); 065 066 private EntityBareJid workgroupJID; 067 private final List<ChatInfo> currentChats = new ArrayList<>(); 068 private int maxChats = -1; 069 070 AgentStatus() { 071 } 072 073 public EntityBareJid getWorkgroupJID() { 074 return workgroupJID; 075 } 076 077 /** 078 * Returns a collection of ChatInfo where each ChatInfo represents a Chat where this agent 079 * is participating. 080 * 081 * @return a collection of ChatInfo where each ChatInfo represents a Chat where this agent 082 * is participating. 083 */ 084 public List<ChatInfo> getCurrentChats() { 085 return Collections.unmodifiableList(currentChats); 086 } 087 088 public int getMaxChats() { 089 return maxChats; 090 } 091 092 @Override 093 public String getElementName() { 094 return ELEMENT_NAME; 095 } 096 097 @Override 098 public String getNamespace() { 099 return NAMESPACE; 100 } 101 102 @Override 103 public String toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { 104 StringBuilder buf = new StringBuilder(); 105 106 buf.append('<').append(ELEMENT_NAME).append(" xmlns=\"").append(NAMESPACE).append('"'); 107 if (workgroupJID != null) { 108 buf.append(" jid=\"").append(workgroupJID).append('"'); 109 } 110 buf.append('>'); 111 if (maxChats != -1) { 112 buf.append("<max-chats>").append(maxChats).append("</max-chats>"); 113 } 114 if (!currentChats.isEmpty()) { 115 buf.append("<current-chats xmlns= \"http://jivesoftware.com/protocol/workgroup\">"); 116 for (Iterator<ChatInfo> it = currentChats.iterator(); it.hasNext();) { 117 buf.append(it.next().toXML()); 118 } 119 buf.append("</current-chats>"); 120 } 121 buf.append("</").append(this.getElementName()).append("> "); 122 123 return buf.toString(); 124 } 125 126 /** 127 * Represents information about a Chat where this Agent is participating. 128 * 129 * @author Gaston Dombiak 130 */ 131 public static class ChatInfo { 132 133 private final String sessionID; 134 private final String userID; 135 private final Date date; 136 private final String email; 137 private final String username; 138 private final String question; 139 140 public ChatInfo(String sessionID, String userID, Date date, String email, String username, String question) { 141 this.sessionID = sessionID; 142 this.userID = userID; 143 this.date = date; 144 this.email = email; 145 this.username = username; 146 this.question = question; 147 } 148 149 /** 150 * Returns the sessionID associated to this chat. Each chat will have a unique sessionID 151 * that could be used for retrieving the whole transcript of the conversation. 152 * 153 * @return the sessionID associated to this chat. 154 */ 155 public String getSessionID() { 156 return sessionID; 157 } 158 159 /** 160 * Returns the user unique identification of the user that made the initial request and 161 * for which this chat was generated. If the user joined using an anonymous connection 162 * then the userID will be the value of the ID attribute of the USER element. Otherwise, 163 * the userID will be the bare JID of the user that made the request. 164 * 165 * @return the user unique identification of the user that made the initial request. 166 */ 167 public String getUserID() { 168 return userID; 169 } 170 171 /** 172 * Returns the date when this agent joined the chat. 173 * 174 * @return the date when this agent joined the chat. 175 */ 176 public Date getDate() { 177 return date; 178 } 179 180 /** 181 * Returns the email address associated with the user. 182 * 183 * @return the email address associated with the user. 184 */ 185 public String getEmail() { 186 return email; 187 } 188 189 /** 190 * Returns the username(nickname) associated with the user. 191 * 192 * @return the username associated with the user. 193 */ 194 public String getUsername() { 195 return username; 196 } 197 198 /** 199 * Returns the question the user asked. 200 * 201 * @return the question the user asked, if any. 202 */ 203 public String getQuestion() { 204 return question; 205 } 206 207 public String toXML() { 208 StringBuilder buf = new StringBuilder(); 209 210 buf.append("<chat "); 211 if (sessionID != null) { 212 buf.append(" sessionID=\"").append(sessionID).append('"'); 213 } 214 if (userID != null) { 215 buf.append(" userID=\"").append(userID).append('"'); 216 } 217 if (date != null) { 218 buf.append(" startTime=\""); 219 synchronized (UTC_FORMAT) { 220 buf.append(UTC_FORMAT.format(date)); 221 } 222 buf.append('"'); 223 } 224 if (email != null) { 225 buf.append(" email=\"").append(email).append('"'); 226 } 227 if (username != null) { 228 buf.append(" username=\"").append(username).append('"'); 229 } 230 if (question != null) { 231 buf.append(" question=\"").append(question).append('"'); 232 } 233 buf.append("/>"); 234 235 return buf.toString(); 236 } 237 } 238 239 /** 240 * Stanza extension provider for AgentStatus packets. 241 */ 242 public static class Provider extends ExtensionElementProvider<AgentStatus> { 243 244 @Override 245 public AgentStatus parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) 246 throws XmlPullParserException, IOException, ParseException { 247 AgentStatus agentStatus = new AgentStatus(); 248 249 agentStatus.workgroupJID = ParserUtils.getBareJidAttribute(parser); 250 251 boolean done = false; 252 while (!done) { 253 XmlPullParser.Event eventType = parser.next(); 254 255 if (eventType == XmlPullParser.Event.START_ELEMENT) { 256 if ("chat".equals(parser.getName())) { 257 ChatInfo chatInfo = parseChatInfo(parser); 258 agentStatus.currentChats.add(chatInfo); 259 } 260 else if ("max-chats".equals(parser.getName())) { 261 agentStatus.maxChats = Integer.parseInt(parser.nextText()); 262 } 263 } 264 else if (eventType == XmlPullParser.Event.END_ELEMENT && 265 ELEMENT_NAME.equals(parser.getName())) { 266 done = true; 267 } 268 } 269 return agentStatus; 270 } 271 272 private static ChatInfo parseChatInfo(XmlPullParser parser) throws ParseException { 273 274 String sessionID = parser.getAttributeValue("", "sessionID"); 275 String userID = parser.getAttributeValue("", "userID"); 276 Date date = null; 277 synchronized (UTC_FORMAT) { 278 date = UTC_FORMAT.parse(parser.getAttributeValue("", "startTime")); 279 } 280 281 String email = parser.getAttributeValue("", "email"); 282 String username = parser.getAttributeValue("", "username"); 283 String question = parser.getAttributeValue("", "question"); 284 285 return new ChatInfo(sessionID, userID, date, email, username, question); 286 } 287 } 288}