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