1 11 12 package org.jivesoftware.messenger.disco; 13 14 import org.dom4j.DocumentHelper; 15 import org.dom4j.Element; 16 import org.dom4j.QName; 17 import org.jivesoftware.messenger.IQHandlerInfo; 18 import org.jivesoftware.messenger.XMPPServer; 19 import org.jivesoftware.messenger.auth.UnauthorizedException; 20 import org.jivesoftware.messenger.forms.spi.XDataFormImpl; 21 import org.jivesoftware.messenger.handler.IQHandler; 22 import org.jivesoftware.messenger.user.UserManager; 23 import org.jivesoftware.messenger.user.UserNotFoundException; 24 import org.xmpp.packet.IQ; 25 import org.xmpp.packet.JID; 26 import org.xmpp.packet.PacketError; 27 28 import java.util.*; 29 import java.util.concurrent.ConcurrentHashMap ; 30 31 51 public class IQDiscoInfoHandler extends IQHandler { 52 53 private HashMap entities = new HashMap (); 54 private List<String > serverFeatures = new ArrayList<String >(); 55 private Map <String , DiscoInfoProvider> serverNodeProviders = 56 new ConcurrentHashMap <String , DiscoInfoProvider>(); 57 private IQHandlerInfo info; 58 59 private List<Element> userIdentities = new ArrayList<Element>(); 60 private List<String > userFeatures = new ArrayList<String >(); 61 62 public IQDiscoInfoHandler() { 63 super("XMPP Disco Info Handler"); 64 info = new IQHandlerInfo("query", "http://jabber.org/protocol/disco#info"); 65 serverFeatures.add("http://jabber.org/protocol/disco#info"); 66 Element userIdentity = DocumentHelper.createElement("identity"); 69 userIdentity.addAttribute("category", "account"); 70 userIdentity.addAttribute("type", "registered"); 71 userIdentities.add(userIdentity); 72 userFeatures.add("http://jabber.org/protocol/disco#info"); 73 } 74 75 public IQHandlerInfo getInfo() { 76 return info; 77 } 78 79 public IQ handleIQ(IQ packet) throws UnauthorizedException { 80 82 IQ reply = IQ.createResultIQ(packet); 86 87 DiscoInfoProvider infoProvider = getProvider(packet.getTo() == null ? 92 XMPPServer.getInstance().getServerInfo().getName() : packet.getTo().getDomain()); 93 if (infoProvider != null) { 94 String name = packet.getTo() == null ? null : packet.getTo().getNode(); 96 if (name == null || name.trim().length() == 0) { 97 name = null; 98 } 99 Element iq = packet.getChildElement(); 101 String node = iq.attributeValue("node"); 102 104 if (infoProvider.hasInfo(name, node, packet.getFrom())) { 106 reply.setChildElement(iq.createCopy()); 107 Element queryElement = reply.getChildElement(); 108 109 Element identity; 111 Iterator identities = infoProvider.getIdentities(name, node, packet.getFrom()); 112 while (identities.hasNext()) { 113 identity = (Element)identities.next(); 114 identity.setQName(new QName(identity.getName(), queryElement.getNamespace())); 115 queryElement.add((Element)identity.clone()); 116 } 117 118 Iterator features = infoProvider.getFeatures(name, node, packet.getFrom()); 120 while (features.hasNext()) { 121 queryElement.addElement("feature").addAttribute("var", (String )features.next()); 122 } 123 124 XDataFormImpl dataForm = infoProvider.getExtendedInfo(name, node, packet.getFrom()); 126 if (dataForm != null) { 127 queryElement.add(dataForm.asXMLElement()); 128 } 129 } 130 else { 131 reply.setChildElement(packet.getChildElement().createCopy()); 134 reply.setError(PacketError.Condition.item_not_found); 135 } 136 } 137 else { 138 reply.setChildElement(packet.getChildElement().createCopy()); 140 reply.setError(PacketError.Condition.item_not_found); 141 } 142 143 return reply; 144 } 145 146 155 public void setServerNodeInfoProvider(String node, DiscoInfoProvider provider) { 156 serverNodeProviders.put(node, provider); 157 } 158 159 165 public void removeServerNodeInfoProvider(String node) { 166 serverNodeProviders.remove(node); 167 } 168 169 177 private DiscoInfoProvider getProvider(String name) { 178 return (DiscoInfoProvider)entities.get(name); 179 } 180 181 189 protected void setProvider(String name, DiscoInfoProvider provider) { 190 entities.put(name, provider); 191 } 192 193 198 protected void removeProvider(String name) { 199 entities.remove(name); 200 } 201 202 210 private void addServerFeaturesProvider(ServerFeaturesProvider provider) { 211 for (Iterator<String > it = provider.getFeatures(); it.hasNext();) { 212 serverFeatures.add(it.next()); 213 } 214 } 215 216 public void initialize(XMPPServer server) { 217 super.initialize(server); 218 for (ServerFeaturesProvider provider : server.getServerFeaturesProviders()) { 221 addServerFeaturesProvider(provider); 222 } 223 setProvider(server.getServerInfo().getName(), getServerInfoProvider()); 224 } 225 226 233 private DiscoInfoProvider getServerInfoProvider() { 234 DiscoInfoProvider discoInfoProvider = new DiscoInfoProvider() { 235 ArrayList<Element> identities = new ArrayList<Element>(); 236 237 public Iterator<Element> getIdentities(String name, String node, JID senderJID) { 238 if (node != null && serverNodeProviders.get(node) != null) { 239 return serverNodeProviders.get(node).getIdentities(name, node, senderJID); 241 } 242 if (name == null) { 243 synchronized (identities) { 245 if (identities.isEmpty()) { 246 Element identity = DocumentHelper.createElement("identity"); 247 identity.addAttribute("category", "services"); 248 identity.addAttribute("name", "Messenger Server"); 249 identity.addAttribute("type", "jabber"); 250 251 identities.add(identity); 252 } 253 } 254 return identities.iterator(); 255 } 256 else { 257 return userIdentities.iterator(); 260 } 261 } 262 263 public Iterator<String > getFeatures(String name, String node, JID senderJID) { 264 if (node != null && serverNodeProviders.get(node) != null) { 265 return serverNodeProviders.get(node).getFeatures(name, node, senderJID); 267 } 268 if (name == null) { 269 return serverFeatures.iterator(); 271 } 272 else { 273 return userFeatures.iterator(); 275 } 276 } 277 278 public boolean hasInfo(String name, String node, JID senderJID) { 279 if (node != null) { 280 if (serverNodeProviders.get(node) != null) { 281 return serverNodeProviders.get(node).hasInfo(name, node, senderJID); 283 } 284 return false; 286 } 287 try { 288 return node == null && 291 (name == null || UserManager.getInstance().getUser(name) != null); 292 } 293 catch (UserNotFoundException e) { 294 return false; 295 } 296 } 297 298 public XDataFormImpl getExtendedInfo(String name, String node, JID senderJID) { 299 if (node != null && serverNodeProviders.get(node) != null) { 300 return serverNodeProviders.get(node).getExtendedInfo(name, node, senderJID); 302 } 303 return null; 304 } 305 }; 306 return discoInfoProvider; 307 } 308 } | Popular Tags |