webtester.packages.threads
Class proxyHandler

java.lang.Object
  |
  +--java.lang.Thread
        |
        +--webtester.packages.threads.proxyHandler
All Implemented Interfaces:
java.lang.Runnable

public class proxyHandler
extends java.lang.Thread

The thread that is used for proxying HTTP requests, and examining the responses.

Author:
Aaron DeLong

Field Summary
private  java.util.ArrayList aExcludeList
          Contains a list of regular expressions to exclude while recording.
private  java.util.ArrayList aRegexes
          Contains regular expressions recorded with a test case.
private  boolean bEndProxy
          Inidicates whether the proxy has finished recording.
private  boolean bImageRecord
          Indicates whether images are being recorded.
private  boolean bPostRedirect
          Indicates whether a POST request resulted in a redirect request.
private  boolean bRecord
          Indicates whether the proxy is recording a test case.
private  boolean bSSL
          Indicates whether the user attempted to make an SSL connection.
private  java.util.HashMap hHeaders
          Contains HTTP headers sent by the browser.
private  java.util.HashMap hServerTypes
          Indicates the type of webserver being accessed.
private static int iAllowedTries
          The number of allowed tries for reading data from a socket.
private static int iDefaultPort
          The default port to use for HTTP connections.
private static int iDefaultTimeout
          The default timeout value for HTTP connections.
private static int iHeaderLimit
          The minimum allowed length for http headers before they will be parsed.
private static int iHttpError
          The value at which HTTP return codes indicate an error.
private static int iHttpPort
          The default port for HTTP connections.
private static int iMinTokenLength
          The minimum length for a token to be recorded as a regular expression.
private  java.io.DataInputStream input
          Used to retrieve information from the browser.
private static int iNumTries
          The number of times to attempt to read an http response.
private static int iPostRedirect
          HTTP status code which indicates a POST redirect.
private static int iRequestLineLimit
          The number of lines to read from an incoming HTTP response for header info.
private static int iReturnCodeIndex
          The index to use when retrieving the HTTP status code.
private static int iSleepTime
          How long to wait between attempts to read an http response.
private static int iSocketTimeout
          The default timeout for http connections.
private static int iSSLPort
          The default port for SSL connections.
private static int iTestCaseIndex
          The starting index to use when parsing the test case name.
private static int iURLStartIndex
          The starting index to use when parsing URLs.
private  java.io.PrintStream output
          Used to send information to the browser.
private  java.lang.String sConfigFile
          The configuration file being written.
private  java.lang.String sData
          The data sent with the URL accessed.
private  java.lang.String sExcludeFile
          A file which contains a list of regexes to be excluded while recording.
private  java.lang.String sHost
          The host being contacted for the HTTP request.
private  java.lang.String sMethod
          The method associated with the URL accessed.
private  java.lang.String sParsedHeaders
          Contains the HTTP header information.
private  java.lang.String sSeparator
          The path separator for the OS on which this application is executing.
private  java.lang.String sTestcaseName
          The name of the test case being accessed.
private  java.lang.String sURL
          The URL being accessed.
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
proxyHandler()
          The default constructor for the class, which takes no arguments.
proxyHandler(java.io.InputStream in, java.io.OutputStream out)
          Get the input/output streams for the browser.
 
Method Summary
private  void buildRegexes(java.lang.String sHTMLContent)
          Examine the content and build regular expressions which can be used to validate that content.
private  java.lang.String createGeneralDollarRegex(java.lang.String sStr)
          Given something that might be a dollar amount, create a general regex that represents the value.
private  void customHandler()
          The user calls certain URLs to start and stop testcases.
private  java.lang.String fixHeaders(java.lang.String sHeader)
          Fix the HTTP headers so that microsoft webservers won't return errors.
private  java.lang.String fixRegex(java.lang.String sStr)
          Fix special characters in the regex.
private  void flushImageToFile(java.lang.String sImageURL, java.lang.String sLength)
          Write image details to a file so they can later be added to the generate config file.
private  void flushToFile()
          Write information retrieved to a config file that will processed later.
private  void flushURLToFile()
          For a 302 response, we need to record the URL and data, but record regexes for the subsequent response.
 void generateConfigFile()
          Based on the recorded data, and input from the browser, write the XML config file.
 boolean getEndProxy()
          A boolean value which determines whether or not the proxy should exit and generate a config file.
