2 * BioJava development code
4 * This code may be freely distributed and modified under the
5 * terms of the GNU Lesser General Public Licence. This should
6 * be distributed with the code. If you do not have a copy,
9 * http://www.gnu.org/copyleft/lesser.html
11 * Copyright for this code is held jointly by the individual
12 * authors. These should be listed in @author doc comments.
14 * For more information on the BioJava project and its aims,
15 * or to join the biojava-l mailing list, visit the home page
18 * http://www.biojava.org/
20 * Created on 19.03.2004
21 * @author Andreas Prlic
24 package org.biojava.dasobert.das;
27 import java.lang.reflect.*;
30 import java.util.logging.*;
31 import javax.xml.parsers.*;
36 * A class to perform a DAS features request
38 * @author Andreas Prlic
39 * Adapted for jalview use.
40 * @author Andrew Waterhouse
41 * Updated to Das 1.53e feature spec.
44 public class DAS_FeatureRetrieve
52 * @param url the URL the features should be downloaded from
55 public DAS_FeatureRetrieve(URL url)
59 logger = Logger.getLogger("org.biojava.spice");
60 features = new ArrayList();
66 /** contact the DAS-feature server again. Usually
67 * it is not necessary to call this again, because the constructor already does, but
68 * if comeBackLater > -1 this should be called again.
77 InputStream dasInStream = null;
80 dasInStream = open(url);
85 System.out.println("NO RESPONSE FROM " + url);
86 logger.log(Level.FINE, "could not open connection to " + url, e);
90 SAXParserFactory spfactory =
91 SAXParserFactory.newInstance();
93 spfactory.setValidating(false);
95 SAXParser saxParser = null;
100 spfactory.newSAXParser();
102 catch (ParserConfigurationException e)
107 String vali = System.getProperty("XMLVALIDATION");
109 boolean validation = false;
112 if (vali.equals("true"))
118 XMLReader xmlreader = saxParser.getXMLReader();
120 //XMLReader xmlreader = XMLReaderFactory.createXMLReader();
123 xmlreader.setFeature("http://xml.org/sax/features/validation",
126 catch (SAXException e)
128 logger.log(Level.FINE, "Cannot set validation " + validation);
133 xmlreader.setFeature(
134 "http://apache.org/xml/features/nonvalidating/load-external-dtd",
137 catch (SAXNotRecognizedException e)
140 logger.log(Level.FINE, "Cannot set load-external-dtd " + validation);
144 DAS_Feature_Handler cont_handle = new DAS_Feature_Handler();
145 cont_handle.setDASCommand(url.toString());
146 xmlreader.setContentHandler(cont_handle);
147 xmlreader.setErrorHandler(new org.xml.sax.helpers.DefaultHandler());
148 InputSource insource = new InputSource();
149 insource.setByteStream(dasInStream);
153 xmlreader.parse(insource);
154 features = cont_handle.get_features();
155 version = cont_handle.getVersion();
157 comeBackLater = cont_handle.getComBackLater();
161 System.out.println("Error parsing response from: " + url + "\n" + e);
162 logger.log(Level.FINE, "error while parsing response from " + url);
164 features = new ArrayList();
169 ex.printStackTrace();
174 /** open HttpURLConnection. Recommended way to open
175 * HttpURLConnections, since this take care of setting timeouts
176 * properly for java 1.4 and 1.5*/
177 public static HttpURLConnection openHttpURLConnection(URL url)
178 throws IOException, ConnectException
180 HttpURLConnection huc = null;
181 huc = (HttpURLConnection) url.openConnection();
183 String os_name = java.lang.System.getProperty("os.name");
184 String os_version = java.lang.System.getProperty("os.version");
185 String os_arch = java.lang.System.getProperty("os.arch");
186 String VERSION = "1.0";
188 String userAgent = "Jalview " + VERSION + "(" + os_name + "; " + os_arch +
189 " ; " + os_version + ")";
190 //e.g. "Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.7.2) Gecko/20040803"
191 huc.addRequestProperty("User-Agent", userAgent);
192 //logger.finest("opening "+url);
196 System.setProperty("sun.net.client.defaultConnectTimeout", timeout + "");
197 System.setProperty("sun.net.client.defaultReadTimeout", timeout + "");
199 // use reflection to determine if get and set timeout methods for urlconnection are available
200 // seems java 1.5 does not watch the System properties any longer...
201 // and java 1.4 did not provide these...
202 // for 1.4 see setSystemProperties
206 // try to use reflection to set timeout property
207 Class urlconnectionClass = Class.forName("java.net.HttpURLConnection");
209 Method setconnecttimeout = urlconnectionClass.getMethod(
210 "setConnectTimeout", new Class[]
213 setconnecttimeout.invoke(huc, new Object[]
214 {new Integer(timeout)});
216 Method setreadtimeout = urlconnectionClass.getMethod(
217 "setReadTimeout", new Class[]
220 setreadtimeout.invoke(huc, new Object[]
221 {new Integer(timeout)});
222 //System.out.println("successfully set java 1.5 timeout");
226 //e.printStackTrace();
227 // most likely it was a NoSuchMEthodException and we are running java 1.4.
232 private InputStream open(URL url)
233 throws java.io.IOException, java.net.ConnectException
235 InputStream inStream = null;
237 HttpURLConnection huc = openHttpURLConnection(url);
239 inStream = huc.getInputStream();
245 /** returns a List of Features
246 * @return a List of Maps containing the features*/
247 public List get_features()
254 /** Get the version string of the reference object.
255 * If it does not match the version string that is obtained from the
256 * reference server there is a version problem!
258 * @return version string. (e.g. a MD5 digest of the reference sequence)
260 public String getVersion() {
265 public void setVersion(String version) {
266 this.version = version;
269 /** returns the comeBackLater value - if a server returned suchh -
271 * @return comeBackLater in seconds, or -1 if not provided by server
273 public int getComeBackLater()
276 return comeBackLater;