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;
77 * query the default DAS Source Registry for sources. Uses value of jalview
78 * property DAS_REGISTRY_URL and the DasSourceBrowser.DEFAULT_REGISTRY if that
81 * @return list of sources
83 private List<jalviewSourceI> getDASSources()
86 return getDASSources(getDasRegistryURL(), this);
90 * query the given URL for DasSources.
93 * return sources from registryURL
95 private static List<jalviewSourceI> getDASSources(String registryURL, MultipleConnectionPropertyProviderI registry)
99 URL url = new URL(registryURL);
100 org.biodas.jdas.client.SourcesClientInterface client = new SourcesClient();
102 SOURCES sources = client.fetchDataRegistry(registryURL, null, null,
103 null, null, null, null);
105 List<SOURCE> dassources = sources.getSOURCE();
106 ArrayList<jalviewSourceI> dsrc = new ArrayList<jalviewSourceI>();
107 HashMap<String,Integer> latests=new HashMap<String, Integer>();
109 for (SOURCE src : dassources)
111 JalviewSource jsrc=new JalviewSource(src, registry, false);
112 latest=latests.get(jsrc.getSourceURL());
115 if (jsrc.isNewerThan(dsrc.get(latest.intValue())))
117 dsrc.set(latest.intValue(), jsrc);
119 System.out.println("Debug: Ingored older source "+jsrc.getTitle());
122 latests.put(jsrc.getSourceURL(), Integer.valueOf(dsrc.size()));
127 } catch (Exception ex)
129 System.err.println("Failed to contact DAS1 registry at "
131 ex.printStackTrace();
132 return new ArrayList<jalviewSourceI>();
142 public List<jalviewSourceI> getSources()
144 if (dasSources == null)
146 dasSources = getDASSources();
148 return appendLocalSources();
152 * generate Sources from the local das source list
155 private void addLocalDasSources()
157 if (localSources == null)
159 // get local sources from properties and initialise the local source list
160 String local = jalview.bin.Cache.getProperty("DAS_LOCAL_SOURCE");
163 StringTokenizer st = new StringTokenizer(local, "\t");
164 while (st.hasMoreTokens())
166 String token = st.nextToken();
167 int bar = token.indexOf("|");
168 String url = token.substring(bar + 1);
169 boolean features = true, sequence = false;
170 if (url.startsWith("sequence:"))
172 url = url.substring(9);
173 // this source also serves sequences as well as features
176 createLocalSource(url, token.substring(0, bar), sequence,
183 private List<jalviewSourceI> appendLocalSources()
185 List<jalviewSourceI> srclist = new ArrayList<jalviewSourceI>();
186 addLocalDasSources();
187 sourceNames = new Hashtable<String, jalviewSourceI>();
188 if (dasSources != null)
190 for (jalviewSourceI src : dasSources)
192 sourceNames.put(src.getTitle(), src);
197 if (localSources == null)
201 Enumeration en = localSources.keys();
202 while (en.hasMoreElements())
204 String key = en.nextElement().toString();
205 jalviewSourceI jvsrc = localSources.get(key);
206 sourceNames.put(key, jvsrc);
217 public jalviewSourceI createLocalSource(String url, String name,
218 boolean sequence, boolean features)
220 SOURCE local = _createLocalSource(url, name, sequence, features);
222 if (localSources == null)
224 localSources = new Hashtable<String, jalviewSourceI>();
226 jalviewSourceI src = new JalviewSource(local, this, true);
227 localSources.put(local.getTitle(), src);
231 private SOURCE _createLocalSource(String url, String name,
232 boolean sequence, boolean features)
234 SOURCE local = new SOURCE();
237 local.setTitle(name);
238 local.setVERSION(new ArrayList<VERSION>());
239 VERSION v = new VERSION();
240 List<CAPABILITY> cp = new ArrayList<CAPABILITY>();
244 * Could try and synthesize a coordinate system for the source if needbe
245 * COORDINATES coord = new COORDINATES(); coord.setAuthority("NCBI");
246 * coord.setSource("Chromosome"); coord.setTaxid("9606");
247 * coord.setVersion("35"); version.getCOORDINATES().add(coord);
249 CAPABILITY cap = new CAPABILITY();
250 cap.setType("das1:" + Capabilities.SEQUENCE.getName());
251 cap.setQueryUri(url + "/sequence");
256 CAPABILITY cap = new CAPABILITY();
257 cap.setType("das1:" + Capabilities.FEATURES.getName());
258 cap.setQueryUri(url + "/features");
262 v.getCAPABILITY().addAll(cp);
263 local.getVERSION().add(v);
269 public jalviewSourceI getSource(String nickname)
271 return sourceNames.get(nickname);
275 public boolean removeLocalSource(jalviewSourceI source)
277 if (localSources.containsValue(source))
279 localSources.remove(source.getTitle());
280 sourceNames.remove(source.getTitle());
281 dasSources.remove(source);
282 jalview.bin.Cache.setProperty("DAS_LOCAL_SOURCE",
283 getLocalSourceString());
291 public void refreshSources()
299 public List<jalviewSourceI> resolveSourceNicknames(List<String> sources)
301 ArrayList<jalviewSourceI> resolved = new ArrayList<jalviewSourceI>();
302 if (sourceNames != null)
304 for (String src : sources)
306 jalviewSourceI dsrc = sourceNames.get(src);
317 public String getLocalSourceString()
319 if (localSources != null)
321 StringBuffer sb = new StringBuffer();
322 Enumeration en = localSources.keys();
323 while (en.hasMoreElements())
325 String token = en.nextElement().toString();
326 jalviewSourceI srco = localSources.get(token);
327 sb.append(token + "|"
328 + (srco.isSequenceSource() ? "sequence:" : "")
329 + srco.getUri() + "\t");
331 return sb.toString();
336 private static final Hashtable<URL, String> authStash;
339 authStash = new Hashtable<URL, String>();
343 // TODO: allow same credentials for https and http
344 authStash.put(new URL(
345 "http://www.compbio.dundee.ac.uk/geneweb/das/myseq/"),
346 "Basic SmltOm1pSg==");
347 } catch (MalformedURLException e)
349 // TODO Auto-generated catch block
355 public MultipleConnectionPropertyProviderI getSessionHandler()
361 public ConnectionPropertyProviderI getConnectionPropertyProviderFor(
365 final ConnectionPropertyProviderI conprov = new ConnectionPropertyProviderI()
367 boolean authed = false;
370 public void setConnectionProperties(HttpURLConnection connection)
372 String auth = authStash.get(connection.getURL());
373 if (auth != null && auth.length() > 0)
375 connection.setRequestProperty("Authorisation", auth);
385 public boolean getResponseProperties(HttpURLConnection connection)
387 String auth = authStash.get(connection.getURL());
388 if (auth != null && auth.length() == 0)
390 // don't attempt to check if we authed or not - user entered empty
398 // try and pass credentials.
401 // see if we should try and create a new auth record.
402 String ameth = connection.getHeaderField("X-DAS-AuthMethods");
403 Cache.log.debug("Could authenticate to " + connection.getURL()
404 + " with : " + ameth);
405 // TODO: search auth string and raise login box - return if auth was
411 // check to see if auth was successful
412 String asuc = connection
413 .getHeaderField("X-DAS_AuthenticatedUser");
414 if (asuc != null && asuc.trim().length() > 0)
416 // authentication was successful
417 Cache.log.debug("Authenticated successfully to "
418 + connection.getURL().toString());
421 // it wasn't - so we should tell the user it failed and ask if they
422 // want to attempt authentication again.
423 authStash.remove(connection.getURL());
424 // open a new login/password dialog with cancel button
425 // set new authStash content with password and return true
427 // User cancelled auth - so put empty string in stash to indicate we
428 // don't want to auth with this server.
429 // authStash.put(connection.getURL(), "");