Agent.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.jivesoftware.smackx.workgroup.agent;

  18. import org.jivesoftware.smackx.workgroup.packet.AgentInfo;
  19. import org.jivesoftware.smackx.workgroup.packet.AgentWorkgroups;
  20. import org.jivesoftware.smack.SmackException.NoResponseException;
  21. import org.jivesoftware.smack.SmackException.NotConnectedException;
  22. import org.jivesoftware.smack.XMPPConnection;
  23. import org.jivesoftware.smack.XMPPException.XMPPErrorException;
  24. import org.jivesoftware.smack.packet.IQ;
  25. import org.jxmpp.jid.Jid;

  26. import java.util.Collection;

  27. /**
  28.  * The <code>Agent</code> class is used to represent one agent in a Workgroup Queue.
  29.  *
  30.  * @author Derek DeMoro
  31.  */
  32. public class Agent {
  33.     private XMPPConnection connection;
  34.     private Jid workgroupJID;

  35.     public static Collection<String> getWorkgroups(Jid serviceJID, Jid agentJID, XMPPConnection connection) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  36.         AgentWorkgroups request = new AgentWorkgroups(agentJID);
  37.         request.setTo(serviceJID);
  38.         AgentWorkgroups response = (AgentWorkgroups) connection.createPacketCollectorAndSend(request).nextResultOrThrow();
  39.         return response.getWorkgroups();
  40.     }

  41.     /**
  42.      * Constructs an Agent.
  43.      */
  44.     Agent(XMPPConnection connection, Jid workgroupJID) {
  45.         this.connection = connection;
  46.         this.workgroupJID = workgroupJID;
  47.     }

  48.     /**
  49.      * Return the agents JID
  50.      *
  51.      * @return - the agents JID.
  52.      */
  53.     public Jid getUser() {
  54.         return connection.getUser();
  55.     }

  56.     /**
  57.      * Return the agents name.
  58.      *
  59.      * @return - the agents name.
  60.      * @throws XMPPErrorException
  61.      * @throws NoResponseException
  62.      * @throws NotConnectedException
  63.      * @throws InterruptedException
  64.      */
  65.     public String getName() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  66.         AgentInfo agentInfo = new AgentInfo();
  67.         agentInfo.setType(IQ.Type.get);
  68.         agentInfo.setTo(workgroupJID);
  69.         agentInfo.setFrom(getUser());
  70.         AgentInfo response = (AgentInfo) connection.createPacketCollectorAndSend(agentInfo).nextResultOrThrow();
  71.         return response.getName();
  72.     }

  73.     /**
  74.      * Changes the name of the agent in the server. The server may have this functionality
  75.      * disabled for all the agents or for this agent in particular. If the agent is not
  76.      * allowed to change his name then an exception will be thrown with a service_unavailable
  77.      * error code.
  78.      *
  79.      * @param newName the new name of the agent.
  80.      * @throws XMPPErrorException
  81.      * @throws NoResponseException
  82.      * @throws NotConnectedException
  83.      * @throws InterruptedException
  84.      */
  85.     public void setName(String newName) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  86.         AgentInfo agentInfo = new AgentInfo();
  87.         agentInfo.setType(IQ.Type.set);
  88.         agentInfo.setTo(workgroupJID);
  89.         agentInfo.setFrom(getUser());
  90.         agentInfo.setName(newName);
  91.         connection.createPacketCollectorAndSend(agentInfo).nextResultOrThrow();
  92.     }
  93. }