Skip to content

Commit 612ddd8

Browse files
committed
代理接口新增支持转发 POST form-data,优化代码
1 parent 24940d9 commit 612ddd8

1 file changed

Lines changed: 54 additions & 13 deletions

File tree

‎APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/boot/DemoController.java‎

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
3535
import org.springframework.http.converter.StringHttpMessageConverter;
3636
import org.springframework.stereotype.Service;
37+
import org.springframework.util.LinkedMultiValueMap;
38+
import org.springframework.util.MultiValueMap;
3739
import org.springframework.web.bind.annotation.*;
3840
import org.springframework.web.client.RestClientResponseException;
3941
import org.springframework.web.client.RestTemplate;
@@ -1356,7 +1358,7 @@ public void remove(String key) {
13561358
*/
13571359
@SuppressWarnings("unchecked")
13581360
@RequestMapping(value = "delegate")
1359-
public String delegate(
1361+
public String apiDelegate(
13601362
@RequestParam("$_delegate_url") String rawUrl,
13611363
@RequestParam(value = "$_type", required = false) String type,
13621364
@RequestParam(value = "$_headers", required = false) String headerStr,
@@ -1365,6 +1367,25 @@ public String delegate(
13651367
@RequestParam(value = "$_record", required = false) Integer record,
13661368
@RequestBody(required = false) String body,
13671369
HttpMethod method, HttpSession session
1370+
) {
1371+
return delegate(rawUrl, type, headerStr, exceptHeaders, sessionId, record, body, method, session);
1372+
}
1373+
1374+
public String delegate(String rawUrl, HttpSession session) {
1375+
return delegate(rawUrl, null, null, session);
1376+
}
1377+
public String delegate(String rawUrl, String sessionId, Integer record, HttpSession session) {
1378+
return delegate(rawUrl, null, sessionId, record, null, session);
1379+
}
1380+
public String delegate(String rawUrl, String type, String sessionId, Integer record, HttpMethod method, HttpSession session) {
1381+
return delegate(rawUrl, type, sessionId, record, null, method, session);
1382+
}
1383+
public String delegate(String rawUrl, String type, String sessionId, Integer record, String body, HttpMethod method, HttpSession session) {
1384+
return delegate(rawUrl, type, null, null, sessionId, record, body, method, session);
1385+
}
1386+
1387+
public String delegate(String rawUrl, String type, String headerStr, String exceptHeaders, String sessionId,
1388+
Integer record, String body, HttpMethod method, HttpSession session
13681389
) {
13691390
if (Log.DEBUG == false) {
13701391
return JSON.toJSONString(newErrorResult(new IllegalAccessException("非 DEBUG 模式下不允许使用服务器代理!")));
@@ -1614,7 +1635,7 @@ else if (names != null) {
16141635

16151636
String rspBody = null;
16161637
if (recordType >= 0) {
1617-
rspBody = sendRequest(session, method, url, body, headers);
1638+
rspBody = sendRequest(session, method, url, body, map, headers);
16181639
}
16191640

16201641
if (recordType != 0) {
@@ -1983,7 +2004,7 @@ else if (recordType > 0) {
19832004
}
19842005

19852006
if (recordType < 0) {
1986-
rspBody = sendRequest(session, method, url, body, headers);
2007+
rspBody = sendRequest(session, method, url, body, map, headers);
19872008
}
19882009

19892010
return rspBody;
@@ -2025,7 +2046,8 @@ protected Long getContentLength(String s, MediaType contentType) {
20252046
}
20262047
}
20272048

2028-
protected String sendRequest(HttpSession session, HttpMethod method, String url, String body, HttpHeaders headers) {
2049+
protected String sendRequest(HttpSession session, HttpMethod method, String url, String body, Map<String, String[]> params, HttpHeaders headers) {
2050+
method = method == null ? HttpMethod.GET : method;
20292051
String rspBody = null;
20302052
try {
20312053
// 为JSON请求设置默认的 Content-Type
@@ -2037,7 +2059,7 @@ protected String sendRequest(HttpSession session, HttpMethod method, String url,
20372059
headers.setContentType(MediaType.APPLICATION_JSON);
20382060
} catch (Exception e) {
20392061
// 如果不是JSON,设置为普通文本
2040-
headers.setContentType(MediaType.TEXT_PLAIN);
2062+
headers.setContentType(params == null || params.isEmpty() ? MediaType.MULTIPART_FORM_DATA : MediaType.TEXT_PLAIN);
20412063
}
20422064
}
20432065

@@ -2054,6 +2076,25 @@ protected String sendRequest(HttpSession session, HttpMethod method, String url,
20542076
// 设置分块传输
20552077
headers.set(HttpHeaders.TRANSFER_ENCODING, "chunked");
20562078
}
2079+
2080+
Set<Entry<String, String[]>> set = params == null || params.isEmpty() ? null : params.entrySet();
2081+
MultiValueMap result = set == null || set.isEmpty() ? null : new LinkedMultiValueMap<>(set.size());
2082+
if (result != null) {
2083+
for (Entry<String, String[]> ety : set) {
2084+
String[] vs = ety == null ? null : ety.getValue();
2085+
if (vs == null || vs.length <= 0) {
2086+
continue;
2087+
}
2088+
2089+
String k = ety.getKey();
2090+
for (var i = 0; i < vs.length; i ++) {
2091+
HttpHeaders hds = new HttpHeaders();
2092+
hds.setContentType(headers.getContentType());
2093+
HttpEntity<?> entity = new HttpEntity<>(vs[i], hds);
2094+
result.add(k, entity);
2095+
}
2096+
}
2097+
}
20572098

20582099
HttpEntity<String> requestEntity = new HttpEntity<>(method == HttpMethod.GET ? null : body, headers);
20592100
ResponseEntity<String> entity = CLIENT.exchange(url, method, requestEntity, String.class);
@@ -3346,51 +3387,51 @@ public String startApiTest(HttpSession session) {
33463387
//response.sendRedirect("http://localhost:3000/test/start");
33473388
long id = 100000 + Math.round(899999*Math.random());
33483389
DemoParser.KEY_MAP.put(String.valueOf(id), session); // 调这个接口一般是前端/CI/CD,调查询接口的是 Node,Session 不同 session.setAttribute("key", id);
3349-
return delegate("http://localhost:3000/test/start?key=" + id, null, null, null, null, null, null, HttpMethod.GET, session);
3390+
return delegate("http://localhost:3000/test/start?key=" + id, session);
33503391
}
33513392
@GetMapping("api/test/status")
33523393
public String getApiTestStatus(@RequestParam(value = "key", required = false) String key, HttpSession session) {
33533394
//response.sendRedirect("http://localhost:3000/test/status");
33543395
DemoParser.KEY_MAP.remove(key);
3355-
return delegate("http://localhost:3000/test/status", null, null, null, null, null, null, HttpMethod.GET, session);
3396+
return delegate("http://localhost:3000/test/status", session);
33563397
}
33573398

33583399
@GetMapping("unit/test/start")
33593400
public String startUnitTest(HttpSession session) {
33603401
long id = 100000 + Math.round(899999*Math.random());
33613402
DemoParser.KEY_MAP.put(String.valueOf(id), session); // 调这个接口一般是前端/CI/CD,调查询接口的是 Node,Session 不同 session.setAttribute("key", id);
3362-
return delegate("http://localhost:3001/test/start?key=" + id, null, null, null, null, null, null, HttpMethod.GET, session);
3403+
return delegate("http://localhost:3001/test/start?key=" + id, session);
33633404
}
33643405
@GetMapping("unit/test/status")
33653406
public String getUnitTestStatus(@RequestParam(value = "key", required = false) String key, HttpSession session) {
33663407
DemoParser.KEY_MAP.remove(key);
3367-
return delegate("http://localhost:3001/test/status", null, null, null, null, null, null, HttpMethod.GET, session);
3408+
return delegate("http://localhost:3001/test/status", session);
33683409
}
33693410

33703411
@GetMapping("sql/test/start")
33713412
public String startSQLTest(HttpSession session) {
33723413
long id = 100000 + Math.round(899999*Math.random());
33733414
DemoParser.KEY_MAP.put(String.valueOf(id), session); // 调这个接口一般是前端/CI/CD,调查询接口的是 Node,Session 不同 session.setAttribute("key", id);
3374-
return delegate("http://localhost:3002/test/start?key=" + id, null, null, null, null, null, null, HttpMethod.GET, session);
3415+
return delegate("http://localhost:3002/test/start?key=" + id, session);
33753416
}
33763417
@GetMapping("sql/test/status")
33773418
public String getSQLTestStatus(@RequestParam("key") String key, HttpSession session) {
33783419
DemoParser.KEY_MAP.remove(key);
3379-
return delegate("http://localhost:3002/test/status", null, null, null, null, null, null, HttpMethod.GET, session);
3420+
return delegate("http://localhost:3002/test/status", session);
33803421
}
33813422

33823423
@GetMapping("cv/test/start")
33833424
public String startCvTest(HttpSession session) {
33843425
//response.sendRedirect("http://localhost:3003/test/start");
33853426
long id = 100000 + Math.round(899999*Math.random());
33863427
DemoParser.KEY_MAP.put(String.valueOf(id), session); // 调这个接口一般是前端/CI/CD,调查询接口的是 Node,Session 不同 session.setAttribute("key", id);
3387-
return delegate("http://localhost:3003/test/start?key=" + id, null, null, null, null, null, null, HttpMethod.GET, session);
3428+
return delegate("http://localhost:3003/test/start?key=" + id, session);
33883429
}
33893430
@GetMapping("cv/test/status")
33903431
public String getCvTestStatus(@RequestParam(value = "key", required = false) String key, HttpSession session) {
33913432
//response.sendRedirect("http://localhost:3003/test/status");
33923433
DemoParser.KEY_MAP.remove(key);
3393-
return delegate("http://localhost:3003/test/status", null, null, null, null, null, null, HttpMethod.GET, session);
3434+
return delegate("http://localhost:3003/test/status", session);
33943435
}
33953436

33963437
// 为 APIAuto, UnitAuto, SQLAuto 提供的后台 Headless 无 UI 测试转发接口 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

0 commit comments

Comments
 (0)