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