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.util.Collections; 022import java.util.HashSet; 023import java.util.Iterator; 024import java.util.Set; 025 026import org.jivesoftware.smack.packet.IQ; 027import org.jivesoftware.smack.packet.XmlEnvironment; 028import org.jivesoftware.smack.provider.IQProvider; 029import org.jivesoftware.smack.util.ParserUtils; 030import org.jivesoftware.smack.xml.XmlPullParser; 031import org.jivesoftware.smack.xml.XmlPullParserException; 032 033import org.jxmpp.jid.EntityBareJid; 034 035/** 036 * Agent status request packet. This stanza is used by agents to request the list of 037 * agents in a workgroup. The response stanza contains a list of packets. Presence 038 * packets from individual agents follow. 039 * 040 * @author Matt Tucker 041 */ 042public class AgentStatusRequest extends IQ { 043 044 /** 045 * Element name of the stanza extension. 046 */ 047 public static final String ELEMENT_NAME = "agent-status-request"; 048 049 /** 050 * Namespace of the stanza extension. 051 */ 052 public static final String NAMESPACE = "http://jabber.org/protocol/workgroup"; 053 054 private final Set<Item> agents = new HashSet<>(); 055 056 public AgentStatusRequest() { 057 super(ELEMENT_NAME, NAMESPACE); 058 } 059 060 public int getAgentCount() { 061 return agents.size(); 062 } 063 064 public Set<Item> getAgents() { 065 return Collections.unmodifiableSet(agents); 066 } 067 068 @Override 069 protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) { 070 buf.rightAngleBracket(); 071 synchronized (agents) { 072 for (Iterator<Item> i = agents.iterator(); i.hasNext(); ) { 073 Item item = i.next(); 074 buf.append("<agent jid=\"").append(item.getJID()).append("\">"); 075 if (item.getName() != null) { 076 buf.append("<name xmlns=\"" + AgentInfo.NAMESPACE + "\">"); 077 buf.append(item.getName()); 078 buf.append("</name>"); 079 } 080 buf.append("</agent>"); 081 } 082 } 083 return buf; 084 } 085 086 public static class Item { 087 088 private final EntityBareJid jid; 089 private final String type; 090 private final String name; 091 092 public Item(EntityBareJid jid, String type, String name) { 093 this.jid = jid; 094 this.type = type; 095 this.name = name; 096 } 097 098 public EntityBareJid getJID() { 099 return jid; 100 } 101 102 public String getType() { 103 return type; 104 } 105 106 public String getName() { 107 return name; 108 } 109 } 110 111 /** 112 * Stanza extension provider for AgentStatusRequest packets. 113 */ 114 public static class Provider extends IQProvider<AgentStatusRequest> { 115 116 @Override 117 public AgentStatusRequest parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException { 118 AgentStatusRequest statusRequest = new AgentStatusRequest(); 119 120 boolean done = false; 121 while (!done) { 122 XmlPullParser.Event eventType = parser.next(); 123 if (eventType == XmlPullParser.Event.START_ELEMENT && "agent".equals(parser.getName())) { 124 statusRequest.agents.add(parseAgent(parser)); 125 } 126 else if (eventType == XmlPullParser.Event.END_ELEMENT && 127 "agent-status-request".equals(parser.getName())) { 128 done = true; 129 } 130 } 131 return statusRequest; 132 } 133 134 private static Item parseAgent(XmlPullParser parser) throws XmlPullParserException, IOException { 135 136 boolean done = false; 137 EntityBareJid jid = ParserUtils.getBareJidAttribute(parser); 138 String type = parser.getAttributeValue("", "type"); 139 String name = null; 140 while (!done) { 141 XmlPullParser.Event eventType = parser.next(); 142 if (eventType == XmlPullParser.Event.START_ELEMENT && "name".equals(parser.getName())) { 143 name = parser.nextText(); 144 } 145 else if (eventType == XmlPullParser.Event.END_ELEMENT && 146 "agent".equals(parser.getName())) { 147 done = true; 148 } 149 } 150 return new Item(jid, type, name); 151 } 152 } 153}