com.meterware.httpunit

Class WebResponse

public abstract class WebResponse extends Object implements HTMLSegment, CookieSource

A response to a web request from a web server.

Author: Russell Gold Drew Varner Dave Glowacki Benoit Xhenseval

Nested Class Summary
classWebResponse.Scriptable
Constructor Summary
protected WebResponse(WebClient client, FrameSelector frame, URL url)
Constructs a response object.
protected WebResponse(WebClient client, FrameSelector frame, URL url, String text)
Constructs a response object.
Method Summary
protected voiddefineRawInputStream(InputStream inputStream)
WebApplet[]getApplets()
StringgetCharacterSet()
Returns the character set used in this response.
intgetContentLength()
Returns the content length of this response.
StringgetContentType()
Returns the content type of this response.
DocumentgetDOM()
Returns a copy of the domain object model tree associated with this response.
String[]getElementNames()
Returns a list of HTML element names contained in this HTML section.
HTMLElement[]getElementsWithAttribute(String name, String value)
Returns the HTMLElements found with the specified attribute value.
HTMLElement[]getElementsWithName(String name)
Returns the HTMLElements found in this segment with the specified name.
HTMLElementgetElementWithID(String id)
Returns the HTMLElement with the specified ID.
StringgetExternalStyleSheet()
Returns the stylesheet linked in the head of the page.
WebFormgetFirstMatchingForm(HTMLElementPredicate predicate, Object criteria)
Returns the first form found in the page matching the specified criteria.
WebLinkgetFirstMatchingLink(HTMLElementPredicate predicate, Object criteria)
Returns the first link found in the page matching the specified criteria.
WebTablegetFirstMatchingTable(HTMLElementPredicate predicate, Object criteria)
Returns the first table in the response which matches the specified predicate and value.
TextBlockgetFirstMatchingTextBlock(HTMLElementPredicate predicate, Object criteria)
Returns the first link found in the page matching the specified criteria.
WebForm[]getForms()
Returns the forms found in the page in the order in which they appear.
WebFormgetFormWithID(String ID)
Returns the form found in the page with the specified ID.
WebFormgetFormWithName(String name)
Returns the form found in the page with the specified name.
StringgetFrameName()
Returns the name of the frame containing this page.
String[]getFrameNames()
Returns the names of the frames found in the page in the order in which they appear.
abstract StringgetHeaderField(String fieldName)
Returns the value for the specified header field.
abstract String[]getHeaderFieldNames()
Returns the names of the header fields found in the response.
WebImage[]getImages()
Returns the images found in the page in the order in which they appear.
WebImagegetImageWithAltText(String altText)
Returns the first image found in the page with the specified alt attribute.
WebImagegetImageWithName(String source)
Returns the image found in the page with the specified name attribute.
WebImagegetImageWithSource(String source)
Returns the first image found in the page with the specified src attribute.
InputStreamgetInputStream()
Returns a buffered input stream for reading the contents of this reply.
WebLink[]getLinks()
Returns the links found in the page in the order in which they appear.
WebLinkgetLinkWith(String text)
Returns the first link which contains the specified text.
WebLinkgetLinkWithID(String ID)
Returns the link found in the page with the specified ID.
WebLinkgetLinkWithImageText(String text)
Returns the first link which contains an image with the specified text as its 'alt' attribute.
WebLinkgetLinkWithName(String name)
Returns the link found in the page with the specified name.
WebForm[]getMatchingForms(HTMLElementPredicate predicate, Object criteria)
Returns all forms found in the page matching the specified criteria.
WebLink[]getMatchingLinks(HTMLElementPredicate predicate, Object criteria)
Returns all links found in the page matching the specified criteria.
WebTable[]getMatchingTables(HTMLElementPredicate predicate, Object criteria)
Returns all tables found in the page matching the specified criteria.
String[]getMetaTagContent(String attribute, String attributeValue)
Retrieves the "content" of the meta tags for a key pair attribute-attributeValue.
String[]getNewCookieNames()
Returns a list of new cookie names defined as part of this response.
StringgetNewCookieValue(String name)
Returns the new cookie value defined as part of this response.
TextBlockgetNextTextBlock(TextBlock block)
Returns the text block after the specified block, if any.
intgetRefreshDelay()
Returns the delay before normally following the request to refresh this page, if any.
WebRequestgetRefreshRequest()
Returns a request to refresh this page, if any.
abstract intgetResponseCode()
Returns the response code associated with this response.
abstract StringgetResponseMessage()
Returns the response message associated with this response.
WebResponse.ScriptablegetScriptableObject()
WebResponsegetSubframeContents(String subFrameName)
Returns the contents of the specified subframe of this frameset response.
WebTable[]getTables()
Returns the top-level tables found in this page in the order in which they appear.
WebTablegetTableStartingWith(String text)
Returns the first table in the response which has the specified text as the full text of its first non-blank row and non-blank column.
WebTablegetTableStartingWithPrefix(String text)
Returns the first table in the response which has the specified text as a prefix of the text of its first non-blank row and non-blank column.
WebTablegetTableWithID(String text)
Returns the first table in the response which has the specified text as its ID attribute.
WebTablegetTableWithSummary(String text)
Returns the first table in the response which has the specified text as its summary attribute.
StringgetText()
Returns the text of the response (excluding headers) as a string.
TextBlock[]getTextBlocks()
Returns an array of text blocks found in the page.
StringgetTitle()
Returns the title of the page.
URLgetURL()
Returns the URL which invoked this response.
booleanisHTML()
Returns true if the response is HTML.
protected voidloadResponseText()
static ScriptableDelegatenewDelegate(String delegateClassName)
static WebResponsenewResponse(URLConnection connection)
Returns a web response built from a URL connection.
protected voidsetContentTypeHeader(String value)
Overwrites the current value (if any) of the content type header.
abstract StringtoString()