private  void getExcludeList()
          Read the file which contains regexes to exclude and store the regexes in an ArrayList.
 boolean getImageRecord()
          Get the value which determines if images are being recorded.
 java.lang.String getParsedHeaders()
          Return the headers that were set previously.
 boolean getPostRedirect()
          Retrieve the value which indicates if a POST redirect request was encountered.
private  java.util.HashMap getPreferences()
          Retrieve user preferences from a text file, if they exist.
 boolean getRecordOn()
          Indicates whether a testcase is currently being recorded.
private  boolean getServerType()
          Determine the server type.
 java.util.HashMap getServerTypes()
          Return the server type information.
 boolean getSSL()
          Indicates whether the user tried to access an SSL connection.
 java.lang.String getTestcaseName()
          Return the currently assigned testcase name.
 java.lang.String getURL()
          Return the last URL accessed.
private  boolean isExcluded(java.lang.String sStr)
          Determines if the given String represents an excluded regular expression.
private  java.lang.String parseContent(java.lang.String sHTML)
          Extract the HTML content from the data returned by the webserver.
private  void parseHeaders(java.lang.String sStr)
          Parse the request headers into a hashmap.
 void run()
          The method used to start this thread.
private  void sendRequest(byte[] b)
          Send the browser request to the specified host, then read the response data.
 void setData(java.lang.String sVal)
          Set the data that will be used to generate the config file.
 void setExcludeFile(java.lang.String sFile)
          Set the name of the file which needs to be read to determine regexes that should NOT appear.
 void setImageRecord(boolean bVal)
          Set the value which indicates if images are being recorded.
 void setParsedHeaders(java.lang.String sStr)
          Set the HTTP headers.
 void setPostRedirect(boolean bVal)
          Set the value which indicates if a POST redirect request was encountered.
 void setRecordOn(boolean bVal)
          Sets the value to indicate whether a testcase is being recorded.
 void setSeparator(java.lang.String sVal)
          Set the path separator so that the correct classpath can be displayed to the user.
 void setServerTypes(java.util.HashMap hData)
          Set the server type information.
 void setTestcaseName(java.lang.String sStr)
          Assign the current test case name a new value.
private  boolean validCharactersInRegex(java.lang.String sRegex)
          Determine if the characters in the regex can be handled by the xml parser.
private  boolean validResponse(java.lang.String sCheck)
          Determine if the response contains HTML content.
private  boolean validToken(java.lang.String sToken)
          Determines if the given token is valid.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

input

private java.io.DataInputStream input
Used to retrieve information from the browser.


output

private java.io.PrintStream output
Used to send information to the browser.


hHeaders

private java.util.HashMap hHeaders
Contains HTTP headers sent by the browser.


hServerTypes

private java.util.HashMap hServerTypes
Indicates the type of webserver being accessed.


sHost

private java.lang.String sHost
The host being contacted for the HTTP request.


sMethod

private java.lang.String sMethod
The method associated with the URL accessed.


sData

private java.lang.String sData
The data sent with the URL accessed.


sURL

private java.lang.String sURL
The URL being accessed.


sParsedHeaders

private java.lang.String sParsedHeaders
Contains the HTTP header information.


sTestcaseName

private java.lang.String sTestcaseName
The name of the test case being accessed.


sExcludeFile

private java.lang.String sExcludeFile
A file which contains a list of regexes to be excluded while recording.


sConfigFile

private java.lang.String sConfigFile
The configuration file being written.


sSeparator

private java.lang.String sSeparator
The path separator for the OS on which this application is executing.


aRegexes

private java.util.ArrayList aRegexes
Contains regular expressions recorded with a test case.


aExcludeList

private java.util.ArrayList aExcludeList
Contains a list of regular expressions to exclude while recording.


bRecord

private boolean bRecord
Indicates whether the proxy is recording a test case.


bEndProxy

private boolean bEndProxy
Inidicates whether the proxy has finished recording.


bPostRedirect

private boolean bPostRedirect
Indicates whether a POST request resulted in a redirect request.


bSSL

private boolean bSSL
Indicates whether the user attempted to make an SSL connection.


bImageRecord

