Package org.htmlunit.util
Class DebuggingWebConnection
- java.lang.Object
-
- org.htmlunit.util.WebConnectionWrapper
-
- org.htmlunit.util.DebuggingWebConnection
-
- All Implemented Interfaces:
java.lang.AutoCloseable
,WebConnection
public class DebuggingWebConnection extends WebConnectionWrapper
Wrapper around a "real" WebConnection that will use the wrapped web connection to do the real job and save all received responses in the temp directory with an overview page.
This may be useful at conception time to understand what is "browsed".
Example:final WebClient client = new WebClient(); final WebConnection connection = new DebuggingWebConnection(client.getWebConnection(), "myTest"); client.setWebConnection(connection);
In this example an overview page will be generated under the name myTest/index.html in the temp directory and all received responses will be saved into the myTest folder.
This class is only intended as a help during the conception.
-
-
Field Summary
Fields Modifier and Type Field Description private int
counter_
private static java.util.regex.Pattern
ESCAPE_QUOTE_PATTERN
private java.io.File
javaScriptFile_
private static org.apache.commons.logging.Log
LOG
private java.io.File
reportFolder_
private boolean
uncompressJavaScript_
private WebConnection
wrappedWebConnection_
-
Constructor Summary
Constructors Constructor Description DebuggingWebConnection(WebConnection webConnection, java.lang.String dirName)
Wraps a web connection to have a report generated of the received responses.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addMark(java.lang.String mark)
Adds a mark that will be visible in the HTML result page generated by this class.private void
appendToJSFile(java.lang.String str)
(package private) static java.lang.String
chooseExtension(java.lang.String contentType)
private java.io.File
createFile(java.net.URL url, java.lang.String extension)
Computes the best file to save the response to the given URL.private void
createOverview()
Creates the summary file and the JavaScript file that will be updated for each received response(package private) static java.lang.String
escapeJSString(java.lang.String string)
(package private) java.io.File
getReportFolder()
WebResponse
getResponse(WebRequest request)
Calls the wrapped webconnection and save the received response.(package private) static boolean
isJavaScript(java.lang.String contentType)
Indicates if the response contains JavaScript content.boolean
isUncompressJavaScript()
Indicates if it should try to format responses recognized as JavaScript.(package private) static java.lang.String
nameValueListToJsMap(java.util.List<NameValuePair> headers)
Produces a String that will produce a JS map like "{'key1': 'value1', 'key 2': 'value2'}".protected void
saveResponse(WebResponse response, WebRequest request)
Saves the response content in the temp dir and adds it to the summary page.void
setUncompressJavaScript(boolean decompress)
Indicates that responses recognized as JavaScript should be formatted or not.protected WebResponse
uncompressJavaScript(WebResponse response)
Tries to uncompress the JavaScript code in the provided response.-
Methods inherited from class org.htmlunit.util.WebConnectionWrapper
close, getWrappedWebConnection
-
-
-
-
Field Detail
-
LOG
private static final org.apache.commons.logging.Log LOG
-
ESCAPE_QUOTE_PATTERN
private static final java.util.regex.Pattern ESCAPE_QUOTE_PATTERN
-
counter_
private int counter_
-
wrappedWebConnection_
private final WebConnection wrappedWebConnection_
-
javaScriptFile_
private final java.io.File javaScriptFile_
-
reportFolder_
private final java.io.File reportFolder_
-
uncompressJavaScript_
private boolean uncompressJavaScript_
-
-
Constructor Detail
-
DebuggingWebConnection
public DebuggingWebConnection(WebConnection webConnection, java.lang.String dirName) throws java.io.IOException
Wraps a web connection to have a report generated of the received responses.- Parameters:
webConnection
- the webConnection that do the real workdirName
- the name of the directory to create in the tmp folder to save received responses. If this folder already exists, it will be deleted first.- Throws:
java.io.IOException
- in case of problems writing the files
-
-
Method Detail
-
getResponse
public WebResponse getResponse(WebRequest request) throws java.io.IOException
Calls the wrapped webconnection and save the received response. Submits a request and retrieves a response. The default behavior of this method is to returnWebConnection.getResponse(WebRequest)
on the wrapped connection object.- Specified by:
getResponse
in interfaceWebConnection
- Overrides:
getResponse
in classWebConnectionWrapper
- Parameters:
request
- the request- Returns:
- the response to the request defined by the specified request
- Throws:
java.io.IOException
- if an IO error occurs
-
uncompressJavaScript
protected WebResponse uncompressJavaScript(WebResponse response)
Tries to uncompress the JavaScript code in the provided response.- Parameters:
response
- the response to uncompress- Returns:
- a new response with uncompressed JavaScript code or the original response in case of failure
-
addMark
public void addMark(java.lang.String mark) throws java.io.IOException
Adds a mark that will be visible in the HTML result page generated by this class.- Parameters:
mark
- the text- Throws:
java.io.IOException
- if a problem occurs writing the file
-
saveResponse
protected void saveResponse(WebResponse response, WebRequest request) throws java.io.IOException
Saves the response content in the temp dir and adds it to the summary page.- Parameters:
response
- the response to saverequest
- the request used to get the response- Throws:
java.io.IOException
- if a problem occurs writing the file
-
escapeJSString
static java.lang.String escapeJSString(java.lang.String string)
-
chooseExtension
static java.lang.String chooseExtension(java.lang.String contentType)
-
isJavaScript
static boolean isJavaScript(java.lang.String contentType)
Indicates if the response contains JavaScript content.- Parameters:
contentType
- the response's content type- Returns:
false
if it is not recognized as JavaScript
-
isUncompressJavaScript
public boolean isUncompressJavaScript()
Indicates if it should try to format responses recognized as JavaScript.- Returns:
- default is
false
to deliver the original content
-
setUncompressJavaScript
public void setUncompressJavaScript(boolean decompress)
Indicates that responses recognized as JavaScript should be formatted or not. Formatting is interesting for debugging when the original script is compressed on a single line. It allows to better follow with a debugger and to obtain more interesting error messages.- Parameters:
decompress
-true
if JavaScript responses should be uncompressed
-
appendToJSFile
private void appendToJSFile(java.lang.String str) throws java.io.IOException
- Throws:
java.io.IOException
-
createFile
private java.io.File createFile(java.net.URL url, java.lang.String extension) throws java.io.IOException
Computes the best file to save the response to the given URL.- Parameters:
url
- the requested URLextension
- the preferred extension- Returns:
- the file to create
- Throws:
java.io.IOException
- if a problem occurs creating the file
-
nameValueListToJsMap
static java.lang.String nameValueListToJsMap(java.util.List<NameValuePair> headers)
Produces a String that will produce a JS map like "{'key1': 'value1', 'key 2': 'value2'}".- Parameters:
headers
- a list ofNameValuePair
- Returns:
- the JS String
-
createOverview
private void createOverview() throws java.io.IOException
Creates the summary file and the JavaScript file that will be updated for each received response- Throws:
java.io.IOException
- if a problem occurs writing the file
-
getReportFolder
java.io.File getReportFolder()
-
-