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.client.threads.MultipleConnectionPropertyProviderI;
26 import org.biodas.jdas.dassources.Capabilities;
27 import org.biodas.jdas.schema.sources.CAPABILITY;
28 import org.biodas.jdas.schema.sources.SOURCE;
29 import org.biodas.jdas.schema.sources.SOURCES;
30 import org.biodas.jdas.schema.sources.VERSION;
32 import jalview.bin.Cache;
33 import jalview.ws.dbsources.das.api.DasSourceRegistryI;
34 import jalview.ws.dbsources.das.api.jalviewSourceI;
39 public class DasSourceRegistry implements DasSourceRegistryI,
40 MultipleConnectionPropertyProviderI
42 // private org.biodas.jdas.schema.sources.SOURCE[] dasSources = null;
43 private List<jalviewSourceI> dasSources = null;
45 private Hashtable<String, jalviewSourceI> sourceNames = null;
47 private Hashtable<String, jalviewSourceI> localSources = null;
49 public static String DEFAULT_REGISTRY = "http://www.dasregistry.org/das1/sources/";
52 * true if thread is running and we are talking to DAS registry service
54 private boolean loadingDasSources = false;
56 public boolean isLoadingDasSources()
58 return loadingDasSources;
61 public String getDasRegistryURL()
63 String registry = jalview.bin.Cache.getDefault("DAS_REGISTRY_URL",
66 if (registry.indexOf("/registry/das1/sources/") > -1)
68 jalview.bin.Cache.setProperty(jalview.bin.Cache.DAS_REGISTRY_URL,
70 registry = DEFAULT_REGISTRY;
76 * query the default DAS Source Registry for sources. Uses value of jalview
77 * property DAS_REGISTRY_URL and the DasSourceBrowser.DEFAULT_REGISTRY if that
80 * @return list of sources
82 private List<jalviewSourceI> getDASSources()
85 return getDASSources(getDasRegistryURL());
89 * query the given URL for DasSources.
92 * return sources from registryURL
94 private static List<jalviewSourceI> getDASSources(String registryURL)
98 URL url = new URL(registryURL);
99 org.biodas.jdas.client.SourcesClientInterface client = new SourcesClient();
101 SOURCES sources = client.fetchDataRegistry(registryURL, null, null,
102 null, null, null, null);
104 List<SOURCE> dassources = sources.getSOURCE();
105 ArrayList<jalviewSourceI> dsrc = new ArrayList<jalviewSourceI>();
106 for (SOURCE src : dassources)
108 dsrc.add(new JalviewSource(src, false));
111 } catch (Exception ex)
113 System.err.println("Failed to contact DAS1 registry at "
115 ex.printStackTrace();
116 return new ArrayList<jalviewSourceI>();
126 public List<jalviewSourceI> getSources()
128 if (dasSources == null)
130 dasSources = getDASSources();
132 return appendLocalSources();
136 * generate Sources from the local das source list
139 private void addLocalDasSources()
141 if (localSources == null)
143 // get local sources from properties and initialise the local source list
144 String local = jalview.bin.Cache.getProperty("DAS_LOCAL_SOURCE");
147 StringTokenizer st = new StringTokenizer(local, "\t");
148 while (st.hasMoreTokens())
150 String token = st.nextToken();
151 int bar = token.indexOf("|");
152 String url = token.substring(bar + 1);
153 boolean features = true, sequence = false;
154 if (url.startsWith("sequence:"))
156 url = url.substring(9);
157 // this source also serves sequences as well as features
160 createLocalSource(url, token.substring(0, bar), sequence,
167 private List<jalviewSourceI> appendLocalSources()
169 List<jalviewSourceI> srclist = new ArrayList<jalviewSourceI>();
170 addLocalDasSources();
171 sourceNames = new Hashtable<String, jalviewSourceI>();
172 if (dasSources != null)
174 for (jalviewSourceI src : dasSources)
176 sourceNames.put(src.getTitle(), src);
181 if (localSources == null)
185 Enumeration en = localSources.keys();
186 while (en.hasMoreElements())
188 String key = en.nextElement().toString();
189 jalviewSourceI jvsrc = localSources.get(key);
190 sourceNames.put(key, jvsrc);
201 public jalviewSourceI createLocalSource(String url, String name,
202 boolean sequence, boolean features)
204 SOURCE local = _createLocalSource(url, name, sequence, features);
206 if (localSources == null)
208 localSources = new Hashtable<String, jalviewSourceI>();
210 jalviewSourceI src = new JalviewSource(local, true);
211 localSources.put(local.getTitle(), src);
215 private SOURCE _createLocalSource(String url, String name,
216 boolean sequence, boolean features)
218 SOURCE local = new SOURCE();
221 local.setTitle(name);
222 local.setVERSION(new ArrayList<VERSION>());
223 VERSION v = new VERSION();
224 List<CAPABILITY> cp = new ArrayList<CAPABILITY>();
228 * Could try and synthesize a coordinate system for the source if needbe
229 * COORDINATES coord = new COORDINATES(); coord.setAuthority("NCBI");
230 * coord.setSource("Chromosome"); coord.setTaxid("9606");
231 * coord.setVersion("35"); version.getCOORDINATES().add(coord);
233 CAPABILITY cap = new CAPABILITY();
234 cap.setType("das1:" + Capabilities.SEQUENCE.getName());
235 cap.setQueryUri(url + "/sequence");
240 CAPABILITY cap = new CAPABILITY();
241 cap.setType("das1:" + Capabilities.FEATURES.getName());
242 cap.setQueryUri(url + "/features");
246 v.getCAPABILITY().addAll(cp);
247 local.getVERSION().add(v);
253 public jalviewSourceI getSource(String nickname)
255 return sourceNames.get(nickname);
259 public boolean removeLocalSource(jalviewSourceI source)
261 if (localSources.containsValue(source))
263 localSources.remove(source.getTitle());
264 sourceNames.remove(source.getTitle());
265 dasSources.remove(source);
266 jalview.bin.Cache.setProperty("DAS_LOCAL_SOURCE",
267 getLocalSourceString());
275 public void refreshSources()
283 public List<jalviewSourceI> resolveSourceNicknames(List<String> sources)
285 ArrayList<jalviewSourceI> resolved = new ArrayList<jalviewSourceI>();
286 if (sourceNames != null)
288 for (String src : sources)
290 jalviewSourceI dsrc = sourceNames.get(src);
301 public String getLocalSourceString()
303 if (localSources != null)
305 StringBuffer sb = new StringBuffer();
306 Enumeration en = localSources.keys();
307 while (en.hasMoreElements())
309 String token = en.nextElement().toString();
310 jalviewSourceI srco = localSources.get(token);
311 sb.append(token + "|"
312 + (srco.isSequenceSource() ? "sequence:" : "")
313 + srco.getUri() + "\t");
315 return sb.toString();
320 private static final Hashtable<URL, String> authStash;
323 authStash = new Hashtable<URL, String>();
327 // TODO: allow same credentials for https and http
328 authStash.put(new URL(
329 "http://www.compbio.dundee.ac.uk/geneweb/das/myseq/"),
330 "Basic SmltOm1pSg==");
331 } catch (MalformedURLException e)
333 // TODO Auto-generated catch block
339 public MultipleConnectionPropertyProviderI getSessionHandler()
345 public ConnectionPropertyProviderI getConnectionPropertyProviderFor(
349 final ConnectionPropertyProviderI conprov = new ConnectionPropertyProviderI()
351 boolean authed = false;
354 public void setConnectionProperties(HttpURLConnection connection)
356 String auth = authStash.get(connection.getURL());
357 if (auth != null && auth.length() > 0)
359 connection.setRequestProperty("Authorisation", auth);
369 public boolean getResponseProperties(HttpURLConnection connection)
371 String auth = authStash.get(connection.getURL());
372 if (auth != null && auth.length() == 0)
374 // don't attempt to check if we authed or not - user entered empty
382 // try and pass credentials.
385 // see if we should try and create a new auth record.
386 String ameth = connection.getHeaderField("X-DAS-AuthMethods");
387 Cache.log.debug("Could authenticate to " + connection.getURL()
388 + " with : " + ameth);
389 // TODO: search auth string and raise login box - return if auth was
395 // check to see if auth was successful
396 String asuc = connection
397 .getHeaderField("X-DAS_AuthenticatedUser");
398 if (asuc != null && asuc.trim().length() > 0)
400 // authentication was successful
401 Cache.log.debug("Authenticated successfully to "
402 + connection.getURL().toString());
405 // it wasn't - so we should tell the user it failed and ask if they
406 // want to attempt authentication again.
407 authStash.remove(connection.getURL());
408 // open a new login/password dialog with cancel button
409 // set new authStash content with password and return true
411 // User cancelled auth - so put empty string in stash to indicate we
412 // don't want to auth with this server.
413 // authStash.put(connection.getURL(), "");