private boolean bImageRecord
Indicates whether images are being recorded.


iDefaultTimeout

private static final int iDefaultTimeout
The default timeout value for HTTP connections.

See Also:
Constant Field Values

iAllowedTries

private static final int iAllowedTries
The number of allowed tries for reading data from a socket.

See Also:
Constant Field Values

iDefaultPort

private static final int iDefaultPort
The default port to use for HTTP connections.

See Also:
Constant Field Values

iRequestLineLimit

private static final int iRequestLineLimit
The number of lines to read from an incoming HTTP response for header info.

See Also:
Constant Field Values

iReturnCodeIndex

private static final int iReturnCodeIndex
The index to use when retrieving the HTTP status code.

See Also:
Constant Field Values

iHttpError

private static final int iHttpError
The value at which HTTP return codes indicate an error.

See Also:
Constant Field Values

iPostRedirect

private static final int iPostRedirect
HTTP status code which indicates a POST redirect.

See Also:
Constant Field Values

iMinTokenLength

private static final int iMinTokenLength
The minimum length for a token to be recorded as a regular expression.

See Also:
Constant Field Values

iHttpPort

private static final int iHttpPort
The default port for HTTP connections.

See Also:
Constant Field Values

iSocketTimeout

private static final int iSocketTimeout
The default timeout for http connections.

See Also:
Constant Field Values

iNumTries

private static final int iNumTries
The number of times to attempt to read an http response.

See Also:
Constant Field Values

iSleepTime

private static final int iSleepTime
How long to wait between attempts to read an http response.

See Also:
Constant Field Values

iURLStartIndex

private static final int iURLStartIndex
The starting index to use when parsing URLs.

See Also:
Constant Field Values

iTestCaseIndex

private static final int iTestCaseIndex
The starting index to use when parsing the test case name.

See Also:
Constant Field Values

iHeaderLimit

private static final int iHeaderLimit
The minimum allowed length for http headers before they will be parsed.

See Also:
Constant Field Values

iSSLPort

private static final int iSSLPort
The default port for SSL connections.

See Also:
Constant Field Values
Constructor Detail

proxyHandler

public proxyHandler(java.io.InputStream in,
                    java.io.OutputStream out)
Get the input/output streams for the browser.

Parameters:
in - the browser input stream
out - the browser output stream

proxyHandler

public proxyHandler()
The default constructor for the class, which takes no arguments.

Method Detail

getTestcaseName

public java.lang.String getTestcaseName()
Return the currently assigned testcase name.

Returns:
a String which contains the test case name

getPostRedirect

public boolean getPostRedirect()
Retrieve the value which indicates if a POST redirect request was encountered.

Returns:
FALSE if the request was not encountered

setPostRedirect

public void setPostRedirect(boolean bVal)
Set the value which indicates if a POST redirect request was encountered.

Parameters:
bVal - the boolean value to use for assignment

setTestcaseName

public void setTestcaseName(java.lang.String sStr)
Assign the current test case name a new value.

Parameters:
sStr - the current test case name

getEndProxy

public boolean getEndProxy()
A boolean value which determines whether or not the proxy should exit and generate a config file.

Returns:
FALSE if the proxy has not finished recording

getRecordOn

public boolean getRecordOn()
Indicates whether a testcase is currently being recorded.

Returns:
FALSe if a test case is not being recorded

getSSL

public boolean getSSL()
Indicates whether the user tried to access an SSL connection.

Returns:
FALSE if the user did not try to access a connection via SSL

getImageRecord

public boolean getImageRecord()
Get the value which determines if images are being recorded.

Returns:
TRUE if images are being recorded

setImageRecord

public void setImageRecord(boolean bVal)
Set the value which indicates if images are being recorded.

Parameters:
bVal - indicates whether images are being recorded

setSeparator

public void setSeparator(java.lang.String sVal)
Set the path separator so that the correct classpath can be displayed to the user.

Parameters:
sVal - the value for the path separator

setRecordOn

public void setRecordOn(boolean bVal)
Sets the value to indicate whether a testcase is being recorded.

Parameters:
bVal - the value for the parameter

setExcludeFile

public void setExcludeFile(java.lang.String sFile)
Set the name of the file which needs to be read to determine regexes that should NOT appear.

