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