cz.vity.freerapid.plugins.webclient
Class MethodBuilder

java.lang.Object
  extended by cz.vity.freerapid.plugins.webclient.MethodBuilder

public final class MethodBuilder
extends Object

Method builder's purpose is to make creating POST and GET request as much as easy as possible.
It uses several parsing methods for searching action method (extraction from <form>, <a>, <img> tag).
By default you can get new instance by calling getMethodBuilder() on your plugin "runner" instance. You can also use a constructor for special purposes (searching on the specific text content).

To get final POST or GET method you need to have full absolut URL to target server - the URL has to start with 'http'.

Typical use:
HttpMethod method = getMethodBuilder().setActionFromFormByName("freeForm", true).setBaseURLForAction("http://freeupload.com/").toMethod();
if (makeRedirectedRequest(method))...

Many examples of using you can find in the class MethodBuilderTest.java in the main project.

Since:
0.82
Author:
Vity

Nested Class Summary
static class MethodBuilder.HttpMethodEnum
          Enumeration of HTTP method possibilities
 
Constructor Summary
MethodBuilder(HttpDownloadClient client)
          Constructor
The content from the last request is taken for parsing text.
MethodBuilder(String content, HttpDownloadClient client)
          Constructor
 
Method Summary
 MethodBuilder addParameters(String... parameters)
          Searches for the parameters in the content and adds them (with their values) to the final result HTTP method.
 MethodBuilder clearParameters()
          Removes all parameters from the result method.
 MethodBuilder encodeLastPartOfAction()
          Deprecated.  
 String getAction()
          Getter for property 'action'.
 String getBaseURL()
          Getter for property 'baseURL'.
 HttpDownloadClient getClient()
          Getter for property 'client'.
 String getEncoding()
          Getter for property 'encoding'.
 MethodBuilder.HttpMethodEnum getMethodAction()
          Returns actual set POST or GET method extracted from result.
 Map<String,String> getParameters()
          Returns parameter map - name/value
 String getReferer()
          Getter for property 'referer'.
 boolean isAutoReplaceEntitiesEnabled()
          Getter for property 'autoReplaceEntities'.
 boolean isEncodeParameters()
          Getter for property 'encodeParameters'.
 MethodBuilder removeParameter(String name)
          Removes POST/GET parameter from the result method.
 MethodBuilder replaceEntitiesInAction()
          All &amp; entity is replaced to &.
 MethodBuilder setAction(String action)
          Sets new action
 MethodBuilder setActionFromAHrefWhereATagContains(String text)
          Searches content for <A> tag with given text and extracts the href attribute.
 MethodBuilder setActionFromFormByIndex(int index, boolean useFormParameters)
          Searches content for form tag with given text and extracts the action attribute.
 MethodBuilder setActionFromFormByName(String formIDOrName, boolean useFormParameters)
          Searches content for form tag with given value of ID or name attribute and extracts the action attribute.
 MethodBuilder setActionFromFormWhereActionContains(String text, boolean useFormParameters)
          Searches content for form tag with given text in action attribute and extracts the action attribute.
 MethodBuilder setActionFromFormWhereTagContains(String text, boolean useFormParameters)
          Searches content for form tag with given text and extracts the action attribute.
 MethodBuilder setActionFromImgSrcWhereTagContains(String text)
          Searches content for <Img> tag with given text and extracts the src attribute.
 MethodBuilder setActionFromTextBetween(String textBefore, String textAfter)
          Extracts string between other 2 strings and sets it as an action
 MethodBuilder setAndEncodeParameter(String name, String value)
          Adds new parameter to result HTTP method.
 void setAutoReplaceEntities(boolean autoReplaceEntities)
          Setter for property 'autoReplaceEntities'.
 MethodBuilder setBaseURL(String baseURL)
          Sets base url for the final method.
 void setClient(HttpDownloadClient client)
          Setter for property 'client'.
 MethodBuilder setEncodeParameters(boolean encodeParameters)
          Setter for property 'encodeParameters'.
 MethodBuilder setEncodePathAndQuery(boolean b)
          Calls URLEncoder to over path and query part of the action or base URL.
 MethodBuilder setEncoding(String encoding)
          Setter for property 'encoding'.
 MethodBuilder setMethodAction(String action)
          Sets "form" action for builder.
 MethodBuilder setParameter(String name, String value)
          Adds new parameter to result HTTP method.
 MethodBuilder setReferer(String referer)
          Setter for property 'referer'.
 org.apache.commons.httpclient.HttpMethod toGetMethod()
          Returns result GET method.
 org.apache.commons.httpclient.HttpMethod toHttpMethod()
          Returns result HTTP method.
 org.apache.commons.httpclient.HttpMethod toPostMethod()
          Returns result POST method.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MethodBuilder

