4 package jalview.ws.dbsources.das.datamodel;
6 import java.net.HttpURLConnection;
7 import java.net.MalformedURLException;
9 import java.util.ArrayList;
10 import java.util.Collection;
11 import java.util.Enumeration;
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.Hashtable;
15 import java.util.List;
18 import java.util.StringTokenizer;
19 import java.util.Vector;
21 import javax.swing.JOptionPane;
23 import org.apache.http.auth.InvalidCredentialsException;
24 import org.biodas.jdas.client.ConnectionPropertyProviderI;
25 import org.biodas.jdas.client.SourcesClient;
26 import org.biodas.jdas.client.threads.MultipleConnectionPropertyProviderI;
27 import org.biodas.jdas.dassources.Capabilities;
28 import org.biodas.jdas.schema.sources.CAPABILITY;
29 import org.biodas.jdas.schema.sources.SOURCE;
30 import org.biodas.jdas.schema.sources.SOURCES;
31 import org.biodas.jdas.schema.sources.VERSION;
33 import jalview.bin.Cache;
34 import jalview.ws.dbsources.das.api.DasSourceRegistryI;
35 import jalview.ws.dbsources.das.api.jalviewSourceI;
40 public class DasSourceRegistry implements DasSourceRegistryI,
41 MultipleConnectionPropertyProviderI
43 // private org.biodas.jdas.schema.sources.SOURCE[] dasSources = null;
44 private List<jalviewSourceI> dasSources = null;
46 private Hashtable<String, jalviewSourceI> sourceNames = null;
48 private Hashtable<String, jalviewSourceI> localSources = null;
50 public static String DEFAULT_REGISTRY = "http://www.dasregistry.org/das/";
53 * true if thread is running and we are talking to DAS registry service
55 private boolean loadingDasSources = false;
57 public boolean isLoadingDasSources()
59 return loadingDasSources;
62 public String getDasRegistryURL()
64 String registry = jalview.bin.Cache.getDefault("DAS_REGISTRY_URL",
67 if (registry.indexOf("/registry/das1/sources/") > -1)
69 jalview.bin.Cache.setProperty(jalview.bin.Cache.DAS_REGISTRY_URL,
71 registry = DEFAULT_REGISTRY;
73 if (registry.lastIndexOf("sources.xml") == registry.length() - 11)
75 // no trailing sources.xml document for registry in JDAS
76 jalview.bin.Cache.setProperty(
77 jalview.bin.Cache.DAS_REGISTRY_URL,
78 registry = registry.substring(0,
79 registry.lastIndexOf("sources.xml")));
85 * query the default DAS Source Registry for sources. Uses value of jalview
86 * property DAS_REGISTRY_URL and the DasSourceBrowser.DEFAULT_REGISTRY if that
89 * @return list of sources
91 private List<jalviewSourceI> getDASSources()
94 return getDASSources(getDasRegistryURL(), this);
98 * query the given URL for DasSources.
101 * return sources from registryURL
103 private static List<jalviewSourceI> getDASSources(String registryURL,
104 MultipleConnectionPropertyProviderI registry)
108 URL url = new URL(registryURL);
109 org.biodas.jdas.client.SourcesClientInterface client = new SourcesClient();
111 SOURCES sources = client.fetchDataRegistry(registryURL, null, null,
112 null, null, null, null);
114 List<SOURCE> dassources = sources.getSOURCE();
115 ArrayList<jalviewSourceI> dsrc = new ArrayList<jalviewSourceI>();
116 HashMap<String, Integer> latests = new HashMap<String, Integer>();
118 for (SOURCE src : dassources)
120 JalviewSource jsrc = new JalviewSource(src, registry, false);
121 latest = latests.get(jsrc.getSourceURL());
124 if (jsrc.isNewerThan(dsrc.get(latest.intValue())))
126 dsrc.set(latest.intValue(), jsrc);
130 System.out.println("Debug: Ignored older source "
136 latests.put(jsrc.getSourceURL(), Integer.valueOf(dsrc.size()));
141 } catch (Exception ex)
143 System.err.println("Failed to contact DAS1 registry at "
145 ex.printStackTrace();
146 return new ArrayList<jalviewSourceI>();
156 public List<jalviewSourceI> getSources()
158 if (dasSources == null)
160 dasSources = getDASSources();
162 return appendLocalSources();
166 * generate Sources from the local das source list
169 private void addLocalDasSources()
171 if (localSources == null)
173 // get local sources from properties and initialise the local source list
174 String local = jalview.bin.Cache.getProperty("DAS_LOCAL_SOURCE");
177 StringTokenizer st = new StringTokenizer(local, "\t");
178 while (st.hasMoreTokens())
180 String token = st.nextToken();
181 int bar = token.indexOf("|");
182 String url = token.substring(bar + 1);
183 boolean features = true, sequence = false;
184 if (url.startsWith("sequence:"))
186 url = url.substring(9);
187 // this source also serves sequences as well as features
190 createLocalSource(url, token.substring(0, bar), sequence,
197 private List<jalviewSourceI> appendLocalSources()
199 List<jalviewSourceI> srclist = new ArrayList<jalviewSourceI>();
200 addLocalDasSources();
201 sourceNames = new Hashtable<String, jalviewSourceI>();
202 if (dasSources != null)
204 for (jalviewSourceI src : dasSources)
206 sourceNames.put(src.getTitle(), src);
211 if (localSources == null)
215 Enumeration en = localSources.keys();
216 while (en.hasMoreElements())
218 String key = en.nextElement().toString();
219 jalviewSourceI jvsrc = localSources.get(key);
220 sourceNames.put(key, jvsrc);
231 public jalviewSourceI createLocalSource(String url, String name,
232 boolean sequence, boolean features)
234 SOURCE local = _createLocalSource(url, name, sequence, features);
236 if (localSources == null)
238 localSources = new Hashtable<String, jalviewSourceI>();
240 jalviewSourceI src = new JalviewSource(local, this, true);
241 localSources.put(local.getTitle(), src);
245 private SOURCE _createLocalSource(String url, String name,
246 boolean sequence, boolean features)
248 SOURCE local = new SOURCE();
251 local.setTitle(name);
252 local.setVERSION(new ArrayList<VERSION>());
253 VERSION v = new VERSION();
254 List<CAPABILITY> cp = new ArrayList<CAPABILITY>();
258 * Could try and synthesize a coordinate system for the source if needbe
259 * COORDINATES coord = new COORDINATES(); coord.setAuthority("NCBI");
260 * coord.setSource("Chromosome"); coord.setTaxid("9606");
261 * coord.setVersion("35"); version.getCOORDINATES().add(coord);
263 CAPABILITY cap = new CAPABILITY();
264 cap.setType("das1:" + Capabilities.SEQUENCE.getName());
265 cap.setQueryUri(url + "/sequence");
270 CAPABILITY cap = new CAPABILITY();
271 cap.setType("das1:" + Capabilities.FEATURES.getName());
272 cap.setQueryUri(url + "/features");
276 v.getCAPABILITY().addAll(cp);
277 local.getVERSION().add(v);
283 public jalviewSourceI getSource(String nickname)
285 return sourceNames.get(nickname);
289 public boolean removeLocalSource(jalviewSourceI source)
291 if (localSources.containsValue(source))
293 localSources.remove(source.getTitle());
294 sourceNames.remove(source.getTitle());
295 dasSources.remove(source);
296 jalview.bin.Cache.setProperty("DAS_LOCAL_SOURCE",
297 getLocalSourceString());
305 public void refreshSources()
313 public List<jalviewSourceI> resolveSourceNicknames(List<String> sources)
315 ArrayList<jalviewSourceI> resolved = new ArrayList<jalviewSourceI>();
316 if (sourceNames != null)
318 for (String src : sources)
320 jalviewSourceI dsrc = sourceNames.get(src);
331 public String getLocalSourceString()
333 if (localSources != null)
335 StringBuffer sb = new StringBuffer();
336 Enumeration en = localSources.keys();
337 while (en.hasMoreElements())
339 String token = en.nextElement().toString();
340 jalviewSourceI srco = localSources.get(token);
341 sb.append(token + "|"
342 + (srco.isSequenceSource() ? "sequence:" : "")
343 + srco.getUri() + "\t");
345 return sb.toString();
350 private static final Hashtable<URL, String> authStash;
353 authStash = new Hashtable<URL, String>();
357 // TODO: allow same credentials for https and http
358 authStash.put(new URL(
359 "http://www.compbio.dundee.ac.uk/geneweb/das/myseq/"),
360 "Basic SmltOm1pSg==");
361 } catch (MalformedURLException e)
363 // TODO Auto-generated catch block
369 public MultipleConnectionPropertyProviderI getSessionHandler()
375 public ConnectionPropertyProviderI getConnectionPropertyProviderFor(
379 final ConnectionPropertyProviderI conprov = new ConnectionPropertyProviderI()
381 boolean authed = false;
384 public void setConnectionProperties(HttpURLConnection connection)
386 String auth = authStash.get(connection.getURL());
387 if (auth != null && auth.length() > 0)
389 connection.setRequestProperty("Authorisation", auth);
399 public boolean getResponseProperties(HttpURLConnection connection)
401 String auth = authStash.get(connection.getURL());
402 if (auth != null && auth.length() == 0)
404 // don't attempt to check if we authed or not - user entered empty
412 // try and pass credentials.
415 // see if we should try and create a new auth record.
416 String ameth = connection.getHeaderField("X-DAS-AuthMethods");
417 Cache.log.debug("Could authenticate to " + connection.getURL()
418 + " with : " + ameth);
419 // TODO: search auth string and raise login box - return if auth was
425 // check to see if auth was successful
426 String asuc = connection
427 .getHeaderField("X-DAS_AuthenticatedUser");
428 if (asuc != null && asuc.trim().length() > 0)
430 // authentication was successful
431 Cache.log.debug("Authenticated successfully to "
432 + connection.getURL().toString());
435 // it wasn't - so we should tell the user it failed and ask if they
436 // want to attempt authentication again.
437 authStash.remove(connection.getURL());
438 // open a new login/password dialog with cancel button
439 // set new authStash content with password and return true
441 // User cancelled auth - so put empty string in stash to indicate we
442 // don't want to auth with this server.
443 // authStash.put(connection.getURL(), "");