Constructor Detail

WebResponse

protected WebResponse(WebClient client, FrameSelector frame, URL url)
Constructs a response object.

Parameters: frame the frame to hold the response url the url from which the response was received

WebResponse

protected WebResponse(WebClient client, FrameSelector frame, URL url, String text)
Constructs a response object.

Parameters: frame the frame to hold the response url the url from which the response was received

Method Detail

defineRawInputStream

protected final void defineRawInputStream(InputStream inputStream)

getApplets

public WebApplet[] getApplets()

getCharacterSet

public String getCharacterSet()
Returns the character set used in this response.

getContentLength

public int getContentLength()
Returns the content length of this response.

Returns: the content length, if known, or -1.

getContentType

public String getContentType()
Returns the content type of this response.

getDOM

public Document getDOM()
Returns a copy of the domain object model tree associated with this response. If the response is HTML, it will use a special parser which can transform HTML into an XML DOM.

Throws: SAXException thrown if there is an error parsing the response.

getElementNames

public String[] getElementNames()
Returns a list of HTML element names contained in this HTML section.

getElementsWithAttribute

public HTMLElement[] getElementsWithAttribute(String name, String value)
Returns the HTMLElements found with the specified attribute value.

Since: 1.6

getElementsWithName

public HTMLElement[] getElementsWithName(String name)
Returns the HTMLElements found in this segment with the specified name.

getElementWithID

public HTMLElement getElementWithID(String id)
Returns the HTMLElement with the specified ID.

Throws: SAXException thrown if there is an error parsing the response.

getExternalStyleSheet

public String getExternalStyleSheet()
Returns the stylesheet linked in the head of the page. will return "/mystyle.css".

Throws: SAXException thrown if there is an error parsing this response

getFirstMatchingForm

public WebForm getFirstMatchingForm(HTMLElementPredicate predicate, Object criteria)
Returns the first form found in the page matching the specified criteria.