public MethodBuilder(String content,
                     HttpDownloadClient client)
Constructor

Parameters:
content - string content - the parsed text
client - httpclient used for generating HTTPMethod

MethodBuilder

public MethodBuilder(HttpDownloadClient client)
Constructor
The content from the last request is taken for parsing text.

Parameters:
client - httpclient used for generating HTTPMethod
Method Detail

getMethodAction

public MethodBuilder.HttpMethodEnum getMethodAction()
Returns actual set POST or GET method extracted from result.
Its value is used in toMethod() method.
POST method is a default value.

Returns:
GET or POST method type

setActionFromFormByName

public MethodBuilder setActionFromFormByName(String formIDOrName,
                                             boolean useFormParameters)
                                      throws BuildMethodException
Searches content for form tag with given value of ID or name attribute and extracts the action attribute.
All &amp; entity is replaced to & by default.

Parameters:
formIDOrName - form ID or name
useFormParameters - if true then it extracts all input parameters from the form tag
Returns:
builder instance
Throws:
BuildMethodException - when no such FORM was found

setActionFromFormWhereActionContains

public MethodBuilder setActionFromFormWhereActionContains(String text,
                                                          boolean useFormParameters)
                                                   throws BuildMethodException
Searches content for form tag with given text in action attribute and extracts the action attribute.

Content:
<form action="http://blabla/" >

setActionFromFormWhereActionContains("BLABLA") - an action http://blabla/ will be extracted

All &amp; entity is replaced to & by default.

Parameters:
text - form ID or name
useFormParameters - if true then it extracts all input parameters from the form tag
Returns:
builder instance
Throws:
BuildMethodException - when no such FORM with action was found

setActionFromFormWhereTagContains

public MethodBuilder setActionFromFormWhereTagContains(String text,
                                                       boolean useFormParameters)
                                                throws BuildMethodException
Searches content for form tag with given text and extracts the action attribute.

Content:
<form class="xx" action="http://blabla/" >
hahahaha<form>

Using:
setActionFromFormWhereTagContains("class=\"xx\"")
- an action http://blabla/ will be extracted
also this is possible:
setActionFromFormWhereTagContains("hahahaha")
- an action http://blabla/ will be extracted

all &amp; entity is replaced to & by default.

Parameters:
text - a string searched in the form tag
useFormParameters - if true then it extracts all input parameters from the form tag
Returns:
builder instance
Throws:
BuildMethodException - when no such FORM with text in the tag was found

setActionFromTextBetween

public MethodBuilder setActionFromTextBetween(String textBefore,
                                              String textAfter)
                                       throws BuildMethodException
Extracts string between other 2 strings and sets it as an action

Parameters:
textBefore - the text before extracted action
textAfter - the text after extracted action
Returns:
builder instance
Throws:
BuildMethodException - if there was no such text found between selected strings

setActionFromAHrefWhereATagContains

public MethodBuilder setActionFromAHrefWhereATagContains(String text)
                                                  throws BuildMethodException
Searches content for <A> tag with given text and extracts the href attribute.

Content:
<a class="xx" href="http://blabla/" >
hahahaha<a>

