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
41 public class DAS_FeatureRetrieve
49 * @param url the URL the features should be downloaded from
52 public DAS_FeatureRetrieve(URL url)
56 logger = Logger.getLogger("org.biojava.spice");
57 features = new ArrayList();
63 /** contact the DAS-feature server again. Usually
64 * it is not necessary to call this again, because the constructor already does, but
65 * if comeBackLater > -1 this should be called again.
74 InputStream dasInStream = null;
77 dasInStream = open(url);
82 System.out.println("NO RESPONSE FROM " + url);
83 logger.log(Level.FINE, "could not open connection to " + url, e);
87 SAXParserFactory spfactory =
88 SAXParserFactory.newInstance();
90 spfactory.setValidating(false);
92 SAXParser saxParser = null;
97 spfactory.newSAXParser();
99 catch (ParserConfigurationException e)
104 String vali = System.getProperty("XMLVALIDATION");
106 boolean validation = false;
109 if (vali.equals("true"))
115 XMLReader xmlreader = saxParser.getXMLReader();
117 //XMLReader xmlreader = XMLReaderFactory.createXMLReader();
120 xmlreader.setFeature("http://xml.org/sax/features/validation",
123 catch (SAXException e)
125 logger.log(Level.FINE, "Cannot set validation " + validation);
130 xmlreader.setFeature(
131 "http://apache.org/xml/features/nonvalidating/load-external-dtd",
134 catch (SAXNotRecognizedException e)
137 logger.log(Level.FINE, "Cannot set load-external-dtd " + validation);
141 DAS_Feature_Handler cont_handle = new DAS_Feature_Handler();
142 cont_handle.setDASCommand(url.toString());
143 xmlreader.setContentHandler(cont_handle);
144 xmlreader.setErrorHandler(new org.xml.sax.helpers.DefaultHandler());
145 InputSource insource = new InputSource();
146 insource.setByteStream(dasInStream);
150 xmlreader.parse(insource);
151 features = cont_handle.get_features();
152 comeBackLater = cont_handle.getComBackLater();
156 System.out.println("Error parsing response from: " + url + "\n" + e);
157 logger.log(Level.FINE, "error while parsing response from " + url);
159 features = new ArrayList();
164 ex.printStackTrace();
169 /** open HttpURLConnection. Recommended way to open
170 * HttpURLConnections, since this take care of setting timeouts
171 * properly for java 1.4 and 1.5*/
172 public static HttpURLConnection openHttpURLConnection(URL url)
173 throws IOException, ConnectException
175 HttpURLConnection huc = null;
176 huc = (HttpURLConnection) url.openConnection();
178 String os_name = java.lang.System.getProperty("os.name");
179 String os_version = java.lang.System.getProperty("os.version");
180 String os_arch = java.lang.System.getProperty("os.arch");
181 String VERSION = "1.0";
183 String userAgent = "Jalview " + VERSION + "(" + os_name + "; " + os_arch +
184 " ; " + os_version + ")";
185 //e.g. "Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.7.2) Gecko/20040803"
186 huc.addRequestProperty("User-Agent", userAgent);
187 //logger.finest("opening "+url);
191 System.setProperty("sun.net.client.defaultConnectTimeout", timeout + "");
192 System.setProperty("sun.net.client.defaultReadTimeout", timeout + "");
194 // use reflection to determine if get and set timeout methods for urlconnection are available
195 // seems java 1.5 does not watch the System properties any longer...
196 // and java 1.4 did not provide these...
197 // for 1.4 see setSystemProperties
201 // try to use reflection to set timeout property
202 Class urlconnectionClass = Class.forName("java.net.HttpURLConnection");
204 Method setconnecttimeout = urlconnectionClass.getMethod(
205 "setConnectTimeout", new Class[]
208 setconnecttimeout.invoke(huc, new Object[]
209 {new Integer(timeout)});
211 Method setreadtimeout = urlconnectionClass.getMethod(
212 "setReadTimeout", new Class[]
215 setreadtimeout.invoke(huc, new Object[]
216 {new Integer(timeout)});
217 //System.out.println("successfully set java 1.5 timeout");
221 //e.printStackTrace();
222 // most likely it was a NoSuchMEthodException and we are running java 1.4.
227 private InputStream open(URL url)
228 throws java.io.IOException, java.net.ConnectException
230 InputStream inStream = null;
232 HttpURLConnection huc = openHttpURLConnection(url);
234 inStream = huc.getInputStream();
240 /** returns a List of Features
241 * @return a List of Maps containing the features*/
242 public List get_features()
248 /** returns the comeBackLater value - if a server returned suchh -
250 * @return comeBackLater in seconds, or -1 if not provided by server
252 public int getComeBackLater()
255 return comeBackLater;