Commit 17f199b4 authored by Daniel Henninger's avatar Daniel Henninger Committed by dhenninger

Some more code cleanup.

git-svn-id: http://svn.igniterealtime.org/svn/repos/wildfire/trunk@4383 b35dd754-fafc-0310-a699-88a17e54d16e
parent 61a17711
...@@ -30,34 +30,34 @@ public abstract class AbstractGatewaySession implements GatewaySession, Endpoint ...@@ -30,34 +30,34 @@ public abstract class AbstractGatewaySession implements GatewaySession, Endpoint
this.gateway = gateway; this.gateway = gateway;
this.subscription = info; this.subscription = info;
} }
/** /**
* The gateway. * The gateway.
*/ */
protected transient Gateway gateway; protected transient Gateway gateway;
/** /**
* Has the client registered with the gateway? * Has the client registered with the gateway?
*/ */
public boolean clientRegistered; public boolean clientRegistered;
/** /**
* Has the server attempted to register with the client? * Has the server attempted to register with the client?
*/ */
public boolean serverRegistered; public boolean serverRegistered;
/** /**
* The subscriptionInfo. * The subscriptionInfo.
* @see org.jivesoftware.wildfire.gateway.SubscriptionInfo * @see org.jivesoftware.wildfire.gateway.SubscriptionInfo
*/ */
private final SubscriptionInfo subscription; private final SubscriptionInfo subscription;
/** /**
* The jabber endpoint. * The jabber endpoint.
* @see org.jivesoftware.wildfire.gateway.Endpoint * @see org.jivesoftware.wildfire.gateway.Endpoint
*/ */
private Endpoint jabberEndpoint; private Endpoint jabberEndpoint;
/** /**
* Set the Jabber <code>Endpoint</code>. * Set the Jabber <code>Endpoint</code>.
* *
...@@ -66,17 +66,17 @@ public abstract class AbstractGatewaySession implements GatewaySession, Endpoint ...@@ -66,17 +66,17 @@ public abstract class AbstractGatewaySession implements GatewaySession, Endpoint
public void setJabberEndpoint(Endpoint jabberEndpoint) { public void setJabberEndpoint(Endpoint jabberEndpoint) {
this.jabberEndpoint = jabberEndpoint; this.jabberEndpoint = jabberEndpoint;
} }
/** /**
* Return the jabber <code>Endpoint</code>. * Return the Jabber <code>Endpoint</code>.
* *
* @return endpoint The jabber endpoint. * @return endpoint The Jabber endpoint.
* @see org.jivesoftware.wildfire.gateway.Endpoint * @see org.jivesoftware.wildfire.gateway.Endpoint
*/ */
public Endpoint getJabberEndpoint() { public Endpoint getJabberEndpoint() {
return jabberEndpoint; return jabberEndpoint;
} }
/** /**
* Return the legacy <code>Endpoint</code>. * Return the legacy <code>Endpoint</code>.
* *
...@@ -86,7 +86,7 @@ public abstract class AbstractGatewaySession implements GatewaySession, Endpoint ...@@ -86,7 +86,7 @@ public abstract class AbstractGatewaySession implements GatewaySession, Endpoint
public Endpoint getLegacyEndpoint() { public Endpoint getLegacyEndpoint() {
return this; return this;
} }
/** /**
* Return the <code>SubscriptionInfo</code> * Return the <code>SubscriptionInfo</code>
* *
...@@ -97,7 +97,7 @@ public abstract class AbstractGatewaySession implements GatewaySession, Endpoint ...@@ -97,7 +97,7 @@ public abstract class AbstractGatewaySession implements GatewaySession, Endpoint
public SubscriptionInfo getSubscriptionInfo() { public SubscriptionInfo getSubscriptionInfo() {
return this.subscription; return this.subscription;
} }
/** /**
* Return the gateway associated with this session. * Return the gateway associated with this session.
* *
...@@ -114,4 +114,5 @@ public abstract class AbstractGatewaySession implements GatewaySession, Endpoint ...@@ -114,4 +114,5 @@ public abstract class AbstractGatewaySession implements GatewaySession, Endpoint
public EndpointValve getValve() { public EndpointValve getValve() {
return this.jabberEndpoint.getValve(); return this.jabberEndpoint.getValve();
} }
} }
/**
* $Revision$
* $Date$
*
* Copyright (C) 2006 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.wildfire.gateway; package org.jivesoftware.wildfire.gateway;
import java.util.*; import java.util.*;
...@@ -31,11 +41,10 @@ import org.xmpp.packet.Presence; ...@@ -31,11 +41,10 @@ import org.xmpp.packet.Presence;
import org.xmpp.packet.PacketError.Condition; import org.xmpp.packet.PacketError.Condition;
/** /**
* Handle a good share of the tasks for a gateway. Dealing with lookups for ids, * Handle a good share of the tasks for a gateway. Deals with lookups for
* dealing with registration, dealing with presence information. * ids, registration, and presence information.
* *
* @author ncampbell * @author ncampbell
* @version 1.0
*/ */
public abstract class BaseGateway implements Gateway, Component, Runnable { public abstract class BaseGateway implements Gateway, Component, Runnable {
...@@ -48,12 +57,12 @@ public abstract class BaseGateway implements Gateway, Component, Runnable { ...@@ -48,12 +57,12 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
* @see org.jivesoftware.wildfire.gateway.Gateway#setName(String) * @see org.jivesoftware.wildfire.gateway.Gateway#setName(String)
*/ */
public abstract void setName(String newname); public abstract void setName(String newname);
/** /**
* @see org.jivesoftware.wildfire.gateway.Gateway#getDescription() * @see org.jivesoftware.wildfire.gateway.Gateway#getDescription()
*/ */
public abstract String getDescription(); public abstract String getDescription();
/** /**
* @see org.jivesoftware.wildfire.gateway.Gateway#getDomain() * @see org.jivesoftware.wildfire.gateway.Gateway#getDomain()
*/ */
...@@ -62,19 +71,19 @@ public abstract class BaseGateway implements Gateway, Component, Runnable { ...@@ -62,19 +71,19 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
logger.log(Level.FINE, "basegateway.domainname", domainName); logger.log(Level.FINE, "basegateway.domainname", domainName);
return domainName; return domainName;
} }
/** /**
* @return version the version of the gateway * @return version The version of the gateway
*/ */
public abstract String getVersion(); public abstract String getVersion();
/** /**
* @return gatewayType the type of gateway. THis should always be "gateway" * @return gatewayType the type of gateway. THis should always be "gateway"
*/ */
public String getType() { public String getType() {
return "gateway"; return "gateway";
} }
/** The logger. @see java.util.logging.Logger */ /** The logger. @see java.util.logging.Logger */
private static final Logger logger = Logger.getLogger("BaseGateway","gateway_i18n"); private static final Logger logger = Logger.getLogger("BaseGateway","gateway_i18n");
...@@ -82,34 +91,32 @@ public abstract class BaseGateway implements Gateway, Component, Runnable { ...@@ -82,34 +91,32 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
* Jabber endpoint. * Jabber endpoint.
*/ */
protected JabberEndpoint jabberEndpoint; protected JabberEndpoint jabberEndpoint;
/** /**
* Component Manager to handle communication to the XMPP server. * Component Manager to handle communication with the XMPP server.
*/ */
public final ComponentManager componentManager = ComponentManagerFactory.getComponentManager(); public final ComponentManager componentManager = ComponentManagerFactory.getComponentManager();
/** /**
* JID of component. * JID of component.
*/ */
public JID jid; public JID jid;
/** /**
* Handles registration for this gateway. * Handles registration for this gateway.
*/ */
public final PersistenceManager rosterManager = PersistenceManager.Factory.get(this); public final PersistenceManager rosterManager = PersistenceManager.Factory.get(this);
/** The threadPool. @see java.util.concurrent.ScheduledExecutorService */ /** The threadPool. @see java.util.concurrent.ScheduledExecutorService */
protected static final ScheduledExecutorService threadPool = Executors.newSingleThreadScheduledExecutor(new BackgroundThreadFactory()); protected static final ScheduledExecutorService threadPool = Executors.newSingleThreadScheduledExecutor(new BackgroundThreadFactory());
/** /**
* Handle Foreign Contacts and JID Mapping * Handle Foreign Contacts and JID Mapping
*/ */
private final Map<JID, String> foreignContacts = new HashMap<JID, String>(); private final Map<JID, String> foreignContacts = new HashMap<JID, String>();
/** /**
* helper method for getting the ns from a packet. * Helper method for getting the ns (namespace) from a packet.
* *
* @param packet * @param packet
* @return namespace The namespaceUri. * @return namespace The namespaceUri.
...@@ -120,14 +127,14 @@ public abstract class BaseGateway implements Gateway, Component, Runnable { ...@@ -120,14 +127,14 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
if (childElement != null) { if (childElement != null) {
namespace = childElement.getNamespaceURI(); namespace = childElement.getNamespaceURI();
} }
return namespace; return namespace;
} }
/** /**
* Process an IQ packet. * Process an IQ packet.
* *
* @param iq the IQ packet sent from the client. * @param iq The IQ packet sent from the client.
* @return packetList A list of Packets to be sent to the client. * @return packetList A list of Packets to be sent to the client.
* @see java.util.List * @see java.util.List
* @see org.xmpp.packet.IQ * @see org.xmpp.packet.IQ
...@@ -135,24 +142,29 @@ public abstract class BaseGateway implements Gateway, Component, Runnable { ...@@ -135,24 +142,29 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
private List<Packet> processPacket(IQ iq) { private List<Packet> processPacket(IQ iq) {
String namespace = getNS(iq); String namespace = getNS(iq);
List<Packet> response = new ArrayList<Packet>(); List<Packet> response = new ArrayList<Packet>();
if("http://jabber.org/protocol/disco#info".equals(namespace)) { if ("http://jabber.org/protocol/disco#info".equals(namespace)) {
response.add(handleServiceDiscovery(iq)); response.add(handleServiceDiscovery(iq));
} else if("jabber:iq:agents".equals(namespace)) { }
else if ("jabber:iq:agents".equals(namespace)) {
response.add(handleLegacyServiceDiscovery(iq)); response.add(handleLegacyServiceDiscovery(iq));
} else if("http://jabber.org/protocol/disco#items".equals(namespace)) { }
else if ("http://jabber.org/protocol/disco#items".equals(namespace)) {
response.add(handleDiscoveryItems(iq)); response.add(handleDiscoveryItems(iq));
} else if("jabber:iq:register".equals(namespace)) { }
else if ("jabber:iq:register".equals(namespace)) {
response.addAll(handleRegisterInBand(iq)); response.addAll(handleRegisterInBand(iq));
} else if("jabber:iq:version".equals(namespace)) { }
else if ("jabber:iq:version".equals(namespace)) {
response.add(handleVersion(iq)); response.add(handleVersion(iq));
} else if("jabber:iq:browse".equals(namespace)) { }
else if ("jabber:iq:browse".equals(namespace)) {
response.add(handleBrowse(iq)); response.add(handleBrowse(iq));
} }
return response; return response;
} }
/** /**
* Proces a browse request. * Process a browse request.
* *
* @param iq The client IQ packet. * @param iq The client IQ packet.
* @return iq The response IQ packet. * @return iq The response IQ packet.
...@@ -161,27 +173,24 @@ public abstract class BaseGateway implements Gateway, Component, Runnable { ...@@ -161,27 +173,24 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
IQ reply = IQ.createResultIQ(iq); IQ reply = IQ.createResultIQ(iq);
Element responseElement = DocumentHelper.createElement(QName.get( Element responseElement = DocumentHelper.createElement(QName.get(
"query", "jabber:iq:browse")); "query", "jabber:iq:browse"));
try { try {
for (ForeignContact fc : PersistenceManager.Factory.get(this).getRegistrar().getGatewaySession(iq.getFrom()).getContacts()) {
for(ForeignContact fc : PersistenceManager.Factory.get(this).getRegistrar().getGatewaySession(iq.getFrom()).getContacts()) { Element item = DocumentHelper.createElement("item");
Element item = DocumentHelper.createElement("item"); item.addAttribute("category", "user");
item.addAttribute("category", "user"); item.addAttribute("jid", fc + "@" + this.getName() + "." + this.getDomain());
item.addAttribute("jid", fc + "@" + this.getName() + "." + this.getDomain()); item.addAttribute("type", "client");
item.addAttribute("type", "client"); responseElement.add(item);
responseElement.add(item); }
reply.setChildElement(responseElement);
} }
catch (Exception e) {
reply.setChildElement(responseElement);
} catch (Exception e) {
logger.log(Level.WARNING, "basegateway.notfound", iq.getFrom().toBareJID()); logger.log(Level.WARNING, "basegateway.notfound", iq.getFrom().toBareJID());
} }
return reply; return reply;
} }
/** /**
* Process a version request. * Process a version request.
* *
...@@ -198,24 +207,25 @@ public abstract class BaseGateway implements Gateway, Component, Runnable { ...@@ -198,24 +207,25 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
reply.setChildElement(responseElement); reply.setChildElement(responseElement);
return reply; return reply;
} }
/** /**
* Handle a register command...this may be the request or the response for * Handle a register command...this may be the request or the response for
* the registration process. * the registration process.
* *
* @param iq The client iq packet. * @param iq The client iq packet.
* @return response The <code>Collection</code> of <code>Packet</code> that * @return response The <code>Collection</code> of <code>Packet</code>s
* make up the response. * that make up the response.
*/ */
private Collection<Packet> handleRegisterInBand(final IQ iq) { private Collection<Packet> handleRegisterInBand(final IQ iq) {
Element remove = iq.getChildElement().element("remove"); Element remove = iq.getChildElement().element("remove");
Collection<Packet> response = new ArrayList<Packet>(); Collection<Packet> response = new ArrayList<Packet>();
if(remove != null) { if (remove != null) {
response.addAll(unregister(iq)); response.addAll(unregister(iq));
} else { }
else {
response.addAll(register(iq)); response.addAll(register(iq));
} }
return response; return response;
} }
...@@ -228,33 +238,33 @@ public abstract class BaseGateway implements Gateway, Component, Runnable { ...@@ -228,33 +238,33 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
private Collection<Packet> unregister(IQ iq) { private Collection<Packet> unregister(IQ iq) {
IQ reply = IQ.createResultIQ(iq); IQ reply = IQ.createResultIQ(iq);
PersistenceManager.Factory.get(this).remove(iq.getFrom()); PersistenceManager.Factory.get(this).remove(iq.getFrom());
Collection<Packet> results = new ArrayList<Packet>(); Collection<Packet> results = new ArrayList<Packet>();
results.add(reply); results.add(reply);
Presence unsubscribed, unsubscribe, unavailable; Presence unsubscribed, unsubscribe, unavailable;
unavailable = new Presence(Presence.Type.unavailable); unavailable = new Presence(Presence.Type.unavailable);
unavailable.setTo(reply.getTo()); unavailable.setTo(reply.getTo());
unavailable.setFrom(reply.getFrom()); unavailable.setFrom(reply.getFrom());
unsubscribed = new Presence(Presence.Type.unsubscribed); unsubscribed = new Presence(Presence.Type.unsubscribed);
unsubscribed.setTo(reply.getTo()); unsubscribed.setTo(reply.getTo());
unsubscribed.setFrom(reply.getFrom()); unsubscribed.setFrom(reply.getFrom());
unsubscribe = new Presence(Presence.Type.unsubscribe); unsubscribe = new Presence(Presence.Type.unsubscribe);
unsubscribe.setTo(reply.getTo()); unsubscribe.setTo(reply.getTo());
unsubscribe.setFrom(reply.getFrom()); unsubscribe.setFrom(reply.getFrom());
results.add(unsubscribe); results.add(unsubscribe);
results.add(unsubscribed); results.add(unsubscribed);
results.add(unavailable); results.add(unavailable);
logger.log(Level.INFO, "basegateway.unregister", iq.getFrom()); logger.log(Level.INFO, "basegateway.unregister", iq.getFrom());
return results; return results;
} }
/** /**
* Handle a register request. * Handle a register request.
* *
...@@ -262,77 +272,75 @@ public abstract class BaseGateway implements Gateway, Component, Runnable { ...@@ -262,77 +272,75 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
* @return response A <code>Collection</code> of <code>Packet</code>s that make up the response. * @return response A <code>Collection</code> of <code>Packet</code>s that make up the response.
*/ */
private Collection<Packet> register(IQ iq) { private Collection<Packet> register(IQ iq) {
Collection<Packet> response = new ArrayList<Packet>(); Collection<Packet> response = new ArrayList<Packet>();
IQ reply = IQ.createResultIQ(iq); IQ reply = IQ.createResultIQ(iq);
Element responseElement = DocumentHelper.createElement(QName.get( Element responseElement = DocumentHelper.createElement(QName.get(
"query", "jabber:iq:register")); "query", "jabber:iq:register"));
if(iq.getType().equals(IQ.Type.set)) { if (iq.getType().equals(IQ.Type.set)) {
String username = null; String username = null;
String password = null; String password = null;
try { try {
DataForm form = new DataForm(iq.getChildElement().element("x")); DataForm form = new DataForm(iq.getChildElement().element("x"));
List<FormField> fields = form.getFields(); List<FormField> fields = form.getFields();
for(FormField field : fields) { for (FormField field : fields) {
String var = field.getVariable(); String var = field.getVariable();
if(var.equalsIgnoreCase("username")) { if (var.equalsIgnoreCase("username")) {
username = field.getValues().get(0); username = field.getValues().get(0);
} else if (var.equalsIgnoreCase("password")) { }
else if (var.equalsIgnoreCase("password")) {
/** /**
* The password is stored in Whack and DOM4J as a String * The password is stored in Whack and DOM4J as a String
* so the password is sent in the clear and stored in * so the password is sent in the clear and stored in
* JVM until termination. * JVM until termination.
*/ */
password = field.getValues().get(0); password = field.getValues().get(0);
} }
} }
} catch (Exception e) { }
catch (Exception e) {
// unable to use dataform // unable to use dataform
logger.log(Level.FINER, "basegateway.dataformnotused", e); logger.log(Level.FINER, "basegateway.dataformnotused", e);
} }
if(username == null || password == null) { if (username == null || password == null) {
// try non DataForm. // try non DataForm.
Element usernameElement = iq.getChildElement().element("username"); Element usernameElement = iq.getChildElement().element("username");
Element passwordElement = iq.getChildElement().element("password"); Element passwordElement = iq.getChildElement().element("password");
if(usernameElement != null) { if (usernameElement != null) {
username = usernameElement.getTextTrim(); username = usernameElement.getTextTrim();
} }
if(passwordElement != null) { if (passwordElement != null) {
password = passwordElement.getTextTrim(); password = passwordElement.getTextTrim();
} }
} }
// make sure that something was collected, otherwise return an error. // make sure that something was collected, otherwise return an error
if(username == null || password == null) { if (username == null || password == null) {
IQ result = IQ.createResultIQ(iq); IQ result = IQ.createResultIQ(iq);
result.setError(Condition.bad_request); result.setError(Condition.bad_request);
response.add(result); response.add(result);
} else { }
else {
logger.log(Level.INFO, "basegateway.register", username.trim()); logger.log(Level.INFO, "basegateway.register", username.trim());
SubscriptionInfo info = new SubscriptionInfo(username.trim(), password); SubscriptionInfo info = new SubscriptionInfo(username.trim(), password);
PersistenceManager.Factory.get(this).getRegistrar().add(iq.getFrom(), info); PersistenceManager.Factory.get(this).getRegistrar().add(iq.getFrom(), info);
reply.setChildElement(responseElement); reply.setChildElement(responseElement);
response.add( reply ); response.add( reply );
Presence subscribe = new Presence(Presence.Type.subscribe); Presence subscribe = new Presence(Presence.Type.subscribe);
subscribe.setTo(iq.getFrom()); subscribe.setTo(iq.getFrom());
subscribe.setFrom(iq.getTo()); subscribe.setFrom(iq.getTo());
response.add(subscribe); response.add(subscribe);
} }
}
} else if(iq.getType().equals(IQ.Type.get)) { else if (iq.getType().equals(IQ.Type.get)) {
DataForm form = new DataForm(DataForm.Type.form); DataForm form = new DataForm(DataForm.Type.form);
// This needs to ask the specific gateway what to say.
form.addInstruction("Please enter your AIM Screenname and Password"); form.addInstruction("Please enter your AIM Screenname and Password");
FormField usernameField = form.addField(); FormField usernameField = form.addField();
usernameField.setLabel("Username"); usernameField.setLabel("Username");
...@@ -342,26 +350,27 @@ public abstract class BaseGateway implements Gateway, Component, Runnable { ...@@ -342,26 +350,27 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
passwordField.setLabel("Password"); passwordField.setLabel("Password");
passwordField.setVariable("password"); passwordField.setVariable("password");
passwordField.setType(FormField.Type.text_private); passwordField.setType(FormField.Type.text_private);
/** /**
* Add standard elements to request. * Add standard elements to request.
*/ */
// This needs to ask the specific gateway what to say.
responseElement.addElement("instruction").addText("Please enter your AIM screenname and password"); responseElement.addElement("instruction").addText("Please enter your AIM screenname and password");
responseElement.addElement("username"); responseElement.addElement("username");
responseElement.addElement("password"); responseElement.addElement("password");
/** /**
* Add data form (for those that can support it. * Add data form for clients that can support it.
*/ */
responseElement.add(form.getElement()); responseElement.add(form.getElement());
reply.setChildElement(responseElement); reply.setChildElement(responseElement);
response.add( reply ); response.add( reply );
} }
return response; return response;
} }
/** /**
* Returns the contacts for the user that made the request. * Returns the contacts for the user that made the request.
* *
...@@ -372,9 +381,9 @@ public abstract class BaseGateway implements Gateway, Component, Runnable { ...@@ -372,9 +381,9 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
IQ reply = IQ.createResultIQ(iq); IQ reply = IQ.createResultIQ(iq);
Element responseElement = DocumentHelper.createElement(QName.get( Element responseElement = DocumentHelper.createElement(QName.get(
"query", "http://jabber.org/protocol/disco#info")); "query", "http://jabber.org/protocol/disco#info"));
Roster roster = this.rosterManager.getContactManager().getRoster(iq.getFrom()); Roster roster = this.rosterManager.getContactManager().getRoster(iq.getFrom());
for( ForeignContact entry : roster.getAll()) { for (ForeignContact entry : roster.getAll()) {
Element item = responseElement.addElement("item"); Element item = responseElement.addElement("item");
item.addAttribute("jid", entry.getJid().toBareJID()); item.addAttribute("jid", entry.getJid().toBareJID());
item.addAttribute("name", entry.getName()); item.addAttribute("name", entry.getName());
...@@ -384,7 +393,7 @@ public abstract class BaseGateway implements Gateway, Component, Runnable { ...@@ -384,7 +393,7 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
System.out.println(reply); System.out.println(reply);
return reply; return reply;
} }
/** /**
* TODO: handle a legacy service discovery request. * TODO: handle a legacy service discovery request.
* *
...@@ -395,6 +404,7 @@ public abstract class BaseGateway implements Gateway, Component, Runnable { ...@@ -395,6 +404,7 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
return IQ.createResultIQ(iq); return IQ.createResultIQ(iq);
// TODO: Implement Legacy Service Discovery. // TODO: Implement Legacy Service Discovery.
} }
/** /**
* Handle service discovery. * Handle service discovery.
* *
...@@ -403,19 +413,23 @@ public abstract class BaseGateway implements Gateway, Component, Runnable { ...@@ -403,19 +413,23 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
*/ */
private IQ handleServiceDiscovery(IQ iq) { private IQ handleServiceDiscovery(IQ iq) {
IQ reply = IQ.createResultIQ(iq); IQ reply = IQ.createResultIQ(iq);
Element responseElement = DocumentHelper.createElement(QName.get( Element responseElement = DocumentHelper.createElement(QName.get(
"query", "http://jabber.org/protocol/disco#info")); "query", "http://jabber.org/protocol/disco#info"));
responseElement.addElement("identity").addAttribute("category", "gateway") responseElement.addElement("identity")
.addAttribute("type", this.getType()) .addAttribute("category", "gateway")
.addAttribute("name", this.getDescription()); .addAttribute("type", this.getType())
responseElement.addElement("feature").addAttribute("var", "jabber:iq:time"); .addAttribute("name", this.getDescription());
responseElement.addElement("feature").addAttribute("var", "jabber:iq:version"); responseElement.addElement("feature")
responseElement.addElement("feature").addAttribute("var", "jabber:iq:register"); .addAttribute("var", "jabber:iq:time");
responseElement.addElement("feature")
.addAttribute("var", "jabber:iq:version");
responseElement.addElement("feature")
.addAttribute("var", "jabber:iq:register");
reply.setChildElement(responseElement); reply.setChildElement(responseElement);
return reply; return reply;
} }
/** /**
* Dispatch the appropriate message type. * Dispatch the appropriate message type.
* *
...@@ -424,30 +438,36 @@ public abstract class BaseGateway implements Gateway, Component, Runnable { ...@@ -424,30 +438,36 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
public void processPacket(Packet packet) { public void processPacket(Packet packet) {
try { try {
List<Packet> response = new ArrayList<Packet>(); List<Packet> response = new ArrayList<Packet>();
if(packet instanceof IQ) { if (packet instanceof IQ) {
response.addAll( processPacket((IQ)packet) ); response.addAll( processPacket((IQ)packet) );
} else if (packet instanceof Presence) { }
else if (packet instanceof Presence) {
response.addAll( processPacket((Presence)packet) ); response.addAll( processPacket((Presence)packet) );
} else if (packet instanceof Message) { }
else if (packet instanceof Message) {
processPacket((Message)packet); processPacket((Message)packet);
} else { }
else {
System.out.println("UNHANDLED: " + packet.toString()); System.out.println("UNHANDLED: " + packet.toString());
} }
// process // process
if(response.size() > 0) { if (response.size() > 0) {
for(Packet p : response) { for (Packet p : response) {
componentManager.sendPacket(this, p); componentManager.sendPacket(this, p);
} }
} else { }
else {
//System.out.println("Request[" + packet.toString() + "] with no response"); //System.out.println("Request[" + packet.toString() + "] with no response");
} }
} catch (Exception e) { }
catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
/** /**
* Process a message from the client. * Process a message from the client.
* *
...@@ -457,13 +477,13 @@ public abstract class BaseGateway implements Gateway, Component, Runnable { ...@@ -457,13 +477,13 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
*/ */
private void processPacket(Message message) throws Exception { private void processPacket(Message message) throws Exception {
logger.finer(message.toString()); logger.finer(message.toString());
GatewaySession session = PersistenceManager.Factory.get(this).getRegistrar().getGatewaySession(message.getFrom()); GatewaySession session = PersistenceManager.Factory.get(this).getRegistrar().getGatewaySession(message.getFrom());
session.getLegacyEndpoint().sendPacket(message); session.getLegacyEndpoint().sendPacket(message);
} }
/** /**
* Proces a presense packet. * Process a presense packet.
* *
* @param presence The presence packet from the client. * @param presence The presence packet from the client.
* @return list A <code>List</code> of <code>Presence</code> packets. * @return list A <code>List</code> of <code>Presence</code> packets.
...@@ -472,16 +492,16 @@ public abstract class BaseGateway implements Gateway, Component, Runnable { ...@@ -472,16 +492,16 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
private List<Presence> processPacket(Presence presence) throws Exception { private List<Presence> processPacket(Presence presence) throws Exception {
List<Presence> p = new ArrayList<Presence>(); List<Presence> p = new ArrayList<Presence>();
JID from = presence.getFrom(); JID from = presence.getFrom();
logger.finest(presence.toString()); logger.finest(presence.toString());
/* /*
* Unknown entity is trying to access the gateway. * Unknown entity is trying to access the gateway.
*/ */
if(!rosterManager.getRegistrar().isRegistered(NormalizedJID.wrap(from))) { if (!rosterManager.getRegistrar().isRegistered(NormalizedJID.wrap(from))) {
logger.log(Level.INFO, "basegateway.unabletofind", from); logger.log(Level.INFO, "basegateway.unabletofind", from);
// silently ignore a delete request // silently ignore a delete request
if(!Presence.Type.unavailable.equals(presence.getType())) { if (!Presence.Type.unavailable.equals(presence.getType())) {
logger.log(Level.INFO, "basegateway.unauthorizedrequest", new Object[] { presence.getType(), from.toString() }); logger.log(Level.INFO, "basegateway.unauthorizedrequest", new Object[] { presence.getType(), from.toString() });
Presence result = new Presence(); Presence result = new Presence();
result.setError(Condition.not_authorized); result.setError(Condition.not_authorized);
...@@ -490,20 +510,21 @@ public abstract class BaseGateway implements Gateway, Component, Runnable { ...@@ -490,20 +510,21 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
} }
return p; return p;
} }
/* /*
* Get the underlying session for this JID and handle accordingly. * Get the underlying session for this JID and handle accordingly.
*/ */
GatewaySession sessionInfo = rosterManager.getRegistrar().getGatewaySession(from); GatewaySession sessionInfo = rosterManager.getRegistrar().getGatewaySession(from);
if(sessionInfo == null) { if (sessionInfo == null) {
logger.log(Level.WARNING, "basegateway.unabletolocatesession" , from); logger.log(Level.WARNING, "basegateway.unabletolocatesession" , from);
return p; return p;
} }
if(Presence.Type.subscribe.equals(presence.getType())) { if (Presence.Type.subscribe.equals(presence.getType())) {
if(presence.getTo().equals(this.jid)) { if (presence.getTo().equals(this.jid)) {
sessionInfo.getSubscriptionInfo().serverRegistered = true; sessionInfo.getSubscriptionInfo().serverRegistered = true;
} else { }
else {
Presence subscribe = new Presence(Presence.Type.subscribe); Presence subscribe = new Presence(Presence.Type.subscribe);
subscribe.setTo(presence.getFrom()); subscribe.setTo(presence.getFrom());
subscribe.setFrom(presence.getTo()); subscribe.setFrom(presence.getTo());
...@@ -516,22 +537,22 @@ public abstract class BaseGateway implements Gateway, Component, Runnable { ...@@ -516,22 +537,22 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
} }
else if (Presence.Type.subscribed.equals(presence.getType())) { else if (Presence.Type.subscribed.equals(presence.getType())) {
if(presence.getTo().equals(this.jid)) { // subscribed to server if (presence.getTo().equals(this.jid)) { // subscribed to server
sessionInfo.getSubscriptionInfo().clientRegistered = true; sessionInfo.getSubscriptionInfo().clientRegistered = true;
} else { // subscribe to legacy user }
else { // subscribe to legacy user
logger.log(Level.FINE,"basegateway.subscribed"); logger.log(Level.FINE,"basegateway.subscribed");
} }
} }
else if (Presence.Type.unavailable.equals(presence.getType())){ else if (Presence.Type.unavailable.equals(presence.getType())){
/** /**
* If an unavailable presence stanza is received then logout the * If an unavailable presence stanza is received then logout the
* current user and send back and unavailable stanza. * current user and send back and unavailable stanza.
*/ */
if(sessionInfo.isConnected()) { if (sessionInfo.isConnected()) {
sessionInfo.logout(); sessionInfo.logout();
} }
Presence reply = new Presence(Presence.Type.unavailable); Presence reply = new Presence(Presence.Type.unavailable);
reply.setTo(presence.getFrom()); reply.setTo(presence.getFrom());
reply.setFrom(presence.getTo()); reply.setFrom(presence.getTo());
...@@ -546,14 +567,14 @@ public abstract class BaseGateway implements Gateway, Component, Runnable { ...@@ -546,14 +567,14 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
} }
else { else {
GatewaySession session = rosterManager.getRegistrar().getGatewaySession(presence.getFrom()); GatewaySession session = rosterManager.getRegistrar().getGatewaySession(presence.getFrom());
try { try {
ForeignContact fc = session.getContact(presence.getTo()); ForeignContact fc = session.getContact(presence.getTo());
Status status = fc.getStatus(); Status status = fc.getStatus();
Presence p2 = new Presence(); Presence p2 = new Presence();
p2.setFrom(presence.getTo()); p2.setFrom(presence.getTo());
p2.setTo(presence.getFrom()); p2.setTo(presence.getFrom());
if(status.isOnline()) { if (status.isOnline()) {
p2.setStatus(status.getValue()); p2.setStatus(status.getValue());
} }
else { else {
...@@ -578,7 +599,7 @@ public abstract class BaseGateway implements Gateway, Component, Runnable { ...@@ -578,7 +599,7 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
public JID getJID() { public JID getJID() {
return this.jid; return this.jid;
} }
/** /**
* Handle initialization. * Handle initialization.
* *
...@@ -593,7 +614,7 @@ public abstract class BaseGateway implements Gateway, Component, Runnable { ...@@ -593,7 +614,7 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
EndpointValve jabberEndpointValve = new EndpointValve(false); EndpointValve jabberEndpointValve = new EndpointValve(false);
this.jabberEndpoint = new JabberEndpoint(componentManager, this, jabberEndpointValve); this.jabberEndpoint = new JabberEndpoint(componentManager, this, jabberEndpointValve);
} }
/** /**
* Reverse lookup. This will always succeed. A new JID will be created for a * Reverse lookup. This will always succeed. A new JID will be created for a
* foreign contact. * foreign contact.
...@@ -606,7 +627,7 @@ public abstract class BaseGateway implements Gateway, Component, Runnable { ...@@ -606,7 +627,7 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
foreignContacts.put(jid, foreignContact); foreignContacts.put(jid, foreignContact);
return jid; return jid;
} }
/** /**
* Lookup a foreign contact * Lookup a foreign contact
* *
...@@ -636,23 +657,23 @@ public abstract class BaseGateway implements Gateway, Component, Runnable { ...@@ -636,23 +657,23 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
* subscribed the contact to the JID. * subscribed the contact to the JID.
*/ */
public void run() { public void run() {
logger.log(Level.FINEST, "basegateway.maintenancestart"); logger.log(Level.FINEST, "basegateway.maintenancestart");
for(SubscriptionInfo si : rosterManager.getRegistrar().getAllGatewaySessions()) { for (SubscriptionInfo si : rosterManager.getRegistrar().getAllGatewaySessions()) {
if(!si.clientRegistered) { if (!si.clientRegistered) {
Presence p = new Presence(); Presence p = new Presence();
p.setType(Presence.Type.subscribe); p.setType(Presence.Type.subscribe);
p.setTo(si.jid); p.setTo(si.jid);
p.setFrom(this.jid); p.setFrom(this.jid);
try { try {
componentManager.sendPacket(this, p); componentManager.sendPacket(this, p);
} catch (ComponentException e) { }
catch (ComponentException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
} }
// for(ForeignContact fc : rosterManager.getContactManager().getRoster(si.jid).getAll()) { // for(ForeignContact fc : rosterManager.getContactManager().getRoster(si.jid).getAll()) {
// Presence p = new Presence(); // Presence p = new Presence();
// p.setFrom(fc.getJid()); // p.setFrom(fc.getJid());
...@@ -664,7 +685,6 @@ public abstract class BaseGateway implements Gateway, Component, Runnable { ...@@ -664,7 +685,6 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
// logger.log(Level.WARNING, "basegateway.unabletosendpresence", e); // logger.log(Level.WARNING, "basegateway.unabletosendpresence", e);
// } // }
// } // }
} }
// for(ForeignContact contact : rosterManager.getContactManager().getAllForeignContacts()) { // for(ForeignContact contact : rosterManager.getContactManager().getAllForeignContacts()) {
...@@ -678,10 +698,10 @@ public abstract class BaseGateway implements Gateway, Component, Runnable { ...@@ -678,10 +698,10 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
// logger.log(Level.WARNING, "basegateway.unabletosendpresence", e); // logger.log(Level.WARNING, "basegateway.unabletosendpresence", e);
// } // }
// } // }
logger.log(Level.FINEST, "basegateway.maintenancestop"); logger.log(Level.FINEST, "basegateway.maintenancestop");
} }
/** /**
* Returns a <code>SessionFactory</code>. The session factory utilizes the * Returns a <code>SessionFactory</code>. The session factory utilizes the
* abstract method <code>getSessionInstance</code> to get a new instance and * abstract method <code>getSessionInstance</code> to get a new instance and
...@@ -692,23 +712,21 @@ public abstract class BaseGateway implements Gateway, Component, Runnable { ...@@ -692,23 +712,21 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
*/ */
public SessionFactory getSessionFactory() { public SessionFactory getSessionFactory() {
return new SessionFactory() { return new SessionFactory() {
public GatewaySession newInstance(SubscriptionInfo info) { public GatewaySession newInstance(SubscriptionInfo info) {
GatewaySession session = getSessionInstance(info); GatewaySession session = getSessionInstance(info);
session.setJabberEndpoint(jabberEndpoint); session.setJabberEndpoint(jabberEndpoint);
return session; return session;
} }
}; };
} }
/** /**
* @see org.jivesoftware.wildfire.gateway.Endpoint#getValve() * @see org.jivesoftware.wildfire.gateway.Endpoint#getValve()
*/ */
public EndpointValve getValve() { public EndpointValve getValve() {
return this.jabberEndpoint.getValve(); return this.jabberEndpoint.getValve();
} }
/** /**
* Return a <code>GatewaySession</code> given the <code>SubscriptionInfo</code> * Return a <code>GatewaySession</code> given the <code>SubscriptionInfo</code>
* *
...@@ -718,4 +736,5 @@ public abstract class BaseGateway implements Gateway, Component, Runnable { ...@@ -718,4 +736,5 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
* @see Gateway#getSessionFactory() * @see Gateway#getSessionFactory()
*/ */
protected abstract GatewaySession getSessionInstance(SubscriptionInfo info); protected abstract GatewaySession getSessionInstance(SubscriptionInfo info);
} }
...@@ -21,7 +21,7 @@ import org.xmpp.packet.Packet; ...@@ -21,7 +21,7 @@ import org.xmpp.packet.Packet;
* @version 1.0 * @version 1.0
*/ */
public interface Endpoint { public interface Endpoint {
/** /**
* Send a packet to the underlying messaging services * Send a packet to the underlying messaging services
* *
...@@ -29,8 +29,7 @@ public interface Endpoint { ...@@ -29,8 +29,7 @@ public interface Endpoint {
* @throws ComponentException * @throws ComponentException
*/ */
public void sendPacket(Packet packet) throws ComponentException; public void sendPacket(Packet packet) throws ComponentException;
/** /**
* Return the <code>EndpointValve</code>. This provides the ability of the * Return the <code>EndpointValve</code>. This provides the ability of the
* caller to open or close the valve to control the follow of packets to the * caller to open or close the valve to control the follow of packets to the
...@@ -39,4 +38,5 @@ public interface Endpoint { ...@@ -39,4 +38,5 @@ public interface Endpoint {
* @return valve The <code>EndpointValve</code> associated with this <code>Endpoint</code> * @return valve The <code>EndpointValve</code> associated with this <code>Endpoint</code>
*/ */
public EndpointValve getValve(); public EndpointValve getValve();
} }
...@@ -34,20 +34,20 @@ public class JabberEndpoint implements Endpoint { ...@@ -34,20 +34,20 @@ public class JabberEndpoint implements Endpoint {
* @see ComponentManager * @see ComponentManager
*/ */
private final ComponentManager componentManager; private final ComponentManager componentManager;
/** /**
* The component * The component
* *
* @see Component * @see Component
*/ */
private final Component component; private final Component component;
/** /**
* The value. * The value.
* @see EndpointValve * @see EndpointValve
*/ */
private final EndpointValve valve; private final EndpointValve valve;
/** /**
* Construct a new <code>JabberEndpoint</code>. * Construct a new <code>JabberEndpoint</code>.
* @param componentManager The componentManager. * @param componentManager The componentManager.
...@@ -56,7 +56,7 @@ public class JabberEndpoint implements Endpoint { ...@@ -56,7 +56,7 @@ public class JabberEndpoint implements Endpoint {
public JabberEndpoint(ComponentManager componentManager, Component component) { public JabberEndpoint(ComponentManager componentManager, Component component) {
this(componentManager, component, new EndpointValve()); this(componentManager, component, new EndpointValve());
} }
/** /**
* Construct a new <code>JabberEndpoint</code>. * Construct a new <code>JabberEndpoint</code>.
* @param componentManager * @param componentManager
...@@ -68,19 +68,6 @@ public class JabberEndpoint implements Endpoint { ...@@ -68,19 +68,6 @@ public class JabberEndpoint implements Endpoint {
this.component = component; this.component = component;
this.valve= valve; this.valve= valve;
} }
//
//
// /**
// * @param jid
// * @param string
// * @throws Exception
// */
// public void sendMessage(JID jid, String string) throws Exception {
// Message message = new Message();
// message.setBody(string);
// message.setTo(jid);
// this.componentManager.sendPacket(this.component, message);
// }
/** /**
* @see org.jivesoftware.wildfire.gateway.Endpoint#sendPacket(Packet) * @see org.jivesoftware.wildfire.gateway.Endpoint#sendPacket(Packet)
...@@ -103,10 +90,9 @@ public class JabberEndpoint implements Endpoint { ...@@ -103,10 +90,9 @@ public class JabberEndpoint implements Endpoint {
/** The backlog queue. */ /** The backlog queue. */
private final ConcurrentLinkedQueue<Packet> queue = new ConcurrentLinkedQueue<Packet>(); private final ConcurrentLinkedQueue<Packet> queue = new ConcurrentLinkedQueue<Packet>();
/** The logger. */ /** The logger. */
final static private Logger logger = Logger.getLogger("JabberEndpoint", "gateway_i18n"); final static private Logger logger = Logger.getLogger("JabberEndpoint", "gateway_i18n");
/** /**
* @see org.jivesoftware.wildfire.gateway.Endpoint#getValve() * @see org.jivesoftware.wildfire.gateway.Endpoint#getValve()
*/ */
......
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