use of org.xmpp.packet.PacketError in project Openfire by igniterealtime.
the class WorkgroupIQHandler method handleIQSet.
private void handleIQSet(IQ packet) {
IQ reply;
// TODO: verify namespace and send error if wrong
Element iq = packet.getChildElement();
JID sender = packet.getFrom();
reply = IQ.createResultIQ(packet);
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;
}
}
}
}
} catch (NotFoundException e) {
reply.setChildElement(packet.getChildElement().createCopy());
reply.setError(new PacketError(PacketError.Condition.item_not_found));
Log.debug("Request not found " + "while rejecting 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 ("invite".equals(queryName)) {
// Get the type of inviation (i.e. entity type is being invited)
InvitationRequest request = new InvitationRequest(packet, workgroup);
workgroup.processInvitation(request, packet);
reply = null;
} else if ("transfer".equals(queryName)) {
// Get the type of transfer (i.e. entity type is going to get the transfer offer)
TransferRequest request = new TransferRequest(packet, workgroup);
workgroup.processTransfer(request, packet);
reply = null;
} else if ("jabber:iq:private".equals(queryNamespace)) {
// IQ private for agents global macro storage
setIQPrivate(packet);
} else if ("agent-info".equals(queryName)) {
if (!JiveGlobals.getBooleanProperty("xmpp.live.agent.change-properties", true)) {
// Answer that agents are not allowed to change their properties (feature disabled)
reply = IQ.createResultIQ(packet);
reply.setChildElement(packet.getChildElement().createCopy());
reply.setError(new PacketError(PacketError.Condition.service_unavailable));
} else {
try {
AgentSession agentSession = agentManager.getAgentSession(packet.getFrom());
if (agentSession == null) {
reply = IQ.createResultIQ(packet);
reply.setChildElement(packet.getChildElement().createCopy());
reply.setError(new PacketError(PacketError.Condition.item_not_found));
} else {
String allowsToChange = agentSession.getAgent().getProperties().getProperty("change-properties");
if (!"false".equals(allowsToChange)) {
// Set the new agent's info
agentSession.getAgent().updateAgentInfo(packet);
} else {
// Answer that this agent is not allowed to change his properties
reply = IQ.createResultIQ(packet);
reply.setChildElement(packet.getChildElement().createCopy());
reply.setError(new PacketError(PacketError.Condition.service_unavailable));
}
}
} catch (AgentNotFoundException e) {
reply = IQ.createResultIQ(packet);
reply.setChildElement(packet.getChildElement().createCopy());
reply.setError(new PacketError(PacketError.Condition.item_not_found));
}
}
} else {
// none are found, send bad request error.
for (WorkgroupProvider provider : providerManager.getWorkgroupProviders()) {
// Handle packet?
if (provider.handleSet(packet)) {
// If provider accepts responsibility, hand off packet.
provider.executeSet(packet, workgroup);
return;
}
}
dropPacket(packet);
reply.setChildElement(packet.getChildElement().createCopy());
reply.setError(new PacketError(PacketError.Condition.bad_request));
}
if (reply != null) {
workgroup.send(reply);
}
}
use of org.xmpp.packet.PacketError in project Openfire by igniterealtime.
the class WorkgroupManager method process.
/**
* Returns true if the IQ packet was processed. This method should only process disco packets
* sent to the workgroup service.
*
* @param iq the IQ packet to process.
* @return true if the IQ packet was processed.
*/
private boolean process(IQ iq) {
if (iq.getType() == IQ.Type.error) {
// Skip IQ packets of type error
return false;
}
Element childElement = iq.getChildElement();
String name = null;
String namespace = null;
if (childElement != null) {
namespace = childElement.getNamespaceURI();
name = childElement.getName();
}
if ("http://jabber.org/protocol/disco#info".equals(namespace)) {
IQ reply = iqDiscoInfoHandler.handleIQ(iq);
if (reply != null) {
send(reply);
}
} else if ("http://jabber.org/protocol/disco#items".equals(namespace)) {
IQ reply = iqDiscoItemsHandler.handleIQ(iq);
if (reply != null) {
send(reply);
}
} else if ("jabber:iq:version".equals(namespace)) {
IQ reply = IQ.createResultIQ(iq);
Element version = reply.setChildElement("query", "jabber:iq:version");
version.addElement("name").setText("Spark Fastpath");
version.addElement("version").setText("3.2");
version.addElement("os").setText("Java 5");
send(reply);
} else if ("workgroups".equals(name)) {
try {
// Check that the sender of this IQ is an agent
getAgentManager().getAgent(iq.getFrom());
// Get the agent JID to return his workgroups
String agentJID = childElement.attributeValue("jid");
try {
// Answer the workgroups where the agent can work in
Agent agent = getAgentManager().getAgent(new JID(agentJID));
sendWorkgroups(iq, agent);
} catch (AgentNotFoundException e) {
IQ reply = IQ.createResultIQ(iq);
reply.setChildElement(iq.getChildElement().createCopy());
reply.setError(new PacketError(PacketError.Condition.item_not_found));
send(reply);
}
} catch (AgentNotFoundException e) {
IQ reply = IQ.createResultIQ(iq);
reply.setChildElement(iq.getChildElement().createCopy());
reply.setError(new PacketError(PacketError.Condition.not_authorized));
send(reply);
}
} else if ("transcript-search".equals(name)) {
iqChatSearchHandler.handleIQ(iq);
} else if ("http://jabber.org/protocol/commands".equals(namespace)) {
// Process ad-hoc command
IQ reply = commandManager.process(iq);
send(reply);
} else {
return false;
}
return true;
}
use of org.xmpp.packet.PacketError in project Openfire by igniterealtime.
the class WorkgroupPresence method process.
public void process(Presence packet) {
try {
JID sender = packet.getFrom();
// Handling Subscription
if (Presence.Type.subscribe == packet.getType()) {
// Only create item if user was not already subscribed to workgroup's presence
if (!presenceSubscribers.contains(sender.toBareJID())) {
createRosterItem(sender);
}
// Reply that the subscription request was approved
Presence reply = new Presence();
reply.setTo(sender);
reply.setFrom(workgroup.getJID());
reply.setType(Presence.Type.subscribed);
workgroup.send(reply);
// Send the presence of the workgroup to the user that requested it
sendPresence(packet.getFrom());
} else if (Presence.Type.unsubscribe == packet.getType()) {
// Remove sender from the workgroup roster
deleteRosterItem(sender);
// Send confirmation of unsubscription
Presence reply = new Presence();
reply.setTo(sender);
reply.setFrom(workgroup.getJID());
reply.setType(Presence.Type.unsubscribed);
workgroup.send(reply);
// Send unavailable presence of the workgroup
reply = new Presence();
reply.setTo(sender);
reply.setFrom(workgroup.getJID());
reply.setType(Presence.Type.unavailable);
workgroup.send(reply);
} else if (Presence.Type.subscribed == packet.getType()) {
// ignore
} else if (Presence.Type.unsubscribed == packet.getType()) {
// ignore
} else if (Presence.Type.probe == packet.getType()) {
// Send the presence of the workgroup to the user that requested it
sendPresence(packet.getFrom());
} else {
try {
agentToWorkgroup(packet);
} catch (AgentNotFoundException e) {
Presence reply = new Presence();
reply.setError(new PacketError(PacketError.Condition.not_authorized));
reply.setTo(packet.getFrom());
reply.setFrom(workgroup.getJID());
workgroup.send(reply);
StringBuilder errorMessage = new StringBuilder();
errorMessage.append("Sender: ");
errorMessage.append(packet.getFrom().toString());
errorMessage.append(" Workgroup: ");
errorMessage.append(workgroup.getJID().toString());
Log.debug(errorMessage.toString(), e);
}
}
} catch (Exception e) {
Log.error(e.getMessage(), e);
Presence reply = new Presence();
reply.setError(new PacketError(PacketError.Condition.internal_server_error));
reply.setTo(packet.getFrom());
reply.setFrom(workgroup.getJID());
workgroup.send(reply);
}
}
use of org.xmpp.packet.PacketError in project Openfire by igniterealtime.
the class WorkgroupStats method getChatTranscript.
public void getChatTranscript(IQ iq, String sessionID) {
final IQ reply = IQ.createResultIQ(iq);
String transcriptXML = null;
try {
Element transcript = reply.setChildElement("transcript", "http://jivesoftware.com/protocol/workgroup");
transcript.addAttribute("sessionID", sessionID);
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(GET_TRANSCRIPT);
pstmt.setString(1, sessionID);
rs = pstmt.executeQuery();
if (rs.next()) {
transcriptXML = DbConnectionManager.getLargeTextField(rs, 1);
}
} catch (SQLException sqle) {
Log.error(sqle.getMessage(), sqle);
} finally {
DbConnectionManager.closeConnection(rs, pstmt, con);
}
if (transcriptXML != null) {
Document element = DocumentHelper.parseText(transcriptXML);
// Add the Messages and Presences contained in the retrieved transcript element
for (Iterator<Element> it = element.getRootElement().elementIterator(); it.hasNext(); ) {
Element packet = it.next();
transcript.add(packet.createCopy());
}
}
workgroup.send(reply);
} catch (Exception ex) {
Log.error("There was an error retrieving the following transcript. SessionID = " + sessionID + " Transcript=" + transcriptXML, ex);
reply.setChildElement(iq.getChildElement().createCopy());
reply.setError(new PacketError(PacketError.Condition.item_not_found));
workgroup.send(reply);
}
}
use of org.xmpp.packet.PacketError in project Openfire by igniterealtime.
the class IQChatSearchHandler method handleIQ.
public void handleIQ(IQ packet) {
try {
// Check that the sender of this IQ is an agent
workgroupManager.getAgentManager().getAgent(packet.getFrom());
Element iq = packet.getChildElement();
IQ reply = IQ.createResultIQ(packet);
if (iq.elements().isEmpty()) {
reply.setChildElement(iq.createCopy());
// Send the search form to the agent
reply.addExtension(searchForm.createCopy());
workgroupManager.send(reply);
} else {
// Send the result of the search to the agent
Date startDate = null;
Date endDate = null;
Collection<Workgroup> workgroups = WorkgroupManager.getInstance().getWorkgroups();
JID agentJID = null;
String queryString = null;
// Get the search parameters from the completed form
DataForm submitedForm = (DataForm) packet.getExtension(DataForm.ELEMENT_NAME, DataForm.NAMESPACE);
for (FormField field : submitedForm.getFields()) {
if ("date/start".equals(field.getVariable())) {
try {
startDate = DataForm.parseDate(field.getValues().get(0));
} catch (ParseException e) {
Log.debug("Invalid startDate " + field.getValues().get(0), e);
}
} else if ("date/end".equals(field.getVariable())) {
try {
endDate = DataForm.parseDate(field.getValues().get(0));
} catch (ParseException e) {
Log.debug("Invalid endDate " + field.getValues().get(0), e);
}
} else if ("workgroups".equals(field.getVariable())) {
if (!field.getValues().isEmpty()) {
workgroups = new ArrayList<Workgroup>();
for (String value : field.getValues()) {
try {
workgroups.add(WorkgroupManager.getInstance().getWorkgroup(new JID(value)));
} catch (UserNotFoundException e) {
Log.debug("Invalid workgroup JID " + value, e);
}
}
} else {
// Search in all the workgroups since no one was specified
workgroups = WorkgroupManager.getInstance().getWorkgroups();
}
} else if ("agent".equals(field.getVariable())) {
agentJID = new JID(field.getValues().get(0));
} else if ("queryString".equals(field.getVariable())) {
queryString = field.getValues().get(0);
}
}
// Build the response
DataForm searchResults = resultForm.createCopy();
// Perform the search
for (Workgroup workgroup : workgroups) {
ChatSearch search = new ChatSearch(workgroup, startDate, endDate, agentJID, queryString);
for (QueryResult result : search.getResults()) {
Map<String, Object> fields = new LinkedHashMap<String, Object>();
fields.put("workgroup", result.getWorkgroup().getJID().toBareJID());
fields.put("sessionID", result.getSessionID());
fields.put("startDate", result.getStartDate());
fields.put("agentJIDs", result.getAgentJIDs());
fields.put("relevance", result.getRelevance());
// Add Metadata
Map<String, String> metadata = getMetadataMap(result.getSessionID());
if (metadata.containsKey("question")) {
fields.put("question", metadata.get("question"));
}
if (metadata.containsKey("email")) {
fields.put("email", metadata.get("email"));
}
if (metadata.containsKey("username")) {
fields.put("username", metadata.get("username"));
}
searchResults.addItemFields(fields);
}
}
reply.setChildElement(iq.getName(), iq.getNamespaceURI());
reply.addExtension(searchResults);
workgroupManager.send(reply);
}
} catch (AgentNotFoundException e) {
IQ reply = IQ.createResultIQ(packet);
reply.setChildElement(packet.getChildElement().createCopy());
reply.setError(new PacketError(PacketError.Condition.not_authorized));
workgroupManager.send(reply);
}
}
Aggregations