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

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

Loading…
Cancel
Save