cz.vity.freerapid.plugins.webclient
Class DownloadClient

java.lang.Object
  extended by cz.vity.freerapid.plugins.webclient.DownloadClient
All Implemented Interfaces:
HttpDownloadClient

public class DownloadClient
extends Object
implements HttpDownloadClient

Robot to browse on the web.

Author:
Vity
See Also:
HttpDownloadClient

Field Summary
protected  String asString
          string content of last request
protected  org.apache.commons.httpclient.HttpClient client
          Field client
protected  String referer
          Field referer - HTTP referer
 
Constructor Summary
DownloadClient()
          Constructor - creates a new DownloadClient instance.
 
Method Summary
 String getContentAsString()
          Method returns string content (web page - server response), which was gotten from the last GET or POST request
protected  String getContentPageCharset()
           
 org.apache.commons.httpclient.methods.GetMethod getGetMethod(String uri)
          Returns setuble object for creation GET HTTP request
 org.apache.commons.httpclient.HttpClient getHTTPClient()
          Method returns direct access to instance of HttpClient
 org.apache.commons.httpclient.methods.PostMethod getPostMethod(String uri)
          Returns setuble object for creation POST HTTP request
 String getReferer()
          Return value of HTTP referer from the last HTTP request
 ConnectionSettings getSettings()
          Getter
protected  String inflate(InputStream in)
          Converts given GZIPed input stream into string.
 void initClient(ConnectionSettings settings)
          Initialization of client with connection settings that should be used for creating http connection
protected  boolean isRedirect(int statuscode)
          Checks if the given status code is type redirect
 InputStream makeFinalRequestForFile(org.apache.commons.httpclient.HttpMethod method, HttpFile file, boolean allowRedirect)
          Runs HTTP request to get file specified in file parameter.
 int makeRequest(org.apache.commons.httpclient.HttpMethod method, boolean allowRedirect)
          Runs simple HTTP request with optional redirect.
 InputStream makeRequestForFile(org.apache.commons.httpclient.HttpMethod method)
          Runs simple direct HTTP request to get a file - eg.
protected  void setDefaultsForMethod(org.apache.commons.httpclient.HttpMethod method)
          Method setDefaultsForMethod sets default header request values - emulates Mozilla Firefox
 void setReferer(String referer)
          Sets referer value for the next HTTP request This method should be called before getGetMethod or getPostMethod
protected  void toString(org.apache.commons.httpclient.HttpMethod method)
          Help method for to log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

client

protected org.apache.commons.httpclient.HttpClient client
Field client

See Also:
HttpClient

referer

protected String referer
Field referer - HTTP referer


asString

protected String asString
string content of last request

Constructor Detail

DownloadClient

public DownloadClient()
Constructor - creates a new DownloadClient instance.

Method Detail

initClient

public void initClient(ConnectionSettings settings)
Description copied from interface: HttpDownloadClient
Initialization of client with connection settings that should be used for creating http connection

Specified by:
initClient in interface HttpDownloadClient
Parameters:
settings - internet connection settings

setDefaultsForMethod

protected void setDefaultsForMethod(org.apache.commons.httpclient.HttpMethod method)
Method setDefaultsForMethod sets default header request values - emulates Mozilla Firefox

Parameters:
method -

getPostMethod

public org.apache.commons.httpclient.methods.PostMethod getPostMethod(String uri)
Description copied from interface: HttpDownloadClient
Returns setuble object for creation POST HTTP request

Specified by:
getPostMethod in interface HttpDownloadClient
Parameters:
uri - URI for given HTTP request
Returns:
new instance of PostMethod with default settings; referer is already set
See Also:
PostMethod

getReferer

public String getReferer()
Description copied from interface: HttpDownloadClient
Return value of HTTP referer from the last HTTP request

Specified by:
getReferer in interface HttpDownloadClient
Returns:
actual value of referer parsed from HTTP response header

makeFinalRequestForFile

public InputStream makeFinalRequestForFile(org.apache.commons.httpclient.HttpMethod method,
                                           HttpFile file,
                                           boolean allowRedirect)
                                    throws IOException
