2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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/>.
18 package jalview.ws.dbsources.das.datamodel;
20 import java.net.HttpURLConnection;
21 import java.net.MalformedURLException;
23 import java.util.ArrayList;
24 import java.util.Enumeration;
25 import java.util.HashMap;
26 import java.util.Hashtable;
27 import java.util.List;
28 import java.util.StringTokenizer;
30 import org.biodas.jdas.client.ConnectionPropertyProviderI;
31 import org.biodas.jdas.client.SourcesClient;
32 import org.biodas.jdas.client.threads.MultipleConnectionPropertyProviderI;
33 import org.biodas.jdas.dassources.Capabilities;
34 import org.biodas.jdas.schema.sources.CAPABILITY;
35 import org.biodas.jdas.schema.sources.SOURCE;
36 import org.biodas.jdas.schema.sources.SOURCES;
37 import org.biodas.jdas.schema.sources.VERSION;
39 import jalview.bin.Cache;
40 import jalview.ws.dbsources.das.api.DasSourceRegistryI;
41 import jalview.ws.dbsources.das.api.jalviewSourceI;
46 public class DasSourceRegistry implements DasSourceRegistryI,
47 MultipleConnectionPropertyProviderI
49 // private org.biodas.jdas.schema.sources.SOURCE[] dasSources = null;
50 private List<jalviewSourceI> dasSources = null;
52 private Hashtable<String, jalviewSourceI> sourceNames = null;
54 private Hashtable<String, jalviewSourceI> localSources = null;
56 public static String DEFAULT_REGISTRY = "http://www.dasregistry.org/das/";
59 * true if thread is running and we are talking to DAS registry service
61 private boolean loadingDasSources = false;
63 public boolean isLoadingDasSources()
65 return loadingDasSources;
68 public String getDasRegistryURL()
70 String registry = jalview.bin.Cache.getDefault("DAS_REGISTRY_URL",
73 if (registry.indexOf("/registry/das1/sources/") > -1)
75 jalview.bin.Cache.setProperty(jalview.bin.Cache.DAS_REGISTRY_URL,
77 registry = DEFAULT_REGISTRY;
79 if (registry.lastIndexOf("sources.xml") == registry.length() - 11)
81 // no trailing sources.xml document for registry in JDAS
82 jalview.bin.Cache.setProperty(
83 jalview.bin.Cache.DAS_REGISTRY_URL,
84 registry = registry.substring(0,
85 registry.lastIndexOf("sources.xml")));
91 * query the default DAS Source Registry for sources. Uses value of jalview
92 * property DAS_REGISTRY_URL and the DasSourceBrowser.DEFAULT_REGISTRY if that
95 * @return list of sources
97 private List<jalviewSourceI> getDASSources()
100 return getDASSources(getDasRegistryURL(), this);
104 * query the given URL for DasSources.
107 * return sources from registryURL
109 private static List<jalviewSourceI> getDASSources(String registryURL,
110 MultipleConnectionPropertyProviderI registry)
114 URL url = new URL(registryURL);
115 org.biodas.jdas.client.SourcesClientInterface client = new SourcesClient();
117 SOURCES sources = client.fetchDataRegistry(registryURL, null, null,
118 null, null, null, null);
120 List<SOURCE> dassources = sources.getSOURCE();
121 ArrayList<jalviewSourceI> dsrc = new ArrayList<jalviewSourceI>();
122 HashMap<String, Integer> latests = new HashMap<String, Integer>();
124 for (SOURCE src : dassources)
126 JalviewSource jsrc = new JalviewSource(src, registry, false);
127 latest = latests.get(jsrc.getSourceURL());
130 if (jsrc.isNewerThan(dsrc.get(latest.intValue())))
132 dsrc.set(latest.intValue(), jsrc);
136 System.out.println("Debug: Ignored older source "
142 latests.put(jsrc.getSourceURL(), Integer.valueOf(dsrc.size()));
147 } catch (Exception ex)
149 System.err.println("Failed to contact DAS1 registry at "
151 ex.printStackTrace();
152 return new ArrayList<jalviewSourceI>();
162 public List<jalviewSourceI> getSources()
164 if (dasSources == null)
166 dasSources = getDASSources();
168 return appendLocalSources();
172 * generate Sources from the local das source list
175 private void addLocalDasSources()
177 if (localSources == null)
179 // get local sources from properties and initialise the local source list
180 String local = jalview.bin.Cache.getProperty("DAS_LOCAL_SOURCE");
185 StringTokenizer st = new StringTokenizer(local, "\t");
186 while (st.hasMoreTokens())
188 String token = st.nextToken();
189 int bar = token.indexOf("|");
193 .println("Warning: DAS user local source appears to have no nickname (expected a '|' followed by nickname)\nOffending definition: '"
196 String url = token.substring(bar + 1);
197 boolean features = true, sequence = false;
198 if (url.startsWith("sequence:"))
200 url = url.substring(9);
201 // this source also serves sequences as well as features
208 createLocalSource(url, token.substring(0, bar), sequence,
213 createLocalSource(url, "User Source" + n, sequence, features);
215 } catch (Exception q)
218 .println("Unexpected exception when creating local source from '"
228 private List<jalviewSourceI> appendLocalSources()
230 List<jalviewSourceI> srclist = new ArrayList<jalviewSourceI>();
231 addLocalDasSources();
232 sourceNames = new Hashtable<String, jalviewSourceI>();
233 if (dasSources != null)
235 for (jalviewSourceI src : dasSources)
237 sourceNames.put(src.getTitle(), src);
242 if (localSources == null)
246 Enumeration en = localSources.keys();
247 while (en.hasMoreElements())
249 String key = en.nextElement().toString();
250 jalviewSourceI jvsrc = localSources.get(key);
251 sourceNames.put(key, jvsrc);
262 public jalviewSourceI createLocalSource(String url, String name,
263 boolean sequence, boolean features)
265 SOURCE local = _createLocalSource(url, name, sequence, features);
267 if (localSources == null)
269 localSources = new Hashtable<String, jalviewSourceI>();
271 jalviewSourceI src = new JalviewSource(local, this, true);
272 localSources.put(local.getTitle(), src);
276 private SOURCE _createLocalSource(String url, String name,
277 boolean sequence, boolean features)
279 SOURCE local = new SOURCE();
282 local.setTitle(name);
283 local.setVERSION(new ArrayList<VERSION>());
284 VERSION v = new VERSION();
285 List<CAPABILITY> cp = new ArrayList<CAPABILITY>();
289 * Could try and synthesize a coordinate system for the source if needbe
290 * COORDINATES coord = new COORDINATES(); coord.setAuthority("NCBI");
291 * coord.setSource("Chromosome"); coord.setTaxid("9606");
292 * coord.setVersion("35"); version.getCOORDINATES().add(coord);
294 CAPABILITY cap = new CAPABILITY();
295 cap.setType("das1:" + Capabilities.SEQUENCE.getName());
296 cap.setQueryUri(url + "/sequence");
301 CAPABILITY cap = new CAPABILITY();
302 cap.setType("das1:" + Capabilities.FEATURES.getName());
303 cap.setQueryUri(url + "/features");
307 v.getCAPABILITY().addAll(cp);
308 local.getVERSION().add(v);
314 public jalviewSourceI getSource(String nickname)
316 return sourceNames.get(nickname);
320 public boolean removeLocalSource(jalviewSourceI source)
322 if (localSources.containsValue(source))
324 localSources.remove(source.getTitle());
325 sourceNames.remove(source.getTitle());
326 dasSources.remove(source);
327 jalview.bin.Cache.setProperty("DAS_LOCAL_SOURCE",
328 getLocalSourceString());
336 public void refreshSources()
344 public List<jalviewSourceI> resolveSourceNicknames(List<String> sources)
346 ArrayList<jalviewSourceI> resolved = new ArrayList<jalviewSourceI>();
347 if (sourceNames != null)
349 for (String src : sources)
351 jalviewSourceI dsrc = sourceNames.get(src);
362 public String getLocalSourceString()
364 if (localSources != null)
366 StringBuffer sb = new StringBuffer();
367 Enumeration en = localSources.keys();
368 while (en.hasMoreElements())
370 String token = en.nextElement().toString();
371 jalviewSourceI srco = localSources.get(token);
372 sb.append(token + "|"
373 + (srco.isSequenceSource() ? "sequence:" : "")
374 + srco.getUri() + "\t");
376 return sb.toString();
381 private static final Hashtable<URL, String> authStash;
384 authStash = new Hashtable<URL, String>();
388 // TODO: allow same credentials for https and http
389 authStash.put(new URL(
390 "http://www.compbio.dundee.ac.uk/geneweb/das/myseq/"),
391 "Basic SmltOm1pSg==");
392 } catch (MalformedURLException e)
394 // TODO Auto-generated catch block
400 public MultipleConnectionPropertyProviderI getSessionHandler()
406 public ConnectionPropertyProviderI getConnectionPropertyProviderFor(
410 final ConnectionPropertyProviderI conprov = new ConnectionPropertyProviderI()
412 boolean authed = false;
415 public void setConnectionProperties(HttpURLConnection connection)
417 String auth = authStash.get(connection.getURL());
418 if (auth != null && auth.length() > 0)
420 connection.setRequestProperty("Authorisation", auth);
430 public boolean getResponseProperties(HttpURLConnection connection)
432 String auth = authStash.get(connection.getURL());
433 if (auth != null && auth.length() == 0)
435 // don't attempt to check if we authed or not - user entered empty
443 // try and pass credentials.
446 // see if we should try and create a new auth record.
447 String ameth = connection.getHeaderField("X-DAS-AuthMethods");
448 Cache.log.debug("Could authenticate to " + connection.getURL()
449 + " with : " + ameth);
450 // TODO: search auth string and raise login box - return if auth was
456 // check to see if auth was successful
457 String asuc = connection
458 .getHeaderField("X-DAS_AuthenticatedUser");
459 if (asuc != null && asuc.trim().length() > 0)
461 // authentication was successful
462 Cache.log.debug("Authenticated successfully to "
463 + connection.getURL().toString());
466 // it wasn't - so we should tell the user it failed and ask if they
467 // want to attempt authentication again.
468 authStash.remove(connection.getURL());
469 // open a new login/password dialog with cancel button
470 // set new authStash content with password and return true
472 // User cancelled auth - so put empty string in stash to indicate we
473 // don't want to auth with this server.
474 // authStash.put(connection.getURL(), "");