Commit d3e9efe8 authored by Derek DeMoro's avatar Derek DeMoro Committed by derek

Refactoring changes


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@611 b35dd754-fafc-0310-a699-88a17e54d16e
parent 85d4b6a2
...@@ -12,16 +12,18 @@ ...@@ -12,16 +12,18 @@
package org.jivesoftware.messenger.disco; package org.jivesoftware.messenger.disco;
import org.jivesoftware.messenger.container.TrackInfo; import org.jivesoftware.messenger.container.TrackInfo;
import org.jivesoftware.messenger.forms.XDataForm; import org.jivesoftware.messenger.forms.spi.XDataFormImpl;
import org.jivesoftware.messenger.*; import org.jivesoftware.messenger.*;
import org.jivesoftware.messenger.auth.UnauthorizedException; import org.jivesoftware.messenger.auth.UnauthorizedException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import javax.xml.stream.XMLStreamException;
import org.dom4j.DocumentHelper; import org.dom4j.DocumentHelper;
import org.dom4j.Element; import org.dom4j.Element;
import org.xmpp.packet.IQ;
import org.xmpp.packet.PacketError;
import org.xmpp.packet.JID;
/** /**
* IQDiscoInfoHandler is responsible for handling disco#info requests. This class holds a map with * IQDiscoInfoHandler is responsible for handling disco#info requests. This class holds a map with
...@@ -59,40 +61,40 @@ public class IQDiscoInfoHandler extends IQDiscoHandler { ...@@ -59,40 +61,40 @@ public class IQDiscoInfoHandler extends IQDiscoHandler {
return info; return info;
} }
public IQ handleIQ(IQ packet) throws UnauthorizedException, XMLStreamException { public IQ handleIQ(IQ packet) throws UnauthorizedException {
// TODO Let configure an authorization policy (ACL?). Currently anyone can discover info. // TODO Let configure an authorization policy (ACL?). Currently anyone can discover info.
// Create a copy of the sent pack that will be used as the reply // Create a copy of the sent pack that will be used as the reply
// we only need to add the requested info to the reply if any otherwise add // we only need to add the requested info to the reply if any otherwise add
// a not found error // a not found error
IQ reply = (IQ)packet.createDeepCopy(); IQ reply = IQ.createResultIQ(packet);
reply.setType(IQ.RESULT); reply.setType(IQ.Type.result);
reply.setRecipient(packet.getSender()); reply.setTo(packet.getFrom());
reply.setSender(packet.getRecipient()); reply.setFrom(packet.getTo());
// Look for a DiscoInfoProvider associated with the requested entity. // Look for a DiscoInfoProvider associated with the requested entity.
// We consider the host of the recipient JID of the packet as the entity. It's the // We consider the host of the recipient JID of the packet as the entity. It's the
// DiscoInfoProvider responsibility to provide information about the JID's name together // DiscoInfoProvider responsibility to provide information about the JID's name together
// with any possible requested node. // with any possible requested node.
DiscoInfoProvider infoProvider = getProvider(packet.getRecipient().getHost()); DiscoInfoProvider infoProvider = getProvider(packet.getTo().getDomain());
if (infoProvider != null) { if (infoProvider != null) {
// Get the JID's name // Get the JID's name
String name = packet.getRecipient().getName(); String name = packet.getTo().getNode();
if (name == null || name.trim().length() == 0) { if (name == null || name.trim().length() == 0) {
name = null; name = null;
} }
// Get the requested node // Get the requested node
XMPPFragment iq = packet.getChildFragment(); Element iq = packet.getChildElement();
MetaDataFragment metaData = MetaDataFragment.convertToMetaData(iq); String node = iq.attributeValue("node");
String node = metaData.getProperty("query:node"); //String node = metaData.getProperty("query:node");
// Check if we have information about the requested name and node // Check if we have information about the requested name and node
if (infoProvider.hasInfo(name, node, packet.getSender())) { if (infoProvider.hasInfo(name, node, packet.getFrom())) {
Element queryElement = ((XMPPDOMFragment)reply.getChildFragment()).getRootElement(); Element queryElement = reply.getChildElement();
// Add to the reply all the identities provided by the DiscoInfoProvider // Add to the reply all the identities provided by the DiscoInfoProvider
Element identity; Element identity;
Iterator identities = infoProvider.getIdentities(name, node, packet.getSender()); Iterator identities = infoProvider.getIdentities(name, node, packet.getFrom());
while (identities.hasNext()) { while (identities.hasNext()) {
identity = (Element)identities.next(); identity = (Element)identities.next();
queryElement.add((Element)identity.clone()); queryElement.add((Element)identity.clone());
...@@ -100,7 +102,7 @@ public class IQDiscoInfoHandler extends IQDiscoHandler { ...@@ -100,7 +102,7 @@ public class IQDiscoInfoHandler extends IQDiscoHandler {
// Add to the reply all the features provided by the DiscoInfoProvider // Add to the reply all the features provided by the DiscoInfoProvider
Element featureElement; Element featureElement;
Iterator features = infoProvider.getFeatures(name, node, packet.getSender()); Iterator features = infoProvider.getFeatures(name, node, packet.getFrom());
while (features.hasNext()) { while (features.hasNext()) {
featureElement = DocumentHelper.createElement("feature"); featureElement = DocumentHelper.createElement("feature");
featureElement.addAttribute("var", (String)features.next()); featureElement.addAttribute("var", (String)features.next());
...@@ -108,7 +110,7 @@ public class IQDiscoInfoHandler extends IQDiscoHandler { ...@@ -108,7 +110,7 @@ public class IQDiscoInfoHandler extends IQDiscoHandler {
} }
// Add to the reply the extended info (XDataForm) provided by the DiscoInfoProvider // Add to the reply the extended info (XDataForm) provided by the DiscoInfoProvider
XDataForm dataForm = infoProvider.getExtendedInfo(name, node, packet.getSender()); XDataFormImpl dataForm = infoProvider.getExtendedInfo(name, node, packet.getFrom());
if (dataForm != null) { if (dataForm != null) {
queryElement.add(dataForm.asXMLElement()); queryElement.add(dataForm.asXMLElement());
} }
...@@ -116,12 +118,12 @@ public class IQDiscoInfoHandler extends IQDiscoHandler { ...@@ -116,12 +118,12 @@ public class IQDiscoInfoHandler extends IQDiscoHandler {
else { else {
// If the DiscoInfoProvider has no information for the requested name and node // If the DiscoInfoProvider has no information for the requested name and node
// then answer a not found error // then answer a not found error
reply.setError(XMPPError.Code.NOT_FOUND); reply.setError(PacketError.Condition.item_not_found);
} }
} }
else { else {
// If we didn't find a DiscoInfoProvider then answer a not found error // If we didn't find a DiscoInfoProvider then answer a not found error
reply.setError(XMPPError.Code.NOT_FOUND); reply.setError(PacketError.Condition.item_not_found);
} }
return reply; return reply;
...@@ -213,7 +215,7 @@ public class IQDiscoInfoHandler extends IQDiscoHandler { ...@@ -213,7 +215,7 @@ public class IQDiscoInfoHandler extends IQDiscoHandler {
ArrayList identities = new ArrayList(); ArrayList identities = new ArrayList();
ArrayList features = new ArrayList(); ArrayList features = new ArrayList();
public Iterator getIdentities(String name, String node, XMPPAddress senderJID) { public Iterator getIdentities(String name, String node, JID senderJID) {
synchronized (identities) { synchronized (identities) {
if (identities.isEmpty()) { if (identities.isEmpty()) {
Element identity = DocumentHelper.createElement("identity"); Element identity = DocumentHelper.createElement("identity");
...@@ -227,16 +229,16 @@ public class IQDiscoInfoHandler extends IQDiscoHandler { ...@@ -227,16 +229,16 @@ public class IQDiscoInfoHandler extends IQDiscoHandler {
return identities.iterator(); return identities.iterator();
} }
public Iterator getFeatures(String name, String node, XMPPAddress senderJID) { public Iterator getFeatures(String name, String node, JID senderJID) {
return serverFeatures.iterator(); return serverFeatures.iterator();
} }
public boolean hasInfo(String name, String node, XMPPAddress senderJID) public boolean hasInfo(String name, String node, JID senderJID)
throws UnauthorizedException { throws UnauthorizedException {
return name == null && node == null; return name == null && node == null;
} }
public XDataForm getExtendedInfo(String name, String node, XMPPAddress senderJID) { public XDataFormImpl getExtendedInfo(String name, String node, JID senderJID) {
return null; return null;
} }
}; };
......
...@@ -11,18 +11,20 @@ ...@@ -11,18 +11,20 @@
package org.jivesoftware.messenger.disco; package org.jivesoftware.messenger.disco;
import org.jivesoftware.messenger.container.TrackInfo;
import org.jivesoftware.messenger.*;
import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.util.StringUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import javax.xml.stream.XMLStreamException;
import org.dom4j.DocumentHelper; import org.dom4j.DocumentHelper;
import org.dom4j.Element; import org.dom4j.Element;
import org.jivesoftware.messenger.IQHandlerInfo;
import org.jivesoftware.messenger.XMPPServer;
import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.messenger.container.TrackInfo;
import org.jivesoftware.util.StringUtils;
import org.xmpp.packet.IQ;
import org.xmpp.packet.JID;
import org.xmpp.packet.PacketError;
/** /**
* IQDiscoItemsHandler is responsible for handling disco#items requests. This class holds a map with * IQDiscoItemsHandler is responsible for handling disco#items requests. This class holds a map with
...@@ -62,20 +64,20 @@ public class IQDiscoItemsHandler extends IQDiscoHandler implements ServerFeature ...@@ -62,20 +64,20 @@ public class IQDiscoItemsHandler extends IQDiscoHandler implements ServerFeature
return info; return info;
} }
public IQ handleIQ(IQ packet) throws UnauthorizedException, XMLStreamException { public IQ handleIQ(IQ packet) throws UnauthorizedException {
// TODO Let configure an authorization policy (ACL?). Currently anyone can discover items. // TODO Let configure an authorization policy (ACL?). Currently anyone can discover items.
// Create a copy of the sent pack that will be used as the reply // Create a copy of the sent pack that will be used as the reply
// we only need to add the requested items to the reply if any otherwise add // we only need to add the requested items to the reply if any otherwise add
// a not found error // a not found error
IQ reply = (IQ)packet.createDeepCopy(); IQ reply = IQ.createResultIQ(packet);
reply.setType(IQ.RESULT); reply.setType(IQ.Type.result);
reply.setRecipient(packet.getSender()); reply.setTo(packet.getFrom());
reply.setSender(packet.getRecipient()); reply.setFrom(packet.getFrom());
// TODO Implement publishing client items // TODO Implement publishing client items
if (IQ.SET == packet.getType()) { if (IQ.Type.set == packet.getType()) {
reply.setError(XMPPError.Code.NOT_IMPLEMENTED); reply.setError(PacketError.Condition.feature_not_implemented);
return reply; return reply;
} }
...@@ -83,22 +85,22 @@ public class IQDiscoItemsHandler extends IQDiscoHandler implements ServerFeature ...@@ -83,22 +85,22 @@ public class IQDiscoItemsHandler extends IQDiscoHandler implements ServerFeature
// We consider the host of the recipient JID of the packet as the entity. It's the // We consider the host of the recipient JID of the packet as the entity. It's the
// DiscoItemsProvider responsibility to provide the items associated with the JID's name // DiscoItemsProvider responsibility to provide the items associated with the JID's name
// together with any possible requested node. // together with any possible requested node.
DiscoItemsProvider itemsProvider = getProvider(packet.getRecipient().getHost()); DiscoItemsProvider itemsProvider = getProvider(packet.getTo().getDomain());
if (itemsProvider != null) { if (itemsProvider != null) {
// Get the JID's name // Get the JID's name
String name = packet.getRecipient().getName(); String name = packet.getTo().getNode();
if (name == null || name.trim().length() == 0) { if (name == null || name.trim().length() == 0) {
name = null; name = null;
} }
// Get the requested node // Get the requested node
XMPPFragment iq = packet.getChildFragment(); Element iq = packet.getChildElement();
MetaDataFragment metaData = MetaDataFragment.convertToMetaData(iq); String node = iq.attributeValue("node");
String node = metaData.getProperty("query:node"); //String node = metaData.getProperty("query:node");
// Check if we have items associated with the requested name and node // Check if we have items associated with the requested name and node
Iterator itemsItr = itemsProvider.getItems(name, node, packet.getSender()); Iterator itemsItr = itemsProvider.getItems(name, node, packet.getFrom());
if (itemsItr != null) { if (itemsItr != null) {
Element queryElement = ((XMPPDOMFragment)reply.getChildFragment()).getRootElement(); Element queryElement = reply.getChildElement();
// Add to the reply all the items provided by the DiscoItemsProvider // Add to the reply all the items provided by the DiscoItemsProvider
Element item; Element item;
...@@ -111,12 +113,12 @@ public class IQDiscoItemsHandler extends IQDiscoHandler implements ServerFeature ...@@ -111,12 +113,12 @@ public class IQDiscoItemsHandler extends IQDiscoHandler implements ServerFeature
else { else {
// If the DiscoItemsProvider has no items for the requested name and node // If the DiscoItemsProvider has no items for the requested name and node
// then answer a not found error // then answer a not found error
reply.setError(XMPPError.Code.NOT_FOUND); reply.setError(PacketError.Condition.item_not_found);
} }
} }
else { else {
// If we didn't find a DiscoItemsProvider then answer a not found error // If we didn't find a DiscoItemsProvider then answer a not found error
reply.setError(XMPPError.Code.NOT_FOUND); reply.setError(PacketError.Condition.item_not_found);
} }
return reply; return reply;
...@@ -239,7 +241,7 @@ public class IQDiscoItemsHandler extends IQDiscoHandler implements ServerFeature ...@@ -239,7 +241,7 @@ public class IQDiscoItemsHandler extends IQDiscoHandler implements ServerFeature
private DiscoItemsProvider getServerItemsProvider() { private DiscoItemsProvider getServerItemsProvider() {
DiscoItemsProvider discoItemsProvider = new DiscoItemsProvider() { DiscoItemsProvider discoItemsProvider = new DiscoItemsProvider() {
public Iterator getItems(String name, String node, XMPPAddress senderJID) public Iterator getItems(String name, String node, JID senderJID)
throws UnauthorizedException { throws UnauthorizedException {
return serverItems.iterator(); return serverItems.iterator();
} }
......
...@@ -15,6 +15,10 @@ import org.jivesoftware.util.LocaleUtils; ...@@ -15,6 +15,10 @@ import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log; import org.jivesoftware.util.Log;
import org.jivesoftware.messenger.*; import org.jivesoftware.messenger.*;
import org.jivesoftware.messenger.auth.UnauthorizedException; import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.xmpp.packet.Packet;
import org.xmpp.packet.JID;
import org.xmpp.packet.Message;
import org.xmpp.packet.Presence;
/** /**
* This ChannelHandler writes packet data to connections. * This ChannelHandler writes packet data to connections.
...@@ -32,11 +36,11 @@ public class SocketPacketWriteHandler implements ChannelHandler { ...@@ -32,11 +36,11 @@ public class SocketPacketWriteHandler implements ChannelHandler {
this.messageStrategy = messageStrategy; this.messageStrategy = messageStrategy;
} }
public void process(XMPPPacket packet) throws UnauthorizedException, PacketException { public void process(Packet packet) throws UnauthorizedException, PacketException {
try { try {
XMPPAddress recipient = packet.getRecipient(); JID recipient = packet.getTo();
Session senderSession = packet.getOriginatingSession(); Session senderSession = sessionManager.getSession(packet.getFrom());
if (recipient == null || (recipient.getName() == null && recipient.getResource() == null)) { if (recipient == null || (recipient.getNode() == null && recipient.getResource() == null)) {
if (senderSession != null && !senderSession.getConnection().isClosed()) { if (senderSession != null && !senderSession.getConnection().isClosed()) {
senderSession.getConnection().deliver(packet); senderSession.getConnection().deliver(packet);
} }
...@@ -79,7 +83,7 @@ public class SocketPacketWriteHandler implements ChannelHandler { ...@@ -79,7 +83,7 @@ public class SocketPacketWriteHandler implements ChannelHandler {
* *
* @param packet The packet being dropped * @param packet The packet being dropped
*/ */
private void dropPacket(XMPPPacket packet) { private void dropPacket(Packet packet) {
Log.warn(LocaleUtils.getLocalizedString("admin.error.routing") + "\n" + packet.toString()); Log.warn(LocaleUtils.getLocalizedString("admin.error.routing") + "\n" + packet.toString());
} }
} }
...@@ -17,14 +17,14 @@ import org.jivesoftware.util.Log; ...@@ -17,14 +17,14 @@ import org.jivesoftware.util.Log;
import org.jivesoftware.messenger.*; import org.jivesoftware.messenger.*;
import org.jivesoftware.messenger.auth.UnauthorizedException; import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.messenger.transport.TransportHandler; import org.jivesoftware.messenger.transport.TransportHandler;
import javax.xml.stream.XMLStreamException; import org.xmpp.packet.Packet;
/** /**
* In-memory implementation of the packet transporter service. * In-memory implementation of the packet transporter service.
* *
* @author Iain Shigeoka * @author Iain Shigeoka
*/ */
public class PacketTransporterImpl extends BasicModule implements PacketTransporter { public class PacketTransporterImpl extends BasicModule {
/** /**
* The handler that does the actual delivery (could be a channel instead) * The handler that does the actual delivery (could be a channel instead)
...@@ -71,13 +71,12 @@ public class PacketTransporterImpl extends BasicModule implements PacketTranspor ...@@ -71,13 +71,12 @@ public class PacketTransporterImpl extends BasicModule implements PacketTranspor
* @throws NullPointerException If the packet is null or the * @throws NullPointerException If the packet is null or the
* packet could not be routed * packet could not be routed
*/ */
public void deliver(XMPPPacket packet) public void deliver(Packet packet) throws UnauthorizedException, PacketException {
throws UnauthorizedException, PacketException, XMLStreamException {
if (packet == null) { if (packet == null) {
throw new NullPointerException(); throw new NullPointerException();
} }
if (xmppServer != null && xmppServer.isLocal(packet.getRecipient())) { if (xmppServer != null && xmppServer.isLocal(packet.getTo())) {
deliverer.deliver(packet); deliverer.deliver(packet);
} }
else if (transportHandler != null) { else if (transportHandler != null) {
......
...@@ -13,10 +13,6 @@ package org.jivesoftware.messenger.user; ...@@ -13,10 +13,6 @@ package org.jivesoftware.messenger.user;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
import org.dom4j.Document; import org.dom4j.Document;
import org.dom4j.DocumentException; import org.dom4j.DocumentException;
import org.dom4j.Element; import org.dom4j.Element;
...@@ -51,30 +47,6 @@ public class IQRoster extends IQ implements Roster { ...@@ -51,30 +47,6 @@ public class IQRoster extends IQ implements Roster {
return "query"; return "query";
} }
public void send(XMLStreamWriter xmlSerializer, int version) throws
XMLStreamException {
try {
super.sendRoot(xmlSerializer, version, "iq", null);
xmlSerializer.setPrefix("", "jabber:iq:roster");
xmlSerializer.writeStartElement("query");
xmlSerializer.writeDefaultNamespace("jabber:iq:roster");
Iterator items = basicRoster.getRosterItems();
while (items.hasNext()) {
Object item = items.next();
if (item instanceof IQRosterItem) {
((IQRosterItem)item).send(xmlSerializer, version);
}
else {
new IQRosterItemImpl((RosterItem)item).send(xmlSerializer, version);
}
}
xmlSerializer.writeEndElement();
xmlSerializer.writeEndElement();
}
catch (UnauthorizedException e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
}
}
public void parse(Document doc) { public void parse(Document doc) {
Element root = doc.getRootElement(); Element root = doc.getRootElement();
...@@ -111,31 +83,7 @@ public class IQRoster extends IQ implements Roster { ...@@ -111,31 +83,7 @@ public class IQRoster extends IQ implements Roster {
} }
} }
public void parse(XMLStreamReader xpp) throws XMLStreamException {
// We're one past the root iq-element
int event = xpp.getEventType();
Document doc = null;
// The one query element or the error element
if (event == XMLStreamConstants.START_ELEMENT) {
if ("query".equals(xpp.getLocalName())) {
}
else {
// error, we'll punt and implement later
throw new XMLStreamException("Error packets not supported yet");
}
try {
doc = XPPReader.parseDocument(xpp);
this.setChildElement(doc.getRootElement().getName(), doc.getRootElement().getNamespaceURI());
}
catch (DocumentException e) {
throw new XMLStreamException();
}
}
while (event != XMLStreamConstants.END_ELEMENT) {
event = xpp.next();
}
}
// ################################################################################## // ##################################################################################
// Basic Roster usage - the downside of single inheritance // Basic Roster usage - the downside of single inheritance
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment