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