com.univocity.api.net
Class UrlReaderProvider

java.lang.Object
  extended by com.univocity.api.io.ReaderProvider
      extended by com.univocity.api.net.UrlReaderProvider
All Implemented Interfaces:
ResourceProvider<Reader>, Cloneable

public class UrlReaderProvider
extends ReaderProvider
implements Cloneable

A ReaderProvider for URLs (parameterized or not). This provider works with an internal HttpRequest configuration object that allows you to configure a HTTP request. The actual remote invocation is performed when getResponse() is called, and the result will be provided in a HttpResponse object. Example of a URL with 2 parameters (QUERY and PERIOD): "https://www.google.com/?q={QUERY}#q={QUERY}&tbs=qdr:{PERIOD}" Use HttpRequest.setUrlParameter(String, Object) to set the values of any parameters so that getUrl() generates the final URL. In case of failure receiving a response, the call can be retried for a number of times using setRetries(int) and each retry can occur after a given interval, which can be defined with setRetryInterval(long) The response body of a call can be stored in a local file defined using the storeLocalCopyIn(File) method.

Author:
uniVocity Software Pty Ltd - dev@univocity.com

Constructor Summary
UrlReaderProvider(String url)
          Creates a new instance to read content from a given URL.
UrlReaderProvider(String url, RateLimiter rateLimiter)
          Creates a new instance to read content from a given URL.
 
