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;
28 import java.io.InputStream ;
29 import org.xml.sax.InputSource ;
30 import org.xml.sax.XMLReader ;
31 import javax.xml.parsers.* ;
32 import org.xml.sax.* ;
33 import java.util.ArrayList ;
34 import java.util.List;
35 import java.util.logging.* ;
36 import java.net.HttpURLConnection ;
37 import java.io.IOException;
38 import java.net.ConnectException;
39 import java.lang.reflect.Method;
45 * A class to perform a DAS features request
47 * @author Andreas Prlic
50 public class DAS_FeatureRetrieve {
57 * @param url the URL the features should be downloaded from
60 public DAS_FeatureRetrieve(URL url) {
63 logger = Logger.getLogger("org.biojava.spice");
64 features = new ArrayList() ;
71 /** contact the DAS-feature server again. Usually
72 * it is not necessary to call this again, because the constructor already does, but
73 * if comeBackLater > -1 this should be called again.
80 InputStream dasInStream = null;
82 dasInStream = open(url);
83 } catch (Exception e ){
85 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 ;
99 spfactory.newSAXParser();
100 } catch (ParserConfigurationException e) {
106 String vali = System.getProperty("XMLVALIDATION");
108 boolean validation = false ;
110 if ( vali.equals("true") )
114 XMLReader xmlreader = saxParser.getXMLReader();
116 //XMLReader xmlreader = XMLReaderFactory.createXMLReader();
118 xmlreader.setFeature("http://xml.org/sax/features/validation", validation);
119 } catch (SAXException e) {
120 logger.log(Level.FINE,"Cannot set validation " + validation);
124 xmlreader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd",validation);
125 } catch (SAXNotRecognizedException e){
127 logger.log(Level.FINE,"Cannot set load-external-dtd "+validation);
131 DAS_Feature_Handler cont_handle = new DAS_Feature_Handler() ;
132 cont_handle.setDASCommand(url.toString());
133 xmlreader.setContentHandler(cont_handle);
134 xmlreader.setErrorHandler(new org.xml.sax.helpers.DefaultHandler());
135 InputSource insource = new InputSource() ;
136 insource.setByteStream(dasInStream);
139 xmlreader.parse(insource);
140 features = cont_handle.get_features();
141 comeBackLater = cont_handle.getComBackLater();
145 System.out.println("Error parsing response from: "+url+"\n"+e);
146 logger.log(Level.FINE,"error while parsing response from "+ url);
148 features = new ArrayList();
153 ex.printStackTrace();
159 /** open HttpURLConnection. Recommended way to open
160 * HttpURLConnections, since this take care of setting timeouts
161 * properly for java 1.4 and 1.5*/
162 public static HttpURLConnection openHttpURLConnection(URL url)
163 throws IOException, ConnectException {
164 HttpURLConnection huc = null;
165 huc = (HttpURLConnection) url.openConnection();
167 String os_name = java.lang.System.getProperty("os.name");
168 String os_version = java.lang.System.getProperty("os.version");
169 String os_arch = java.lang.System.getProperty("os.arch");
170 String VERSION = "1.0";
172 String userAgent = "Jalview " + VERSION + "("+os_name+"; "+os_arch + " ; "+ os_version+")";
173 //e.g. "Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.7.2) Gecko/20040803"
174 huc.addRequestProperty("User-Agent", userAgent);
175 //logger.finest("opening "+url);
178 // use reflection to determine if get and set timeout methods for urlconnection are available
179 // seems java 1.5 does not watch the System properties any longer...
180 // and java 1.4 did not provide these...
181 // for 1.4 see setSystemProperties
184 // try to use reflection to set timeout property
185 Class urlconnectionClass = Class.forName("java.net.HttpURLConnection");
187 Method setconnecttimeout = urlconnectionClass.getMethod (
188 "setConnectTimeout", new Class [] {int.class}
190 setconnecttimeout.invoke(huc,new Object[] {new Integer(timeout)});
192 Method setreadtimeout = urlconnectionClass.getMethod (
193 "setReadTimeout", new Class[] {int.class}
195 setreadtimeout.invoke(huc,new Object[] {new Integer(timeout)});
196 //System.out.println("successfully set java 1.5 timeout");
197 } catch (Exception e) {
198 //e.printStackTrace();
199 // most likely it was a NoSuchMEthodException and we are running java 1.4.
205 private InputStream open(URL url)
206 throws java.io.IOException, java.net.ConnectException
208 InputStream inStream = null;
211 HttpURLConnection huc = openHttpURLConnection(url);
213 inStream = huc.getInputStream();
219 /** returns a List of Features
220 * @return a List of Maps containing the features*/
221 public List get_features() {
226 /** returns the comeBackLater value - if a server returned suchh -
228 * @return comeBackLater in seconds, or -1 if not provided by server
230 public int getComeBackLater(){
232 return comeBackLater;