Package com.avoka.component.http
Class HttpResponse
java.lang.Object
com.avoka.component.http.HttpResponse
Provides a HttpResponse value object class.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionbyte[]
long
Return the content as a XML Document object, or null if not defined.Return the map of HTTP response headers.Return the content as a JSON object, or null if not defined.int
Return the content as a UTF-8 encoded string, or null if not defined.groovy.util.slurpersupport.GPathResult
Return the content as a Groovy XML GPathResult object, or null if not defined.boolean
Return true if the HTTP response status code is NOT FOUND (404).boolean
Return true if the HTTP response status code is OK (200).void
setContent
(byte[] content) void
setContentEncoding
(String contentEncoding) void
setContentLength
(long contentLength) void
setContentType
(String contentType) void
setHeaders
(Map<String, String> headers) Set the map of HTTP response headers.void
setStatus
(int status) void
setStatusLine
(String statusLine) toString()
Return the string representation of this class.
-
Constructor Details
-
HttpResponse
public HttpResponse()
-
-
Method Details
-
getTextContent
Return the content as a UTF-8 encoded string, or null if not defined.- Returns:
- the content as a UTF-8 encoded string, or null if not defined.
-
getJsonContent
Return the content as a JSON object, or null if not defined. This method uses the Groovygroovy.json.JsonSlurper
to parse the response text content into a JSON object.- Returns:
- the content as a JSON object, or null if not defined.
-
getXmlContent
public groovy.util.slurpersupport.GPathResult getXmlContent()Return the content as a Groovy XML GPathResult object, or null if not defined. This method uses the Groovygroovy.util.XmlSlurper
to parse the response text content into a Groovy XML GPathResult object. Example GPathResult object graph navigation provided below:// XML Content <root><one a1="uno!"/><two>Some text!</two></root> // Perform request, and parse response .. def rootNode = response.getXmlContent() assert rootNode.name() == 'root' assert rootNode.one[0].@a1 == 'uno!' assert rootNode.two.text() == 'Some text!' rootNode.children().each { assert it.name() in ['one','two'] }
Note that in some cases, a 'selector' expression may not resolve to a single node. For example:
// XML Content <root> <a>one!</a> <a>two!</a> </root> // Perform request, and parse response .. def rootNode = response.getXmlContent() assert rootNode.a.size() == 2 rootNode.a.each { assert it.text() in ['one!','two!'] }
- Returns:
- the content as a Groovy XML GPathResult, or null if not defined
- Since:
- 4.3.0
-
getDocumentContent
Return the content as a XML Document object, or null if not defined. This method usesXmlUtils.buildDocument(byte[])
to parse the response content into an Document object.- Returns:
- the content as a XML Document object, or null if not defined.
-
getHeaders
Return the map of HTTP response headers.- Returns:
- the map of HTTP response headers
- Since:
- 5.0.0
-
setHeaders
Set the map of HTTP response headers.- Parameters:
headers
- the map of HTTP response headers- Since:
- 5.0.0
-
toString
Return the string representation of this class. -
isStatusOK
public boolean isStatusOK()Return true if the HTTP response status code is OK (200).- Returns:
- true if the HTTP response status code is OK (200).
-
isStatusNotFound
public boolean isStatusNotFound()Return true if the HTTP response status code is NOT FOUND (404).- Returns:
- true if the HTTP response status code is NOT FOUND (404).
-
getStatus
public int getStatus()- Returns:
- the HTTP response status code
-
setStatus
public void setStatus(int status) - Parameters:
status
- the status to set
-
getStatusLine
- Returns:
- the statusLine
-
setStatusLine
- Parameters:
statusLine
- the statusLine to set
-
getContent
public byte[] getContent()- Returns:
- the content
-
setContent
public void setContent(byte[] content) - Parameters:
content
- the content to set
-
getContentLength
public long getContentLength()- Returns:
- the contentLength
-
setContentLength
public void setContentLength(long contentLength) - Parameters:
contentLength
- the contentLength to set
-
getContentType
- Returns:
- the contentType
-
setContentType
- Parameters:
contentType
- the contentType to set
-
getContentEncoding
- Returns:
- the contentEncoding
-
setContentEncoding
- Parameters:
contentEncoding
- the contentEncoding to set
-