1 /** 2 * $Revision$ 3 * $Date$ 4 * 5 * Copyright 2003-2007 Jive Software. 6 * 7 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 */ 19 20 package org.jivesoftware.smackx.workgroup.packet; 21 22 import org.jivesoftware.smack.packet.IQ; 23 import org.jivesoftware.smack.provider.IQProvider; 24 import org.xmlpull.v1.XmlPullParser; 25 26 import java.util.Collections; 27 import java.util.HashSet; 28 import java.util.Iterator; 29 import java.util.Set; 30 31 /** 32 * Agent status request packet. This packet is used by agents to request the list of 33 * agents in a workgroup. The response packet contains a list of packets. Presence 34 * packets from individual agents follow. 35 * 36 * @author Matt Tucker 37 */ 38 public class AgentStatusRequest extends IQ { 39 40 /** 41 * Element name of the packet extension. 42 */ 43 public static final String ELEMENT_NAME = "agent-status-request"; 44 45 /** 46 * Namespace of the packet extension. 47 */ 48 public static final String NAMESPACE = "http://jabber.org/protocol/workgroup"; 49 50 private Set<Item> agents; 51 52 public AgentStatusRequest() { 53 agents = new HashSet<Item>(); 54 } 55 56 public int getAgentCount() { 57 return agents.size(); 58 } 59 60 public Set<Item> getAgents() { 61 return Collections.unmodifiableSet(agents); 62 } 63 64 public String getElementName() { 65 return ELEMENT_NAME; 66 } 67 68 public String getNamespace() { 69 return NAMESPACE; 70 } 71 72 public String getChildElementXML() { 73 StringBuilder buf = new StringBuilder(); 74 buf.append("<").append(ELEMENT_NAME).append(" xmlns=\"").append(NAMESPACE).append("\">"); 75 synchronized (agents) { 76 for (Iterator<Item> i=agents.iterator(); i.hasNext(); ) { 77 Item item = (Item) i.next(); 78 buf.append("<agent jid=\"").append(item.getJID()).append("\">"); 79 if (item.getName() != null) { 80 buf.append("<name xmlns=\""+ AgentInfo.NAMESPACE + "\">"); 81 buf.append(item.getName()); 82 buf.append("</name>"); 83 } 84 buf.append("</agent>"); 85 } 86 } 87 buf.append("</").append(this.getElementName()).append("> "); 88 return buf.toString(); 89 } 90 91 public static class Item { 92 93 private String jid; 94 private String type; 95 private String name; 96 97 public Item(String jid, String type, String name) { 98 this.jid = jid; 99 this.type = type; 100 this.name = name; 101 } 102 103 public String getJID() { 104 return jid; 105 } 106 107 public String getType() { 108 return type; 109 } 110 111 public String getName() { 112 return name; 113 } 114 } 115 116 /** 117 * Packet extension provider for AgentStatusRequest packets. 118 */ 119 public static class Provider implements IQProvider { 120 121 public IQ parseIQ(XmlPullParser parser) throws Exception { 122 AgentStatusRequest statusRequest = new AgentStatusRequest(); 123 124 if (parser.getEventType() != XmlPullParser.START_TAG) { 125 throw new IllegalStateException("Parser not in proper position, or bad XML."); 126 } 127 128 boolean done = false; 129 while (!done) { 130 int eventType = parser.next(); 131 if ((eventType == XmlPullParser.START_TAG) && ("agent".equals(parser.getName()))) { 132 statusRequest.agents.add(parseAgent(parser)); 133 } 134 else if (eventType == XmlPullParser.END_TAG && 135 "agent-status-request".equals(parser.getName())) 136 { 137 done = true; 138 } 139 } 140 return statusRequest; 141 } 142 143 private Item parseAgent(XmlPullParser parser) throws Exception { 144 145 boolean done = false; 146 String jid = parser.getAttributeValue("", "jid"); 147 String type = parser.getAttributeValue("", "type"); 148 String name = null; 149 while (!done) { 150 int eventType = parser.next(); 151 if ((eventType == XmlPullParser.START_TAG) && ("name".equals(parser.getName()))) { 152 name = parser.nextText(); 153 } 154 else if (eventType == XmlPullParser.END_TAG && 155 "agent".equals(parser.getName())) 156 { 157 done = true; 158 } 159 } 160 return new Item(jid, type, name); 161 } 162 } 163 }