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.Hashtable;
14 import java.util.List;
17 import java.util.StringTokenizer;
18 import java.util.Vector;
20 import javax.swing.JOptionPane;
22 import org.apache.http.auth.InvalidCredentialsException;
23 import org.biodas.jdas.client.ConnectionPropertyProviderI;
24 import org.biodas.jdas.client.SourcesClient;
25 import org.biodas.jdas.dassources.Capabilities;
26 import org.biodas.jdas.schema.sources.CAPABILITY;
27 import org.biodas.jdas.schema.sources.SOURCE;
28 import org.biodas.jdas.schema.sources.SOURCES;
29 import org.biodas.jdas.schema.sources.VERSION;
31 import jalview.bin.Cache;
32 import jalview.ws.dbsources.das.api.DasSourceRegistryI;
33 import jalview.ws.dbsources.das.api.jalviewSourceI;
38 public class DasSourceRegistry implements DasSourceRegistryI
40 // private org.biodas.jdas.schema.sources.SOURCE[] dasSources = null;
41 private List<jalviewSourceI> dasSources = null;
43 private Hashtable<String, jalviewSourceI> sourceNames = null;
45 private Hashtable<String, jalviewSourceI> localSources = null;
47 public static String DEFAULT_REGISTRY = "http://www.dasregistry.org/das1/sources/";
50 * true if thread is running and we are talking to DAS registry service
52 private boolean loadingDasSources = false;
54 public boolean isLoadingDasSources()
56 return loadingDasSources;
59 public String getDasRegistryURL()
61 String registry = jalview.bin.Cache.getDefault("DAS_REGISTRY_URL",
64 if (registry.indexOf("/registry/das1/sources/") > -1)
66 jalview.bin.Cache.setProperty(jalview.bin.Cache.DAS_REGISTRY_URL,
68 registry = DEFAULT_REGISTRY;
74 * query the default DAS Source Registry for sources. Uses value of jalview
75 * property DAS_REGISTRY_URL and the DasSourceBrowser.DEFAULT_REGISTRY if that
78 * @return list of sources
80 private List<jalviewSourceI> getDASSources()
83 return getDASSources(getDasRegistryURL());
87 * query the given URL for DasSources.
90 * return sources from registryURL
92 private static List<jalviewSourceI> getDASSources(String registryURL)
96 URL url = new URL(registryURL);
97 org.biodas.jdas.client.SourcesClientInterface client = new SourcesClient();
99 SOURCES sources = client.fetchDataRegistry(registryURL, null, null, null, null, null, null);
101 List<SOURCE> dassources = sources.getSOURCE();
102 ArrayList<jalviewSourceI> dsrc = new ArrayList<jalviewSourceI>();
103 for (SOURCE src : dassources)
105 dsrc.add(new JalviewSource(src, false));
108 } catch (Exception ex)
110 System.err.println("Failed to contact DAS1 registry at "
112 ex.printStackTrace();
113 return new ArrayList<jalviewSourceI>();
124 public List<jalviewSourceI> getSources()
126 if (dasSources == null)
128 dasSources = getDASSources();
130 return appendLocalSources();
134 * generate Sources from the local das source list
137 private void addLocalDasSources()
139 String local = jalview.bin.Cache.getProperty("DAS_LOCAL_SOURCE");
142 StringTokenizer st = new StringTokenizer(local, "\t");
143 while (st.hasMoreTokens())
145 String token = st.nextToken();
146 int bar = token.indexOf("|");
147 String url = token.substring(bar + 1);
148 boolean features = true, sequence = false;
149 if (url.startsWith("sequence:"))
151 url = url.substring(9);
152 // this source also serves sequences as well as features
155 createLocalSource(url, token.substring(0, bar), sequence, features);
160 private List<jalviewSourceI> appendLocalSources()
162 List<jalviewSourceI> srclist=new ArrayList<jalviewSourceI>();
163 addLocalDasSources();
164 sourceNames = new Hashtable<String, jalviewSourceI>();
165 if (dasSources != null)
167 for (jalviewSourceI src : dasSources)
169 sourceNames.put(src.getTitle(), src);
174 if (localSources == null)
178 Enumeration en = localSources.keys();
179 while (en.hasMoreElements())
181 String key = en.nextElement().toString();
182 jalviewSourceI jvsrc = localSources.get(key);
183 sourceNames.put(key, jvsrc);
194 public jalviewSourceI createLocalSource(String url, String name,
195 boolean sequence, boolean features)
197 SOURCE local = _createLocalSource(url, name, sequence, features);
199 if (localSources == null)
201 localSources = new Hashtable<String, jalviewSourceI>();
203 jalviewSourceI src=new JalviewSource(local, true);
204 localSources.put(local.getTitle(), src);
208 private SOURCE _createLocalSource(String url, String name,
209 boolean sequence, boolean features)
211 SOURCE local = new SOURCE();
214 local.setTitle(name);
215 local.setVERSION(new ArrayList<VERSION>());
216 VERSION v = new VERSION();
217 List<CAPABILITY> cp = new ArrayList<CAPABILITY>();
221 * Could try and synthesize a coordinate system for the source if needbe
222 * COORDINATES coord = new COORDINATES(); coord.setAuthority("NCBI");
223 * coord.setSource("Chromosome"); coord.setTaxid("9606");
224 * coord.setVersion("35"); version.getCOORDINATES().add(coord);
226 CAPABILITY cap = new CAPABILITY();
227 cap.setType("das1:" + Capabilities.SEQUENCE.getName());
228 cap.setQueryUri(url+"/sequence");
233 CAPABILITY cap = new CAPABILITY();
234 cap.setType("das1:" + Capabilities.FEATURES.getName());
235 cap.setQueryUri(url+"/features");
239 v.getCAPABILITY().addAll(cp);
240 local.getVERSION().add(v);
246 public jalviewSourceI getSource(String nickname)
248 return sourceNames.get(nickname);
252 public boolean removeLocalSource(jalviewSourceI source)
254 if (localSources.containsValue(source))
256 localSources.remove(source.getTitle());
257 sourceNames.remove(source.getTitle());
258 dasSources.remove(source);
265 public void refreshSources()
273 public List<jalviewSourceI> resolveSourceNicknames(List<String> sources)
275 ArrayList<jalviewSourceI> resolved = new ArrayList<jalviewSourceI>();
276 if (sourceNames != null)
278 for (String src : sources)
280 jalviewSourceI dsrc = sourceNames.get(src);
291 public String getLocalSourceString()
293 if (localSources != null)
295 StringBuffer sb = new StringBuffer();
296 Enumeration en = localSources.keys();
297 while (en.hasMoreElements())
299 String token = en.nextElement().toString();
300 jalviewSourceI srco = localSources.get(token);
301 sb.append(token + "|"
302 + (srco.isSequenceSource() ? "sequence:" : "")
303 + srco.getUri() + "\t");
305 return sb.toString();
310 private static final Hashtable<URL, String> authStash;
313 authStash = new Hashtable<URL, String>();
317 // TODO: allow same credentials for https and http
318 authStash.put(new URL(
319 "http://www.compbio.dundee.ac.uk/geneweb/das/myseq/"),
320 "Basic SmltOm1pSg==");
321 } catch (MalformedURLException e)
323 // TODO Auto-generated catch block
329 public Map<String, ConnectionPropertyProviderI> getSessionHandler()
331 final ConnectionPropertyProviderI conprov = new ConnectionPropertyProviderI()
333 boolean authed = false;
336 public void setConnectionProperties(HttpURLConnection connection)
338 String auth = authStash.get(connection.getURL());
339 if (auth != null && auth.length() > 0)
341 connection.setRequestProperty("Authorisation", auth);
351 public boolean getResponseProperties(HttpURLConnection connection)
353 String auth = authStash.get(connection.getURL());
354 if (auth != null && auth.length() == 0)
356 // don't attempt to check if we authed or not - user entered empty
364 // try and pass credentials.
367 // see if we should try and create a new auth record.
368 String ameth = connection.getHeaderField("X-DAS-AuthMethods");
369 Cache.log.debug("Could authenticate to " + connection.getURL()
370 + " with : " + ameth);
371 // TODO: search auth string and raise login box - return if auth was
377 // check to see if auth was successful
378 String asuc = connection
379 .getHeaderField("X-DAS_AuthenticatedUser");
380 if (asuc != null && asuc.trim().length() > 0)
382 // authentication was successful
383 Cache.log.debug("Authenticated successfully as " + asuc
384 + " with " + connection.getURL().toString());
387 // it wasn't - so we should tell the user it failed and ask if they
388 // want to attempt authentication again.
389 authStash.remove(connection.getURL());
390 // open a new login/password dialog with cancel button
391 // set new authStash content with password and return true
393 // User cancelled auth - so put empty string in stash to indicate we
394 // don't want to auth with this server.
395 // authStash.put(connection.getURL(), "");
400 return new Map<String, ConnectionPropertyProviderI>()
406 // TODO Auto-generated method stub
411 public boolean containsKey(Object key)
413 // TODO Auto-generated method stub
418 public boolean containsValue(Object value)
420 // TODO Auto-generated method stub
425 public Set<java.util.Map.Entry<String, ConnectionPropertyProviderI>> entrySet()
427 // TODO Auto-generated method stub
432 public ConnectionPropertyProviderI get(Object key)
438 public boolean isEmpty()
440 // TODO Auto-generated method stub
445 public Set<String> keySet()
447 // TODO Auto-generated method stub
452 public ConnectionPropertyProviderI put(String key,
453 ConnectionPropertyProviderI value)
455 // TODO Auto-generated method stub
461 Map<? extends String, ? extends ConnectionPropertyProviderI> m)
463 // TODO Auto-generated method stub
468 public ConnectionPropertyProviderI remove(Object key)
470 // TODO Auto-generated method stub
477 // TODO Auto-generated method stub
482 public Collection<ConnectionPropertyProviderI> values()
484 // TODO Auto-generated method stub