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.Collection;
25 import java.util.Enumeration;
26 import java.util.HashMap;
27 import java.util.HashSet;
28 import java.util.Hashtable;
29 import java.util.List;
32 import java.util.StringTokenizer;
33 import java.util.Vector;
35 import javax.swing.JOptionPane;
37 import org.apache.http.auth.InvalidCredentialsException;
38 import org.biodas.jdas.client.ConnectionPropertyProviderI;
39 import org.biodas.jdas.client.SourcesClient;
40 import org.biodas.jdas.client.threads.MultipleConnectionPropertyProviderI;
41 import org.biodas.jdas.dassources.Capabilities;
42 import org.biodas.jdas.schema.sources.CAPABILITY;
43 import org.biodas.jdas.schema.sources.SOURCE;
44 import org.biodas.jdas.schema.sources.SOURCES;
45 import org.biodas.jdas.schema.sources.VERSION;
47 import jalview.bin.Cache;
48 import jalview.ws.dbsources.das.api.DasSourceRegistryI;
49 import jalview.ws.dbsources.das.api.jalviewSourceI;
54 public class DasSourceRegistry implements DasSourceRegistryI,
55 MultipleConnectionPropertyProviderI
57 // private org.biodas.jdas.schema.sources.SOURCE[] dasSources = null;
58 private List<jalviewSourceI> dasSources = null;
60 private Hashtable<String, jalviewSourceI> sourceNames = null;
62 private Hashtable<String, jalviewSourceI> localSources = null;
64 public static String DEFAULT_REGISTRY = "http://www.dasregistry.org/das/";
67 * true if thread is running and we are talking to DAS registry service
69 private boolean loadingDasSources = false;
71 public boolean isLoadingDasSources()
73 return loadingDasSources;
76 public String getDasRegistryURL()
78 String registry = jalview.bin.Cache.getDefault("DAS_REGISTRY_URL",
81 if (registry.indexOf("/registry/das1/sources/") > -1)
83 jalview.bin.Cache.setProperty(jalview.bin.Cache.DAS_REGISTRY_URL,
85 registry = DEFAULT_REGISTRY;
87 if (registry.lastIndexOf("sources.xml") == registry.length() - 11)
89 // no trailing sources.xml document for registry in JDAS
90 jalview.bin.Cache.setProperty(
91 jalview.bin.Cache.DAS_REGISTRY_URL,
92 registry = registry.substring(0,
93 registry.lastIndexOf("sources.xml")));
99 * query the default DAS Source Registry for sources. Uses value of jalview
100 * property DAS_REGISTRY_URL and the DasSourceBrowser.DEFAULT_REGISTRY if that
103 * @return list of sources
105 private List<jalviewSourceI> getDASSources()
108 return getDASSources(getDasRegistryURL(), this);
112 * query the given URL for DasSources.
115 * return sources from registryURL
117 private static List<jalviewSourceI> getDASSources(String registryURL,
118 MultipleConnectionPropertyProviderI registry)
122 URL url = new URL(registryURL);
123 org.biodas.jdas.client.SourcesClientInterface client = new SourcesClient();
125 SOURCES sources = client.fetchDataRegistry(registryURL, null, null,
126 null, null, null, null);
128 List<SOURCE> dassources = sources.getSOURCE();
129 ArrayList<jalviewSourceI> dsrc = new ArrayList<jalviewSourceI>();
130 HashMap<String, Integer> latests = new HashMap<String, Integer>();
132 for (SOURCE src : dassources)
134 JalviewSource jsrc = new JalviewSource(src, registry, false);
135 latest = latests.get(jsrc.getSourceURL());
138 if (jsrc.isNewerThan(dsrc.get(latest.intValue())))
140 dsrc.set(latest.intValue(), jsrc);
144 System.out.println("Debug: Ignored older source "
150 latests.put(jsrc.getSourceURL(), Integer.valueOf(dsrc.size()));
155 } catch (Exception ex)
157 System.err.println("Failed to contact DAS1 registry at "
159 ex.printStackTrace();
160 return new ArrayList<jalviewSourceI>();
170 public List<jalviewSourceI> getSources()
172 if (dasSources == null)
174 dasSources = getDASSources();
176 return appendLocalSources();
180 * generate Sources from the local das source list
183 private void addLocalDasSources()
185 if (localSources == null)
187 // get local sources from properties and initialise the local source list
188 String local = jalview.bin.Cache.getProperty("DAS_LOCAL_SOURCE");
191 StringTokenizer st = new StringTokenizer(local, "\t");
192 while (st.hasMoreTokens())
194 String token = st.nextToken();
195 int bar = token.indexOf("|");
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
204 createLocalSource(url, token.substring(0, bar), sequence,
211 private List<jalviewSourceI> appendLocalSources()
213 List<jalviewSourceI> srclist = new ArrayList<jalviewSourceI>();
214 addLocalDasSources();
215 sourceNames = new Hashtable<String, jalviewSourceI>();
216 if (dasSources != null)
218 for (jalviewSourceI src : dasSources)
220 sourceNames.put(src.getTitle(), src);
225 if (localSources == null)
229 Enumeration en = localSources.keys();
230 while (en.hasMoreElements())
232 String key = en.nextElement().toString();
233 jalviewSourceI jvsrc = localSources.get(key);
234 sourceNames.put(key, jvsrc);
245 public jalviewSourceI createLocalSource(String url, String name,
246 boolean sequence, boolean features)
248 SOURCE local = _createLocalSource(url, name, sequence, features);
250 if (localSources == null)
252 localSources = new Hashtable<String, jalviewSourceI>();
254 jalviewSourceI src = new JalviewSource(local, this, true);
255 localSources.put(local.getTitle(), src);
259 private SOURCE _createLocalSource(String url, String name,
260 boolean sequence, boolean features)
262 SOURCE local = new SOURCE();
265 local.setTitle(name);
266 local.setVERSION(new ArrayList<VERSION>());
267 VERSION v = new VERSION();
268 List<CAPABILITY> cp = new ArrayList<CAPABILITY>();
272 * Could try and synthesize a coordinate system for the source if needbe
273 * COORDINATES coord = new COORDINATES(); coord.setAuthority("NCBI");
274 * coord.setSource("Chromosome"); coord.setTaxid("9606");
275 * coord.setVersion("35"); version.getCOORDINATES().add(coord);
277 CAPABILITY cap = new CAPABILITY();
278 cap.setType("das1:" + Capabilities.SEQUENCE.getName());
279 cap.setQueryUri(url + "/sequence");
284 CAPABILITY cap = new CAPABILITY();
285 cap.setType("das1:" + Capabilities.FEATURES.getName());
286 cap.setQueryUri(url + "/features");
290 v.getCAPABILITY().addAll(cp);
291 local.getVERSION().add(v);
297 public jalviewSourceI getSource(String nickname)
299 return sourceNames.get(nickname);
303 public boolean removeLocalSource(jalviewSourceI source)
305 if (localSources.containsValue(source))
307 localSources.remove(source.getTitle());
308 sourceNames.remove(source.getTitle());
309 dasSources.remove(source);
310 jalview.bin.Cache.setProperty("DAS_LOCAL_SOURCE",
311 getLocalSourceString());
319 public void refreshSources()
327 public List<jalviewSourceI> resolveSourceNicknames(List<String> sources)
329 ArrayList<jalviewSourceI> resolved = new ArrayList<jalviewSourceI>();
330 if (sourceNames != null)
332 for (String src : sources)
334 jalviewSourceI dsrc = sourceNames.get(src);
345 public String getLocalSourceString()
347 if (localSources != null)
349 StringBuffer sb = new StringBuffer();
350 Enumeration en = localSources.keys();
351 while (en.hasMoreElements())
353 String token = en.nextElement().toString();
354 jalviewSourceI srco = localSources.get(token);
355 sb.append(token + "|"
356 + (srco.isSequenceSource() ? "sequence:" : "")
357 + srco.getUri() + "\t");
359 return sb.toString();
364 private static final Hashtable<URL, String> authStash;
367 authStash = new Hashtable<URL, String>();
371 // TODO: allow same credentials for https and http
372 authStash.put(new URL(
373 "http://www.compbio.dundee.ac.uk/geneweb/das/myseq/"),
374 "Basic SmltOm1pSg==");
375 } catch (MalformedURLException e)
377 // TODO Auto-generated catch block
383 public MultipleConnectionPropertyProviderI getSessionHandler()
389 public ConnectionPropertyProviderI getConnectionPropertyProviderFor(
393 final ConnectionPropertyProviderI conprov = new ConnectionPropertyProviderI()
395 boolean authed = false;
398 public void setConnectionProperties(HttpURLConnection connection)
400 String auth = authStash.get(connection.getURL());
401 if (auth != null && auth.length() > 0)
403 connection.setRequestProperty("Authorisation", auth);
413 public boolean getResponseProperties(HttpURLConnection connection)
415 String auth = authStash.get(connection.getURL());
416 if (auth != null && auth.length() == 0)
418 // don't attempt to check if we authed or not - user entered empty
426 // try and pass credentials.
429 // see if we should try and create a new auth record.
430 String ameth = connection.getHeaderField("X-DAS-AuthMethods");
431 Cache.log.debug("Could authenticate to " + connection.getURL()
432 + " with : " + ameth);
433 // TODO: search auth string and raise login box - return if auth was
439 // check to see if auth was successful
440 String asuc = connection
441 .getHeaderField("X-DAS_AuthenticatedUser");
442 if (asuc != null && asuc.trim().length() > 0)
444 // authentication was successful
445 Cache.log.debug("Authenticated successfully to "
446 + connection.getURL().toString());
449 // it wasn't - so we should tell the user it failed and ask if they
450 // want to attempt authentication again.
451 authStash.remove(connection.getURL());
452 // open a new login/password dialog with cancel button
453 // set new authStash content with password and return true
455 // User cancelled auth - so put empty string in stash to indicate we
456 // don't want to auth with this server.
457 // authStash.put(connection.getURL(), "");