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, MultipleConnectionPropertyProviderI registry)
105 URL url = new URL(registryURL);
106 org.biodas.jdas.client.SourcesClientInterface client = new SourcesClient();
108 SOURCES sources = client.fetchDataRegistry(registryURL, null, null,
109 null, null, null, null);
111 List<SOURCE> dassources = sources.getSOURCE();
112 ArrayList<jalviewSourceI> dsrc = new ArrayList<jalviewSourceI>();
113 HashMap<String,Integer> latests=new HashMap<String, Integer>();
115 for (SOURCE src : dassources)
117 JalviewSource jsrc=new JalviewSource(src, registry, false);
118 latest=latests.get(jsrc.getSourceURL());
121 if (jsrc.isNewerThan(dsrc.get(latest.intValue())))
123 dsrc.set(latest.intValue(), jsrc);
125 System.out.println("Debug: Ingored older source "+jsrc.getTitle());
128 latests.put(jsrc.getSourceURL(), Integer.valueOf(dsrc.size()));
133 } catch (Exception ex)
135 System.err.println("Failed to contact DAS1 registry at "
137 ex.printStackTrace();
138 return new ArrayList<jalviewSourceI>();
148 public List<jalviewSourceI> getSources()
150 if (dasSources == null)
152 dasSources = getDASSources();
154 return appendLocalSources();
158 * generate Sources from the local das source list
161 private void addLocalDasSources()
163 if (localSources == null)
165 // get local sources from properties and initialise the local source list
166 String local = jalview.bin.Cache.getProperty("DAS_LOCAL_SOURCE");
169 StringTokenizer st = new StringTokenizer(local, "\t");
170 while (st.hasMoreTokens())
172 String token = st.nextToken();
173 int bar = token.indexOf("|");
174 String url = token.substring(bar + 1);
175 boolean features = true, sequence = false;
176 if (url.startsWith("sequence:"))
178 url = url.substring(9);
179 // this source also serves sequences as well as features
182 createLocalSource(url, token.substring(0, bar), sequence,
189 private List<jalviewSourceI> appendLocalSources()
191 List<jalviewSourceI> srclist = new ArrayList<jalviewSourceI>();
192 addLocalDasSources();
193 sourceNames = new Hashtable<String, jalviewSourceI>();
194 if (dasSources != null)
196 for (jalviewSourceI src : dasSources)
198 sourceNames.put(src.getTitle(), src);
203 if (localSources == null)
207 Enumeration en = localSources.keys();
208 while (en.hasMoreElements())
210 String key = en.nextElement().toString();
211 jalviewSourceI jvsrc = localSources.get(key);
212 sourceNames.put(key, jvsrc);
223 public jalviewSourceI createLocalSource(String url, String name,
224 boolean sequence, boolean features)
226 SOURCE local = _createLocalSource(url, name, sequence, features);
228 if (localSources == null)
230 localSources = new Hashtable<String, jalviewSourceI>();
232 jalviewSourceI src = new JalviewSource(local, this, true);
233 localSources.put(local.getTitle(), src);
237 private SOURCE _createLocalSource(String url, String name,
238 boolean sequence, boolean features)
240 SOURCE local = new SOURCE();
243 local.setTitle(name);
244 local.setVERSION(new ArrayList<VERSION>());
245 VERSION v = new VERSION();
246 List<CAPABILITY> cp = new ArrayList<CAPABILITY>();
250 * Could try and synthesize a coordinate system for the source if needbe
251 * COORDINATES coord = new COORDINATES(); coord.setAuthority("NCBI");
252 * coord.setSource("Chromosome"); coord.setTaxid("9606");
253 * coord.setVersion("35"); version.getCOORDINATES().add(coord);
255 CAPABILITY cap = new CAPABILITY();
256 cap.setType("das1:" + Capabilities.SEQUENCE.getName());
257 cap.setQueryUri(url + "/sequence");
262 CAPABILITY cap = new CAPABILITY();
263 cap.setType("das1:" + Capabilities.FEATURES.getName());
264 cap.setQueryUri(url + "/features");
268 v.getCAPABILITY().addAll(cp);
269 local.getVERSION().add(v);
275 public jalviewSourceI getSource(String nickname)
277 return sourceNames.get(nickname);
281 public boolean removeLocalSource(jalviewSourceI source)
283 if (localSources.containsValue(source))
285 localSources.remove(source.getTitle());
286 sourceNames.remove(source.getTitle());
287 dasSources.remove(source);
288 jalview.bin.Cache.setProperty("DAS_LOCAL_SOURCE",
289 getLocalSourceString());
297 public void refreshSources()
305 public List<jalviewSourceI> resolveSourceNicknames(List<String> sources)
307 ArrayList<jalviewSourceI> resolved = new ArrayList<jalviewSourceI>();
308 if (sourceNames != null)
310 for (String src : sources)
312 jalviewSourceI dsrc = sourceNames.get(src);
323 public String getLocalSourceString()
325 if (localSources != null)
327 StringBuffer sb = new StringBuffer();
328 Enumeration en = localSources.keys();
329 while (en.hasMoreElements())
331 String token = en.nextElement().toString();
332 jalviewSourceI srco = localSources.get(token);
333 sb.append(token + "|"
334 + (srco.isSequenceSource() ? "sequence:" : "")
335 + srco.getUri() + "\t");
337 return sb.toString();
342 private static final Hashtable<URL, String> authStash;
345 authStash = new Hashtable<URL, String>();
349 // TODO: allow same credentials for https and http
350 authStash.put(new URL(
351 "http://www.compbio.dundee.ac.uk/geneweb/das/myseq/"),
352 "Basic SmltOm1pSg==");
353 } catch (MalformedURLException e)
355 // TODO Auto-generated catch block
361 public MultipleConnectionPropertyProviderI getSessionHandler()
367 public ConnectionPropertyProviderI getConnectionPropertyProviderFor(
371 final ConnectionPropertyProviderI conprov = new ConnectionPropertyProviderI()
373 boolean authed = false;
376 public void setConnectionProperties(HttpURLConnection connection)
378 String auth = authStash.get(connection.getURL());
379 if (auth != null && auth.length() > 0)
381 connection.setRequestProperty("Authorisation", auth);
391 public boolean getResponseProperties(HttpURLConnection connection)
393 String auth = authStash.get(connection.getURL());
394 if (auth != null && auth.length() == 0)
396 // don't attempt to check if we authed or not - user entered empty
404 // try and pass credentials.
407 // see if we should try and create a new auth record.
408 String ameth = connection.getHeaderField("X-DAS-AuthMethods");
409 Cache.log.debug("Could authenticate to " + connection.getURL()
410 + " with : " + ameth);
411 // TODO: search auth string and raise login box - return if auth was
417 // check to see if auth was successful
418 String asuc = connection
419 .getHeaderField("X-DAS_AuthenticatedUser");
420 if (asuc != null && asuc.trim().length() > 0)
422 // authentication was successful
423 Cache.log.debug("Authenticated successfully to "
424 + connection.getURL().toString());
427 // it wasn't - so we should tell the user it failed and ask if they
428 // want to attempt authentication again.
429 authStash.remove(connection.getURL());
430 // open a new login/password dialog with cancel button
431 // set new authStash content with password and return true
433 // User cancelled auth - so put empty string in stash to indicate we
434 // don't want to auth with this server.
435 // authStash.put(connection.getURL(), "");