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, null, null, null, null);
103 List<SOURCE> dassources = sources.getSOURCE();
104 ArrayList<jalviewSourceI> dsrc = new ArrayList<jalviewSourceI>();
105 for (SOURCE src : dassources)
107 dsrc.add(new JalviewSource(src, false));
110 } catch (Exception ex)
112 System.err.println("Failed to contact DAS1 registry at "
114 ex.printStackTrace();
115 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 String local = jalview.bin.Cache.getProperty("DAS_LOCAL_SOURCE");
144 StringTokenizer st = new StringTokenizer(local, "\t");
145 while (st.hasMoreTokens())
147 String token = st.nextToken();
148 int bar = token.indexOf("|");
149 String url = token.substring(bar + 1);
150 boolean features = true, sequence = false;
151 if (url.startsWith("sequence:"))
153 url = url.substring(9);
154 // this source also serves sequences as well as features
157 createLocalSource(url, token.substring(0, bar), sequence, features);
162 private List<jalviewSourceI> appendLocalSources()
164 List<jalviewSourceI> srclist=new ArrayList<jalviewSourceI>();
165 addLocalDasSources();
166 sourceNames = new Hashtable<String, jalviewSourceI>();
167 if (dasSources != null)
169 for (jalviewSourceI src : dasSources)
171 sourceNames.put(src.getTitle(), src);
176 if (localSources == null)
180 Enumeration en = localSources.keys();
181 while (en.hasMoreElements())
183 String key = en.nextElement().toString();
184 jalviewSourceI jvsrc = localSources.get(key);
185 sourceNames.put(key, jvsrc);
196 public jalviewSourceI createLocalSource(String url, String name,
197 boolean sequence, boolean features)
199 SOURCE local = _createLocalSource(url, name, sequence, features);
201 if (localSources == null)
203 localSources = new Hashtable<String, jalviewSourceI>();
205 jalviewSourceI src=new JalviewSource(local, true);
206 localSources.put(local.getTitle(), src);
210 private SOURCE _createLocalSource(String url, String name,
211 boolean sequence, boolean features)
213 SOURCE local = new SOURCE();
216 local.setTitle(name);
217 local.setVERSION(new ArrayList<VERSION>());
218 VERSION v = new VERSION();
219 List<CAPABILITY> cp = new ArrayList<CAPABILITY>();
223 * Could try and synthesize a coordinate system for the source if needbe
224 * COORDINATES coord = new COORDINATES(); coord.setAuthority("NCBI");
225 * coord.setSource("Chromosome"); coord.setTaxid("9606");
226 * coord.setVersion("35"); version.getCOORDINATES().add(coord);
228 CAPABILITY cap = new CAPABILITY();
229 cap.setType("das1:" + Capabilities.SEQUENCE.getName());
230 cap.setQueryUri(url+"/sequence");
235 CAPABILITY cap = new CAPABILITY();
236 cap.setType("das1:" + Capabilities.FEATURES.getName());
237 cap.setQueryUri(url+"/features");
241 v.getCAPABILITY().addAll(cp);
242 local.getVERSION().add(v);
248 public jalviewSourceI getSource(String nickname)
250 return sourceNames.get(nickname);
254 public boolean removeLocalSource(jalviewSourceI source)
256 if (localSources.containsValue(source))
258 localSources.remove(source.getTitle());
259 sourceNames.remove(source.getTitle());
260 dasSources.remove(source);
267 public void refreshSources()
275 public List<jalviewSourceI> resolveSourceNicknames(List<String> sources)
277 ArrayList<jalviewSourceI> resolved = new ArrayList<jalviewSourceI>();
278 if (sourceNames != null)
280 for (String src : sources)
282 jalviewSourceI dsrc = sourceNames.get(src);
293 public String getLocalSourceString()
295 if (localSources != null)
297 StringBuffer sb = new StringBuffer();
298 Enumeration en = localSources.keys();
299 while (en.hasMoreElements())
301 String token = en.nextElement().toString();
302 jalviewSourceI srco = localSources.get(token);
303 sb.append(token + "|"
304 + (srco.isSequenceSource() ? "sequence:" : "")
305 + srco.getUri() + "\t");
307 return sb.toString();
312 private static final Hashtable<URL, String> authStash;
315 authStash = new Hashtable<URL, String>();
319 // TODO: allow same credentials for https and http
320 authStash.put(new URL(
321 "http://www.compbio.dundee.ac.uk/geneweb/das/myseq/"),
322 "Basic SmltOm1pSg==");
323 } catch (MalformedURLException e)
325 // TODO Auto-generated catch block
331 public MultipleConnectionPropertyProviderI getSessionHandler()
337 public ConnectionPropertyProviderI getConnectionPropertyProviderFor(
341 final ConnectionPropertyProviderI conprov = new ConnectionPropertyProviderI()
343 boolean authed = false;
346 public void setConnectionProperties(HttpURLConnection connection)
348 String auth = authStash.get(connection.getURL());
349 if (auth != null && auth.length() > 0)
351 connection.setRequestProperty("Authorisation", auth);
361 public boolean getResponseProperties(HttpURLConnection connection)
363 String auth = authStash.get(connection.getURL());
364 if (auth != null && auth.length() == 0)
366 // don't attempt to check if we authed or not - user entered empty
374 // try and pass credentials.
377 // see if we should try and create a new auth record.
378 String ameth = connection.getHeaderField("X-DAS-AuthMethods");
379 Cache.log.debug("Could authenticate to " + connection.getURL()
380 + " with : " + ameth);
381 // TODO: search auth string and raise login box - return if auth was
387 // check to see if auth was successful
388 String asuc = connection
389 .getHeaderField("X-DAS_AuthenticatedUser");
390 if (asuc != null && asuc.trim().length() > 0)
392 // authentication was successful
393 Cache.log.debug("Authenticated successfully to "
394 + connection.getURL().toString());
397 // it wasn't - so we should tell the user it failed and ask if they
398 // want to attempt authentication again.
399 authStash.remove(connection.getURL());
400 // open a new login/password dialog with cancel button
401 // set new authStash content with password and return true
403 // User cancelled auth - so put empty string in stash to indicate we
404 // don't want to auth with this server.
405 // authStash.put(connection.getURL(), "");