This commit was manufactured by cvs2svn to create branch 'VamJalview'.
[jalview.git] / src / org / biojava / dasobert / das / DAS_FeatureRetrieve.java
1 /**
2  *                    BioJava development code
3  *
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,
7  * see:
8  *
9  *      http://www.gnu.org/copyleft/lesser.html
10  *
11  * Copyright for this code is held jointly by the individual
12  * authors.  These should be listed in @author doc comments.
13  *
14  * For more information on the BioJava project and its aims,
15  * or to join the biojava-l mailing list, visit the home page
16  * at:
17  *
18  *      http://www.biojava.org/
19  *
20  * Created on 19.03.2004
21  * @author Andreas Prlic
22  *
23  */
24 package org.biojava.dasobert.das;
25
26
27 import java.net.URL                         ;
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;
40
41
42
43
44 /**
45  * A class to perform a DAS features request
46  *
47  * @author Andreas Prlic
48  *
49  */
50 public class DAS_FeatureRetrieve {
51
52     List features ;
53     Logger logger     ;
54     int comeBackLater;
55     URL url;
56     /**
57      * @param url the URL the features should be downloaded from
58      *
59      */
60     public DAS_FeatureRetrieve(URL url) {
61         super();
62
63         logger = Logger.getLogger("org.biojava.spice");
64         features = new ArrayList() ;
65         comeBackLater = -1;
66         this.url=url;
67         reload();
68     }
69
70
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.
74      *
75      */
76     public void reload(){
77
78         try {
79
80             InputStream dasInStream = null;
81             try {
82                 dasInStream     = open(url);
83             } catch (Exception e ){
84                 comeBackLater = -1;
85                 logger.log(Level.FINE,"could not open connection to " + url,e);
86                 return ;
87             }
88
89
90             SAXParserFactory spfactory =
91                 SAXParserFactory.newInstance();
92
93             spfactory.setValidating(false);
94
95             SAXParser saxParser = null ;
96
97             try{
98                 saxParser =
99                     spfactory.newSAXParser();
100             } catch (ParserConfigurationException e) {
101                 e.printStackTrace();
102             }
103
104
105
106             String vali = System.getProperty("XMLVALIDATION");
107
108             boolean validation = false ;
109             if ( vali != null )
110                 if ( vali.equals("true") )
111                     validation = true ;
112
113
114             XMLReader xmlreader = saxParser.getXMLReader();
115
116             //XMLReader xmlreader = XMLReaderFactory.createXMLReader();
117             try {
118                 xmlreader.setFeature("http://xml.org/sax/features/validation", validation);
119             } catch (SAXException e) {
120                 logger.log(Level.FINE,"Cannot set validation " + validation);
121             }
122
123             try {
124                 xmlreader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd",validation);
125             } catch (SAXNotRecognizedException e){
126                 e.printStackTrace();
127                 logger.log(Level.FINE,"Cannot set load-external-dtd "+validation);
128
129             }
130
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);
137
138             try {
139                 xmlreader.parse(insource);
140                 features = cont_handle.get_features();
141                 comeBackLater = cont_handle.getComBackLater();
142             }
143             catch ( Exception e){
144                 e.printStackTrace();
145                 logger.log(Level.FINE,"error while parsing response from "+ url);
146                 comeBackLater = -1;
147                 features = new ArrayList();
148             }
149         }
150         catch (Exception ex) {
151             ex.printStackTrace();
152             comeBackLater = -1;
153         }
154     }
155
156
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();
164
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";
169
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);
174
175
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
180     int timeout = 15000;
181     try {
182         // try to use reflection to set timeout property
183         Class urlconnectionClass = Class.forName("java.net.HttpURLConnection");
184
185             Method setconnecttimeout = urlconnectionClass.getMethod (
186                                      "setConnectTimeout", new Class [] {int.class}
187                                      );
188         setconnecttimeout.invoke(huc,new Object[] {new Integer(timeout)});
189
190         Method setreadtimeout = urlconnectionClass.getMethod (
191                                   "setReadTimeout", new Class[] {int.class}
192                                   );
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.
198     }
199     return huc;
200     }
201
202
203     private InputStream open(URL url)
204     throws java.io.IOException, java.net.ConnectException
205     {
206         InputStream inStream = null;
207
208
209         HttpURLConnection huc = openHttpURLConnection(url);
210
211         inStream = huc.getInputStream();
212
213         return inStream;
214
215     }
216
217     /** returns a List of Features
218      * @return a List of Maps containing the features*/
219     public List get_features() {
220
221         return features;
222     }
223
224     /** returns the comeBackLater value - if a server returned suchh -
225      *
226      * @return comeBackLater in seconds, or -1 if not provided by server
227      */
228     public int getComeBackLater(){
229
230         return comeBackLater;
231
232     }
233
234
235 }