blob: bebac3721a1c06f82919f70383c6a9f68f252506 [file] [log] [blame]
Shuyi Chend7955ce2013-05-22 14:51:55 -07001/**
2 * $Revision$
3 * $Date$
4 *
5 * Copyright 2003-2007 Jive Software.
6 *
7 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20package org.jivesoftware.smackx.workgroup.agent;
21
22import org.jivesoftware.smackx.workgroup.packet.AgentInfo;
23import org.jivesoftware.smackx.workgroup.packet.AgentWorkgroups;
24import org.jivesoftware.smack.PacketCollector;
25import org.jivesoftware.smack.SmackConfiguration;
26import org.jivesoftware.smack.Connection;
27import org.jivesoftware.smack.XMPPException;
28import org.jivesoftware.smack.filter.PacketIDFilter;
29import org.jivesoftware.smack.packet.IQ;
30
31import java.util.Collection;
32
33/**
34 * The <code>Agent</code> class is used to represent one agent in a Workgroup Queue.
35 *
36 * @author Derek DeMoro
37 */
38public class Agent {
39 private Connection connection;
40 private String workgroupJID;
41
42 public static Collection<String> getWorkgroups(String serviceJID, String agentJID, Connection connection) throws XMPPException {
43 AgentWorkgroups request = new AgentWorkgroups(agentJID);
44 request.setTo(serviceJID);
45 PacketCollector collector = connection.createPacketCollector(new PacketIDFilter(request.getPacketID()));
46 // Send the request
47 connection.sendPacket(request);
48
49 AgentWorkgroups response = (AgentWorkgroups)collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
50
51 // Cancel the collector.
52 collector.cancel();
53 if (response == null) {
54 throw new XMPPException("No response from server on status set.");
55 }
56 if (response.getError() != null) {
57 throw new XMPPException(response.getError());
58 }
59 return response.getWorkgroups();
60 }
61
62 /**
63 * Constructs an Agent.
64 */
65 Agent(Connection connection, String workgroupJID) {
66 this.connection = connection;
67 this.workgroupJID = workgroupJID;
68 }
69
70 /**
71 * Return the agents JID
72 *
73 * @return - the agents JID.
74 */
75 public String getUser() {
76 return connection.getUser();
77 }
78
79 /**
80 * Return the agents name.
81 *
82 * @return - the agents name.
83 */
84 public String getName() throws XMPPException {
85 AgentInfo agentInfo = new AgentInfo();
86 agentInfo.setType(IQ.Type.GET);
87 agentInfo.setTo(workgroupJID);
88 agentInfo.setFrom(getUser());
89 PacketCollector collector = connection.createPacketCollector(new PacketIDFilter(agentInfo.getPacketID()));
90 // Send the request
91 connection.sendPacket(agentInfo);
92
93 AgentInfo response = (AgentInfo)collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
94
95 // Cancel the collector.
96 collector.cancel();
97 if (response == null) {
98 throw new XMPPException("No response from server on status set.");
99 }
100 if (response.getError() != null) {
101 throw new XMPPException(response.getError());
102 }
103 return response.getName();
104 }
105
106 /**
107 * Changes the name of the agent in the server. The server may have this functionality
108 * disabled for all the agents or for this agent in particular. If the agent is not
109 * allowed to change his name then an exception will be thrown with a service_unavailable
110 * error code.
111 *
112 * @param newName the new name of the agent.
113 * @throws XMPPException if the agent is not allowed to change his name or no response was
114 * obtained from the server.
115 */
116 public void setName(String newName) throws XMPPException {
117 AgentInfo agentInfo = new AgentInfo();
118 agentInfo.setType(IQ.Type.SET);
119 agentInfo.setTo(workgroupJID);
120 agentInfo.setFrom(getUser());
121 agentInfo.setName(newName);
122 PacketCollector collector = connection.createPacketCollector(new PacketIDFilter(agentInfo.getPacketID()));
123 // Send the request
124 connection.sendPacket(agentInfo);
125
126 IQ response = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
127
128 // Cancel the collector.
129 collector.cancel();
130 if (response == null) {
131 throw new XMPPException("No response from server on status set.");
132 }
133 if (response.getError() != null) {
134 throw new XMPPException(response.getError());
135 }
136 return;
137 }
138}