AgentStatusRequest.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.jivesoftware.smackx.workgroup.packet;

  18. import org.jivesoftware.smack.packet.IQ;
  19. import org.jivesoftware.smack.provider.IQProvider;
  20. import org.xmlpull.v1.XmlPullParser;
  21. import org.xmlpull.v1.XmlPullParserException;

  22. import java.io.IOException;
  23. import java.util.Collections;
  24. import java.util.HashSet;
  25. import java.util.Iterator;
  26. import java.util.Set;

  27. /**
  28.  * Agent status request packet. This packet is used by agents to request the list of
  29.  * agents in a workgroup. The response packet contains a list of packets. Presence
  30.  * packets from individual agents follow.
  31.  *
  32.  * @author Matt Tucker
  33.  */
  34. public class AgentStatusRequest extends IQ {

  35.      /**
  36.      * Element name of the packet extension.
  37.      */
  38.     public static final String ELEMENT_NAME = "agent-status-request";

  39.     /**
  40.      * Namespace of the packet extension.
  41.      */
  42.     public static final String NAMESPACE = "http://jabber.org/protocol/workgroup";

  43.     private Set<Item> agents;

  44.     public AgentStatusRequest() {
  45.         super(ELEMENT_NAME, NAMESPACE);
  46.         agents = new HashSet<Item>();
  47.     }

  48.     public int getAgentCount() {
  49.         return agents.size();
  50.     }

  51.     public Set<Item> getAgents() {
  52.         return Collections.unmodifiableSet(agents);
  53.     }

  54.     public String getElementName() {
  55.         return ELEMENT_NAME;
  56.     }

  57.     public String getNamespace() {
  58.         return NAMESPACE;
  59.     }

  60.     @Override
  61.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
  62.         buf.rightAngleBracket();
  63.         synchronized (agents) {
  64.             for (Iterator<Item> i=agents.iterator(); i.hasNext(); ) {
  65.                 Item item = (Item) i.next();
  66.                 buf.append("<agent jid=\"").append(item.getJID()).append("\">");
  67.                 if (item.getName() != null) {
  68.                     buf.append("<name xmlns=\""+ AgentInfo.NAMESPACE + "\">");
  69.                     buf.append(item.getName());
  70.                     buf.append("</name>");
  71.                 }
  72.                 buf.append("</agent>");
  73.             }
  74.         }
  75.         return buf;
  76.     }

  77.     public static class Item {

  78.         private String jid;
  79.         private String type;
  80.         private String name;

  81.         public Item(String jid, String type, String name) {
  82.             this.jid = jid;
  83.             this.type = type;
  84.             this.name = name;
  85.         }

  86.         public String getJID() {
  87.             return jid;
  88.         }

  89.         public String getType() {
  90.             return type;
  91.         }

  92.         public String getName() {
  93.             return name;
  94.         }
  95.     }

  96.     /**
  97.      * Packet extension provider for AgentStatusRequest packets.
  98.      */
  99.     public static class Provider extends IQProvider<AgentStatusRequest> {

  100.         @Override
  101.         public AgentStatusRequest parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
  102.             AgentStatusRequest statusRequest = new AgentStatusRequest();

  103.             boolean done = false;
  104.             while (!done) {
  105.                 int eventType = parser.next();
  106.                 if ((eventType == XmlPullParser.START_TAG) && ("agent".equals(parser.getName()))) {
  107.                     statusRequest.agents.add(parseAgent(parser));
  108.                 }
  109.                 else if (eventType == XmlPullParser.END_TAG &&
  110.                         "agent-status-request".equals(parser.getName()))
  111.                 {
  112.                     done = true;
  113.                 }
  114.             }
  115.             return statusRequest;
  116.         }

  117.         private Item parseAgent(XmlPullParser parser) throws XmlPullParserException, IOException {

  118.             boolean done = false;
  119.             String jid = parser.getAttributeValue("", "jid");
  120.             String type = parser.getAttributeValue("", "type");
  121.             String name = null;
  122.             while (!done) {
  123.                 int eventType = parser.next();
  124.                 if ((eventType == XmlPullParser.START_TAG) && ("name".equals(parser.getName()))) {
  125.                     name = parser.nextText();
  126.                 }
  127.                 else if (eventType == XmlPullParser.END_TAG &&
  128.                         "agent".equals(parser.getName()))
  129.                 {
  130.                     done = true;
  131.                 }
  132.             }
  133.             return new Item(jid, type, name);
  134.         }
  135.     }
  136. }