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;
43 * A class to perform a DAS features request
45 * @author Andreas Prlic
48 public class DAS_FeatureRetrieve {
57 public DAS_FeatureRetrieve(URL url) {
60 logger = Logger.getLogger("org.biojava.spice");
61 features = new ArrayList() ;
68 /** contact the DAS-feature server again. Usually
69 * it is not necessary to call this again, because the constructor already does, but
70 * if comeBackLater > -1 this should be called again.
77 InputStream dasInStream = null;
79 dasInStream = open(url);
80 } catch (Exception e ){
82 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 ;
96 spfactory.newSAXParser();
97 } catch (ParserConfigurationException e) {
103 String vali = System.getProperty("XMLVALIDATION");
105 boolean validation = false ;
107 if ( vali.equals("true") )
111 XMLReader xmlreader = saxParser.getXMLReader();
113 //XMLReader xmlreader = XMLReaderFactory.createXMLReader();
115 xmlreader.setFeature("http://xml.org/sax/features/validation", validation);
116 } catch (SAXException e) {
117 logger.log(Level.FINE,"Cannot set validation " + validation);
121 xmlreader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd",validation);
122 } catch (SAXNotRecognizedException e){
124 logger.log(Level.FINE,"Cannot set load-external-dtd "+validation);
128 DAS_Feature_Handler cont_handle = new DAS_Feature_Handler() ;
129 cont_handle.setDASCommand(url.toString());
130 xmlreader.setContentHandler(cont_handle);
131 xmlreader.setErrorHandler(new org.xml.sax.helpers.DefaultHandler());
132 InputSource insource = new InputSource() ;
133 insource.setByteStream(dasInStream);
136 xmlreader.parse(insource);
137 features = cont_handle.get_features();
138 comeBackLater = cont_handle.getComBackLater();
140 catch ( Exception e){
141 System.out.println("ERROR PARSING RESULT FROM "+url+ "\n"+e+"\n");
142 // e.printStackTrace();
143 logger.log(Level.FINE,"error while parsing response from "+ url);
145 features = new ArrayList();
148 catch (Exception ex) {
149 ex.printStackTrace();
154 /** open HttpURLConnection. Recommended way to open
155 * HttpURLConnections, since this take care of setting timeouts
156 * properly for java 1.4 and 1.5*/
157 public static HttpURLConnection openHttpURLConnection(URL url)
158 throws IOException, ConnectException {
159 HttpURLConnection huc = null;
160 huc = (HttpURLConnection) url.openConnection();
162 String os_name = java.lang.System.getProperty("os.name");
163 String os_version = java.lang.System.getProperty("os.version");
164 String os_arch = java.lang.System.getProperty("os.arch");
165 String VERSION = "1.0";
167 String userAgent = "Jalview " + VERSION + "("+os_name+"; "+os_arch + " ; "+ os_version+")";
168 //e.g. "Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.7.2) Gecko/20040803"
169 huc.addRequestProperty("User-Agent", userAgent);
170 //logger.finest("opening "+url);
173 // use reflection to determine if get and set timeout methods for urlconnection are available
174 // seems java 1.5 does not watch the System properties any longer...
175 // and java 1.4 did not provide these...
176 // for 1.4 see setSystemProperties
179 // try to use reflection to set timeout property
180 Class urlconnectionClass = Class.forName("java.net.HttpURLConnection");
182 Method setconnecttimeout = urlconnectionClass.getMethod (
183 "setConnectTimeout", new Class [] {int.class}
185 setconnecttimeout.invoke(huc,new Object[] {new Integer(timeout)});
187 Method setreadtimeout = urlconnectionClass.getMethod (
188 "setReadTimeout", new Class[] {int.class}
190 setreadtimeout.invoke(huc,new Object[] {new Integer(timeout)});
191 //System.out.println("successfully set java 1.5 timeout");
192 } catch (Exception e) {
193 //e.printStackTrace();
194 // most likely it was a NoSuchMEthodException and we are running java 1.4.
199 private InputStream open(URL url)
200 throws java.io.IOException, java.net.ConnectException
202 InputStream inStream = null;
205 HttpURLConnection huc = openHttpURLConnection(url);
207 inStream = huc.getInputStream();
213 /** returns a List of Features */
214 public List get_features() {
219 /** returns the comeBackLater value - if a server returned suchh -
221 * @return comeBackLater in seconds, or -1 if not provided by server
223 public int getComeBackLater(){
225 return comeBackLater;