Throws: SAXException thrown if there is an error parsing the response.

getFirstMatchingLink

public WebLink getFirstMatchingLink(HTMLElementPredicate predicate, Object criteria)
Returns the first link found in the page matching the specified criteria.

Throws: SAXException thrown if there is an error parsing the response.

getFirstMatchingTable

public WebTable getFirstMatchingTable(HTMLElementPredicate predicate, Object criteria)
Returns the first table in the response which matches the specified predicate and value. Will recurse into any nested tables, as needed.

Returns: the selected table, or null if none is found

getFirstMatchingTextBlock

public TextBlock getFirstMatchingTextBlock(HTMLElementPredicate predicate, Object criteria)
Returns the first link found in the page matching the specified criteria.

Throws: SAXException thrown if there is an error parsing the response.

Since: 1.6

getForms

public WebForm[] getForms()
Returns the forms found in the page in the order in which they appear.

Throws: SAXException thrown if there is an error parsing the response.

getFormWithID

public WebForm getFormWithID(String ID)
Returns the form found in the page with the specified ID.

Throws: SAXException thrown if there is an error parsing the response.

getFormWithName

public WebForm getFormWithName(String name)
Returns the form found in the page with the specified name.

Throws: SAXException thrown if there is an error parsing the response.

getFrameName

public String getFrameName()
Returns the name of the frame containing this page.

getFrameNames

public String[] getFrameNames()
Returns the names of the frames found in the page in the order in which they appear.

Throws: SAXException thrown if there is an error parsing this response

getHeaderField

public abstract String getHeaderField(String fieldName)
Returns the value for the specified header field. If no such field is defined, will return null. If more than one header is defined for the specified name, returns only the first found.

getHeaderFieldNames

public abstract String[] getHeaderFieldNames()
Returns the names of the header fields found in the response.

getImages

public WebImage[] getImages()
Returns the images found in the page in the order in which they appear.

Throws: SAXException thrown if there is an error parsing the response.

getImageWithAltText

public WebImage getImageWithAltText(String altText)
Returns the first image found in the page with the specified alt attribute.

getImageWithName

public WebImage getImageWithName(String source)
Returns the image found in the page with the specified name attribute.

Throws: SAXException thrown if there is an error parsing the response.

getImageWithSource

public WebImage getImageWithSource(String source)
Returns the first image found in the page with the specified src attribute.

Throws: SAXException thrown if there is an error parsing the response.

getInputStream

public InputStream getInputStream()
Returns a buffered input stream for reading the contents of this reply.

getLinks

public WebLink[] getLinks()
Returns the links found in the page in the order in which they appear.

Throws: SAXException thrown if there is an error parsing the response.

getLinkWith

public WebLink getLinkWith(String text)
Returns the first link which contains the specified text.

Throws: SAXException thrown if there is an error parsing the response.

getLinkWithID

public WebLink getLinkWithID(String ID)
Returns the link found in the page with the specified ID.

Throws: SAXException thrown if there is an error parsing the response.

getLinkWithImageText

public WebLink getLinkWithImageText(String text)
Returns the first link which contains an image with the specified text as its 'alt' attribute.

Throws: SAXException thrown if there is an error parsing the response.

getLinkWithName

public WebLink getLinkWithName(String name)
Returns the link found in the page with the specified name.

Throws: SAXException thrown if there is an error parsing the response.

getMatchingForms

public WebForm[] getMatchingForms(HTMLElementPredicate predicate, Object criteria)
Returns all forms found in the page matching the specified criteria.

Throws: SAXException thrown if there is an error parsing the response.

getMatchingLinks

public WebLink[] getMatchingLinks(HTMLElementPredicate predicate, Object criteria)
Returns all links found in the page matching the specified criteria.

Throws: SAXException thrown if there is an error parsing the response.

getMatchingTables

public WebTable[] getMatchingTables(HTMLElementPredicate predicate, Object criteria)
Returns all tables found in the page matching the specified criteria.

