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 java.io.IOException;
  19. import java.util.Collections;
  20. import java.util.HashSet;
  21. import java.util.Iterator;
  22. import java.util.Set;

  23. import org.jivesoftware.smack.packet.IQ;
  24. import org.jivesoftware.smack.packet.IqData;
  25. import org.jivesoftware.smack.packet.XmlEnvironment;
  26. import org.jivesoftware.smack.provider.IqProvider;
  27. import org.jivesoftware.smack.util.ParserUtils;
  28. import org.jivesoftware.smack.xml.XmlPullParser;
  29. import org.jivesoftware.smack.xml.XmlPullParserException;

  30. import org.jxmpp.jid.EntityBareJid;

  31. /**
  32.  * Agent status request packet. This stanza is used by agents to request the list of
  33.  * agents in a workgroup. The response stanza 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.      * Element name of the stanza extension.
  41.      */
  42.     public static final String ELEMENT_NAME = "agent-status-request";

  43.     /**
  44.      * Namespace of the stanza extension.
  45.      */
  46.     public static final String NAMESPACE = "http://jabber.org/protocol/workgroup";

  47.     private final Set<Item> agents = new HashSet<>();

  48.     public AgentStatusRequest() {
  49.         super(ELEMENT_NAME, NAMESPACE);
  50.     }

  51.     public int getAgentCount() {
  52.         return agents.size();
  53.     }

  54.     public Set<Item> getAgents() {
  55.         return Collections.unmodifiableSet(agents);
  56.     }

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

  74.     public static class Item {

  75.         private final EntityBareJid jid;
  76.         private final String type;
  77.         private final String name;

  78.         public Item(EntityBareJid jid, String type, String name) {
  79.             this.jid = jid;
  80.             this.type = type;
  81.             this.name = name;
  82.         }

  83.         public EntityBareJid getJID() {
  84.             return jid;
  85.         }

  86.         public String getType() {
  87.             return type;
  88.         }

  89.         public String getName() {
  90.             return name;
  91.         }
  92.     }

  93.     /**
  94.      * Stanza extension provider for AgentStatusRequest packets.
  95.      */
  96.     public static class Provider extends IqProvider<AgentStatusRequest> {

  97.         @Override
  98.         public AgentStatusRequest parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
  99.             AgentStatusRequest statusRequest = new AgentStatusRequest();

  100.             boolean done = false;
  101.             while (!done) {
  102.                 XmlPullParser.Event eventType = parser.next();
  103.                 if (eventType == XmlPullParser.Event.START_ELEMENT && "agent".equals(parser.getName())) {
  104.                     statusRequest.agents.add(parseAgent(parser));
  105.                 }
  106.                 else if (eventType == XmlPullParser.Event.END_ELEMENT &&
  107.                         "agent-status-request".equals(parser.getName())) {
  108.                     done = true;
  109.                 }
  110.             }
  111.             return statusRequest;
  112.         }

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

  114.             boolean done = false;
  115.             EntityBareJid jid = ParserUtils.getBareJidAttribute(parser);
  116.             String type = parser.getAttributeValue("", "type");
  117.             String name = null;
  118.             while (!done) {
  119.                 XmlPullParser.Event eventType = parser.next();
  120.                 if (eventType == XmlPullParser.Event.START_ELEMENT && "name".equals(parser.getName())) {
  121.                     name = parser.nextText();
  122.                 }
  123.                 else if (eventType == XmlPullParser.Event.END_ELEMENT &&
  124.                         "agent".equals(parser.getName())) {
  125.                     done = true;
  126.                 }
  127.             }
  128.             return new Item(jid, type, name);
  129.         }
  130.     }
  131. }