2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3 * Copyright (C) 2014 The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.ws.dbsources.das.datamodel;
21 import java.net.HttpURLConnection;
22 import java.net.MalformedURLException;
24 import java.util.ArrayList;
25 import java.util.Enumeration;
26 import java.util.HashMap;
27 import java.util.Hashtable;
28 import java.util.List;
29 import java.util.StringTokenizer;
31 import org.biodas.jdas.client.ConnectionPropertyProviderI;
32 import org.biodas.jdas.client.SourcesClient;
33 import org.biodas.jdas.client.threads.MultipleConnectionPropertyProviderI;
34 import org.biodas.jdas.dassources.Capabilities;
35 import org.biodas.jdas.schema.sources.CAPABILITY;
36 import org.biodas.jdas.schema.sources.SOURCE;
37 import org.biodas.jdas.schema.sources.SOURCES;
38 import org.biodas.jdas.schema.sources.VERSION;
40 import jalview.bin.Cache;
41 import jalview.ws.dbsources.das.api.DasSourceRegistryI;
42 import jalview.ws.dbsources.das.api.jalviewSourceI;
47 public class DasSourceRegistry implements DasSourceRegistryI,
48 MultipleConnectionPropertyProviderI
50 // private org.biodas.jdas.schema.sources.SOURCE[] dasSources = null;
51 private List<jalviewSourceI> dasSources = null;
53 private Hashtable<String, jalviewSourceI> sourceNames = null;
55 private Hashtable<String, jalviewSourceI> localSources = null;
57 public static String DEFAULT_REGISTRY = "http://www.dasregistry.org/das/";
60 * true if thread is running and we are talking to DAS registry service
62 private boolean loadingDasSources = false;
64 public boolean isLoadingDasSources()
66 return loadingDasSources;
69 public String getDasRegistryURL()
71 String registry = jalview.bin.Cache.getDefault("DAS_REGISTRY_URL",
74 if (registry.indexOf("/registry/das1/sources/") > -1)
76 jalview.bin.Cache.setProperty(jalview.bin.Cache.DAS_REGISTRY_URL,
78 registry = DEFAULT_REGISTRY;
80 if (registry.lastIndexOf("sources.xml") == registry.length() - 11)
82 // no trailing sources.xml document for registry in JDAS
83 jalview.bin.Cache.setProperty(
84 jalview.bin.Cache.DAS_REGISTRY_URL,
85 registry = registry.substring(0,
86 registry.lastIndexOf("sources.xml")));
92 * query the default DAS Source Registry for sources. Uses value of jalview
93 * property DAS_REGISTRY_URL and the DasSourceBrowser.DEFAULT_REGISTRY if that
96 * @return list of sources
98 private List<jalviewSourceI> getDASSources()
101 return getDASSources(getDasRegistryURL(), this);
105 * query the given URL for DasSources.
108 * return sources from registryURL
110 private static List<jalviewSourceI> getDASSources(String registryURL,
111 MultipleConnectionPropertyProviderI registry)
115 URL url = new URL(registryURL);
116 org.biodas.jdas.client.SourcesClientInterface client = new SourcesClient();
118 SOURCES sources = client.fetchDataRegistry(registryURL, null, null,
119 null, null, null, null);
121 List<SOURCE> dassources = sources.getSOURCE();
122 ArrayList<jalviewSourceI> dsrc = new ArrayList<jalviewSourceI>();
123 HashMap<String, Integer> latests = new HashMap<String, Integer>();
125 for (SOURCE src : dassources)
127 JalviewSource jsrc = new JalviewSource(src, registry, false);
128 latest = latests.get(jsrc.getSourceURL());
131 if (jsrc.isNewerThan(dsrc.get(latest.intValue())))
133 dsrc.set(latest.intValue(), jsrc);
137 System.out.println("Debug: Ignored older source "
143 latests.put(jsrc.getSourceURL(), Integer.valueOf(dsrc.size()));
148 } catch (Exception ex)
150 System.err.println("Failed to contact DAS1 registry at "
152 ex.printStackTrace();
153 return new ArrayList<jalviewSourceI>();
163 public List<jalviewSourceI> getSources()
165 if (dasSources == null)
167 dasSources = getDASSources();
169 return appendLocalSources();
173 * generate Sources from the local das source list
176 private void addLocalDasSources()
178 if (localSources == null)
180 // get local sources from properties and initialise the local source list
181 String local = jalview.bin.Cache.getProperty("DAS_LOCAL_SOURCE");
186 StringTokenizer st = new StringTokenizer(local, "\t");
187 while (st.hasMoreTokens())
189 String token = st.nextToken();
190 int bar = token.indexOf("|");
194 .println("Warning: DAS user local source appears to have no nickname (expected a '|' followed by nickname)\nOffending definition: '"
197 String url = token.substring(bar + 1);
198 boolean features = true, sequence = false;
199 if (url.startsWith("sequence:"))
201 url = url.substring(9);
202 // this source also serves sequences as well as features
209 createLocalSource(url, token.substring(0, bar), sequence,
214 createLocalSource(url, "User Source" + n, sequence, features);
216 } catch (Exception q)
219 .println("Unexpected exception when creating local source from '"
229 private List<jalviewSourceI> appendLocalSources()
231 List<jalviewSourceI> srclist = new ArrayList<jalviewSourceI>();
232 addLocalDasSources();
233 sourceNames = new Hashtable<String, jalviewSourceI>();
234 if (dasSources != null)
236 for (jalviewSourceI src : dasSources)
238 sourceNames.put(src.getTitle(), src);
243 if (localSources == null)
247 Enumeration en = localSources.keys();
248 while (en.hasMoreElements())
250 String key = en.nextElement().toString();
251 jalviewSourceI jvsrc = localSources.get(key);
252 sourceNames.put(key, jvsrc);
263 public jalviewSourceI createLocalSource(String url, String name,
264 boolean sequence, boolean features)
266 SOURCE local = _createLocalSource(url, name, sequence, features);
268 if (localSources == null)
270 localSources = new Hashtable<String, jalviewSourceI>();
272 jalviewSourceI src = new JalviewSource(local, this, true);
273 localSources.put(local.getTitle(), src);
277 private SOURCE _createLocalSource(String url, String name,
278 boolean sequence, boolean features)
280 SOURCE local = new SOURCE();
283 local.setTitle(name);
284 local.setVERSION(new ArrayList<VERSION>());
285 VERSION v = new VERSION();
286 List<CAPABILITY> cp = new ArrayList<CAPABILITY>();
290 * Could try and synthesize a coordinate system for the source if needbe
291 * COORDINATES coord = new COORDINATES(); coord.setAuthority("NCBI");
292 * coord.setSource("Chromosome"); coord.setTaxid("9606");
293 * coord.setVersion("35"); version.getCOORDINATES().add(coord);
295 CAPABILITY cap = new CAPABILITY();
296 cap.setType("das1:" + Capabilities.SEQUENCE.getName());
297 cap.setQueryUri(url + "/sequence");
302 CAPABILITY cap = new CAPABILITY();
303 cap.setType("das1:" + Capabilities.FEATURES.getName());
304 cap.setQueryUri(url + "/features");
308 v.getCAPABILITY().addAll(cp);
309 local.getVERSION().add(v);
315 public jalviewSourceI getSource(String nickname)
317 return sourceNames.get(nickname);
321 public boolean removeLocalSource(jalviewSourceI source)
323 if (localSources.containsValue(source))
325 localSources.remove(source.getTitle());
326 sourceNames.remove(source.getTitle());
327 dasSources.remove(source);
328 jalview.bin.Cache.setProperty("DAS_LOCAL_SOURCE",
329 getLocalSourceString());
337 public void refreshSources()
345 public List<jalviewSourceI> resolveSourceNicknames(List<String> sources)
347 ArrayList<jalviewSourceI> resolved = new ArrayList<jalviewSourceI>();
348 if (sourceNames != null)
350 for (String src : sources)
352 jalviewSourceI dsrc = sourceNames.get(src);
363 public String getLocalSourceString()
365 if (localSources != null)
367 StringBuffer sb = new StringBuffer();
368 Enumeration en = localSources.keys();
369 while (en.hasMoreElements())
371 String token = en.nextElement().toString();
372 jalviewSourceI srco = localSources.get(token);
373 sb.append(token + "|"
374 + (srco.isSequenceSource() ? "sequence:" : "")
375 + srco.getUri() + "\t");
377 return sb.toString();
382 private static final Hashtable<URL, String> authStash;
385 authStash = new Hashtable<URL, String>();
389 // TODO: allow same credentials for https and http
390 authStash.put(new URL(
391 "http://www.compbio.dundee.ac.uk/geneweb/das/myseq/"),
392 "Basic SmltOm1pSg==");
393 } catch (MalformedURLException e)
395 // TODO Auto-generated catch block
401 public MultipleConnectionPropertyProviderI getSessionHandler()
407 public ConnectionPropertyProviderI getConnectionPropertyProviderFor(
411 final ConnectionPropertyProviderI conprov = new ConnectionPropertyProviderI()
413 boolean authed = false;
416 public void setConnectionProperties(HttpURLConnection connection)
418 String auth = authStash.get(connection.getURL());
419 if (auth != null && auth.length() > 0)
421 connection.setRequestProperty("Authorisation", auth);
431 public boolean getResponseProperties(HttpURLConnection connection)
433 String auth = authStash.get(connection.getURL());
434 if (auth != null && auth.length() == 0)
436 // don't attempt to check if we authed or not - user entered empty
444 // try and pass credentials.
447 // see if we should try and create a new auth record.
448 String ameth = connection.getHeaderField("X-DAS-AuthMethods");
449 Cache.log.debug("Could authenticate to " + connection.getURL()
450 + " with : " + ameth);
451 // TODO: search auth string and raise login box - return if auth was
457 // check to see if auth was successful
458 String asuc = connection
459 .getHeaderField("X-DAS_AuthenticatedUser");
460 if (asuc != null && asuc.trim().length() > 0)
462 // authentication was successful
463 Cache.log.debug("Authenticated successfully to "
464 + connection.getURL().toString());
467 // it wasn't - so we should tell the user it failed and ask if they
468 // want to attempt authentication again.
469 authStash.remove(connection.getURL());
470 // open a new login/password dialog with cancel button
471 // set new authStash content with password and return true
473 // User cancelled auth - so put empty string in stash to indicate we
474 // don't want to auth with this server.
475 // authStash.put(connection.getURL(), "");