Throws: SAXException thrown if there is an error parsing the response.

getMetaTagContent

public String[] getMetaTagContent(String attribute, String attributeValue)
Retrieves the "content" of the meta tags for a key pair attribute-attributeValue. this can be used like this getMetaTagContent("name","robots") will return { "index","follow" } getMetaTagContent("http-equiv","Expires") will return { "now" }

Throws: SAXException thrown if there is an error parsing this response

getNewCookieNames

public String[] getNewCookieNames()
Returns a list of new cookie names defined as part of this response.

getNewCookieValue

public String getNewCookieValue(String name)
Returns the new cookie value defined as part of this response.

getNextTextBlock

public TextBlock getNextTextBlock(TextBlock block)
Returns the text block after the specified block, if any.

Since: 1.6

getRefreshDelay

public int getRefreshDelay()
Returns the delay before normally following the request to refresh this page, if any. This request will be defined by a tag in the header. If no tag exists, will return zero.

getRefreshRequest

public WebRequest getRefreshRequest()
Returns a request to refresh this page, if any. This request will be defined by a tag in the header. If no tag exists, will return null.

getResponseCode

public abstract int getResponseCode()
Returns the response code associated with this response.

getResponseMessage

public abstract String getResponseMessage()
Returns the response message associated with this response.

getScriptableObject

public WebResponse.Scriptable getScriptableObject()

getSubframeContents

public WebResponse getSubframeContents(String subFrameName)
Returns the contents of the specified subframe of this frameset response.

Parameters: subFrameName the name of the desired frame as defined in the frameset.

getTables

public WebTable[] getTables()
Returns the top-level tables found in this page in the order in which they appear.

Throws: SAXException thrown if there is an error parsing the response.

getTableStartingWith

public WebTable getTableStartingWith(String text)
Returns the first table in the response which has the specified text as the full text of its first non-blank row and non-blank column. Will recurse into any nested tables, as needed. Case is ignored.

Returns: the selected table, or null if none is found

Throws: SAXException thrown if there is an error parsing the response.

getTableStartingWithPrefix

public WebTable getTableStartingWithPrefix(String text)
Returns the first table in the response which has the specified text as a prefix of the text of its first non-blank row and non-blank column. Will recurse into any nested tables, as needed. Case is ignored.

Returns: the selected table, or null if none is found

Throws: SAXException thrown if there is an error parsing the response.

getTableWithID

public WebTable getTableWithID(String text)
Returns the first table in the response which has the specified text as its ID attribute. Will recurse into any nested tables, as needed. Case is ignored.

Returns: the selected table, or null if none is found

Throws: SAXException thrown if there is an error parsing the response.

getTableWithSummary

public WebTable getTableWithSummary(String text)
Returns the first table in the response which has the specified text as its summary attribute. Will recurse into any nested tables, as needed. Case is ignored.

Returns: the selected table, or null if none is found

Throws: SAXException thrown if there is an error parsing the response.

getText

public String getText()
Returns the text of the response (excluding headers) as a string. Use this method in preference to 'toString' which may be used to represent internal state of this object.

getTextBlocks

public TextBlock[] getTextBlocks()
Returns an array of text blocks found in the page.

Since: 1.6

getTitle

public String getTitle()
Returns the title of the page.

Throws: SAXException thrown if there is an error parsing this response

getURL

public URL getURL()
Returns the URL which invoked this response.

isHTML

public boolean isHTML()
Returns true if the response is HTML.

loadResponseText

protected void loadResponseText()

newDelegate

public static ScriptableDelegate newDelegate(String delegateClassName)

newResponse

public static WebResponse newResponse(URLConnection connection)
Returns a web response built from a URL connection. Provided to allow access to WebResponse parsing without using a WebClient.

setContentTypeHeader

protected void setContentTypeHeader(String value)
Overwrites the current value (if any) of the content type header.

toString

public abstract String toString()