2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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 // public static String DEFAULT_REGISTRY = "http://www.dasregistry.org/das/";
60 public static String DEFAULT_REGISTRY = "http://www.ebi.ac.uk/das-srv/registry/das/";
63 * true if thread is running and we are talking to DAS registry service
65 private boolean loadingDasSources = false;
67 public boolean isLoadingDasSources()
69 return loadingDasSources;
73 public String getDasRegistryURL()
75 String registry = jalview.bin.Cache.getDefault("DAS_REGISTRY_URL",
78 if (registry.indexOf("/registry/das1/sources/") > -1)
80 jalview.bin.Cache.setProperty(jalview.bin.Cache.DAS_REGISTRY_URL,
82 registry = DEFAULT_REGISTRY;
84 if (registry.lastIndexOf("sources.xml") == registry.length() - 11)
86 // no trailing sources.xml document for registry in JDAS
87 jalview.bin.Cache.setProperty(
88 jalview.bin.Cache.DAS_REGISTRY_URL,
89 registry = registry.substring(0,
90 registry.lastIndexOf("sources.xml")));
96 * query the default DAS Source Registry for sources. Uses value of jalview
97 * property DAS_REGISTRY_URL and the DasSourceBrowser.DEFAULT_REGISTRY if that
100 * @return list of sources
102 private List<jalviewSourceI> getDASSources()
105 return getDASSources(getDasRegistryURL(), this);
109 * query the given URL for DasSources.
112 * return sources from registryURL
114 private static List<jalviewSourceI> getDASSources(String registryURL,
115 MultipleConnectionPropertyProviderI registry)
119 URL url = new URL(registryURL);
120 org.biodas.jdas.client.SourcesClientInterface client = new SourcesClient();
122 SOURCES sources = client.fetchDataRegistry(registryURL, null, null,
123 null, null, null, null);
125 List<SOURCE> dassources = sources.getSOURCE();
126 ArrayList<jalviewSourceI> dsrc = new ArrayList<jalviewSourceI>();
127 HashMap<String, Integer> latests = new HashMap<String, Integer>();
129 for (SOURCE src : dassources)
131 JalviewSource jsrc = new JalviewSource(src, registry, false);
132 latest = latests.get(jsrc.getSourceURL());
135 if (jsrc.isNewerThan(dsrc.get(latest.intValue())))
137 dsrc.set(latest.intValue(), jsrc);
141 System.out.println("Debug: Ignored older source "
147 latests.put(jsrc.getSourceURL(), Integer.valueOf(dsrc.size()));
152 } catch (Exception ex)
154 System.out.println("DAS1 registry at " + registryURL
155 + " no longer exists");
156 return new ArrayList<jalviewSourceI>();
166 public List<jalviewSourceI> getSources()
168 if (dasSources == null)
170 dasSources = getDASSources();
172 return appendLocalSources();
176 * generate Sources from the local das source list
179 private void addLocalDasSources()
181 if (localSources == null)
183 // get local sources from properties and initialise the local source list
184 String local = jalview.bin.Cache.getProperty("DAS_LOCAL_SOURCE");
189 StringTokenizer st = new StringTokenizer(local, "\t");
190 while (st.hasMoreTokens())
192 String token = st.nextToken();
193 int bar = token.indexOf("|");
197 .println("Warning: DAS user local source appears to have no nickname (expected a '|' followed by nickname)\nOffending definition: '"
200 String url = token.substring(bar + 1);
201 boolean features = true, sequence = false;
202 if (url.startsWith("sequence:"))
204 url = url.substring(9);
205 // this source also serves sequences as well as features
212 createLocalSource(url, token.substring(0, bar), sequence,
217 createLocalSource(url, "User Source" + n, sequence, features);
219 } catch (Exception q)
222 .println("Unexpected exception when creating local source from '"
232 private List<jalviewSourceI> appendLocalSources()
234 List<jalviewSourceI> srclist = new ArrayList<jalviewSourceI>();
235 addLocalDasSources();
236 sourceNames = new Hashtable<String, jalviewSourceI>();
237 if (dasSources != null)
239 for (jalviewSourceI src : dasSources)
241 sourceNames.put(src.getTitle(), src);
246 if (localSources == null)
250 Enumeration en = localSources.keys();
251 while (en.hasMoreElements())
253 String key = en.nextElement().toString();
254 jalviewSourceI jvsrc = localSources.get(key);
255 sourceNames.put(key, jvsrc);
266 public jalviewSourceI createLocalSource(String url, String name,
267 boolean sequence, boolean features)
269 SOURCE local = _createLocalSource(url, name, sequence, features);
271 if (localSources == null)
273 localSources = new Hashtable<String, jalviewSourceI>();
275 jalviewSourceI src = new JalviewSource(local, this, true);
276 localSources.put(local.getTitle(), src);
280 private SOURCE _createLocalSource(String url, String name,
281 boolean sequence, boolean features)
283 SOURCE local = new SOURCE();
286 local.setTitle(name);
287 local.setVERSION(new ArrayList<VERSION>());
288 VERSION v = new VERSION();
289 List<CAPABILITY> cp = new ArrayList<CAPABILITY>();
293 * Could try and synthesize a coordinate system for the source if needbe
294 * COORDINATES coord = new COORDINATES(); coord.setAuthority("NCBI");
295 * coord.setSource("Chromosome"); coord.setTaxid("9606");
296 * coord.setVersion("35"); version.getCOORDINATES().add(coord);
298 CAPABILITY cap = new CAPABILITY();
299 cap.setType("das1:" + Capabilities.SEQUENCE.getName());
300 cap.setQueryUri(url + "/sequence");
305 CAPABILITY cap = new CAPABILITY();
306 cap.setType("das1:" + Capabilities.FEATURES.getName());
307 cap.setQueryUri(url + "/features");
311 v.getCAPABILITY().addAll(cp);
312 local.getVERSION().add(v);
318 public jalviewSourceI getSource(String nickname)
320 return sourceNames.get(nickname);
324 public boolean removeLocalSource(jalviewSourceI source)
326 if (localSources.containsValue(source))
328 localSources.remove(source.getTitle());
329 sourceNames.remove(source.getTitle());
330 dasSources.remove(source);
331 jalview.bin.Cache.setProperty("DAS_LOCAL_SOURCE",
332 getLocalSourceString());
340 public void refreshSources()
348 public List<jalviewSourceI> resolveSourceNicknames(List<String> sources)
350 ArrayList<jalviewSourceI> resolved = new ArrayList<jalviewSourceI>();
351 if (sourceNames != null)
353 for (String src : sources)
355 jalviewSourceI dsrc = sourceNames.get(src);
366 public String getLocalSourceString()
368 if (localSources != null)
370 StringBuffer sb = new StringBuffer();
371 Enumeration en = localSources.keys();
372 while (en.hasMoreElements())
374 String token = en.nextElement().toString();
375 jalviewSourceI srco = localSources.get(token);
376 sb.append(token + "|"
377 + (srco.isSequenceSource() ? "sequence:" : "")
378 + srco.getUri() + "\t");
380 return sb.toString();
385 private static final Hashtable<URL, String> authStash;
388 authStash = new Hashtable<URL, String>();
392 // TODO: allow same credentials for https and http
393 authStash.put(new URL(
394 "http://www.compbio.dundee.ac.uk/geneweb/das/myseq/"),
395 "Basic SmltOm1pSg==");
396 } catch (MalformedURLException e)
398 // TODO Auto-generated catch block
404 public MultipleConnectionPropertyProviderI getSessionHandler()
410 public ConnectionPropertyProviderI getConnectionPropertyProviderFor(
414 final ConnectionPropertyProviderI conprov = new ConnectionPropertyProviderI()
416 boolean authed = false;
419 public void setConnectionProperties(HttpURLConnection connection)
421 String auth = authStash.get(connection.getURL());
422 if (auth != null && auth.length() > 0)
424 connection.setRequestProperty("Authorisation", auth);
434 public boolean getResponseProperties(HttpURLConnection connection)
436 String auth = authStash.get(connection.getURL());
437 if (auth != null && auth.length() == 0)
439 // don't attempt to check if we authed or not - user entered empty
447 // try and pass credentials.
450 // see if we should try and create a new auth record.
451 String ameth = connection.getHeaderField("X-DAS-AuthMethods");
452 Cache.log.debug("Could authenticate to " + connection.getURL()
453 + " with : " + ameth);
454 // TODO: search auth string and raise login box - return if auth was
460 // check to see if auth was successful
461 String asuc = connection
462 .getHeaderField("X-DAS_AuthenticatedUser");
463 if (asuc != null && asuc.trim().length() > 0)
465 // authentication was successful
466 Cache.log.debug("Authenticated successfully to "
467 + connection.getURL().toString());
470 // it wasn't - so we should tell the user it failed and ask if they
471 // want to attempt authentication again.
472 authStash.remove(connection.getURL());
473 // open a new login/password dialog with cancel button
474 // set new authStash content with password and return true
476 // User cancelled auth - so put empty string in stash to indicate we
477 // don't want to auth with this server.
478 // authStash.put(connection.getURL(), "");