Merge branch 'releases/Release_2_11_1_Branch' into merge/JAL_1842+JAL-3509+releases_R...
[jalview.git] / src / jalview / ws / ebi / EBIFetchClient.java
index dd3cc16..8ab5fbb 100644 (file)
@@ -27,8 +27,10 @@ import java.io.BufferedInputStream;
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileOutputStream;
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.List;
@@ -90,8 +92,8 @@ public class EBIFetchClient
    *          the query formatted as db:query1;query2;query3
    * @param format
    *          the format wanted
-   * @param extension
-   *          for the temporary file to hold response
+   * @param ext
+   *          for the temporary file to hold response (without separator)
    * @return the file holding the response
    * @throws OutOfMemoryError
    */
@@ -102,7 +104,7 @@ public class EBIFetchClient
     File outFile = null;
     try
     {
-      outFile = File.createTempFile("jalview", ext);
+      outFile = File.createTempFile("jalview", "." + ext);
       outFile.deleteOnExit();
       fetchData(ids, format, outFile);
       if (outFile.length() == 0)
@@ -116,28 +118,33 @@ public class EBIFetchClient
     return outFile;
   }
 
-  String[] fetchData(String ids, String f, File outFile)
+  /**
+   * Fetches queries and either saves the response to a file or returns as
+   * string data
+   * 
+   * @param ids
+   * @param format
+   * @param outFile
+   * @return
+   * @throws OutOfMemoryError
+   */
+  String[] fetchData(String ids, String format, File outFile)
           throws OutOfMemoryError
   {
-    String[] rslts = new String[0];
     StringBuilder querystring = new StringBuilder(ids.length());
-    String db = parseIds(ids, querystring);
-    if (db == null)
+    String database = parseIds(ids, querystring);
+    if (database == null)
     {
       System.err.println("Invalid Query string : '" + ids + "'");
       System.err.println("Should be of form 'dbname:q1;q2;q3;q4'");
       return null;
     }
-    String[] rslt = fetchBatch(querystring.toString(), db, f, outFile);
-    if (rslt != null)
-    {
-      String[] nrslts = new String[rslt.length + rslts.length];
-      System.arraycopy(rslts, 0, nrslts, 0, rslts.length);
-      System.arraycopy(rslt, 0, nrslts, rslts.length, rslt.length);
-      rslts = nrslts;
-    }
 
-    return (rslts.length == 0 ? null : rslts);
+    // note: outFile is currently always specified, so return value is null
+    String[] rslt = fetchBatch(querystring.toString(), database, format,
+            outFile);
+
+    return (rslt != null && rslt.length > 0 ? rslt : null);
   }
 
   /**
@@ -179,17 +186,36 @@ public class EBIFetchClient
     return database;
   }
 
-  String[] fetchBatch(String ids, String dbPath, String format,
+  /**
+   * Fetches queries and either saves the response to a file or (if no file
+   * specified) returns as string data
+   * 
+   * @param ids
+   * @param database
+   * @param format
+   * @param outFile
+   * @return
+   * @throws OutOfMemoryError
+   */
+  String[] fetchBatch(String ids, String database, String format,
           File outFile) throws OutOfMemoryError
   {
     // long time = System.currentTimeMillis();
-    String url = buildUrl(ids, dbPath, format);
-
+    String url = buildUrl(ids, database, format);
+    InputStream is = null;
+    BufferedReader br = null;
     try
     {
       URL rcall = new URL(url);
 
-      InputStream is = new BufferedInputStream(rcall.openStream());
+      HttpURLConnection conn = (HttpURLConnection) rcall.openConnection();
+      int responseCode = conn.getResponseCode();
+      if (responseCode != 200)
+      {
+        System.err.println("Warning: response code " + responseCode
+                + " for " + url);
+      }
+      is = new BufferedInputStream(conn.getInputStream());
       if (outFile != null)
       {
         FileOutputStream fio = new FileOutputStream(outFile);
@@ -204,7 +230,7 @@ public class EBIFetchClient
       }
       else
       {
-        BufferedReader br = new BufferedReader(new InputStreamReader(is));
+        br = new BufferedReader(new InputStreamReader(is));
         String rtn;
         List<String> arl = new ArrayList<String>();
         while ((rtn = br.readLine()) != null)
@@ -215,8 +241,7 @@ public class EBIFetchClient
       }
     } catch (OutOfMemoryError er)
     {
-
-      System.out.println("OUT OF MEMORY DOWNLOADING QUERY FROM " + dbPath
+      System.out.println("OUT OF MEMORY DOWNLOADING QUERY FROM " + database
               + ":\n" + ids);
       throw er;
     } catch (Exception ex)
@@ -227,14 +252,31 @@ public class EBIFetchClient
         return null;
       }
       System.err.println("Unexpected exception when retrieving from "
-              + dbPath
-              + "\nQuery was : '" + ids + "'");
+              + database + "\nQuery was : '" + ids + "'");
       ex.printStackTrace(System.err);
       return null;
     } finally
     {
       // System.err.println("EBIFetch took " + (System.currentTimeMillis() -
       // time) + " ms");
+      if (is != null)
+      {
+        try
+        {
+          is.close();
+        } catch (IOException e)
+        {
+        }
+      }
+      if (br != null)
+      {
+        try
+        {
+          br.close();
+        } catch (IOException e)
+        {
+        }
+      }
     }
     return null;
   }
@@ -253,12 +295,12 @@ public class EBIFetchClient
     if (database.equalsIgnoreCase(DBRefSource.EMBL)
             || database.equalsIgnoreCase(DBRefSource.EMBLCDS))
     {
-      url = "http://www.ebi.ac.uk/ena/data/view/" + ids.toLowerCase()
-              + (format != null ? "&" + format : "");
+      url = "https://www.ebi.ac.uk/ena/browser/api/embl/"
+              + ids.toLowerCase() + "?download=true&gzip=true";
     }
     else
     {
-      url = "http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/"
+      url = "https://www.ebi.ac.uk/Tools/dbfetch/dbfetch/"
               + database.toLowerCase() + "/" + ids.toLowerCase()
               + (format != null ? "/" + format : "");
     }