11package { {invokerPackage} }
22
3+ { {#withXml} }
4+ import java.io.StringReader
5+ import { {javaxPackage} }.xml.bind.JAXB
6+ { {/withXml} }
37import groovy.json.JsonBuilder
48import groovy.json.JsonGenerator
59import groovyx.net.http.ChainedHttpConfig
610import groovyx.net.http.ContentTypes
711import groovyx.net.http.NativeHandlers
12+ import groovyx.net.http.FromServer
813import groovyx.net.http.ToServer
914
1015import static groovyx.net.http.HttpBuilder.configure
@@ -25,17 +30,35 @@ class ApiUtils {
2530 request.uri = url
2631 request.uri.path = uriPath
2732 request.encoder(ContentTypes.JSON, { final ChainedHttpConfig config, final ToServer ts ->
28- final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest();
33+ final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest()
2934 if (NativeHandlers.Encoders.handleRawUpload(config, ts)) {
30- return;
35+ return
3136 }
3237
33- final Object body = NativeHandlers.Encoders.checkNull(request.actualBody());
38+ final Object body = NativeHandlers.Encoders.checkNull(request.actualBody())
3439 final String json = ((body instanceof String || body instanceof GString)
3540 ? body.toString()
36- : new JsonBuilder(body, jsonGenerator).toString());
37- ts.toServer(NativeHandlers.Encoders.stringToStream(json, request.actualCharset()));
41+ : new JsonBuilder(body, jsonGenerator).toString())
42+ ts.toServer(NativeHandlers.Encoders.stringToStream(json, request.actualCharset()))
3843 })
44+ { {#withXml} }
45+ request.encoder(ContentTypes.XML, { final ChainedHttpConfig config, final ToServer ts ->
46+ final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest()
47+ if (NativeHandlers.Encoders.handleRawUpload(config, ts)) {
48+ return
49+ }
50+
51+ final Object body = NativeHandlers.Encoders.checkNull(request.actualBody())
52+ String xml
53+ if (body instanceof String || body instanceof GString) {
54+ xml = body.toString()
55+ } else {
56+ StringWriter writer = new StringWriter()
57+ JAXB.marshal(body, writer)
58+ xml = writer.toString()
59+ }
60+ ts.toServer(NativeHandlers.Encoders.stringToStream(xml, request.actualCharset()))
61+ }){ {/withXml} }
3962 }
4063 .invokeMethod(String.valueOf(method).toLowerCase()) {
4164 request.uri.query = queryParams
@@ -45,10 +68,14 @@ class ApiUtils {
4568 }
4669 request.accept = accept
4770 request.contentType = contentType
48-
49- response.success { resp, json ->
71+ { {#withXml} }
72+ response.parser(ContentTypes.XML) { final ChainedHttpConfig cfg, final FromServer fs ->
73+ fs.inputStream.text
74+ }
75+ { {/withXml} }
76+ response.success { resp, body ->
5077 if (type != null) {
51- onSuccess(parse(json , container, type))
78+ onSuccess(parse(resp, body , container, type))
5279 }
5380 }
5481 response.failure { resp ->
@@ -69,10 +96,15 @@ class ApiUtils {
6996 [basePath-pathOnly, pathOnly+versionPath+resourcePath]
7097 }
7198
72- private def parse(object, container, clazz) {
99+ private def parse(response, object, container, clazz) {
100+ {{#withXml} }
101+ if (response.getContentType().toLowerCase().contains("xml")) {
102+ return JAXB.unmarshal(new StringReader(object), clazz)
103+ }
104+ { {/withXml} }
73105 if (container == "array") {
74- return object.collect {parse(it, " " , clazz)}
75- } else {
106+ return object.collect { parse(it, " " , clazz) }
107+ } else {
76108 return clazz.newInstance(object)
77109 }
78110 }
@@ -84,5 +116,4 @@ class ApiUtils {
84116 }
85117 return accepts
86118 }
87-
88119}
0 commit comments