Description copied from interface: HttpDownloadClient
Runs HTTP request to get file specified in file parameter. According to HTTP response filename and file size attributes are updated.

Specified by:
makeFinalRequestForFile in interface HttpDownloadClient
Parameters:
method - a descendant of HttpMethod - PostMethod or GetMethod
file - the file that is downloaded
allowRedirect - allow redirect flag
Returns:
beginning of the stream for reading or null if there was no file stream in response
Throws:
IOException - error I/O
See Also:
HttpClient.executeMethod(org.apache.commons.httpclient.HttpMethod)

makeRequestForFile

public InputStream makeRequestForFile(org.apache.commons.httpclient.HttpMethod method)
                               throws IOException
Description copied from interface: HttpDownloadClient
Runs simple direct HTTP request to get a file - eg. CAPTCHA picture
Redirection is off

Specified by:
makeRequestForFile in interface HttpDownloadClient
Parameters:
method - a descendant of HttpMethod - PostMethod or GetMethod
Returns:
beginning of the stream for reading or null if there was no file stream in HTTP response
Throws:
IOException - error I/O
See Also:
HttpClient.executeMethod(org.apache.commons.httpclient.HttpMethod)

makeRequest

public int makeRequest(org.apache.commons.httpclient.HttpMethod method,
                       boolean allowRedirect)
                throws IOException
Description copied from interface: HttpDownloadClient
Runs simple HTTP request with optional redirect.

Specified by:
makeRequest in interface HttpDownloadClient
Parameters:
method - a descendant of HttpMethod - PostMethod or GetMethod
allowRedirect - allow redirect flag
Returns:
HTTP result code
Throws:
IOException - error I/O
See Also:
HttpClient.executeMethod(org.apache.commons.httpclient.HttpMethod)

isRedirect

protected boolean isRedirect(int statuscode)
Checks if the given status code is type redirect

Parameters:
statuscode - http response status code
Returns:
true, if status code is one of the redirect code

getGetMethod

public org.apache.commons.httpclient.methods.GetMethod getGetMethod(String uri)
Description copied from interface: HttpDownloadClient
Returns setuble object for creation GET HTTP request

Specified by:
getGetMethod in interface HttpDownloadClient
Parameters:
uri - URI for given HTTP request
Returns:
new instance of GetMethod with default settings
See Also:
GetMethod

toString

protected void toString(org.apache.commons.httpclient.HttpMethod method)
Help method for to log

Parameters:
method -

inflate

protected String inflate(InputStream in)
                  throws IOException
Converts given GZIPed input stream into string.
UTF-8 encoding is used as default.
Shouldn't be called to file input streams.

Parameters:
in - input stream which should be converted
Returns:
input stream as string
Throws:
IOException - when there was an error during reading from the stream

getContentPageCharset

protected String getContentPageCharset()

setReferer

public void setReferer(String referer)
Description copied from interface: HttpDownloadClient
Sets referer value for the next HTTP request This method should be called before getGetMethod or getPostMethod

Specified by:
setReferer in interface HttpDownloadClient
Parameters:
referer - URI for the next request made by method makeRequest

getSettings

public ConnectionSettings getSettings()
Description copied from interface: HttpDownloadClient
Getter

Specified by:
getSettings in interface HttpDownloadClient
Returns:
Returns actual internet connection settings used by HTTP client

getHTTPClient

public org.apache.commons.httpclient.HttpClient getHTTPClient()
Description copied from interface: HttpDownloadClient
Method returns direct access to instance of HttpClient

Specified by:
getHTTPClient in interface HttpDownloadClient
Returns:
actual used HttpClient instance
See Also:
HttpClient

getContentAsString

public String getContentAsString()
Description copied from interface: HttpDownloadClient
Method returns string content (web page - server response), which was gotten from the last GET or POST request

Specified by:
getContentAsString in interface HttpDownloadClient
Returns:
webpage content, If request returned a file stream then this method empty string.