2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.ws.dbsources.das.datamodel;
23 import jalview.bin.Cache;
24 import jalview.ws.dbsources.das.api.DasSourceRegistryI;
25 import jalview.ws.dbsources.das.api.jalviewSourceI;
27 import java.net.HttpURLConnection;
28 import java.net.MalformedURLException;
30 import java.util.ArrayList;
31 import java.util.Enumeration;
32 import java.util.HashMap;
33 import java.util.Hashtable;
34 import java.util.List;
35 import java.util.StringTokenizer;
37 import org.biodas.jdas.client.ConnectionPropertyProviderI;
38 import org.biodas.jdas.client.SourcesClient;
39 import org.biodas.jdas.client.threads.MultipleConnectionPropertyProviderI;
40 import org.biodas.jdas.dassources.Capabilities;
41 import org.biodas.jdas.schema.sources.CAPABILITY;
42 import org.biodas.jdas.schema.sources.SOURCE;
43 import org.biodas.jdas.schema.sources.SOURCES;
44 import org.biodas.jdas.schema.sources.VERSION;
49 public class DasSourceRegistry implements DasSourceRegistryI,
50 MultipleConnectionPropertyProviderI
52 // private org.biodas.jdas.schema.sources.SOURCE[] dasSources = null;
53 private List<jalviewSourceI> dasSources = null;
55 private Hashtable<String, jalviewSourceI> sourceNames = null;
57 private Hashtable<String, jalviewSourceI> localSources = null;
59 // This is the EBI DAS registry archive - temporary fix
60 public static String DEFAULT_REGISTRY = "http://www.ebi.ac.uk/das-srv/registry/das";
62 private static String[] blackList = new String[]
63 { "http://www.dasregistry.org", "sanger.ac.uk/registry/das1/sources/" };
65 * true if thread is running and we are talking to DAS registry service
67 private boolean loadingDasSources = false;
69 public boolean isLoadingDasSources()
71 return loadingDasSources;
74 public String getDasRegistryURL()
76 String registry = jalview.bin.Cache.getDefault("DAS_REGISTRY_URL",
78 // replace and update old entries
79 for (String blacklisted : blackList)
81 if (registry.indexOf(blacklisted) > -1)
83 jalview.bin.Cache.setProperty(jalview.bin.Cache.DAS_REGISTRY_URL,
85 registry = DEFAULT_REGISTRY;
89 if (registry.lastIndexOf("sources.xml") == registry.length() - 11)
91 // no trailing sources.xml document for registry in JDAS
92 jalview.bin.Cache.setProperty(
93 jalview.bin.Cache.DAS_REGISTRY_URL,
94 registry = registry.substring(0,
95 registry.lastIndexOf("sources.xml")));
101 * query the default DAS Source Registry for sources. Uses value of jalview
102 * property DAS_REGISTRY_URL and the DasSourceBrowser.DEFAULT_REGISTRY if that
105 * @return list of sources
107 private List<jalviewSourceI> getDASSources()
110 return getDASSources(getDasRegistryURL(), this);
114 * query the given URL for DasSources.
117 * return sources from registryURL
119 private static List<jalviewSourceI> getDASSources(String registryURL,
120 MultipleConnectionPropertyProviderI registry)
124 URL url = new URL(registryURL);
125 org.biodas.jdas.client.SourcesClientInterface client = new SourcesClient();
127 SOURCES sources = client.fetchDataRegistry(registryURL, null, null,
128 null, null, null, null);
130 List<SOURCE> dassources = sources.getSOURCE();
131 ArrayList<jalviewSourceI> dsrc = new ArrayList<jalviewSourceI>();
132 HashMap<String, Integer> latests = new HashMap<String, Integer>();
134 for (SOURCE src : dassources)
136 JalviewSource jsrc = new JalviewSource(src, registry, false);
137 latest = latests.get(jsrc.getSourceURL());
140 if (jsrc.isNewerThan(dsrc.get(latest.intValue())))
142 dsrc.set(latest.intValue(), jsrc);
146 System.out.println("Debug: Ignored older source "
152 latests.put(jsrc.getSourceURL(), Integer.valueOf(dsrc.size()));
157 } catch (Exception ex)
159 System.err.println("Failed to contact DAS1 registry at "
161 ex.printStackTrace();
162 return new ArrayList<jalviewSourceI>();
172 public List<jalviewSourceI> getSources()
174 if (dasSources == null)
176 dasSources = getDASSources();
178 return appendLocalSources();
182 * generate Sources from the local das source list
185 private void addLocalDasSources()
187 if (localSources == null)
189 // get local sources from properties and initialise the local source list
190 String local = jalview.bin.Cache.getProperty("DAS_LOCAL_SOURCE");
195 StringTokenizer st = new StringTokenizer(local, "\t");
196 while (st.hasMoreTokens())
198 String token = st.nextToken();
199 int bar = token.indexOf("|");
203 .println("Warning: DAS user local source appears to have no nickname (expected a '|' followed by nickname)\nOffending definition: '"
206 String url = token.substring(bar + 1);
207 boolean features = true, sequence = false;
208 if (url.startsWith("sequence:"))
210 url = url.substring(9);
211 // this source also serves sequences as well as features
218 createLocalSource(url, token.substring(0, bar), sequence,
223 createLocalSource(url, "User Source" + n, sequence, features);
225 } catch (Exception q)
228 .println("Unexpected exception when creating local source from '"
238 private List<jalviewSourceI> appendLocalSources()
240 List<jalviewSourceI> srclist = new ArrayList<jalviewSourceI>();
241 addLocalDasSources();
242 sourceNames = new Hashtable<String, jalviewSourceI>();
243 if (dasSources != null)
245 for (jalviewSourceI src : dasSources)
247 sourceNames.put(src.getTitle(), src);
252 if (localSources == null)
256 Enumeration en = localSources.keys();
257 while (en.hasMoreElements())
259 String key = en.nextElement().toString();
260 jalviewSourceI jvsrc = localSources.get(key);
261 sourceNames.put(key, jvsrc);
272 public jalviewSourceI createLocalSource(String url, String name,
273 boolean sequence, boolean features)
275 SOURCE local = _createLocalSource(url, name, sequence, features);
277 if (localSources == null)
279 localSources = new Hashtable<String, jalviewSourceI>();
281 jalviewSourceI src = new JalviewSource(local, this, true);
282 localSources.put(local.getTitle(), src);
286 private SOURCE _createLocalSource(String url, String name,
287 boolean sequence, boolean features)
289 SOURCE local = new SOURCE();
292 local.setTitle(name);
293 local.setVERSION(new ArrayList<VERSION>());
294 VERSION v = new VERSION();
295 List<CAPABILITY> cp = new ArrayList<CAPABILITY>();
299 * Could try and synthesize a coordinate system for the source if needbe
300 * COORDINATES coord = new COORDINATES(); coord.setAuthority("NCBI");
301 * coord.setSource("Chromosome"); coord.setTaxid("9606");
302 * coord.setVersion("35"); version.getCOORDINATES().add(coord);
304 CAPABILITY cap = new CAPABILITY();
305 cap.setType("das1:" + Capabilities.SEQUENCE.getName());
306 cap.setQueryUri(url + "/sequence");
311 CAPABILITY cap = new CAPABILITY();
312 cap.setType("das1:" + Capabilities.FEATURES.getName());
313 cap.setQueryUri(url + "/features");
317 v.getCAPABILITY().addAll(cp);
318 local.getVERSION().add(v);
324 public jalviewSourceI getSource(String nickname)
326 return sourceNames.get(nickname);
330 public boolean removeLocalSource(jalviewSourceI source)
332 if (localSources.containsValue(source))
334 localSources.remove(source.getTitle());
335 sourceNames.remove(source.getTitle());
336 dasSources.remove(source);
337 jalview.bin.Cache.setProperty("DAS_LOCAL_SOURCE",
338 getLocalSourceString());
346 public void refreshSources()
354 public List<jalviewSourceI> resolveSourceNicknames(List<String> sources)
356 ArrayList<jalviewSourceI> resolved = new ArrayList<jalviewSourceI>();
357 if (sourceNames != null)
359 for (String src : sources)
361 jalviewSourceI dsrc = sourceNames.get(src);
372 public String getLocalSourceString()
374 if (localSources != null)
376 StringBuffer sb = new StringBuffer();
377 Enumeration en = localSources.keys();
378 while (en.hasMoreElements())
380 String token = en.nextElement().toString();
381 jalviewSourceI srco = localSources.get(token);
382 sb.append(token + "|"
383 + (srco.isSequenceSource() ? "sequence:" : "")
384 + srco.getUri() + "\t");
386 return sb.toString();
391 private static final Hashtable<URL, String> authStash;
394 authStash = new Hashtable<URL, String>();
398 // TODO: allow same credentials for https and http
399 authStash.put(new URL(
400 "http://www.compbio.dundee.ac.uk/geneweb/das/myseq/"),
401 "Basic SmltOm1pSg==");
402 } catch (MalformedURLException e)
404 // TODO Auto-generated catch block
410 public MultipleConnectionPropertyProviderI getSessionHandler()
416 public ConnectionPropertyProviderI getConnectionPropertyProviderFor(
420 final ConnectionPropertyProviderI conprov = new ConnectionPropertyProviderI()
422 boolean authed = false;
425 public void setConnectionProperties(HttpURLConnection connection)
427 String auth = authStash.get(connection.getURL());
428 if (auth != null && auth.length() > 0)
430 connection.setRequestProperty("Authorisation", auth);
440 public boolean getResponseProperties(HttpURLConnection connection)
442 String auth = authStash.get(connection.getURL());
443 if (auth != null && auth.length() == 0)
445 // don't attempt to check if we authed or not - user entered empty
453 // try and pass credentials.
456 // see if we should try and create a new auth record.
457 String ameth = connection.getHeaderField("X-DAS-AuthMethods");
458 Cache.log.debug("Could authenticate to " + connection.getURL()
459 + " with : " + ameth);
460 // TODO: search auth string and raise login box - return if auth was
466 // check to see if auth was successful
467 String asuc = connection
468 .getHeaderField("X-DAS_AuthenticatedUser");
469 if (asuc != null && asuc.trim().length() > 0)
471 // authentication was successful
472 Cache.log.debug("Authenticated successfully to "
473 + connection.getURL().toString());
476 // it wasn't - so we should tell the user it failed and ask if they
477 // want to attempt authentication again.
478 authStash.remove(connection.getURL());
479 // open a new login/password dialog with cancel button
480 // set new authStash content with password and return true
482 // User cancelled auth - so put empty string in stash to indicate we
483 // don't want to auth with this server.
484 // authStash.put(connection.getURL(), "");