X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fjalview%2Fws%2Febi%2FEBIFetchClient.java;h=1d04351f06a9f057a9b6d1e5e58dab20ae245998;hb=9d2408483e451285fd555c3cd6e0273977acbaa7;hp=50151d962f92fa0c1810d19485712c373985c5a9;hpb=2467c106c4be8b70f244a160739be63efc6b58ea;p=jalview.git diff --git a/src/jalview/ws/ebi/EBIFetchClient.java b/src/jalview/ws/ebi/EBIFetchClient.java index 50151d9..1d04351 100644 --- a/src/jalview/ws/ebi/EBIFetchClient.java +++ b/src/jalview/ws/ebi/EBIFetchClient.java @@ -20,13 +20,15 @@ */ package jalview.ws.ebi; +import java.util.Locale; + import jalview.datamodel.DBRefSource; import jalview.util.MessageManager; +import jalview.util.Platform; -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; @@ -91,7 +93,7 @@ public class EBIFetchClient * the query formatted as db:query1;query2;query3 * @param format * the format wanted - * @param extension + * @param ext * for the temporary file to hold response (without separator) * @return the file holding the response * @throws OutOfMemoryError @@ -193,50 +195,40 @@ public class EBIFetchClient * @param database * @param format * @param outFile - * @return + * @return array of lines from EBI only if outFile is null (which it will not + * be) * @throws OutOfMemoryError */ String[] fetchBatch(String ids, String database, String format, File outFile) throws OutOfMemoryError { - // long time = System.currentTimeMillis(); String url = buildUrl(ids, database, format); - + InputStream is = null; + BufferedReader br = null; try { URL rcall = new URL(url); - HttpURLConnection conn = (HttpURLConnection) rcall.openConnection(); int responseCode = conn.getResponseCode(); - if (responseCode != 200) - { - System.out.println("Warning: response code " + responseCode - + " for " + url); - } - InputStream is = new BufferedInputStream(conn.getInputStream()); - if (outFile != null) + if (responseCode == 200) { - FileOutputStream fio = new FileOutputStream(outFile); - byte[] bb = new byte[32 * 1024]; - int l; - while ((l = is.read(bb)) > 0) + is = conn.getInputStream(); + if (outFile != null) { - fio.write(bb, 0, l); + Platform.streamToFile(is, outFile); + return null; } - fio.close(); - is.close(); - } - else - { - BufferedReader br = new BufferedReader(new InputStreamReader(is)); + br = new BufferedReader(new InputStreamReader(is)); String rtn; - List arl = new ArrayList(); + List arl = new ArrayList<>(); while ((rtn = br.readLine()) != null) { arl.add(rtn); } - return arl.toArray(new String[arl.size()]); + return (String[]) arl.toArray(); } + System.err.println( + "Warning: response code " + responseCode + " for " + url); } catch (OutOfMemoryError er) { System.out.println("OUT OF MEMORY DOWNLOADING QUERY FROM " + database @@ -244,19 +236,33 @@ public class EBIFetchClient throw er; } catch (Exception ex) { - if (ex.getMessage().startsWith( + if (!ex.getMessage().startsWith( "uk.ac.ebi.jdbfetch.exceptions.DbfNoEntryFoundException")) { - return null; + System.err.println("Unexpected exception when retrieving from " + + database + "\nQuery was : '" + ids + "'"); + ex.printStackTrace(System.err); } - System.err.println("Unexpected exception when retrieving from " - + 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; } @@ -275,13 +281,13 @@ public class EBIFetchClient if (database.equalsIgnoreCase(DBRefSource.EMBL) || database.equalsIgnoreCase(DBRefSource.EMBLCDS)) { - url = "https://www.ebi.ac.uk/ena/data/view/" + ids.toLowerCase() - + (format != null ? "&" + format : ""); + url = "https://www.ebi.ac.uk/ena/browser/api/embl/" + + ids.toLowerCase(Locale.ROOT) + "?download=true&gzip=true"; } else { url = "https://www.ebi.ac.uk/Tools/dbfetch/dbfetch/" - + database.toLowerCase() + "/" + ids.toLowerCase() + + database.toLowerCase(Locale.ROOT) + "/" + ids.toLowerCase(Locale.ROOT) + (format != null ? "/" + format : ""); } return url;