-
Notifications
You must be signed in to change notification settings - Fork 244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a URLHelper factory API to ParsingUtils #1421
Changes from all commits
9b63628
d84e083
05dd043
eec9348
6090ee9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,11 +46,10 @@ public class ParsingUtils { | |
|
||
public static final Map<Object, Color> colorCache = new WeakHashMap<>(100); | ||
|
||
// HTML 4.1 color table, + orange and magenta | ||
static Map<String, String> colorSymbols = new HashMap(); | ||
private static URLHelperFactory urlHelperFactory = RemoteURLHelper::new; | ||
|
||
private static final Class defaultUrlHelperClass = RemoteURLHelper.class; | ||
public static Class urlHelperClass = defaultUrlHelperClass; | ||
// HTML 4.1 color table, + orange and magenta | ||
private static Map<String, String> colorSymbols = new HashMap(); | ||
|
||
static { | ||
colorSymbols.put("white", "FFFFFF"); | ||
|
@@ -423,50 +422,34 @@ public static boolean resourceExists(String resource) throws IOException{ | |
} | ||
|
||
/** | ||
* Return the registered URLHelper, constructed with the provided URL | ||
* @see #registerHelperClass(Class) | ||
* Return a URLHelper from the current URLHelperFactory | ||
* @see #setURLHelperFactory(URLHelperFactory) | ||
* | ||
* @param url | ||
* @return | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update the javadoc here to point to the new method instead of the deprecated. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The deprecated (reflection) method has been removed. |
||
public static URLHelper getURLHelper(URL url) { | ||
try { | ||
return getURLHelper(urlHelperClass, url); | ||
} catch (Exception e) { | ||
return getURLHelper(defaultUrlHelperClass, url); | ||
} | ||
} | ||
|
||
private static URLHelper getURLHelper(Class helperClass, URL url) { | ||
try { | ||
Constructor constr = helperClass.getConstructor(URL.class); | ||
return (URLHelper) constr.newInstance(url); | ||
} catch (Exception e) { | ||
String errMsg = "Error instantiating url helper for class: " + helperClass; | ||
throw new IllegalStateException(errMsg, e); | ||
} | ||
return urlHelperFactory.getHelper(url); | ||
} | ||
|
||
/** | ||
* Register a {@code URLHelper} class to be used for URL operations. The helper | ||
* may be used for both FTP and HTTP operations, so if any FTP URLs are used | ||
* the {@code URLHelper} must support it. | ||
* | ||
* The default helper class is {@link RemoteURLHelper}, which delegates to FTP/HTTP | ||
* helpers as appropriate. | ||
* Set the factory object for providing URLHelpers. {@see URLHelperFactory}. | ||
* | ||
* @see URLHelper | ||
* @param helperClass Class which implements {@link URLHelper}, and have a constructor | ||
* which takes a URL as it's only argument. | ||
* @param factory | ||
*/ | ||
public static void registerHelperClass(Class helperClass) { | ||
if (!URLHelper.class.isAssignableFrom(helperClass)) { | ||
throw new IllegalArgumentException("helperClass must implement URLHelper"); | ||
//TODO check that it has 1 arg constructor of proper type | ||
public static void setURLHelperFactory(URLHelperFactory factory) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably throw if the value is null. |
||
if(factory == null) { | ||
throw new NullPointerException("Null URLHelperFactory"); | ||
} | ||
urlHelperClass = helperClass; | ||
urlHelperFactory = factory; | ||
} | ||
|
||
public static URLHelperFactory getURLHelperFactory() { | ||
return urlHelperFactory; | ||
} | ||
|
||
/** | ||
* | ||
* Add the {@code indexExtension} to the {@code filepath}, preserving | ||
* query string elements if present. Intended for use where {@code filepath} | ||
* is a URL. Will behave correctly on regular file paths (just add the extension | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,8 +23,10 @@ | |
import java.net.URL; | ||
|
||
/** | ||
* Interface defining a helper class for dealing with URL resources. Purpose of this class is to provide the | ||
* flexibility to use either URLConnection or Apache HTTPClient. Also want to delegate to either HTTP or FTP | ||
* Interface defining a helper class for dealing with URL resources. The purpose of this class is to provide the | ||
* flexibility to use alternative http implementations, for example Apache HTTPClient, and secondly to provide | ||
* a hook for clients to inject custom headers, for example oAuth tokens, into the requests. An instance of | ||
* URLHelper is created for a URL (there is a 1-1 relation between a URL and HRLHelper). | ||
* | ||
* @see HTTPHelper | ||
* @see FTPHelper | ||
|
@@ -33,22 +35,34 @@ | |
*/ | ||
public interface URLHelper { | ||
|
||
/** | ||
* @return URL of the associated resource | ||
*/ | ||
URL getUrl(); | ||
|
||
/** | ||
* @return content length of the resource, or -1 if not available | ||
* @throws IOException | ||
*/ | ||
long getContentLength() throws IOException; | ||
|
||
/** | ||
* Open an InputStream to stream the contents of the resource | ||
* @return | ||
* @throws IOException | ||
*/ | ||
InputStream openInputStream() throws IOException; | ||
|
||
/** | ||
* Open an InputStream to stream a slice (range) of the resource. | ||
* | ||
* May throw an OperationUnsupportedException | ||
* @deprecated Will be removed in a future release, as is somewhat fragile | ||
* and not used. | ||
* @param start | ||
* @param end | ||
* @return | ||
* @throws IOException | ||
*/ | ||
@Deprecated | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick, unecessary newline here. |
||
InputStream openInputStreamForRange(long start, long end) throws IOException; | ||
|
||
public boolean exists() throws IOException; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package htsjdk.tribble.util; | ||
|
||
import java.net.URL; | ||
|
||
/** | ||
* A factory for creating {@link URLHelper} instances. The factory contains a single function | ||
* @see #getHelper(URL) which should return a <code>URLHelper</code> instance for the given URL. | ||
*/ | ||
public interface URLHelperFactory { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add javadoc explaining what this interface is for and what the contract of getHelper is. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added, although not much to say. What else would you like to know here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, thank you, it's basically boilerplate but it lets you know where to look for more information and it makes it clear that someone at least cared enough to write SOMETHING. Maybe it's just me, but I'm always suspicious of library code with 0 documentation. I think the updated doc in URLHelper saying that there is a 1-1 relationship between urls and helpers is a useful addition. Naively I would have assumed that a helper could be used for many URL's so it's good to spell that out. |
||
|
||
/** | ||
* @param url | ||
* @return a {@link URLHelper} object for the given URL | ||
*/ | ||
URLHelper getHelper(URL url); | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good check to add if this is important. I definitely did not know this detail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If servers don't support range requests they are free according to the spec to return the entire file with response code 200. This is bad when its a 300 gb BAM