[1014] | 1 | /* |
---|
| 2 | * Jeti, a Java Jabber client, Copyright (C) 2007 E.S. de Boer |
---|
| 3 | * |
---|
| 4 | * This program is free software; you can redistribute it and/or modify |
---|
| 5 | * it under the terms of the GNU General Public License as published by |
---|
| 6 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | * (at your option) any later version. |
---|
| 8 | * |
---|
| 9 | * This program is distributed in the hope that it will be useful, |
---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | * GNU General Public License for more details. |
---|
| 13 | * |
---|
| 14 | * You should have received a copy of the GNU General Public License |
---|
| 15 | * along with this program; if not, write to the Free Software |
---|
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 17 | * |
---|
| 18 | * For questions, comments etc, |
---|
| 19 | * use the website at http://jeti.jabberstudio.org |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | //created on Mar 11, 2007 |
---|
| 23 | package nu.fw.jeti.backend; |
---|
| 24 | |
---|
| 25 | import java.io.BufferedWriter; |
---|
| 26 | import java.io.FileInputStream; |
---|
| 27 | import java.io.FileOutputStream; |
---|
| 28 | import java.io.IOException; |
---|
| 29 | import java.io.InputStream; |
---|
| 30 | import java.io.OutputStreamWriter; |
---|
| 31 | import java.net.URL; |
---|
| 32 | import java.util.HashMap; |
---|
| 33 | import java.util.Iterator; |
---|
| 34 | import java.util.LinkedList; |
---|
| 35 | import java.util.List; |
---|
| 36 | import java.util.Map; |
---|
| 37 | |
---|
| 38 | import javax.xml.parsers.SAXParser; |
---|
| 39 | import javax.xml.parsers.SAXParserFactory; |
---|
| 40 | |
---|
| 41 | import org.xml.sax.Attributes; |
---|
| 42 | import org.xml.sax.helpers.DefaultHandler; |
---|
| 43 | |
---|
| 44 | import nu.fw.jeti.events.DiscoveryListener; |
---|
| 45 | import nu.fw.jeti.jabber.Backend; |
---|
| 46 | import nu.fw.jeti.jabber.JID; |
---|
| 47 | import nu.fw.jeti.jabber.elements.DiscoItem; |
---|
| 48 | import nu.fw.jeti.jabber.elements.DiscoveryInfo; |
---|
| 49 | import nu.fw.jeti.jabber.elements.DiscoveryItem; |
---|
| 50 | import nu.fw.jeti.jabber.elements.IQDiscoInfo; |
---|
| 51 | import nu.fw.jeti.jabber.elements.IQXCaps; |
---|
| 52 | import nu.fw.jeti.jabber.handlers.IQDiscoInfoHandler; |
---|
| 53 | import nu.fw.jeti.util.Log; |
---|
| 54 | import nu.fw.jeti.util.PreferencesHandler; |
---|
| 55 | |
---|
| 56 | |
---|
| 57 | public class CapabilitiesCache implements DiscoveryListener { |
---|
| 58 | private Map capabilities; |
---|
| 59 | private Map cache; |
---|
| 60 | private Backend backend; |
---|
| 61 | |
---|
| 62 | |
---|
| 63 | public CapabilitiesCache(Backend backend) |
---|
| 64 | { |
---|
[1424] | 65 | try |
---|
| 66 | { |
---|
| 67 | this.backend = backend; |
---|
| 68 | capabilities = new HashMap(); |
---|
| 69 | cache = new HashMap(); |
---|
| 70 | } |
---|
| 71 | catch(Exception e){} |
---|
[1014] | 72 | } |
---|
| 73 | |
---|
| 74 | public void setCapability(JID jid, IQXCaps caps){ |
---|
| 75 | String cap = caps.getNode() + "#" + caps.getVer(); |
---|
| 76 | addCap(jid, cap); |
---|
| 77 | if(caps.getExt() != null){ |
---|
| 78 | String[] extensions = caps.getExt().split(" "); |
---|
| 79 | for (int i = 0; i < extensions.length; i++) { |
---|
| 80 | cap = caps.getNode() +"#"+ extensions[i]; |
---|
| 81 | addCap(jid, cap); |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | private void addCap(JID jid, String cap) { |
---|
| 87 | if(cache.containsKey(cap)){ |
---|
| 88 | getJIDCapabilities(jid).addCapabilities((IQDiscoInfo)cache.get(cap)); |
---|
| 89 | } |
---|
| 90 | else { |
---|
| 91 | backend.getInfo(jid, cap, this); |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | private Capabilities getJIDCapabilities(JID jid) { |
---|
| 97 | Capabilities jidCapabilities = null; |
---|
| 98 | if(capabilities.containsKey(jid)){ |
---|
| 99 | jidCapabilities = (Capabilities) capabilities.get(jid); |
---|
| 100 | } |
---|
| 101 | else { |
---|
| 102 | jidCapabilities = new Capabilities(); |
---|
| 103 | capabilities.put(jid,jidCapabilities); |
---|
| 104 | } |
---|
| 105 | return jidCapabilities; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | public void discoveryInfoResult(JID jid, DiscoveryInfo info){ |
---|
| 109 | if(!info.hasFeatures())return; |
---|
| 110 | IQDiscoInfo disco = (IQDiscoInfo)info; |
---|
| 111 | cache.put(disco.getNode(), disco); |
---|
| 112 | getJIDCapabilities(jid).addCapabilities(disco); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | public void discoveryItemResult(JID jid, DiscoveryItem item){} |
---|
| 116 | |
---|
| 117 | |
---|
| 118 | /** |
---|
| 119 | * Has JID jid capability capability |
---|
| 120 | * @param jid |
---|
| 121 | * @param capability |
---|
| 122 | */ |
---|
| 123 | public boolean hasCapability(JID jid, String capability){ |
---|
| 124 | return getJIDCapabilities(jid).hasCapability(capability); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | public void save() |
---|
| 128 | { |
---|
| 129 | StringBuffer xml = new StringBuffer(); |
---|
| 130 | xml.append("<?xml version='1.0'?>"); |
---|
| 131 | xml.append("<queries>"); |
---|
| 132 | for (Iterator iter = cache.values().iterator(); iter.hasNext();) { |
---|
| 133 | |
---|
| 134 | ((IQDiscoInfo)iter.next()).appendToXML(xml); |
---|
| 135 | xml.append("\n"); |
---|
| 136 | } |
---|
| 137 | xml.append("</queries>"); |
---|
| 138 | System.out.println(xml.toString()); |
---|
| 139 | try |
---|
| 140 | { |
---|
| 141 | BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( |
---|
| 142 | new FileOutputStream(Start.path + "capabilitiesCache.xml"), "UTF8")); |
---|
| 143 | writer.write(xml.toString()); |
---|
| 144 | writer.close(); |
---|
| 145 | } |
---|
| 146 | catch (IOException ex2) |
---|
| 147 | { |
---|
| 148 | ex2.printStackTrace(); |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | static class DiscoInfoHandler extends DefaultHandler |
---|
| 153 | { |
---|
| 154 | private Map queries; |
---|
| 155 | private IQDiscoInfoHandler discoHandler; |
---|
| 156 | |
---|
| 157 | public DiscoInfoHandler(Map queries) |
---|
| 158 | { |
---|
| 159 | this.queries = queries; |
---|
| 160 | discoHandler = new IQDiscoInfoHandler(); |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | public void startElement(String uri, String localName, String qName, Attributes attr) |
---|
| 164 | { |
---|
| 165 | if(qName.equals("queries")); //do nothing top level |
---|
| 166 | else if(qName.equals("query")) |
---|
| 167 | { |
---|
| 168 | discoHandler.startHandling(attr); |
---|
| 169 | } |
---|
| 170 | else discoHandler.startElement(qName, attr); |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | public void endElement(String uri, String localName, String qName){ |
---|
| 174 | if(qName.equals("query")){ |
---|
| 175 | IQDiscoInfo info = (IQDiscoInfo) discoHandler.build(); |
---|
| 176 | queries.put(info.getNode(),info); |
---|
| 177 | } |
---|
| 178 | } |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | } |
---|