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 Adapted for jalview use.
39 * @author Andrew Waterhouse Updated to Das 1.53e feature spec.
42 public class DAS_FeatureRetrieve
56 * the URL the features should be downloaded from
59 public DAS_FeatureRetrieve(URL url)
63 logger = Logger.getLogger("org.biojava.spice");
64 features = new ArrayList();
71 * contact the DAS-feature server again. Usually it is not necessary to call
72 * this again, because the constructor already does, but if comeBackLater > -1
73 * this should be called again.
82 InputStream dasInStream = null;
85 dasInStream = open(url);
89 System.out.println("NO RESPONSE FROM " + url);
90 logger.log(Level.FINE, "could not open connection to " + url, e);
94 SAXParserFactory spfactory = SAXParserFactory.newInstance();
96 spfactory.setValidating(false);
98 SAXParser saxParser = null;
102 saxParser = spfactory.newSAXParser();
103 } catch (ParserConfigurationException e)
108 String vali = System.getProperty("XMLVALIDATION");
110 boolean validation = false;
113 if (vali.equals("true"))
119 XMLReader xmlreader = saxParser.getXMLReader();
121 // XMLReader xmlreader = XMLReaderFactory.createXMLReader();
124 xmlreader.setFeature("http://xml.org/sax/features/validation",
126 } catch (SAXException e)
128 logger.log(Level.FINE, "Cannot set validation " + validation);
135 "http://apache.org/xml/features/nonvalidating/load-external-dtd",
137 } catch (SAXNotRecognizedException e)
141 .log(Level.FINE, "Cannot set load-external-dtd "
146 DAS_Feature_Handler cont_handle = new DAS_Feature_Handler();
147 cont_handle.setDASCommand(url.toString());
148 xmlreader.setContentHandler(cont_handle);
149 xmlreader.setErrorHandler(new org.xml.sax.helpers.DefaultHandler());
150 InputSource insource = new InputSource();
151 insource.setByteStream(dasInStream);
155 xmlreader.parse(insource);
156 features = cont_handle.get_features();
157 version = cont_handle.getVersion();
159 comeBackLater = cont_handle.getComBackLater();
160 } catch (Exception e)
163 .println("Error parsing response from: " + url + "\n" + e);
164 logger.log(Level.FINE, "error while parsing response from " + url);
166 features = new ArrayList();
168 } catch (Exception ex)
170 ex.printStackTrace();
176 * open HttpURLConnection. Recommended way to open HttpURLConnections, since
177 * this take care of setting timeouts properly for java 1.4 and 1.5
179 public static HttpURLConnection openHttpURLConnection(URL url)
180 throws IOException, ConnectException
182 HttpURLConnection huc = null;
183 huc = (HttpURLConnection) url.openConnection();
185 String os_name = java.lang.System.getProperty("os.name");
186 String os_version = java.lang.System.getProperty("os.version");
187 String os_arch = java.lang.System.getProperty("os.arch");
188 String VERSION = "1.0";
190 String userAgent = "Jalview " + VERSION + "(" + os_name + "; "
191 + os_arch + " ; " + os_version + ")";
192 // e.g. "Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.7.2) Gecko/20040803"
193 huc.addRequestProperty("User-Agent", userAgent);
194 // logger.finest("opening "+url);
198 .setProperty("sun.net.client.defaultConnectTimeout", timeout
200 System.setProperty("sun.net.client.defaultReadTimeout", timeout + "");
202 // use reflection to determine if get and set timeout methods for
203 // urlconnection are available
204 // seems java 1.5 does not watch the System properties any longer...
205 // and java 1.4 did not provide these...
206 // for 1.4 see setSystemProperties
210 // try to use reflection to set timeout property
211 Class urlconnectionClass = Class
212 .forName("java.net.HttpURLConnection");
214 Method setconnecttimeout = urlconnectionClass.getMethod(
215 "setConnectTimeout", new Class[]
217 setconnecttimeout.invoke(huc, new Object[]
218 { new Integer(timeout) });
220 Method setreadtimeout = urlconnectionClass.getMethod(
221 "setReadTimeout", new Class[]
223 setreadtimeout.invoke(huc, new Object[]
224 { new Integer(timeout) });
225 // System.out.println("successfully set java 1.5 timeout");
226 } catch (Exception e)
228 // e.printStackTrace();
229 // most likely it was a NoSuchMEthodException and we are running java 1.4.
234 private InputStream open(URL url) throws java.io.IOException,
235 java.net.ConnectException
237 InputStream inStream = null;
239 HttpURLConnection huc = openHttpURLConnection(url);
241 inStream = huc.getInputStream();
248 * returns a List of Features
250 * @return a List of Maps containing the features
252 public List get_features()
259 * Get the version string of the reference object. If it does not match the
260 * version string that is obtained from the reference server there is a
263 * @return version string. (e.g. a MD5 digest of the reference sequence)
265 public String getVersion()
270 public void setVersion(String version)
272 this.version = version;
276 * returns the comeBackLater value - if a server returned suchh -
278 * @return comeBackLater in seconds, or -1 if not provided by server
280 public int getComeBackLater()
283 return comeBackLater;