Parameters:
sFile - the name of the exclude file

setData

public void setData(java.lang.String sVal)
Set the data that will be used to generate the config file.

Parameters:
sVal - the data used to generate the config file

getURL

public java.lang.String getURL()
Return the last URL accessed.

Returns:
a String which denotes the last URL accessed

getServerTypes

public java.util.HashMap getServerTypes()
Return the server type information.

Returns:
a HashMap that contains information on webserver types

setServerTypes

public void setServerTypes(java.util.HashMap hData)
Set the server type information.

Parameters:
hData - contains information on webserver types

getParsedHeaders

public java.lang.String getParsedHeaders()
Return the headers that were set previously.

Returns:
a String that contains the header information

setParsedHeaders

public void setParsedHeaders(java.lang.String sStr)
Set the HTTP headers.

Parameters:
sStr - the HTTP header values

parseHeaders

private void parseHeaders(java.lang.String sStr)
Parse the request headers into a hashmap.

Parameters:
sStr - the header data represented as a String

validResponse

private boolean validResponse(java.lang.String sCheck)
Determine if the response contains HTML content.

Parameters:
sCheck - the response data
Returns:
FALSE if the response data is not HTML

createGeneralDollarRegex

private java.lang.String createGeneralDollarRegex(java.lang.String sStr)
Given something that might be a dollar amount, create a general regex that represents the value.

Parameters:
sStr - the value to examine
Returns:
a String containing the general regex, or a String of length 0 if it can't be modified

fixRegex

private java.lang.String fixRegex(java.lang.String sStr)
Fix special characters in the regex.

Parameters:
sStr - the value to modify
Returns:
the modified String which represents a valid regex

validToken

private boolean validToken(java.lang.String sToken)
Determines if the given token is valid.

Parameters:
sToken - the token to validate
Returns:
FALSE if the token is invalid

buildRegexes

private void buildRegexes(java.lang.String sHTMLContent)
Examine the content and build regular expressions which can be used to validate that content.

Parameters:
sHTMLContent - HTML content returned from the webserver

parseContent

private java.lang.String parseContent(java.lang.String sHTML)
Extract the HTML content from the data returned by the webserver.

Parameters:
sHTML - the content returned by the webserver
Returns:
a String that contains just the HTML returned

getServerType

private boolean getServerType()
Determine the server type.

Returns:
FALSE if the server is NOT a microsoft server

sendRequest

private void sendRequest(byte[] b)
Send the browser request to the specified host, then read the response data.

Parameters:
b - an array of bytes to send to the webserver

flushToFile

private void flushToFile()
Write information retrieved to a config file that will processed later.


flushURLToFile

private void flushURLToFile()
For a 302 response, we need to record the URL and data, but record regexes for the subsequent response.


flushImageToFile

private void flushImageToFile(java.lang.String sImageURL,
                              java.lang.String sLength)
Write image details to a file so they can later be added to the generate config file.

Parameters:
sImageURL - the URL of the image being accessed
sLength - the length of the image that is returned, in bytes

getExcludeList

private void getExcludeList()
Read the file which contains regexes to exclude and store the regexes in an ArrayList.


isExcluded

private boolean isExcluded(java.lang.String sStr)
Determines if the given String represents an excluded regular expression.

Parameters:
sStr - the String to examine
Returns:
FALSE if the regex is NOT excluded

validCharactersInRegex

private boolean validCharactersInRegex(java.lang.String sRegex)
Determine if the characters in the regex can be handled by the xml parser.

Parameters:
sRegex - the regular expression to be verified
Returns:
FALSE if the regular expressions cannot be parsed

generateConfigFile

public void generateConfigFile()
Based on the recorded data, and input from the browser, write the XML config file.


getPreferences

private java.util.HashMap getPreferences()
Retrieve user preferences from a text file, if they exist.

Returns:
a HashMap which contains the user preferences

customHandler

private void customHandler()
The user calls certain URLs to start and stop testcases. Handle those specific URLs.


fixHeaders

private java.lang.String fixHeaders(java.lang.String sHeader)
Fix the HTTP headers so that microsoft webservers won't return errors.

Parameters:
sHeader - the header data from the browser
Returns:
a String containing the modified header data

run

public void run()
The method used to start this thread.

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread