reply.setFrom(workgroup.getJID());
String queryName = iq.getName();
String queryNamespace = iq.getNamespace().toString();
if ("join-queue".equals(queryName)) {
InterceptorManager interceptorManager = QueueInterceptorManager.getInstance();
try {
interceptorManager.invokeInterceptors(workgroup.getJID().toBareJID(), packet, true,
false);
// Received a Join Queue request from a visitor, create a new request.
UserRequest request = new UserRequest(packet, workgroup);
// Let the workgroup process the new request
if (!workgroup.queueRequest(request)) {
// It was not possible to add the request to a queue so answer that the
// workgroup is not accepting new join-queue requests
reply.setChildElement(packet.getChildElement().createCopy());
reply.setError(new PacketError(PacketError.Condition.service_unavailable));
}
interceptorManager.invokeInterceptors(workgroup.getJID().toBareJID(), packet, true,
true);
}
catch (PacketRejectedException e) {
workgroup.rejectPacket(packet, e);
reply = null;
}
}
else if ("depart-queue".equals(queryName)) {
// Visitor is departing queue
try {
Request request = UserRequest.getRequest(workgroup, sender);
InterceptorManager interceptorManager = QueueInterceptorManager.getInstance();
try {
interceptorManager.invokeInterceptors(workgroup.getJID().toBareJID(), packet,
true, false);
request.cancel(Request.CancelType.DEPART);
iq.add(request.getSessionElement());
interceptorManager.invokeInterceptors(workgroup.getJID().toBareJID(), packet,
true, true);
}
catch (PacketRejectedException e) {
workgroup.rejectPacket(packet, e);
reply = null;
}
}
catch (NotFoundException e) {
reply.setChildElement(packet.getChildElement().createCopy());
reply.setError(new PacketError(PacketError.Condition.item_not_found));
Log.debug("Request not found" +
" while departing queue:", e);
}
}
else if ("offer-accept".equals(queryName)) {
try {
InterceptorManager interceptorManager = OfferInterceptorManager.getInstance();
String id = iq.attributeValue("id");
String jid = iq.attributeValue("jid");
if (id != null || jid != null) {
Request request;
if (id != null) {
// Search request by its unique ID
request = Request.getRequest(id);
}
else {
// Old version of FP refers to requests by the user's jid. This old version
// implements transfers and invitations on the client and not the server side.
// Therefore, for each user's jid there is always a unique Request
request = UserRequest.getRequest(workgroup, new JID(jid));
}
Offer offer = request.getOffer();
if (offer != null && offer.isOutstanding()) {
AgentSession agentSession = agentManager.getAgentSession(packet.getFrom());
if (agentSession == null) {
reply.setChildElement(packet.getChildElement().createCopy());
reply.setError(new PacketError(PacketError.Condition.item_not_found));
Log
.debug("Agent not found while accepting offer");
}
else {
try {
interceptorManager.invokeInterceptors(workgroup.getJID().toBareJID(),
packet, true, false);
offer.accept(agentSession);
interceptorManager.invokeInterceptors(workgroup.getJID().toBareJID(),
packet, true, true);
}
catch (PacketRejectedException e) {
workgroup.rejectPacket(packet, e);
reply = null;
}
}
}
else {
reply.setChildElement(packet.getChildElement().createCopy());
reply.setError(new PacketError(PacketError.Condition.not_acceptable));
}
}
}
catch (NotFoundException e) {
reply.setChildElement(packet.getChildElement().createCopy());
reply.setError(new PacketError(PacketError.Condition.item_not_found));
Log.debug("Request not found " +
"while accepting offer: ", e);
}
catch (AgentNotFoundException e) {
reply.setChildElement(packet.getChildElement().createCopy());
reply.setError(new PacketError(PacketError.Condition.item_not_found));
Log.debug("Agent not found " +
"while accepting offer: ", e);
}
}
else if ("offer-reject".equals(queryName)) {
try {
InterceptorManager interceptorManager = OfferInterceptorManager.getInstance();
String id = iq.attributeValue("id");
String jid = iq.attributeValue("jid");
if (id != null || jid != null) {
Request request;
if (id != null) {
// Search request by its unique ID
request = Request.getRequest(id);
}
else {
// Old version of FP refers to requests by the user's jid. This old version
// implements transfers and invitations on the client and not the server side.
// Therefore, for each user's jid there is always a unique Request
request = UserRequest.getRequest(workgroup, new JID(jid));
}
Offer offer = request.getOffer();
if (offer != null) {
AgentSession agentSession = agentManager.getAgentSession(packet.getFrom());
if (agentSession == null) {
reply.setChildElement(packet.getChildElement().createCopy());
reply.setError(new PacketError(PacketError.Condition.item_not_found));
Log
.debug("Agent not found while accepting offer");
}
else {
try {
interceptorManager.invokeInterceptors(workgroup.getJID().toBareJID(),
packet, true, false);
offer.reject(agentSession);
interceptorManager.invokeInterceptors(workgroup.getJID().toBareJID(),
packet, true, true);
}
catch (PacketRejectedException e) {
workgroup.rejectPacket(packet, e);
reply = null;