X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fws%2Febi%2FEBIFetchClient.java;h=3eef460bc9bb8f46ae803fa8c2889ff27373271b;hb=c932f0e85a8852824cdd8ce790af68682732c85c;hp=55315129a27d14fa837380abac4c715f8f4b0edb;hpb=30b2b47cbdfa35b127b0fb09e911815cddd9ed7b;p=jalview.git diff --git a/src/jalview/ws/ebi/EBIFetchClient.java b/src/jalview/ws/ebi/EBIFetchClient.java index 5531512..3eef460 100644 --- a/src/jalview/ws/ebi/EBIFetchClient.java +++ b/src/jalview/ws/ebi/EBIFetchClient.java @@ -20,15 +20,18 @@ */ 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; import java.net.URL; import java.util.ArrayList; import java.util.List; @@ -90,8 +93,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 +105,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) @@ -133,13 +136,14 @@ public class EBIFetchClient 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'"); + jalview.bin.Console.errPrintln("Invalid Query string : '" + ids + "'"); + jalview.bin.Console.errPrintln("Should be of form 'dbname:q1;q2;q3;q4'"); return null; } // note: outFile is currently always specified, so return value is null - String[] rslt = fetchBatch(querystring.toString(), database, format, outFile); + String[] rslt = fetchBatch(querystring.toString(), database, format, + outFile); return (rslt != null && rslt.length > 0 ? rslt : null); } @@ -191,69 +195,83 @@ 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); - - InputStream is = new BufferedInputStream(rcall.openStream()); - if (outFile != null) + HttpURLConnection conn = (HttpURLConnection) rcall.openConnection(); + int responseCode = conn.getResponseCode(); + if (responseCode == 200) { - FileOutputStream fio = new FileOutputStream(outFile); - // fio.write("\n".getBytes()); - 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(); } + jalview.bin.Console.errPrintln( + "Warning: response code " + responseCode + " for " + url); } catch (OutOfMemoryError er) { - System.out.println("OUT OF MEMORY DOWNLOADING QUERY FROM " + database + jalview.bin.Console.outPrintln("OUT OF MEMORY DOWNLOADING QUERY FROM " + database + ":\n" + ids); throw er; } catch (Exception ex) { - if (ex.getMessage().startsWith( + if (!ex.getMessage().startsWith( "uk.ac.ebi.jdbfetch.exceptions.DbfNoEntryFoundException")) { - return null; + jalview.bin.Console.errPrintln("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; } + static + { + Platform.addJ2SDirectDatabaseCall("https://www.ebi.ac.uk/"); + } + /** * Constructs the URL to fetch from * @@ -268,13 +286,14 @@ 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(Locale.ROOT) + "?download=true&gzip=true"; } else { - url = "http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/" - + database.toLowerCase() + "/" + ids.toLowerCase() + url = "https://www.ebi.ac.uk/Tools/dbfetch/dbfetch/" + + database.toLowerCase(Locale.ROOT) + "/" + + ids.toLowerCase(Locale.ROOT) + (format != null ? "/" + format : ""); } return url;