Method Summary
 UrlReaderProvider clone()
          Clones this object with all its configuration, but without the result of the HTTP request (i.e.
 boolean equals(Object o)
           
 String getDomainName()
          Returns the domain name in the current URL.
 String getFileName()
          Returns the file name specified in URL, if it exists.
 FileProvider getLocalCopyTarget()
          Returns a FileProvider which indicates where in the filesystem a file with a copy of the response body, obtained after invoking the HTTP request via the getResponse() method, should be stored.
 String getPath()
          Gets the path of the resource identified by the URL.
 String getProtocol()
          Returns the protocol of the current URL.
 HttpRequest getRequest()
          Returns a HttpRequest object with configuration options for executing the HTTP request.
 Reader getResource()
          Returns a Reader instance ready to process the content of the body of the HTTP response obtained after invoking getResponse().
 HttpResponse getResponse()
          Invokes the HTTP request and returns the response as a HttpResponse object.
 int getRetries()
          Return the number of retries to be performed in case the HTTP request call fails.
 long getRetryInterval()
          Returns the interval (in milliseconds) to wait before trying to execute the HTTP request after a failure.
 String getUrl()
          Returns the URL used to produce the current response.
 URL getUrlInstance()
          Returns a URL instance used to produce the current response.
 int hashCode()
           
 boolean isResponseAvailable()
          Returns a flag indicating whether the HTTP request has been executed and a response is already available through getResponse().
 UrlReaderProvider newRequest(String newUrl)
          Clones this object with all its configuration and a new URL, but without the result of the HTTP request (i.e.
 void setRetries(int retries)
          Defines a number of retries to be performed in case the HTTP request call fails.
 void setRetryInterval(long retryInterval)
          Defines the interval (in milliseconds) to wait before trying to execute the HTTP request after a failure.
 void storeLocalCopyIn(File file)
          Defines a file into which a copy of the response body, obtained after invoking the HTTP request via the getResponse() method, should be stored.
 void storeLocalCopyIn(File file, Charset encoding)
          Defines a file into which a copy of the response body, obtained after invoking the HTTP request via the getResponse() method, should be stored.
 void storeLocalCopyIn(FileProvider provider)
          Defines a file into which a copy of the response body, obtained after invoking the HTTP request via the getResponse() method, should be stored be stored.
 void storeLocalCopyIn(File file, String encoding)
          Defines a file into which a copy of the response body, obtained after invoking the HTTP request via the getResponse() method, should be stored.
 void storeLocalCopyIn(String path)
          Defines a file into which a copy of the response body, obtained after invoking the HTTP request via the getResponse() method, should be stored.
 void storeLocalCopyIn(String path, Charset encoding)
          Defines a file into which a copy of the response body, obtained after invoking the HTTP request via the getResponse() method, should be stored.
 void storeLocalCopyIn(String path, String encoding)
          Defines a file into which a copy of the response body, obtained after invoking the HTTP request via the getResponse() method, should be stored
 String toString()
          Prints this object as the original HTTP request URL.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

UrlReaderProvider

public UrlReaderProvider(String url)
Creates a new instance to read content from a given URL.

Parameters:
url - the URL to access.

UrlReaderProvider

public UrlReaderProvider(String url,
                         RateLimiter rateLimiter)
Creates a new instance to read content from a given URL.

Parameters:
url - the URL to access.
rateLimiter - RateLimiter that ensures multiple requests execute one after the other, after a minimum interval.
Method Detail

getUrl

public final String getUrl()
Returns the URL used to produce the current response. If getResponse() was not invoked yet, the URL provided in the constructor of this class will be returned. If redirection was enabled (through HttpRequest.getFollowRedirects()), the redirection URL will be returned.

Returns:
the current URL

getUrlInstance

public final URL getUrlInstance()
Returns a URL instance used to produce the current response. If getResponse() was not invoked yet, the URL provided in the constructor of this class will be returned. If redirection was enabled (through HttpRequest.getFollowRedirects()), the redirection URL will be returned.

Returns:
the current URL

isResponseAvailable

public final boolean isResponseAvailable()
Returns a flag indicating whether the HTTP request has been executed and a response is already available through getResponse().

Returns:
true if a HttpResponse is available, otherwise false

getDomainName

public final String getDomainName()
Returns the domain name in the current URL. If a response has been obtained (through getResponse()) with redirection enabled, the domain name in the redirection URL will be returned.

Returns:
the current domain name.

getRequest

public final HttpRequest getRequest()
Returns a HttpRequest object with configuration options for executing the HTTP request.

Returns:
the request configuration.

getProtocol

public final String getProtocol()
Returns the protocol of the current URL. If a response has been obtained (through getResponse()) with redirection enabled, the protocol of the redirection URL will be returned.

Returns:
the current protocol.

getRetries

public final int getRetries()
Return the number of retries to be performed in case the HTTP request call fails. Defaults to 0 (no retries)

Returns:
the number of retries to perform in case of failure to obtain a response

setRetries

public final void setRetries(int retries)
Defines a number of retries to be performed in case the HTTP request call fails. Defaults to 0 (no retries)

Parameters:
retries - the number of retries to perform in case of failure to obtain a response

getRetryInterval

public final long getRetryInterval()
Returns the interval (in milliseconds) to wait before trying to execute the HTTP request after a failure. Defaults to 2000 (2 seconds)

Returns:
the retry interval in ms

setRetryInterval

public final void setRetryInterval(long retryInterval)
Defines the interval (in milliseconds) to wait before trying to execute the HTTP request after a failure. Defaults to 2000 (2 seconds)

Parameters:
retryInterval - the retry interval in ms

getPath

public final String getPath()
Gets the path of the resource identified by the URL. For example, if the URL is "http://google.com/images/logo.png" then "/images/logo.png" will be returned.

Returns:
the path portion of the URL.

getFileName

public final String getFileName()
Returns the file name specified in URL, if it exists. For example, if the URL is "http://google.com/images/logo.png". "logo.png" will be returned

Returns:
the file name

getResponse

public final HttpResponse getResponse()
Invokes the HTTP request and returns the response as a HttpResponse object. Further calls to this method will produce the same object, and no further HTTP requests will be performed. Use the clone() method to obtain a copy of the current UrlReaderProvider instance if you need to invoke the HTTP request again.

Returns:
the HTTP response originated by the configured HTTP request.

storeLocalCopyIn

public final void storeLocalCopyIn(FileProvider provider)
Defines a file into which a copy of the response body, obtained after invoking the HTTP request via the getResponse() method, should be stored be stored.

Parameters:
provider - the FileProvider defining the target file.

storeLocalCopyIn

public final void storeLocalCopyIn(File file)
Defines a file into which a copy of the response body, obtained after invoking the HTTP request via the getResponse() method, should be stored.

Parameters:
file - the target file.

storeLocalCopyIn

public final void storeLocalCopyIn(File file,
                                   Charset encoding)
Defines a file into which a copy of the response body, obtained after invoking the HTTP request via the getResponse() method, should be stored.

Parameters:
file - the target file.
encoding - the encoding to use for the local copy.

storeLocalCopyIn

public final void storeLocalCopyIn(File file,
                                   String encoding)
Defines a file into which a copy of the response body, obtained after invoking the HTTP request via the getResponse() method, should be stored.

Parameters:
file - the target file.
encoding - the encoding to use for the local copy.

storeLocalCopyIn

public final void storeLocalCopyIn(String path)
Defines a file into which a copy of the response body, obtained after invoking the HTTP request via the getResponse() method, should be stored.

Parameters:
path - a path to the target file. The path can contain system variables enclosed within { and } (e.g. {user.home}/myapp/page.html").

storeLocalCopyIn

public final void storeLocalCopyIn(String path,
                                   Charset encoding)
Defines a file into which a copy of the response body, obtained after invoking the HTTP request via the getResponse() method, should be stored.

Parameters:
path - a path to the target file. The path can contain system variables enclosed within { and } (e.g. {user.home}/myapp/page.html").
encoding - the encoding to use for the local copy.

storeLocalCopyIn

public final void storeLocalCopyIn(String path,
                                   String encoding)
Defines a file into which a copy of the response body, obtained after invoking the HTTP request via the getResponse() method, should be stored

Parameters:
path - a path to the target file. The path can contain system variables enclosed within { and } (e.g. {user.home}/myapp/page.html").
encoding - the encoding to use for the local copy.

getLocalCopyTarget

public final FileProvider getLocalCopyTarget()
Returns a FileProvider which indicates where in the filesystem a file with a copy of the response body, obtained after invoking the HTTP request via the getResponse() method, should be stored.

Returns:
the local copy file configuration for storing the HTTP response body obtained when calling getResponse()

getResource

public final Reader getResource()
Returns a Reader instance ready to process the content of the body of the HTTP response obtained after invoking getResponse(). A HTTP request will be made to obtain the response if required.

Specified by:
getResource in interface ResourceProvider<Reader>
Returns:
a new Reader that can be used to consume the body of the resulting HTTP response

toString

public final String toString()
Prints this object as the original HTTP request URL.

Overrides:
toString in class Object
Returns:
the request URL passed in the constructor of this class.

clone

public final UrlReaderProvider clone()
Clones this object with all its configuration, but without the result of the HTTP request (i.e. no response). The clone allows you to perform a new HTTP request call and obtain a fresh HTTP response through getResponse()

Overrides:
clone in class Object
Returns:
a copy of the current object and all its configurations.

newRequest

public final UrlReaderProvider newRequest(String newUrl)
Clones this object with all its configuration and a new URL, but without the result of the HTTP request (i.e. no response). The clone allows you to perform a new HTTP request call to another URL, while preserving all original parameters, to obtain a fresh HTTP response through getResponse()

Parameters:
newUrl - the new URL to be used.
Returns:
a copy of the current object and all its configurations, but targeting a new URL.

equals

public boolean equals(Object o)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object


Copyright © 2018 uniVocity Software Pty Ltd. All rights reserved.