Using:
setActionFromAHrefWhereATagContains("hahahaha")
- an action http://blabla/ will be extracted

all &amp; entity is replaced to & by default.

Parameters:
text - a string searched in the <A> tag
Returns:
builder instance
Throws:
BuildMethodException - when no such <A> with text in the tag was found

setActionFromImgSrcWhereTagContains

public MethodBuilder setActionFromImgSrcWhereTagContains(String text)
                                                  throws BuildMethodException
Searches content for <Img> tag with given text and extracts the src attribute.

Content:
<mig class="xx" href="http://blabla/" >
Using:
setActionFromImgSrcWhereTagContains("class=\"xx\"")
- an action http://blabla/ will be extracted
also this is possible:
setActionFromImgSrcWhereTagContains("/blabla")
- an action http://blabla/ will be extracted

All &amp; entity is replaced to & by default.

Parameters:
text - a string searched in the <Img> tag
Returns:
builder instance
Throws:
BuildMethodException - when no such <Img> with text in the tag was found

setActionFromFormByIndex

public MethodBuilder setActionFromFormByIndex(int index,
                                              boolean useFormParameters)
                                       throws BuildMethodException
Searches content for form tag with given text and extracts the action attribute.
This method is useful where the FORM tags has empty body or there is only 1 FORM tag in the content.

Content:
...
<form action="http://blabla1/" >hahahaha<form>
<form action="http://blabla2/" >hahahaha<form>
...

Using:
setActionFromFormByIndex(1)
- an action http://blabla1/ will be extracted
also this is possible:
setActionFromFormByIndex(2)
- an action http://blabla2/ will be extracted

All &amp; entity is replaced to & by default.

Parameters:
index - index of the form tag in the content - the lowest index has number 1
useFormParameters - if true then it extracts all input parameters from the form tag
Returns:
builder instance
Throws:
BuildMethodException - when no such FORM with text in the tag was found

setMethodAction

public MethodBuilder setMethodAction(String action)
Sets "form" action for builder.

Parameters:
action - action as string value
Returns:
builder instance

setBaseURL

public MethodBuilder setBaseURL(String baseURL)
                         throws BuildMethodException
Sets base url for the final method.
Slashes (characters '/') at the end of the string to produce final method action are handled automatically.

Parameters:
baseURL - base URL as text
Returns:
builder instance
Throws:
BuildMethodException - if base url does not match URI string

addParameters

public MethodBuilder addParameters(String... parameters)
                            throws BuildMethodException
Searches for the parameters in the content and adds them (with their values) to the final result HTTP method.

Parameters:
parameters - parameter names that should be added to generate within the result HTTP method
Returns:
builder instance
Throws:
BuildMethodException - thrown when any of the parameter was not found

removeParameter

public MethodBuilder removeParameter(String name)
Removes POST/GET parameter from the result method.

Parameters:
name - name of parameter
Returns:
builder instance

replaceEntitiesInAction

public MethodBuilder replaceEntitiesInAction()
All &amp; entity is replaced to &.

Returns:
builder instance

clearParameters

public MethodBuilder clearParameters()
Removes all parameters from the result method.
Note - only extracted parameters and manually added parameters are removed. Parameters in action value are not removed.

Returns:
builder instance

setParameter

public MethodBuilder setParameter(String name,
                                  String value)
Adds new parameter to result HTTP method. Note: Value of the parameter is not encoded - for that purpose you should use setAndEncodeParameter method.

Parameters:
name - name of parameter
value - value of the parameter; if the value is null, parameter is removed
Returns:
builder instance
See Also:
setAndEncodeParameter(String, String)

setAndEncodeParameter

public MethodBuilder setAndEncodeParameter(String name,
                                           String value)
                                    throws BuildMethodException
Adds new parameter to result HTTP method. Note: Value of the parameter is encoded - for not doing that you should use setParameter method.

Parameters:
name - name of parameter
value - value of the parameter; if the value is null, parameter is removed
Returns:
builder instance
Throws:
BuildMethodException
See Also:
setParameter(String, String)

