From da172a9b39d0ca5777c91dde740eff0710429cd6 Mon Sep 17 00:00:00 2001 From: tanghc Date: Thu, 8 Aug 2019 15:18:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gitee/sop/servercommon/util/OpenUtil.java | 6 +- .../storyweb/controller/AlipayController.java | 30 ++--- .../sop/gateway/config/PreParamFilter.java | 116 ++++++++++++++++++ 3 files changed, 132 insertions(+), 20 deletions(-) create mode 100644 sop-gateway/src/main/java/com/gitee/sop/gateway/config/PreParamFilter.java diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/util/OpenUtil.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/util/OpenUtil.java index 61c1d527..a99813cd 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/util/OpenUtil.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/util/OpenUtil.java @@ -67,12 +67,8 @@ public class OpenUtil { for (Map.Entry entry : entrySet) { String name = entry.getKey(); String[] values = entry.getValue(); - if (values.length == 1) { + if (values.length >= 1) { retMap.put(name, values[0]); - } else if (values.length > 1) { - retMap.put(name, values); - } else { - retMap.put(name, ""); } } diff --git a/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/AlipayController.java b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/AlipayController.java index 563c1721..05233fed 100644 --- a/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/AlipayController.java +++ b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/AlipayController.java @@ -127,13 +127,26 @@ public class AlipayController { // http://localhost:2222/alipay.story.get/ @ApiMapping(value = "alipay.story.get") - public Story getStory() { + public Story getStory(Story param) { Story story = new Story(); story.setId(1); - story.setName("海底小纵队(alipay.story.get1.0)"); + story.setName("海底小纵队(alipay.story.get1.0), param:" + param); return story; } + /** + * + * @param param 对应biz_content中的内容,并自动JSR-303校验 + * @return + */ + @ApiMapping(value = "alipay.story.get", version = "1.2") + public Story getStory11(Story param) { + Story story2 = new Story(); + story2.setId(1); + story2.setName("海底小纵队(alipay.story.get1.2), param:" + param); + return story2; + } + /** * 参数绑定 * @param story 对应biz_content中的内容,并自动JSR-303校验 @@ -153,19 +166,6 @@ public class AlipayController { return storyVO; } - /** - * - * @param story 对应biz_content中的内容,并自动JSR-303校验 - * @return - */ - @ApiMapping(value = "alipay.story.get", version = "1.2") - public Story getStory11(Story story) { - Story story2 = new Story(); - story2.setId(1); - story2.setName("海底小纵队(alipay.story.get1.2)"); - return story2; - } - /** * 演示文档表格树 * @param story diff --git a/sop-gateway/src/main/java/com/gitee/sop/gateway/config/PreParamFilter.java b/sop-gateway/src/main/java/com/gitee/sop/gateway/config/PreParamFilter.java new file mode 100644 index 00000000..74b78789 --- /dev/null +++ b/sop-gateway/src/main/java/com/gitee/sop/gateway/config/PreParamFilter.java @@ -0,0 +1,116 @@ +package com.gitee.sop.gateway.config; + +import com.alibaba.fastjson.JSONObject; +import com.gitee.sop.gatewaycommon.param.ApiParam; +import com.gitee.sop.gatewaycommon.param.ParamNames; +import com.gitee.sop.gatewaycommon.zuul.ZuulContext; +import com.gitee.sop.gatewaycommon.zuul.filter.BaseZuulFilter; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.netflix.zuul.context.RequestContext; +import com.netflix.zuul.exception.ZuulException; +import com.netflix.zuul.http.HttpServletRequestWrapper; +import com.netflix.zuul.http.ServletInputStreamWrapper; +import org.apache.commons.lang.StringUtils; +import org.springframework.http.HttpMethod; + +import javax.servlet.ServletInputStream; +import javax.servlet.http.HttpServletRequest; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +/** + * @author tanghc + */ +public class PreParamFilter extends BaseZuulFilter { + @Override + protected FilterType getFilterType() { + return FilterType.PRE; + } + + @Override + protected int getFilterOrder() { + return 0; + } + + @Override + protected Object doRun(RequestContext requestContext) throws ZuulException { + ApiParam apiParam = ZuulContext.getApiParam(); + JSONObject bizContent = apiParam.getJSONObject(ParamNames.BIZ_CONTENT_NAME); + // 修改biz_content中的参数 + bizContent.put("name", "修改了111"); + apiParam.put(ParamNames.BIZ_CONTENT_NAME, bizContent.toJSONString()); + + HttpServletRequest request = requestContext.getRequest(); + String contentType = request.getContentType(); + if (StringUtils.containsIgnoreCase(contentType, "json")) { + byte[] bytes = apiParam.toJSONString().getBytes(StandardCharsets.UTF_8); + requestContext.setRequest(new HttpServletRequestWrapper(requestContext.getRequest()) { + @Override + public ServletInputStream getInputStream() throws IOException { + return new ServletInputStreamWrapper(bytes); + } + + @Override + public byte[] getContentData() { + return bytes; + } + + @Override + public int getContentLength() { + return bytes.length; + } + + @Override + public long getContentLengthLong() { + return bytes.length; + } + }); + } else if(StringUtils.containsIgnoreCase(contentType, "form")) { + List list = Lists.newArrayList(); + try { + for (Map.Entry entry : apiParam.entrySet()) { + list.add(entry.getKey() + "=" + URLEncoder.encode(String.valueOf(entry.getValue()), "UTF-8")); + } + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + String paramsStr = StringUtils.join(list, "&"); + byte[] bytes = paramsStr.getBytes(StandardCharsets.UTF_8); + requestContext.setRequest(new HttpServletRequestWrapper(requestContext.getRequest()) { + @Override + public ServletInputStream getInputStream() throws IOException { + return new ServletInputStreamWrapper(bytes); + } + + @Override + public byte[] getContentData() { + return bytes; + } + + @Override + public int getContentLength() { + return bytes.length; + } + + @Override + public long getContentLengthLong() { + return bytes.length; + } + }); + } else if(HttpMethod.GET.name().equalsIgnoreCase(request.getMethod())) { + Map> newParams = Maps.newHashMap(); + for (Map.Entry entry : apiParam.entrySet()) { + newParams.put(entry.getKey(), Collections.singletonList(String.valueOf(entry.getValue()))); + } + requestContext.setRequestQueryParams(newParams); + } + + return null; + } +}