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