4 package jalview.ws.dbsources.das.datamodel;
6 import java.net.HttpURLConnection;
7 import java.net.MalformedURLException;
9 import java.util.ArrayList;
10 import java.util.Collection;
11 import java.util.Enumeration;
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.Hashtable;
15 import java.util.List;
18 import java.util.StringTokenizer;
19 import java.util.Vector;
21 import javax.swing.JOptionPane;
23 import org.apache.http.auth.InvalidCredentialsException;
24 import org.biodas.jdas.client.ConnectionPropertyProviderI;
25 import org.biodas.jdas.client.SourcesClient;
26 import org.biodas.jdas.client.threads.MultipleConnectionPropertyProviderI;
27 import org.biodas.jdas.dassources.Capabilities;
28 import org.biodas.jdas.schema.sources.CAPABILITY;
29 import org.biodas.jdas.schema.sources.SOURCE;
30 import org.biodas.jdas.schema.sources.SOURCES;
31 import org.biodas.jdas.schema.sources.VERSION;
33 import jalview.bin.Cache;
34 import jalview.ws.dbsources.das.api.DasSourceRegistryI;
35 import jalview.ws.dbsources.das.api.jalviewSourceI;
40 public class DasSourceRegistry implements DasSourceRegistryI,
41 MultipleConnectionPropertyProviderI
43 // private org.biodas.jdas.schema.sources.SOURCE[] dasSources = null;
44 private List<jalviewSourceI> dasSources = null;
46 private Hashtable<String, jalviewSourceI> sourceNames = null;
48 private Hashtable<String, jalviewSourceI> localSources = null;
50 public static String DEFAULT_REGISTRY = "http://www.dasregistry.org/das/";
53 * true if thread is running and we are talking to DAS registry service
55 private boolean loadingDasSources = false;
57 public boolean isLoadingDasSources()
59 return loadingDasSources;
62 public String getDasRegistryURL()
64 String registry = jalview.bin.Cache.getDefault("DAS_REGISTRY_URL",
67 if (registry.indexOf("/registry/das1/sources/") > -1)
69 jalview.bin.Cache.setProperty(jalview.bin.Cache.DAS_REGISTRY_URL,
71 registry = DEFAULT_REGISTRY;
73 if (registry.lastIndexOf("sources.xml") == registry.length() - 11)
75 // no trailing sources.xml document for registry in JDAS
76 jalview.bin.Cache.setProperty(jalview.bin.Cache.DAS_REGISTRY_URL,
77 registry=registry.substring(0, registry.lastIndexOf("sources.xml")));
83 * query the default DAS Source Registry for sources. Uses value of jalview
84 * property DAS_REGISTRY_URL and the DasSourceBrowser.DEFAULT_REGISTRY if that
87 * @return list of sources
89 private List<jalviewSourceI> getDASSources()
92 return getDASSources(getDasRegistryURL(), this);
96 * query the given URL for DasSources.
99 * return sources from registryURL
101 private static List<jalviewSourceI> getDASSources(String registryURL,
102 MultipleConnectionPropertyProviderI registry)
106 URL url = new URL(registryURL);
107 org.biodas.jdas.client.SourcesClientInterface client = new SourcesClient();
109 SOURCES sources = client.fetchDataRegistry(registryURL, null, null,
110 null, null, null, null);
112 List<SOURCE> dassources = sources.getSOURCE();
113 ArrayList<jalviewSourceI> dsrc = new ArrayList<jalviewSourceI>();
114 HashMap<String, Integer> latests = new HashMap<String, Integer>();
116 for (SOURCE src : dassources)
118 JalviewSource jsrc = new JalviewSource(src, registry, false);
119 latest = latests.get(jsrc.getSourceURL());
122 if (jsrc.isNewerThan(dsrc.get(latest.intValue())))
124 dsrc.set(latest.intValue(), jsrc);
128 System.out.println("Debug: Ignored older source "
134 latests.put(jsrc.getSourceURL(), Integer.valueOf(dsrc.size()));
139 } catch (Exception ex)
141 System.err.println("Failed to contact DAS1 registry at "
143 ex.printStackTrace();
144 return new ArrayList<jalviewSourceI>();
154 public List<jalviewSourceI> getSources()
156 if (dasSources == null)
158 dasSources = getDASSources();
160 return appendLocalSources();
164 * generate Sources from the local das source list
167 private void addLocalDasSources()
169 if (localSources == null)
171 // get local sources from properties and initialise the local source list
172 String local = jalview.bin.Cache.getProperty("DAS_LOCAL_SOURCE");
175 StringTokenizer st = new StringTokenizer(local, "\t");
176 while (st.hasMoreTokens())
178 String token = st.nextToken();
179 int bar = token.indexOf("|");
180 String url = token.substring(bar + 1);
181 boolean features = true, sequence = false;
182 if (url.startsWith("sequence:"))
184 url = url.substring(9);
185 // this source also serves sequences as well as features
188 createLocalSource(url, token.substring(0, bar), sequence,
195 private List<jalviewSourceI> appendLocalSources()
197 List<jalviewSourceI> srclist = new ArrayList<jalviewSourceI>();
198 addLocalDasSources();
199 sourceNames = new Hashtable<String, jalviewSourceI>();
200 if (dasSources != null)
202 for (jalviewSourceI src : dasSources)
204 sourceNames.put(src.getTitle(), src);
209 if (localSources == null)
213 Enumeration en = localSources.keys();
214 while (en.hasMoreElements())
216 String key = en.nextElement().toString();
217 jalviewSourceI jvsrc = localSources.get(key);
218 sourceNames.put(key, jvsrc);
229 public jalviewSourceI createLocalSource(String url, String name,
230 boolean sequence, boolean features)
232 SOURCE local = _createLocalSource(url, name, sequence, features);
234 if (localSources == null)
236 localSources = new Hashtable<String, jalviewSourceI>();
238 jalviewSourceI src = new JalviewSource(local, this, true);
239 localSources.put(local.getTitle(), src);
243 private SOURCE _createLocalSource(String url, String name,
244 boolean sequence, boolean features)
246 SOURCE local = new SOURCE();
249 local.setTitle(name);
250 local.setVERSION(new ArrayList<VERSION>());
251 VERSION v = new VERSION();
252 List<CAPABILITY> cp = new ArrayList<CAPABILITY>();
256 * Could try and synthesize a coordinate system for the source if needbe
257 * COORDINATES coord = new COORDINATES(); coord.setAuthority("NCBI");
258 * coord.setSource("Chromosome"); coord.setTaxid("9606");
259 * coord.setVersion("35"); version.getCOORDINATES().add(coord);
261 CAPABILITY cap = new CAPABILITY();
262 cap.setType("das1:" + Capabilities.SEQUENCE.getName());
263 cap.setQueryUri(url + "/sequence");
268 CAPABILITY cap = new CAPABILITY();
269 cap.setType("das1:" + Capabilities.FEATURES.getName());
270 cap.setQueryUri(url + "/features");
274 v.getCAPABILITY().addAll(cp);
275 local.getVERSION().add(v);
281 public jalviewSourceI getSource(String nickname)
283 return sourceNames.get(nickname);
287 public boolean removeLocalSource(jalviewSourceI source)
289 if (localSources.containsValue(source))
291 localSources.remove(source.getTitle());
292 sourceNames.remove(source.getTitle());
293 dasSources.remove(source);
294 jalview.bin.Cache.setProperty("DAS_LOCAL_SOURCE",
295 getLocalSourceString());
303 public void refreshSources()
311 public List<jalviewSourceI> resolveSourceNicknames(List<String> sources)
313 ArrayList<jalviewSourceI> resolved = new ArrayList<jalviewSourceI>();
314 if (sourceNames != null)
316 for (String src : sources)
318 jalviewSourceI dsrc = sourceNames.get(src);
329 public String getLocalSourceString()
331 if (localSources != null)
333 StringBuffer sb = new StringBuffer();
334 Enumeration en = localSources.keys();
335 while (en.hasMoreElements())
337 String token = en.nextElement().toString();
338 jalviewSourceI srco = localSources.get(token);
339 sb.append(token + "|"
340 + (srco.isSequenceSource() ? "sequence:" : "")
341 + srco.getUri() + "\t");
343 return sb.toString();
348 private static final Hashtable<URL, String> authStash;
351 authStash = new Hashtable<URL, String>();
355 // TODO: allow same credentials for https and http
356 authStash.put(new URL(
357 "http://www.compbio.dundee.ac.uk/geneweb/das/myseq/"),
358 "Basic SmltOm1pSg==");
359 } catch (MalformedURLException e)
361 // TODO Auto-generated catch block
367 public MultipleConnectionPropertyProviderI getSessionHandler()
373 public ConnectionPropertyProviderI getConnectionPropertyProviderFor(
377 final ConnectionPropertyProviderI conprov = new ConnectionPropertyProviderI()
379 boolean authed = false;
382 public void setConnectionProperties(HttpURLConnection connection)
384 String auth = authStash.get(connection.getURL());
385 if (auth != null && auth.length() > 0)
387 connection.setRequestProperty("Authorisation", auth);
397 public boolean getResponseProperties(HttpURLConnection connection)
399 String auth = authStash.get(connection.getURL());
400 if (auth != null && auth.length() == 0)
402 // don't attempt to check if we authed or not - user entered empty
410 // try and pass credentials.
413 // see if we should try and create a new auth record.
414 String ameth = connection.getHeaderField("X-DAS-AuthMethods");
415 Cache.log.debug("Could authenticate to " + connection.getURL()
416 + " with : " + ameth);
417 // TODO: search auth string and raise login box - return if auth was
423 // check to see if auth was successful
424 String asuc = connection
425 .getHeaderField("X-DAS_AuthenticatedUser");
426 if (asuc != null && asuc.trim().length() > 0)
428 // authentication was successful
429 Cache.log.debug("Authenticated successfully to "
430 + connection.getURL().toString());
433 // it wasn't - so we should tell the user it failed and ask if they
434 // want to attempt authentication again.
435 authStash.remove(connection.getURL());
436 // open a new login/password dialog with cancel button
437 // set new authStash content with password and return true
439 // User cancelled auth - so put empty string in stash to indicate we
440 // don't want to auth with this server.
441 // authStash.put(connection.getURL(), "");