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
......@@ -68,9 +68,9 @@ public abstract class AbstractGatewaySession implements GatewaySession, Endpoint
}
/**
* 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
*/
public Endpoint getJabberEndpoint() {
......@@ -114,4 +114,5 @@ public abstract class AbstractGatewaySession implements GatewaySession, Endpoint
public EndpointValve 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;
import java.util.*;
......@@ -31,11 +41,10 @@ import org.xmpp.packet.Presence;
import org.xmpp.packet.PacketError.Condition;
/**
* Handle a good share of the tasks for a gateway. Dealing with lookups for ids,
* dealing with registration, dealing with presence information.
* Handle a good share of the tasks for a gateway. Deals with lookups for
* ids, registration, and presence information.
*
* @author ncampbell
* @version 1.0
*/
public abstract class BaseGateway implements Gateway, Component, Runnable {
......@@ -64,7 +73,7 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
}
/**
* @return version the version of the gateway
* @return version The version of the gateway
*/
public abstract String getVersion();
......@@ -84,7 +93,7 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
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();
......@@ -98,8 +107,6 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
*/
public final PersistenceManager rosterManager = PersistenceManager.Factory.get(this);
/** The threadPool. @see java.util.concurrent.ScheduledExecutorService */
protected static final ScheduledExecutorService threadPool = Executors.newSingleThreadScheduledExecutor(new BackgroundThreadFactory());
......@@ -109,7 +116,7 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
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
* @return namespace The namespaceUri.
......@@ -127,7 +134,7 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
/**
* 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.
* @see java.util.List
* @see org.xmpp.packet.IQ
......@@ -135,24 +142,29 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
private List<Packet> processPacket(IQ iq) {
String namespace = getNS(iq);
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));
} else if("jabber:iq:agents".equals(namespace)) {
}
else if ("jabber:iq:agents".equals(namespace)) {
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));
} else if("jabber:iq:register".equals(namespace)) {
}
else if ("jabber:iq:register".equals(namespace)) {
response.addAll(handleRegisterInBand(iq));
} else if("jabber:iq:version".equals(namespace)) {
}
else if ("jabber:iq:version".equals(namespace)) {
response.add(handleVersion(iq));
} else if("jabber:iq:browse".equals(namespace)) {
}
else if ("jabber:iq:browse".equals(namespace)) {
response.add(handleBrowse(iq));
}
return response;
}
/**
* Proces a browse request.
* Process a browse request.
*
* @param iq The client IQ packet.
* @return iq The response IQ packet.
......@@ -163,25 +175,22 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
"query", "jabber:iq:browse"));
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");
item.addAttribute("category", "user");
item.addAttribute("jid", fc + "@" + this.getName() + "." + this.getDomain());
item.addAttribute("type", "client");
responseElement.add(item);
}
reply.setChildElement(responseElement);
} catch (Exception e) {
}
catch (Exception e) {
logger.log(Level.WARNING, "basegateway.notfound", iq.getFrom().toBareJID());
}
return reply;
}
/**
* Process a version request.
*
......@@ -204,15 +213,16 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
* the registration process.
*
* @param iq The client iq packet.
* @return response The <code>Collection</code> of <code>Packet</code> that
* make up the response.
* @return response The <code>Collection</code> of <code>Packet</code>s
* that make up the response.
*/
private Collection<Packet> handleRegisterInBand(final IQ iq) {
Element remove = iq.getChildElement().element("remove");
Collection<Packet> response = new ArrayList<Packet>();
if(remove != null) {
if (remove != null) {
response.addAll(unregister(iq));
} else {
}
else {
response.addAll(register(iq));
}
......@@ -262,14 +272,12 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
* @return response A <code>Collection</code> of <code>Packet</code>s that make up the response.
*/
private Collection<Packet> register(IQ iq) {
Collection<Packet> response = new ArrayList<Packet>();
IQ reply = IQ.createResultIQ(iq);
Element responseElement = DocumentHelper.createElement(QName.get(
"query", "jabber:iq:register"));
if(iq.getType().equals(IQ.Type.set)) {
if (iq.getType().equals(IQ.Type.set)) {
String username = null;
String password = null;
......@@ -277,11 +285,12 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
DataForm form = new DataForm(iq.getChildElement().element("x"));
List<FormField> fields = form.getFields();
for(FormField field : fields) {
for (FormField field : fields) {
String var = field.getVariable();
if(var.equalsIgnoreCase("username")) {
if (var.equalsIgnoreCase("username")) {
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
* so the password is sent in the clear and stored in
......@@ -290,33 +299,33 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
password = field.getValues().get(0);
}
}
} catch (Exception e) {
}
catch (Exception e) {
// unable to use dataform
logger.log(Level.FINER, "basegateway.dataformnotused", e);
}
if(username == null || password == null) {
if (username == null || password == null) {
// try non DataForm.
Element usernameElement = iq.getChildElement().element("username");
Element passwordElement = iq.getChildElement().element("password");
if(usernameElement != null) {
if (usernameElement != null) {
username = usernameElement.getTextTrim();
}
if(passwordElement != null) {
if (passwordElement != null) {
password = passwordElement.getTextTrim();
}
}
// make sure that something was collected, otherwise return an error.
if(username == null || password == null) {
// make sure that something was collected, otherwise return an error
if (username == null || password == null) {
IQ result = IQ.createResultIQ(iq);
result.setError(Condition.bad_request);
response.add(result);
} else {
}
else {
logger.log(Level.INFO, "basegateway.register", username.trim());
SubscriptionInfo info = new SubscriptionInfo(username.trim(), password);
PersistenceManager.Factory.get(this).getRegistrar().add(iq.getFrom(), info);
......@@ -328,11 +337,10 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
subscribe.setFrom(iq.getTo());
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);
// This needs to ask the specific gateway what to say.
form.addInstruction("Please enter your AIM Screenname and Password");
FormField usernameField = form.addField();
usernameField.setLabel("Username");
......@@ -346,12 +354,13 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
/**
* 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("username");
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());
......@@ -374,7 +383,7 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
"query", "http://jabber.org/protocol/disco#info"));
Roster roster = this.rosterManager.getContactManager().getRoster(iq.getFrom());
for( ForeignContact entry : roster.getAll()) {
for (ForeignContact entry : roster.getAll()) {
Element item = responseElement.addElement("item");
item.addAttribute("jid", entry.getJid().toBareJID());
item.addAttribute("name", entry.getName());
......@@ -395,6 +404,7 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
return IQ.createResultIQ(iq);
// TODO: Implement Legacy Service Discovery.
}
/**
* Handle service discovery.
*
......@@ -406,12 +416,16 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
Element responseElement = DocumentHelper.createElement(QName.get(
"query", "http://jabber.org/protocol/disco#info"));
responseElement.addElement("identity").addAttribute("category", "gateway")
responseElement.addElement("identity")
.addAttribute("category", "gateway")
.addAttribute("type", this.getType())
.addAttribute("name", this.getDescription());
responseElement.addElement("feature").addAttribute("var", "jabber:iq:time");
responseElement.addElement("feature").addAttribute("var", "jabber:iq:version");
responseElement.addElement("feature").addAttribute("var", "jabber:iq:register");
responseElement.addElement("feature")
.addAttribute("var", "jabber:iq:time");
responseElement.addElement("feature")
.addAttribute("var", "jabber:iq:version");
responseElement.addElement("feature")
.addAttribute("var", "jabber:iq:register");
reply.setChildElement(responseElement);
return reply;
}
......@@ -424,30 +438,36 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
public void processPacket(Packet packet) {
try {
List<Packet> response = new ArrayList<Packet>();
if(packet instanceof IQ) {
if (packet instanceof IQ) {
response.addAll( processPacket((IQ)packet) );
} else if (packet instanceof Presence) {
}
else if (packet instanceof Presence) {
response.addAll( processPacket((Presence)packet) );
} else if (packet instanceof Message) {
}
else if (packet instanceof Message) {
processPacket((Message)packet);
} else {
}
else {
System.out.println("UNHANDLED: " + packet.toString());
}
// process
if(response.size() > 0) {
for(Packet p : response) {
if (response.size() > 0) {
for (Packet p : response) {
componentManager.sendPacket(this, p);
}
} else {
}
else {
//System.out.println("Request[" + packet.toString() + "] with no response");
}
} catch (Exception e) {
}
catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* Process a message from the client.
*
......@@ -463,7 +483,7 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
}
/**
* Proces a presense packet.
* Process a presense packet.
*
* @param presence The presence packet from the client.
* @return list A <code>List</code> of <code>Presence</code> packets.
......@@ -478,10 +498,10 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
/*
* 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);
// 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() });
Presence result = new Presence();
result.setError(Condition.not_authorized);
......@@ -495,15 +515,16 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
* Get the underlying session for this JID and handle accordingly.
*/
GatewaySession sessionInfo = rosterManager.getRegistrar().getGatewaySession(from);
if(sessionInfo == null) {
if (sessionInfo == null) {
logger.log(Level.WARNING, "basegateway.unabletolocatesession" , from);
return p;
}
if(Presence.Type.subscribe.equals(presence.getType())) {
if(presence.getTo().equals(this.jid)) {
if (Presence.Type.subscribe.equals(presence.getType())) {
if (presence.getTo().equals(this.jid)) {
sessionInfo.getSubscriptionInfo().serverRegistered = true;
} else {
}
else {
Presence subscribe = new Presence(Presence.Type.subscribe);
subscribe.setTo(presence.getFrom());
subscribe.setFrom(presence.getTo());
......@@ -516,19 +537,19 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
}
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;
} else { // subscribe to legacy user
}
else { // subscribe to legacy user
logger.log(Level.FINE,"basegateway.subscribed");
}
}
else if (Presence.Type.unavailable.equals(presence.getType())){
/**
* If an unavailable presence stanza is received then logout the
* current user and send back and unavailable stanza.
*/
if(sessionInfo.isConnected()) {
if (sessionInfo.isConnected()) {
sessionInfo.logout();
}
......@@ -553,7 +574,7 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
Presence p2 = new Presence();
p2.setFrom(presence.getTo());
p2.setTo(presence.getFrom());
if(status.isOnline()) {
if (status.isOnline()) {
p2.setStatus(status.getValue());
}
else {
......@@ -636,18 +657,18 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
* subscribed the contact to the JID.
*/
public void run() {
logger.log(Level.FINEST, "basegateway.maintenancestart");
for(SubscriptionInfo si : rosterManager.getRegistrar().getAllGatewaySessions()) {
if(!si.clientRegistered) {
for (SubscriptionInfo si : rosterManager.getRegistrar().getAllGatewaySessions()) {
if (!si.clientRegistered) {
Presence p = new Presence();
p.setType(Presence.Type.subscribe);
p.setTo(si.jid);
p.setFrom(this.jid);
try {
componentManager.sendPacket(this, p);
} catch (ComponentException e) {
}
catch (ComponentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
......@@ -664,7 +685,6 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
// logger.log(Level.WARNING, "basegateway.unabletosendpresence", e);
// }
// }
}
// for(ForeignContact contact : rosterManager.getContactManager().getAllForeignContacts()) {
......@@ -692,13 +712,11 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
*/
public SessionFactory getSessionFactory() {
return new SessionFactory() {
public GatewaySession newInstance(SubscriptionInfo info) {
GatewaySession session = getSessionInstance(info);
session.setJabberEndpoint(jabberEndpoint);
return session;
}
};
}
......@@ -718,4 +736,5 @@ public abstract class BaseGateway implements Gateway, Component, Runnable {
* @see Gateway#getSessionFactory()
*/
protected abstract GatewaySession getSessionInstance(SubscriptionInfo info);
}
......@@ -30,7 +30,6 @@ public interface Endpoint {
*/
public void sendPacket(Packet packet) throws ComponentException;
/**
* 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
......@@ -39,4 +38,5 @@ public interface Endpoint {
* @return valve The <code>EndpointValve</code> associated with this <code>Endpoint</code>
*/
public EndpointValve getValve();
}
......@@ -34,6 +34,7 @@ public class JabberEndpoint implements Endpoint {
* @see ComponentManager
*/
private final ComponentManager componentManager;
/**
* The component
*
......@@ -41,7 +42,6 @@ public class JabberEndpoint implements Endpoint {
*/
private final Component component;
/**
* The value.
* @see EndpointValve
......@@ -68,19 +68,6 @@ public class JabberEndpoint implements Endpoint {
this.component = component;
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)
......@@ -103,7 +90,6 @@ public class JabberEndpoint implements Endpoint {
/** The backlog queue. */
private final ConcurrentLinkedQueue<Packet> queue = new ConcurrentLinkedQueue<Packet>();
/** The logger. */
final static private Logger logger = Logger.getLogger("JabberEndpoint", "gateway_i18n");
......
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