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();
143 catch ( Exception e){
145 logger.log(Level.FINE,"error while parsing response from "+ url);
147 features = new ArrayList();
150 catch (Exception ex) {
151 ex.printStackTrace();
157 /** open HttpURLConnection. Recommended way to open
158 * HttpURLConnections, since this take care of setting timeouts
159 * properly for java 1.4 and 1.5*/
160 public static HttpURLConnection openHttpURLConnection(URL url)
161 throws IOException, ConnectException {
162 HttpURLConnection huc = null;
163 huc = (HttpURLConnection) url.openConnection();
165 String os_name = java.lang.System.getProperty("os.name");
166 String os_version = java.lang.System.getProperty("os.version");
167 String os_arch = java.lang.System.getProperty("os.arch");
168 String VERSION = "1.0";
170 String userAgent = "Jalview " + VERSION + "("+os_name+"; "+os_arch + " ; "+ os_version+")";
171 //e.g. "Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.7.2) Gecko/20040803"
172 huc.addRequestProperty("User-Agent", userAgent);
173 //logger.finest("opening "+url);
176 // use reflection to determine if get and set timeout methods for urlconnection are available
177 // seems java 1.5 does not watch the System properties any longer...
178 // and java 1.4 did not provide these...
179 // for 1.4 see setSystemProperties
182 // try to use reflection to set timeout property
183 Class urlconnectionClass = Class.forName("java.net.HttpURLConnection");
185 Method setconnecttimeout = urlconnectionClass.getMethod (
186 "setConnectTimeout", new Class [] {int.class}
188 setconnecttimeout.invoke(huc,new Object[] {new Integer(timeout)});
190 Method setreadtimeout = urlconnectionClass.getMethod (
191 "setReadTimeout", new Class[] {int.class}
193 setreadtimeout.invoke(huc,new Object[] {new Integer(timeout)});
194 //System.out.println("successfully set java 1.5 timeout");
195 } catch (Exception e) {
196 //e.printStackTrace();
197 // most likely it was a NoSuchMEthodException and we are running java 1.4.
203 private InputStream open(URL url)
204 throws java.io.IOException, java.net.ConnectException
206 InputStream inStream = null;
209 HttpURLConnection huc = openHttpURLConnection(url);
211 inStream = huc.getInputStream();
217 /** returns a List of Features
218 * @return a List of Maps containing the features*/
219 public List get_features() {
224 /** returns the comeBackLater value - if a server returned suchh -
226 * @return comeBackLater in seconds, or -1 if not provided by server
228 public int getComeBackLater(){
230 return comeBackLater;