From e411c801ba976142e207e28f196d59ab24507767 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Wed, 16 Sep 2015 13:14:23 +0100 Subject: [PATCH] JAL-1859 refactor duplicated code to new HttpUtils class --- src/jalview/bin/JalviewLite.java | 94 +------------------- .../javascript/MouseOverStructureListener.java | 44 +-------- src/jalview/util/HttpUtils.java | 47 ++++++++++ 3 files changed, 55 insertions(+), 130 deletions(-) create mode 100644 src/jalview/util/HttpUtils.java diff --git a/src/jalview/bin/JalviewLite.java b/src/jalview/bin/JalviewLite.java index 30b8d54..3d59a12 100644 --- a/src/jalview/bin/JalviewLite.java +++ b/src/jalview/bin/JalviewLite.java @@ -48,6 +48,7 @@ import jalview.javascript.JsCallBack; import jalview.javascript.MouseOverStructureListener; import jalview.structure.SelectionListener; import jalview.structure.StructureSelectionManager; +import jalview.util.HttpUtils; import jalview.util.MessageManager; import java.applet.Applet; @@ -62,8 +63,6 @@ import java.awt.event.ActionEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; import java.util.ArrayList; @@ -1820,7 +1819,7 @@ public class JalviewLite extends Applet implements */ URL documentBase = getDocumentBase(); String withDocBase = resolveUrlForLocalOrAbsolute(path, documentBase); - if (urlExists(withDocBase)) + if (HttpUtils.isValidUrl(withDocBase)) { if (debug) { @@ -1837,7 +1836,8 @@ public class JalviewLite extends Applet implements URL codeBase = getCodeBase(); String withCodeBase = applet.resolveUrlForLocalOrAbsolute(path, codeBase); - if (!withCodeBase.equals(withDocBase) && urlExists(withCodeBase)) + if (!withCodeBase.equals(withDocBase) + && HttpUtils.isValidUrl(withCodeBase)) { protocol = AppletFormatAdapter.URL; if (debug) @@ -2409,92 +2409,6 @@ public class JalviewLite extends Applet implements return false; } } - - /** - * If the file is not already in URL format, tries to locate it by resolving - * as a URL. - * - * @param f - * @return - */ - String addProtocol(final String f) - { - if (f.indexOf("://") != -1) - { - // already has URL format - return f; - } - - /* - * Try relative to document base - */ - URL documentBase = getDocumentBase(); - System.err.println("Trying documentbase: " + documentBase); - String url = applet.resolveUrlForLocalOrAbsolute(f, documentBase); - if (urlExists(url)) - { - if (true/* debug */) - { - System.err.println("Prepended document base '" + documentBase - + "' to make: '" + url + "'"); - } - return url; - } - - /* - * Try relative to codebase - */ - URL codeBase = getCodeBase(); - System.err.println("Trying codebase: " + codeBase); - url = applet.resolveUrlForLocalOrAbsolute(f, codeBase); - if (urlExists(url)) - { - if (true/* debug */) - { - System.err.println("Prepended codebase '" + codeBase - + "' to make: '" + url + "'"); - } - return url; - } - - return f; - } - - /** - * Returns true if an input stream can be opened on the specified URL, else - * false. - * - * @param url - * @return - */ - private boolean urlExists(String url) - { - InputStream is = null; - try - { - is = new URL(url).openStream(); - if (is != null) - { - return true; - } - } catch (Exception x) - { - // ignore - } finally - { - if (is != null) - { - try - { - is.close(); - } catch (IOException e) - { - // ignore - } - } - } - return false; - } } /** diff --git a/src/jalview/javascript/MouseOverStructureListener.java b/src/jalview/javascript/MouseOverStructureListener.java index 176c881..199db46 100644 --- a/src/jalview/javascript/MouseOverStructureListener.java +++ b/src/jalview/javascript/MouseOverStructureListener.java @@ -32,6 +32,7 @@ import jalview.structure.StructureListener; import jalview.structure.StructureMapping; import jalview.structure.StructureMappingcommandSet; import jalview.structure.StructureSelectionManager; +import jalview.util.HttpUtils; import java.io.IOException; import java.io.InputStream; @@ -105,7 +106,7 @@ public class MouseOverStructureListener extends JSFunctionExec implements public String resolveModelFile(String file) { // TODO reuse JalviewLite.LoadingThread.addProtocol instead - if (isValidUrl(file)) + if (HttpUtils.isValidUrl(file)) { return file; } @@ -113,13 +114,13 @@ public class MouseOverStructureListener extends JSFunctionExec implements String db = jvlite.getDocumentBase().toString(); db = db.substring(0, db.lastIndexOf("/")); String docBaseFile = db + "/" + file; - if (isValidUrl(docBaseFile)) + if (HttpUtils.isValidUrl(docBaseFile)) { return docBaseFile; } String cb = jvlite.getCodeBase() + file; - if (isValidUrl(cb)) + if (HttpUtils.isValidUrl(cb)) { return cb; } @@ -127,43 +128,6 @@ public class MouseOverStructureListener extends JSFunctionExec implements return file; } - /** - * Returns true if it is possible to open an input stream at the given URL, - * else false. The input stream is closed. - * - * @param file - * @return - */ - private boolean isValidUrl(String file) - { - InputStream is = null; - try - { - is = new URL(file).openStream(); - if (is != null) - { - return true; - } - } catch (IOException x) - { - // MalformedURLException, FileNotFoundException - return false; - } finally - { - if (is != null) - { - try - { - is.close(); - } catch (IOException e) - { - // ignore - } - } - } - return false; - } - @Override public String[] getPdbFile() { diff --git a/src/jalview/util/HttpUtils.java b/src/jalview/util/HttpUtils.java new file mode 100644 index 0000000..88df587 --- /dev/null +++ b/src/jalview/util/HttpUtils.java @@ -0,0 +1,47 @@ +package jalview.util; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; + +public class HttpUtils +{ + + /** + * Returns true if it is possible to open an input stream at the given URL, + * else false. The input stream is closed. + * + * @param url + * @return + */ + public static boolean isValidUrl(String url) + { + InputStream is = null; + try + { + is = new URL(url).openStream(); + if (is != null) + { + return true; + } + } catch (IOException x) + { + // MalformedURLException, FileNotFoundException + return false; + } finally + { + if (is != null) + { + try + { + is.close(); + } catch (IOException e) + { + // ignore + } + } + } + return false; + } + +} -- 1.7.10.2