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 @@
package org.jivesoftware.messenger.disco;
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.auth.UnauthorizedException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.xml.stream.XMLStreamException;
import org.dom4j.DocumentHelper;
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
......@@ -59,40 +61,40 @@ public class IQDiscoInfoHandler extends IQDiscoHandler {
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.
// 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
// a not found error
IQ reply = (IQ)packet.createDeepCopy();
reply.setType(IQ.RESULT);
reply.setRecipient(packet.getSender());
reply.setSender(packet.getRecipient());
IQ reply = IQ.createResultIQ(packet);
reply.setType(IQ.Type.result);
reply.setTo(packet.getFrom());
reply.setFrom(packet.getTo());
// 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
// DiscoInfoProvider responsibility to provide information about the JID's name together
// with any possible requested node.
DiscoInfoProvider infoProvider = getProvider(packet.getRecipient().getHost());
DiscoInfoProvider infoProvider = getProvider(packet.getTo().getDomain());
if (infoProvider != null) {
// Get the JID's name
String name = packet.getRecipient().getName();
String name = packet.getTo().getNode();
if (name == null || name.trim().length() == 0) {
name = null;
}
// Get the requested node
XMPPFragment iq = packet.getChildFragment();
MetaDataFragment metaData = MetaDataFragment.convertToMetaData(iq);
String node = metaData.getProperty("query:node");
Element iq = packet.getChildElement();
String node = iq.attributeValue("node");
//String node = metaData.getProperty("query:node");
// Check if we have information about the requested name and node
if (infoProvider.hasInfo(name, node, packet.getSender())) {
Element queryElement = ((XMPPDOMFragment)reply.getChildFragment()).getRootElement();
if (infoProvider.hasInfo(name, node, packet.getFrom())) {
Element queryElement = reply.getChildElement();
// Add to the reply all the identities provided by the DiscoInfoProvider
Element identity;
Iterator identities = infoProvider.getIdentities(name, node, packet.getSender());
Iterator identities = infoProvider.getIdentities(name, node, packet.getFrom());
while (identities.hasNext()) {
identity = (Element)identities.next();
queryElement.add((Element)identity.clone());
......@@ -100,7 +102,7 @@ public class IQDiscoInfoHandler extends IQDiscoHandler {
// Add to the reply all the features provided by the DiscoInfoProvider
Element featureElement;
Iterator features = infoProvider.getFeatures(name, node, packet.getSender());
Iterator features = infoProvider.getFeatures(name, node, packet.getFrom());
while (features.hasNext()) {
featureElement = DocumentHelper.createElement("feature");
featureElement.addAttribute("var", (String)features.next());
......@@ -108,7 +110,7 @@ public class IQDiscoInfoHandler extends IQDiscoHandler {
}
// 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) {
queryElement.add(dataForm.asXMLElement());
}
......@@ -116,12 +118,12 @@ public class IQDiscoInfoHandler extends IQDiscoHandler {
else {
// If the DiscoInfoProvider has no information for the requested name and node
// then answer a not found error
reply.setError(XMPPError.Code.NOT_FOUND);
reply.setError(PacketError.Condition.item_not_found);
}
}
else {
// 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;
......@@ -213,7 +215,7 @@ public class IQDiscoInfoHandler extends IQDiscoHandler {
ArrayList identities = 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) {
if (identities.isEmpty()) {
Element identity = DocumentHelper.createElement("identity");
......@@ -227,16 +229,16 @@ public class IQDiscoInfoHandler extends IQDiscoHandler {
return identities.iterator();
}
public Iterator getFeatures(String name, String node, XMPPAddress senderJID) {
public Iterator getFeatures(String name, String node, JID senderJID) {
return serverFeatures.iterator();
}
public boolean hasInfo(String name, String node, XMPPAddress senderJID)
public boolean hasInfo(String name, String node, JID senderJID)
throws UnauthorizedException {
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;
}
};
......
......@@ -11,18 +11,20 @@
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.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.xml.stream.XMLStreamException;
import org.dom4j.DocumentHelper;
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
......@@ -62,20 +64,20 @@ public class IQDiscoItemsHandler extends IQDiscoHandler implements ServerFeature
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.
// 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
// a not found error
IQ reply = (IQ)packet.createDeepCopy();
reply.setType(IQ.RESULT);
reply.setRecipient(packet.getSender());
reply.setSender(packet.getRecipient());
IQ reply = IQ.createResultIQ(packet);
reply.setType(IQ.Type.result);
reply.setTo(packet.getFrom());
reply.setFrom(packet.getFrom());
// TODO Implement publishing client items
if (IQ.SET == packet.getType()) {
reply.setError(XMPPError.Code.NOT_IMPLEMENTED);
if (IQ.Type.set == packet.getType()) {
reply.setError(PacketError.Condition.feature_not_implemented);
return reply;
}
......@@ -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
// DiscoItemsProvider responsibility to provide the items associated with the JID's name
// together with any possible requested node.
DiscoItemsProvider itemsProvider = getProvider(packet.getRecipient().getHost());
DiscoItemsProvider itemsProvider = getProvider(packet.getTo().getDomain());
if (itemsProvider != null) {
// Get the JID's name
String name = packet.getRecipient().getName();
String name = packet.getTo().getNode();
if (name == null || name.trim().length() == 0) {
name = null;
}
// Get the requested node
XMPPFragment iq = packet.getChildFragment();
MetaDataFragment metaData = MetaDataFragment.convertToMetaData(iq);
String node = metaData.getProperty("query:node");
Element iq = packet.getChildElement();
String node = iq.attributeValue("node");
//String node = metaData.getProperty("query: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) {
Element queryElement = ((XMPPDOMFragment)reply.getChildFragment()).getRootElement();
Element queryElement = reply.getChildElement();
// Add to the reply all the items provided by the DiscoItemsProvider
Element item;
......@@ -111,12 +113,12 @@ public class IQDiscoItemsHandler extends IQDiscoHandler implements ServerFeature
else {
// If the DiscoItemsProvider has no items for the requested name and node
// then answer a not found error
reply.setError(XMPPError.Code.NOT_FOUND);
reply.setError(PacketError.Condition.item_not_found);
}
}
else {
// 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;
......@@ -239,7 +241,7 @@ public class IQDiscoItemsHandler extends IQDiscoHandler implements ServerFeature
private DiscoItemsProvider getServerItemsProvider() {
DiscoItemsProvider discoItemsProvider = new DiscoItemsProvider() {
public Iterator getItems(String name, String node, XMPPAddress senderJID)
public Iterator getItems(String name, String node, JID senderJID)
throws UnauthorizedException {
return serverItems.iterator();
}
......
......@@ -15,6 +15,10 @@ import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
import org.jivesoftware.messenger.*;
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.
......@@ -32,11 +36,11 @@ public class SocketPacketWriteHandler implements ChannelHandler {
this.messageStrategy = messageStrategy;
}
public void process(XMPPPacket packet) throws UnauthorizedException, PacketException {
public void process(Packet packet) throws UnauthorizedException, PacketException {
try {
XMPPAddress recipient = packet.getRecipient();
Session senderSession = packet.getOriginatingSession();
if (recipient == null || (recipient.getName() == null && recipient.getResource() == null)) {
JID recipient = packet.getTo();
Session senderSession = sessionManager.getSession(packet.getFrom());
if (recipient == null || (recipient.getNode() == null && recipient.getResource() == null)) {
if (senderSession != null && !senderSession.getConnection().isClosed()) {
senderSession.getConnection().deliver(packet);
}
......@@ -79,7 +83,7 @@ public class SocketPacketWriteHandler implements ChannelHandler {
*
* @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());
}
}
......@@ -17,14 +17,14 @@ import org.jivesoftware.util.Log;
import org.jivesoftware.messenger.*;
import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.messenger.transport.TransportHandler;
import javax.xml.stream.XMLStreamException;
import org.xmpp.packet.Packet;
/**
* In-memory implementation of the packet transporter service.
*
* @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)
......@@ -71,13 +71,12 @@ public class PacketTransporterImpl extends BasicModule implements PacketTranspor
* @throws NullPointerException If the packet is null or the
* packet could not be routed
*/
public void deliver(XMPPPacket packet)
throws UnauthorizedException, PacketException, XMLStreamException {
public void deliver(Packet packet) throws UnauthorizedException, PacketException {
if (packet == null) {
throw new NullPointerException();
}
if (xmppServer != null && xmppServer.isLocal(packet.getRecipient())) {
if (xmppServer != null && xmppServer.isLocal(packet.getTo())) {
deliverer.deliver(packet);
}
else if (transportHandler != null) {
......
......@@ -13,10 +13,6 @@ package org.jivesoftware.messenger.user;
import java.util.Iterator;
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.DocumentException;
import org.dom4j.Element;
......@@ -51,30 +47,6 @@ public class IQRoster extends IQ implements Roster {
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) {
Element root = doc.getRootElement();
......@@ -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
......
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