forked from netconomy/restflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Response.groovy
70 lines (55 loc) · 1.66 KB
/
Response.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package net.netconomy.tools.restflow.dsl
import groovy.json.JsonSlurper
import groovy.transform.ToString
import groovy.xml.XmlSlurper
import groovy.xml.slurpersupport.GPathResult
import net.netconomy.tools.restflow.impl.CommLog
import net.netconomy.tools.restflow.impl.HttpBody
import org.apache.http.HttpResponse
import java.nio.charset.Charset
/**
* @since 2018-10-12
* @author Raffael Herzog (r.herzog@netconomy.net)
*/
@ToString(includes = ['statusCode', 'contentType'])
class Response {
final CommLog log
final int statusCode
final String reasonPhrase
final Map<String, String> headers
final HttpBody body
Response(RestFlow client, HttpResponse response) {
this.log = client.log
this.statusCode = response.statusLine.statusCode
this.reasonPhrase = response.statusLine.reasonPhrase
this.headers = new LinkedHashMap<String, String>().with {
response.allHeaders.each { h ->
put(h.name, h.value)
}
asImmutable()
}
body = response.entity ? new HttpBody(response.entity, client.defaultResponseCharset) : HttpBody.EMPTY
log.recvBody(body)
}
boolean getHasContent() {
return rawContent != null && rawContent.length
}
String getContentType() {
return body.mimeType
}
Charset getCharset() {
return body.charset
}
byte[] getRawContent() {
return body.rawContent
}
String getContent() {
return body.content
}
Object getJson() {
return new JsonSlurper().parseText(content)
}
GPathResult getXml() {
return new XmlSlurper().parseText(content)
}
}