org.tinyjee.maven.dim.spi
Class UrlFetcher

java.lang.Object
  extended by org.tinyjee.maven.dim.spi.UrlFetcher

public class UrlFetcher
extends Object

Abstracts url fetching, charset handling and caching.

Author:
Juergen_Kellerer, 2011-10-13

Field Summary
static int MAX_CACHE_SIZE
          Defines the max size of the URL content cache.
static int MAX_CACHED_CONTENT_SIZE
          Defines the maximum amount of bytes to keep inside URL content cache per cached element.
 
Method Summary
static long getLastModified(URL sourceUrl)
          Returns the last modification date of the specified url.
static BufferedReader getReadableSource(URL sourceUrl)
          Returns the content text stream of the given source URL.
static BufferedReader getReadableSource(URL sourceUrl, boolean noCache)
          Returns the content text stream of the given source URL.
static InputStream getSource(URL sourceUrl, boolean noCache)
          Returns the content stream of the given source URL.
static Charset getSourceCharset(URL sourceUrl)
          Returns the charset that is used to decode the content that is referenced by the given URL.
static boolean isModified(URL sourceUrl)
          Returns true if the given url was modified between the last read and this call.
static String readerToString(Reader reader, boolean closeReader)
          Utility method that converts the given text stream to a string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_CACHE_SIZE

public static final int MAX_CACHE_SIZE
Defines the max size of the URL content cache.

See Also:
Constant Field Values

MAX_CACHED_CONTENT_SIZE

public static final int MAX_CACHED_CONTENT_SIZE
Defines the maximum amount of bytes to keep inside URL content cache per cached element.

Method Detail

getSource

public static InputStream getSource(URL sourceUrl,
                                    boolean noCache)
                             throws IOException
Returns the content stream of the given source URL.

Parameters:
sourceUrl - the source URL to open.
noCache - whether the content shall be cached / retrieved from the cache or not.
Returns:
the content stream of the given source URL.
Throws:
IOException - in case of the IO transfer failed.

getLastModified

public static long getLastModified(URL sourceUrl)
Returns the last modification date of the specified url.

Parameters:
sourceUrl - the url to retrieve the last modified date for.
Returns:
The last modified date or 0 if unknown.

isModified

public static boolean isModified(URL sourceUrl)
Returns true if the given url was modified between the last read and this call.

Parameters:
sourceUrl - the source url to check.
Returns:
true if the given url was modified between the last read and this call.

getReadableSource

public static BufferedReader getReadableSource(URL sourceUrl)
                                        throws IOException
Returns the content text stream of the given source URL.

Parameters:
sourceUrl - the source URL to open.
Returns:
the content stream of the given source URL.
Throws:
IOException - in case of the IO transfer failed.

getReadableSource

public static BufferedReader getReadableSource(URL sourceUrl,
                                               boolean noCache)
                                        throws IOException
Returns the content text stream of the given source URL.

Parameters:
sourceUrl - the source URL to open.
noCache - whether the content shall be cached / retrieved from the cache or not.
Returns:
the content stream of the given source URL.
Throws:
IOException - in case of the IO transfer failed.

readerToString

public static String readerToString(Reader reader,
                                    boolean closeReader)
                             throws IOException
Utility method that converts the given text stream to a string.

Parameters:
reader - the reader to convert.
closeReader - whether the reader should be closed after reading.
Returns:
the text contained in the reader.
Throws:
IOException - in case of the IO transfer failed.

getSourceCharset

public static Charset getSourceCharset(URL sourceUrl)
Returns the charset that is used to decode the content that is referenced by the given URL.

Depending on the request parameters, this method reads a portion of the content to auto-detect what charset fits best.

Parameters:
sourceUrl - the url to retrieve the charset for.
Returns:
the charset that is used to decode the content that is referenced by the given URL.