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 final Set<Item> agents = new HashSet<>();
051
052    public AgentStatusRequest() {
053        super(ELEMENT_NAME, NAMESPACE);
054    }
055
056    public int getAgentCount() {
057        return agents.size();
058    }
059
060    public Set<Item> getAgents() {
061        return Collections.unmodifiableSet(agents);
062    }
063
064    public String getElementName() {
065        return ELEMENT_NAME;
066    }
067
068    public String getNamespace() {
069        return NAMESPACE;
070    }
071
072    @Override
073    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
074        buf.rightAngleBracket();
075        synchronized (agents) {
076            for (Iterator<Item> i=agents.iterator(); i.hasNext(); ) {
077                Item item = i.next();
078                buf.append("<agent jid=\"").append(item.getJID()).append("\">");
079                if (item.getName() != null) {
080                    buf.append("<name xmlns=\""+ AgentInfo.NAMESPACE + "\">");
081                    buf.append(item.getName());
082                    buf.append("</name>");
083                }
084                buf.append("</agent>");
085            }
086        }
087        return buf;
088    }
089
090    public static class Item {
091
092        private String jid;
093        private String type;
094        private String name;
095
096        public Item(String jid, String type, String name) {
097            this.jid = jid;
098            this.type = type;
099            this.name = name;
100        }
101
102        public String getJID() {
103            return jid;
104        }
105
106        public String getType() {
107            return type;
108        }
109
110        public String getName() {
111            return name;
112        }
113    }
114
115    /**
116     * Stanza(/Packet) extension provider for AgentStatusRequest packets.
117     */
118    public static class Provider extends IQProvider<AgentStatusRequest> {
119
120        @Override
121        public AgentStatusRequest parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
122            AgentStatusRequest statusRequest = new AgentStatusRequest();
123
124            boolean done = false;
125            while (!done) {
126                int eventType = parser.next();
127                if ((eventType == XmlPullParser.START_TAG) && ("agent".equals(parser.getName()))) {
128                    statusRequest.agents.add(parseAgent(parser));
129                }
130                else if (eventType == XmlPullParser.END_TAG &&
131                        "agent-status-request".equals(parser.getName()))
132                {
133                    done = true;
134                }
135            }
136            return statusRequest;
137        }
138
139        private Item parseAgent(XmlPullParser parser) throws XmlPullParserException, IOException {
140
141            boolean done = false;
142            String jid = parser.getAttributeValue("", "jid");
143            String type = parser.getAttributeValue("", "type");
144            String name = null;
145            while (!done) {
146                int eventType = parser.next();
147                if ((eventType == XmlPullParser.START_TAG) && ("name".equals(parser.getName()))) {
148                    name = parser.nextText();
149                }
150                else if (eventType == XmlPullParser.END_TAG &&
151                        "agent".equals(parser.getName()))
152                {
153                    done = true;
154                }
155            }
156            return new Item(jid, type, name);
157        }
158    }
159}