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