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");
183 StringTokenizer st = new StringTokenizer(local, "\t");
184 while (st.hasMoreTokens())
186 String token = st.nextToken();
187 int bar = token.indexOf("|");
188 String url = token.substring(bar + 1);
189 boolean features = true, sequence = false;
190 if (url.startsWith("sequence:"))
192 url = url.substring(9);
193 // this source also serves sequences as well as features
196 createLocalSource(url, token.substring(0, bar), sequence,
203 private List<jalviewSourceI> appendLocalSources()
205 List<jalviewSourceI> srclist = new ArrayList<jalviewSourceI>();
206 addLocalDasSources();
207 sourceNames = new Hashtable<String, jalviewSourceI>();
208 if (dasSources != null)
210 for (jalviewSourceI src : dasSources)
212 sourceNames.put(src.getTitle(), src);
217 if (localSources == null)
221 Enumeration en = localSources.keys();
222 while (en.hasMoreElements())
224 String key = en.nextElement().toString();
225 jalviewSourceI jvsrc = localSources.get(key);
226 sourceNames.put(key, jvsrc);
237 public jalviewSourceI createLocalSource(String url, String name,
238 boolean sequence, boolean features)
240 SOURCE local = _createLocalSource(url, name, sequence, features);
242 if (localSources == null)
244 localSources = new Hashtable<String, jalviewSourceI>();
246 jalviewSourceI src = new JalviewSource(local, this, true);
247 localSources.put(local.getTitle(), src);
251 private SOURCE _createLocalSource(String url, String name,
252 boolean sequence, boolean features)
254 SOURCE local = new SOURCE();
257 local.setTitle(name);
258 local.setVERSION(new ArrayList<VERSION>());
259 VERSION v = new VERSION();
260 List<CAPABILITY> cp = new ArrayList<CAPABILITY>();
264 * Could try and synthesize a coordinate system for the source if needbe
265 * COORDINATES coord = new COORDINATES(); coord.setAuthority("NCBI");
266 * coord.setSource("Chromosome"); coord.setTaxid("9606");
267 * coord.setVersion("35"); version.getCOORDINATES().add(coord);
269 CAPABILITY cap = new CAPABILITY();
270 cap.setType("das1:" + Capabilities.SEQUENCE.getName());
271 cap.setQueryUri(url + "/sequence");
276 CAPABILITY cap = new CAPABILITY();
277 cap.setType("das1:" + Capabilities.FEATURES.getName());
278 cap.setQueryUri(url + "/features");
282 v.getCAPABILITY().addAll(cp);
283 local.getVERSION().add(v);
289 public jalviewSourceI getSource(String nickname)
291 return sourceNames.get(nickname);
295 public boolean removeLocalSource(jalviewSourceI source)
297 if (localSources.containsValue(source))
299 localSources.remove(source.getTitle());
300 sourceNames.remove(source.getTitle());
301 dasSources.remove(source);
302 jalview.bin.Cache.setProperty("DAS_LOCAL_SOURCE",
303 getLocalSourceString());
311 public void refreshSources()
319 public List<jalviewSourceI> resolveSourceNicknames(List<String> sources)
321 ArrayList<jalviewSourceI> resolved = new ArrayList<jalviewSourceI>();
322 if (sourceNames != null)
324 for (String src : sources)
326 jalviewSourceI dsrc = sourceNames.get(src);
337 public String getLocalSourceString()
339 if (localSources != null)
341 StringBuffer sb = new StringBuffer();
342 Enumeration en = localSources.keys();
343 while (en.hasMoreElements())
345 String token = en.nextElement().toString();
346 jalviewSourceI srco = localSources.get(token);
347 sb.append(token + "|"
348 + (srco.isSequenceSource() ? "sequence:" : "")
349 + srco.getUri() + "\t");
351 return sb.toString();
356 private static final Hashtable<URL, String> authStash;
359 authStash = new Hashtable<URL, String>();
363 // TODO: allow same credentials for https and http
364 authStash.put(new URL(
365 "http://www.compbio.dundee.ac.uk/geneweb/das/myseq/"),
366 "Basic SmltOm1pSg==");
367 } catch (MalformedURLException e)
369 // TODO Auto-generated catch block
375 public MultipleConnectionPropertyProviderI getSessionHandler()
381 public ConnectionPropertyProviderI getConnectionPropertyProviderFor(
385 final ConnectionPropertyProviderI conprov = new ConnectionPropertyProviderI()
387 boolean authed = false;
390 public void setConnectionProperties(HttpURLConnection connection)
392 String auth = authStash.get(connection.getURL());
393 if (auth != null && auth.length() > 0)
395 connection.setRequestProperty("Authorisation", auth);
405 public boolean getResponseProperties(HttpURLConnection connection)
407 String auth = authStash.get(connection.getURL());
408 if (auth != null && auth.length() == 0)
410 // don't attempt to check if we authed or not - user entered empty
418 // try and pass credentials.
421 // see if we should try and create a new auth record.
422 String ameth = connection.getHeaderField("X-DAS-AuthMethods");
423 Cache.log.debug("Could authenticate to " + connection.getURL()
424 + " with : " + ameth);
425 // TODO: search auth string and raise login box - return if auth was
431 // check to see if auth was successful
432 String asuc = connection
433 .getHeaderField("X-DAS_AuthenticatedUser");
434 if (asuc != null && asuc.trim().length() > 0)
436 // authentication was successful
437 Cache.log.debug("Authenticated successfully to "
438 + connection.getURL().toString());
441 // it wasn't - so we should tell the user it failed and ask if they
442 // want to attempt authentication again.
443 authStash.remove(connection.getURL());
444 // open a new login/password dialog with cancel button
445 // set new authStash content with password and return true
447 // User cancelled auth - so put empty string in stash to indicate we
448 // don't want to auth with this server.
449 // authStash.put(connection.getURL(), "");