encodeLastPartOfAction

public MethodBuilder encodeLastPartOfAction()
                                     throws BuildMethodException
Deprecated. 

Calls URLEncoder to over the last part of the action or base URL.
Replaces everything after the last '/' character.

Returns:
builder instance
Throws:
BuildMethodException - if action or base url is null or no '/' was found.

setEncodePathAndQuery

public MethodBuilder setEncodePathAndQuery(boolean b)
                                    throws BuildMethodException
Calls URLEncoder to over path and query part of the action or base URL.
Note: This method just sets the flag that the result action in the httpmethod should be encoded.

Parameters:
b -
Returns:
builder instance
Throws:
BuildMethodException - if action or base url is null or no '/' was found.

setReferer

public MethodBuilder setReferer(String referer)
Setter for property 'referer'.

Parameters:
referer - Value to set for property 'referer'.

getReferer

public String getReferer()
Getter for property 'referer'.

Returns:
Value for property 'referer'.

toGetMethod

public org.apache.commons.httpclient.HttpMethod toGetMethod()
                                                     throws BuildMethodException
Returns result GET method.
Result GET method is composed by baseURL + action (if baseURL is not null).
At least one of the parameter has to be not null and the string has to start with 'http'.

Returns:
new instance of HttpMethod with GET request
Throws:
BuildMethodException

toPostMethod

public org.apache.commons.httpclient.HttpMethod toPostMethod()
                                                      throws BuildMethodException
Returns result POST method.
Result POST method is composed by baseURL + action (if baseURL is not null).
All parameters are set and encoded. At least one of the parameter has to be not null and the string has to start with 'http'.

Returns:
new instance of HttpMethod with POST request
Throws:
BuildMethodException

toHttpMethod

public org.apache.commons.httpclient.HttpMethod toHttpMethod()
                                                      throws BuildMethodException
Returns result HTTP method.
Result method is composed by baseURL + action (if baseURL is not null).
All parameters are set and encoded. At least one of the parameter has to be not null and the string has to start with 'http'.

Returns:
new instance of HttpMethod - result method is given by postMethod settings - how it was extracted from the content - default method is POST
Throws:
BuildMethodException

getAction

public String getAction()
Getter for property 'action'.

Returns:
Value for property 'action'.

setAction

public MethodBuilder setAction(String action)
Sets new action

Parameters:
action -
Returns:

getBaseURL

public String getBaseURL()
Getter for property 'baseURL'.

Returns:
Value for property 'baseURL'.

getClient

public HttpDownloadClient getClient()
Getter for property 'client'.

Returns:
Value for property 'client'.

setClient

public void setClient(HttpDownloadClient client)
Setter for property 'client'.

Parameters:
client - Value to set for property 'client'.

isEncodeParameters

public boolean isEncodeParameters()
Getter for property 'encodeParameters'.

Returns:
Value for property 'encodeParameters'.

setEncodeParameters

public MethodBuilder setEncodeParameters(boolean encodeParameters)
Setter for property 'encodeParameters'.

Parameters:
encodeParameters - Value to set for property 'encodeParameters'.

getEncoding

public String getEncoding()
Getter for property 'encoding'.

Returns:
Value for property 'encoding'.

setEncoding

public MethodBuilder setEncoding(String encoding)
Setter for property 'encoding'.

Parameters:
encoding - Value to set for property 'encoding'.

isAutoReplaceEntitiesEnabled

public boolean isAutoReplaceEntitiesEnabled()
Getter for property 'autoReplaceEntities'.

Returns:
Value for property 'autoReplaceEntities'.

setAutoReplaceEntities

public void setAutoReplaceEntities(boolean autoReplaceEntities)
Setter for property 'autoReplaceEntities'.

Parameters:
autoReplaceEntities - Value to set for property 'autoReplaceEntities'.

getParameters

public Map<String,String> getParameters()
Returns parameter map - name/value

Returns:
hash map with key pair name and value