Skip to content

Commit 1248f43

Browse files
committed
refactor: support multipart and general cleanup
1 parent cd53b68 commit 1248f43

File tree

10 files changed

+98
-30
lines changed

10 files changed

+98
-30
lines changed

src/targets/java/restclient.js

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@
1212

1313
const CodeBuilder = require('../../helpers/code-builder')
1414

15-
// Based off org.springframework.http.HttpMethod
1615
const standardMethods = ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'TRACE']
1716

18-
// Based off org.springframework.http.MediaType
1917
const standardMediaTypes = {
2018
'application/atom+xml': 'APPLICATION_ATOM_XML',
2119
'application/cbor': 'APPLICATION_CBOR',
@@ -45,6 +43,20 @@ const standardMediaTypes = {
4543
'text/xml': 'TEXT_XML'
4644
}
4745

46+
const jsonMimeTypes = [
47+
'application/json',
48+
'text/json',
49+
'text/x-json',
50+
'application/x-json'
51+
]
52+
53+
const multipartMimeTypes = [
54+
'multipart/form-data',
55+
'multipart/mixed',
56+
'multipart/related',
57+
'multipart/alternative'
58+
]
59+
4860
module.exports = function (source, options) {
4961
const opts = Object.assign({
5062
indent: ' ',
@@ -56,6 +68,43 @@ module.exports = function (source, options) {
5668
code.push('RestClient restClient = RestClient.create();')
5769
.blank()
5870

71+
if (source.postData && source.postData.mimeType === 'application/x-www-form-urlencoded' && source.postData.params) {
72+
code.push('MultiValueMap<String, String> formDataMap = new LinkedMultiValueMap<>();')
73+
source.postData.params.forEach(function (param) {
74+
code.push('formDataMap.add("%qd", "%qd");', param.name, param.value)
75+
})
76+
code.blank()
77+
}
78+
79+
if (source.postData && multipartMimeTypes.includes(source.postData.mimeType) && source.postData.params) {
80+
code.push('MultipartBodyBuilder multipartBuilder = new MultipartBodyBuilder();')
81+
82+
source.postData.params.forEach(function (param) {
83+
if (param.fileName) {
84+
if (param.value) {
85+
code.push('multipartBuilder.part("%s", "%qd")', param.name, param.value)
86+
code.push(1, '.filename("%s")', param.fileName)
87+
} else {
88+
code.push('multipartBuilder.part("%s", new FileSystemResource("%s"))', param.name, param.fileName)
89+
}
90+
91+
if (param.contentType) {
92+
const mediaTypeConstant = standardMediaTypes[param.contentType]
93+
if (mediaTypeConstant) {
94+
code.push(1, '.contentType(MediaType.%s);', mediaTypeConstant)
95+
} else {
96+
code.push(1, '.contentType(MediaType.parseMediaType("%s"));', param.contentType)
97+
}
98+
} else {
99+
code.push(1, ';')
100+
}
101+
} else {
102+
code.push('multipartBuilder.part("%s", "%qd");', param.name, param.value || '')
103+
}
104+
})
105+
code.blank()
106+
}
107+
59108
code.push('ResponseEntity<%s> response = restClient', opts.entityClass)
60109

61110
if (standardMethods.includes(source.method.toUpperCase())) {
@@ -91,21 +140,30 @@ module.exports = function (source, options) {
91140
const headers = Object.keys(source.headersObj)
92141
if (headers.length) {
93142
headers.forEach(function (key) {
94-
code.push(1, '.header("%s", "%qd")', key, source.headersObj[key])
143+
if (key.toLowerCase() !== 'content-type') {
144+
code.push(1, '.header("%s", "%qd")', key, source.headersObj[key])
145+
}
95146
})
96147
}
97148

98-
if (source.postData && source.postData.text) {
99-
if (source.postData.mimeType) {
100-
const mappedEnumConst = standardMediaTypes[source.postData.mimeType]
101-
if (mappedEnumConst) {
102-
code.push(1, '.contentType(MediaType.%s)', mappedEnumConst)
103-
} else {
104-
code.push(1, '.contentType(MediaType.parseMediaType("%s"))', source.postData.mimeType)
105-
}
149+
if (source.postData && (source.postData.params || source.postData.text)) {
150+
const mediaTypeEnumConstant = standardMediaTypes[source.postData.mimeType]
151+
152+
if (mediaTypeEnumConstant) {
153+
code.push(1, '.contentType(MediaType.%s)', mediaTypeEnumConstant)
154+
} else {
155+
code.push(1, '.contentType(MediaType.parseMediaType("%s"))', source.postData.mimeType)
106156
}
107157

108-
code.push(1, '.body(%s)', JSON.stringify(source.postData.text))
158+
if (source.postData.mimeType === 'application/x-www-form-urlencoded' && source.postData.params) {
159+
code.push(1, '.body(formDataMap)')
160+
} else if (multipartMimeTypes.includes(source.postData.mimeType) && source.postData.params) {
161+
code.push(1, '.body(multipartBuilder.build())')
162+
} else if (source.postData.text) {
163+
code.push(1, '.body(%s)', JSON.stringify(source.postData.text))
164+
} else if (source.postData.jsonObj && jsonMimeTypes.includes(source.postData.mimeType)) {
165+
code.push(1, '.body(%s)', JSON.stringify(JSON.stringify(source.postData.jsonObj)))
166+
}
109167
}
110168

111169
code.push(1, '.retrieve()')
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
RestClient restClient = RestClient.create();
22

3+
MultiValueMap<String, String> formDataMap = new LinkedMultiValueMap<>();
4+
formDataMap.add("foo", "bar");
5+
formDataMap.add("hello", "world");
6+
37
ResponseEntity<String> response = restClient
48
.method(HttpMethod.POST)
59
.uri("http://mockbin.com/har")
6-
.header("content-type", "application/x-www-form-urlencoded")
710
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
8-
.body("foo=bar&hello=world")
11+
.body(formDataMap)
912
.retrieve()
10-
.toEntity(String.class);
13+
.toEntity(String.class);

test/fixtures/output/java/restclient/application-json.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
ResponseEntity<String> response = restClient
44
.method(HttpMethod.POST)
55
.uri("http://mockbin.com/har")
6-
.header("content-type", "application/json")
76
.contentType(MediaType.APPLICATION_JSON)
87
.body("{\"number\":1,\"string\":\"f\\\"oo\",\"arr\":[1,2,3],\"nested\":{\"a\":\"b\"},\"arr_mix\":[1,\"a\",{\"arr_mix_nested\":{}}],\"boolean\":false}")
98
.retrieve()
10-
.toEntity(String.class);
9+
.toEntity(String.class);

test/fixtures/output/java/restclient/full.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
RestClient restClient = RestClient.create();
22

3+
MultiValueMap<String, String> formDataMap = new LinkedMultiValueMap<>();
4+
formDataMap.add("foo", "bar");
5+
36
ResponseEntity<String> response = restClient
47
.method(HttpMethod.POST)
58
.uri("http://mockbin.com/har", uriBuilder -> {
@@ -12,8 +15,7 @@
1215
.cookie("foo", "bar")
1316
.cookie("bar", "baz")
1417
.header("accept", "application/json")
15-
.header("content-type", "application/x-www-form-urlencoded")
1618
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
17-
.body("foo=bar")
19+
.body(formDataMap)
1820
.retrieve()
1921
.toEntity(String.class);

test/fixtures/output/java/restclient/jsonObj-multiline.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
ResponseEntity<String> response = restClient
44
.method(HttpMethod.POST)
55
.uri("http://mockbin.com/har")
6-
.header("content-type", "application/json")
76
.contentType(MediaType.APPLICATION_JSON)
87
.body("{\n \"foo\": \"bar\"\n}")
98
.retrieve()
10-
.toEntity(String.class);
9+
.toEntity(String.class);

test/fixtures/output/java/restclient/jsonObj-null-value.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
ResponseEntity<String> response = restClient
44
.method(HttpMethod.POST)
55
.uri("http://mockbin.com/har")
6-
.header("content-type", "application/json")
76
.contentType(MediaType.APPLICATION_JSON)
87
.body("{\"foo\":null}")
98
.retrieve()
10-
.toEntity(String.class);
9+
.toEntity(String.class);
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
RestClient restClient = RestClient.create();
22

3+
MultipartBodyBuilder multipartBuilder = new MultipartBodyBuilder();
4+
multipartBuilder.part("foo", "Hello World")
5+
.filename("hello.txt")
6+
.contentType(MediaType.TEXT_PLAIN);
7+
38
ResponseEntity<String> response = restClient
49
.method(HttpMethod.POST)
510
.uri("http://mockbin.com/har")
6-
.header("content-type", "multipart/form-data; boundary=---011000010111000001101001")
711
.contentType(MediaType.MULTIPART_FORM_DATA)
8-
.body("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\nHello World\r\n-----011000010111000001101001--\r\n")
12+
.body(multipartBuilder.build())
913
.retrieve()
1014
.toEntity(String.class);
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
RestClient restClient = RestClient.create();
22

3+
MultipartBodyBuilder multipartBuilder = new MultipartBodyBuilder();
4+
multipartBuilder.part("foo", new FileSystemResource("test/fixtures/files/hello.txt"))
5+
.contentType(MediaType.TEXT_PLAIN);
6+
37
ResponseEntity<String> response = restClient
48
.method(HttpMethod.POST)
59
.uri("http://mockbin.com/har")
6-
.header("content-type", "multipart/form-data; boundary=---011000010111000001101001")
710
.contentType(MediaType.MULTIPART_FORM_DATA)
8-
.body("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\n\r\n-----011000010111000001101001--\r\n")
11+
.body(multipartBuilder.build())
912
.retrieve()
1013
.toEntity(String.class);
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
RestClient restClient = RestClient.create();
22

3+
MultipartBodyBuilder multipartBuilder = new MultipartBodyBuilder();
4+
multipartBuilder.part("foo", "bar");
5+
36
ResponseEntity<String> response = restClient
47
.method(HttpMethod.POST)
58
.uri("http://mockbin.com/har")
6-
.header("Content-Type", "multipart/form-data; boundary=---011000010111000001101001")
79
.contentType(MediaType.MULTIPART_FORM_DATA)
8-
.body("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\nbar\r\n-----011000010111000001101001--\r\n")
10+
.body(multipartBuilder.build())
911
.retrieve()
1012
.toEntity(String.class);

test/fixtures/output/java/restclient/text-plain.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
ResponseEntity<String> response = restClient
44
.method(HttpMethod.POST)
55
.uri("http://mockbin.com/har")
6-
.header("content-type", "text/plain")
76
.contentType(MediaType.TEXT_PLAIN)
87
.body("Hello World")
98
.retrieve()

0 commit comments

Comments
 (0)