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.Session; 19 import org.jivesoftware.messenger.SessionManager; 20 import org.jivesoftware.messenger.XMPPServer; 21 import org.jivesoftware.messenger.auth.UnauthorizedException; 22 import org.jivesoftware.messenger.handler.IQHandler; 23 import org.jivesoftware.messenger.roster.RosterItem; 24 import org.jivesoftware.messenger.user.User; 25 import org.jivesoftware.messenger.user.UserManager; 26 import org.jivesoftware.messenger.user.UserNotFoundException; 27 import org.xmpp.packet.IQ; 28 import org.xmpp.packet.JID; 29 import org.xmpp.packet.PacketError; 30 31 import java.util.*; 32 import java.util.concurrent.ConcurrentHashMap ; 33 34 56 public class IQDiscoItemsHandler extends IQHandler implements ServerFeaturesProvider { 57 58 private HashMap entities = new HashMap (); 59 private List<Element> serverItems = new ArrayList<Element>(); 60 private Map <String , DiscoItemsProvider> serverNodeProviders = 61 new ConcurrentHashMap <String , DiscoItemsProvider>(); 62 private IQHandlerInfo info; 63 private IQDiscoInfoHandler infoHandler; 64 65 public IQDiscoItemsHandler() { 66 super("XMPP Disco Items Handler"); 67 info = new IQHandlerInfo("query", "http://jabber.org/protocol/disco#items"); 68 } 69 70 public IQHandlerInfo getInfo() { 71 return info; 72 } 73 74 public IQ handleIQ(IQ packet) throws UnauthorizedException { 75 77 IQ reply = IQ.createResultIQ(packet); 81 82 if (IQ.Type.set == packet.getType()) { 84 reply.setChildElement(packet.getChildElement().createCopy()); 85 reply.setError(PacketError.Condition.feature_not_implemented); 86 return reply; 87 } 88 89 DiscoItemsProvider itemsProvider = getProvider(packet.getTo() == null ? 94 XMPPServer.getInstance().getServerInfo().getName() : packet.getTo().getDomain()); 95 if (itemsProvider != null) { 96 String name = packet.getTo() == null ? null : packet.getTo().getNode(); 98 if (name == null || name.trim().length() == 0) { 99 name = null; 100 } 101 Element iq = packet.getChildElement(); 103 String node = iq.attributeValue("node"); 104 105 Iterator<Element> itemsItr = itemsProvider.getItems(name, node, packet.getFrom()); 107 if (itemsItr != null) { 108 reply.setChildElement(iq.createCopy()); 109 Element queryElement = reply.getChildElement(); 110 111 Element item; 113 while (itemsItr.hasNext()) { 114 item = itemsItr.next(); 115 item.setQName(new QName(item.getName(), queryElement.getNamespace())); 116 queryElement.add(item.createCopy()); 117 } 118 ; 119 } 120 else { 121 reply.setChildElement(packet.getChildElement().createCopy()); 124 reply.setError(PacketError.Condition.item_not_found); 125 } 126 } 127 else { 128 reply.setChildElement(packet.getChildElement().createCopy()); 130 reply.setError(PacketError.Condition.item_not_found); 131 } 132 133 return reply; 134 } 135 136 144 private DiscoItemsProvider getProvider(String name) { 145 return (DiscoItemsProvider)entities.get(name); 146 } 147 148 156 protected void setProvider(String name, DiscoItemsProvider provider) { 157 entities.put(name, provider); 158 } 159 160 165 protected void removeProvider(String name) { 166 entities.remove(name); 167 } 168 169 177 private void addServerItemsProvider(ServerItemsProvider provider) { 178 DiscoServerItem discoItem; 179 for (Iterator it = provider.getItems(); it.hasNext();) { 180 discoItem = (DiscoServerItem)it.next(); 181 Element element = DocumentHelper.createElement("item"); 183 element.addAttribute("jid", discoItem.getJID()); 184 element.addAttribute("node", discoItem.getNode()); 185 element.addAttribute("name", discoItem.getName()); 186 serverItems.add(element); 188 189 String host = new JID(discoItem.getJID()).getDomain(); 191 infoHandler.setProvider(host, discoItem.getDiscoInfoProvider()); 192 setProvider(host, discoItem.getDiscoItemsProvider()); 193 } 194 } 195 196 205 public void setServerNodeInfoProvider(String node, DiscoItemsProvider provider) { 206 serverNodeProviders.put(node, provider); 207 } 208 209 215 public void removeServerNodeInfoProvider(String node) { 216 serverNodeProviders.remove(node); 217 } 218 219 226 public void addComponentItem(String jid, String name) { 227 removeComponentItem(jid); 230 231 Element element = DocumentHelper.createElement("item"); 233 element.addAttribute("jid", jid); 234 element.addAttribute("name", name); 235 serverItems.add(element); 237 } 238 239 244 public void removeComponentItem(String jid) { 245 for (Iterator<Element> it = serverItems.iterator(); it.hasNext();) { 246 if (jid.equals(it.next().attributeValue("jid"))) { 247 it.remove(); 248 } 249 } 250 } 251 252 public void initialize(XMPPServer server) { 253 super.initialize(server); 254 infoHandler = server.getIQDiscoInfoHandler(); 257 setProvider(server.getServerInfo().getName(), getServerItemsProvider()); 258 } 259 260 public void start() throws IllegalStateException { 261 super.start(); 262 for (ServerItemsProvider provider : XMPPServer.getInstance().getServerItemsProviders()) { 263 addServerItemsProvider(provider); 264 } 265 } 266 267 public Iterator getFeatures() { 268 ArrayList features = new ArrayList(); 269 features.add("http://jabber.org/protocol/disco#items"); 270 return features.iterator(); 273 } 274 275 private DiscoItemsProvider getServerItemsProvider() { 276 DiscoItemsProvider discoItemsProvider = new DiscoItemsProvider() { 277 public Iterator<Element> getItems(String name, String node, JID senderJID) { 278 if (node != null) { 279 if (serverNodeProviders.get(node) != null) { 281 return serverNodeProviders.get(node).getItems(name, node, senderJID); 282 } 283 return null; 284 } 285 if (name == null) { 286 return serverItems.iterator(); 287 } 288 else { 289 List<Element> answer = new ArrayList<Element>(); 290 try { 291 User user = UserManager.getInstance().getUser(name); 292 RosterItem item = user.getRoster().getRosterItem(senderJID); 293 if (item.getSubStatus() == RosterItem.SUB_FROM || 296 item.getSubStatus() == RosterItem.SUB_BOTH) { 297 for (Session session : SessionManager.getInstance().getSessions(name)) { 298 Element element = DocumentHelper.createElement("item"); 299 element.addAttribute("jid", session.getAddress().toString()); 300 answer.add(element); 301 } 302 } 303 return answer.iterator(); 304 } 305 catch (UserNotFoundException e) { 306 return answer.iterator(); 307 } 308 } 309 } 310 }; 311 return discoItemsProvider; 312 } 313 } | Popular Tags |