1.x 1.15.2
tanghc 5 years ago
parent 59bca11ed6
commit efc241adac
  1. 12
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/ServerWebExchangeUtil.java
  2. 23
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/common/SopServerHttpRequestDecorator.java

@ -168,28 +168,26 @@ public class ServerWebExchangeUtil {
, Consumer<HttpHeaders> headerConsumer
) {
ServerHttpRequest serverHttpRequest = exchange.getRequest();
HttpHeaders newHeaders = HttpHeaders.writableHttpHeaders(serverHttpRequest.getHeaders());
// 新的request
ServerHttpRequest newRequest;
if (serverHttpRequest.getMethod() == HttpMethod.GET) {
paramsConsumer.accept(apiParam);
if (serverHttpRequest.getMethod() == HttpMethod.GET) {
// 新的查询参数
String queryString = RequestUtil.convertMapToQueryString(apiParam);
// 创建一个新的request,并使用新的uri
newRequest = new SopServerHttpRequestDecorator(serverHttpRequest, newHeaders, queryString);
newRequest = new SopServerHttpRequestDecorator(serverHttpRequest, queryString);
} else {
MediaType mediaType = serverHttpRequest.getHeaders().getContentType();
if (mediaType == null) {
mediaType = MediaType.APPLICATION_FORM_URLENCODED;
}
paramsConsumer.accept(apiParam);
String contentType = mediaType.toString().toLowerCase();
// 修改后的请求体
// 处理json请求(application/json)
if (StringUtils.containsAny(contentType, "json", "text")) {
String bodyStr = JSON.toJSONString(apiParam);
byte[] bodyBytes = bodyStr.getBytes(StandardCharsets.UTF_8);
newRequest = new SopServerHttpRequestDecorator(serverHttpRequest, newHeaders, bodyBytes);
newRequest = new SopServerHttpRequestDecorator(serverHttpRequest, bodyBytes);
} else if (StringUtils.contains(contentType, "multipart")) {
// 处理文件上传请求
FormHttpOutputMessage outputMessage = new FormHttpOutputMessage();
@ -214,7 +212,7 @@ public class ServerWebExchangeUtil {
formHttpMessageConverter.write(builder, mediaType, outputMessage);
// 获取新的上传文件流
byte[] bodyBytes = outputMessage.getInput();
newRequest = new SopServerHttpRequestDecorator(serverHttpRequest, newHeaders, bodyBytes);
newRequest = new SopServerHttpRequestDecorator(serverHttpRequest, bodyBytes);
// 必须要重新指定content-type,因为此时的boundary已经发生改变
MediaType contentTypeMultipart = outputMessage.getHeaders().getContentType();
newRequest.getHeaders().setContentType(contentTypeMultipart);
@ -226,7 +224,7 @@ public class ServerWebExchangeUtil {
// 否则一律按表单请求处理
String bodyStr = RequestUtil.convertMapToQueryString(apiParam);
byte[] bodyBytes = bodyStr.getBytes(StandardCharsets.UTF_8);
newRequest = new SopServerHttpRequestDecorator(serverHttpRequest, newHeaders, bodyBytes);
newRequest = new SopServerHttpRequestDecorator(serverHttpRequest, bodyBytes);
}
}
HttpHeaders headers = newRequest.getHeaders();

@ -23,10 +23,11 @@ public class SopServerHttpRequestDecorator extends ServerHttpRequestDecorator {
/**
* ServerHttpRequest包装作用类似于HttpServletRequestWrapper
*
* @param delegate 老的request
* @param queryString get请求后面的参数
*/
public SopServerHttpRequestDecorator(ServerHttpRequest delegate, HttpHeaders newHeaders, String queryString) {
public SopServerHttpRequestDecorator(ServerHttpRequest delegate, String queryString) {
super(delegate);
if (delegate.getMethod() != HttpMethod.GET) {
throw new IllegalArgumentException("this constructor must be used by GET request.");
@ -34,11 +35,8 @@ public class SopServerHttpRequestDecorator extends ServerHttpRequestDecorator {
if (queryString == null) {
throw new IllegalArgumentException("queryString can not be null.");
}
if (newHeaders == null) {
throw new IllegalArgumentException("newHeaders can not be null.");
}
this.httpHeaders = newHeaders;
// 默认header是只读的,把它改成可写,方便后面的过滤器使用
this.httpHeaders = HttpHeaders.writableHttpHeaders(delegate.getHeaders());
this.uri = UriComponentsBuilder.fromUri(delegate.getURI())
.replaceQuery(queryString)
.build(true)
@ -46,23 +44,19 @@ public class SopServerHttpRequestDecorator extends ServerHttpRequestDecorator {
}
/**
* ServerHttpRequest包装作用类似于HttpServletRequestWrapper
*
* @param delegate 老的request
* @param newHeaders 新的headers
* @param bodyData 请求体内容
*/
public SopServerHttpRequestDecorator(ServerHttpRequest delegate, HttpHeaders newHeaders, byte[] bodyData) {
// 将请求体再次封装写回到request里,传到下一级,否则,由于请求体已被消费,后续的服务将取不到值
public SopServerHttpRequestDecorator(ServerHttpRequest delegate, byte[] bodyData) {
super(delegate);
if (bodyData == null) {
throw new IllegalArgumentException("bodyData can not be null.");
}
if (newHeaders == null) {
throw new IllegalArgumentException("newHeaders can not be null.");
}
this.httpHeaders = newHeaders;
// 默认header是只读的,把它改成可写,方便后面的过滤器使用
this.httpHeaders = HttpHeaders.writableHttpHeaders(delegate.getHeaders());
// 由于请求体已改变,这里要重新设置contentLength
int contentLength = bodyData.length;
httpHeaders.setContentLength(contentLength);
@ -75,6 +69,7 @@ public class SopServerHttpRequestDecorator extends ServerHttpRequestDecorator {
/**
* 字符串转DataBuffer
*
* @param bytes 请求体
* @return 返回buffer
*/

Loading…
Cancel
Save