diff --git a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/isv/IsvApi.java b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/isv/IsvApi.java index 9ffc9333..6502403d 100644 --- a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/isv/IsvApi.java +++ b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/isv/IsvApi.java @@ -211,7 +211,6 @@ public class IsvApi { @Api(name = "isv.keys.update") @ApiDocMethod(description = "修改isv") - @Transactional(rollbackFor = Exception.class) public void updateIsvKeys(IsvKeysFormUpdate param) { IsvKeys isvKeys = isvKeysMapper.getByColumn("app_key", param.getAppKey()); if (isvKeys == null) { diff --git a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/RouteApi.java b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/RouteApi.java index 6d6895f1..409731e2 100644 --- a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/RouteApi.java +++ b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/RouteApi.java @@ -11,7 +11,6 @@ import com.gitee.sop.adminserver.api.service.param.RoutePermissionParam; import com.gitee.sop.adminserver.api.service.param.RouteSearchParam; import com.gitee.sop.adminserver.api.service.param.RouteUpdateParam; import com.gitee.sop.adminserver.api.service.result.RouteVO; -import com.gitee.sop.adminserver.bean.GatewayRouteDefinition; import com.gitee.sop.adminserver.bean.RouteConfigDto; import com.gitee.sop.adminserver.common.BizException; import com.gitee.sop.adminserver.entity.ConfigRouteBase; @@ -100,7 +99,7 @@ public class RouteApi { @Api(name = "route.update") @ApiDocMethod(description = "修改路由") void updateRoute(RouteUpdateParam param) { - // TODO: 修改路由 + this.updateRouteConfig(param); } @Api(name = "route.del") @@ -110,32 +109,32 @@ public class RouteApi { } - private void updateRouteConfig(GatewayRouteDefinition routeDefinition) { + private void updateRouteConfig(RouteUpdateParam routeUpdateParam) { try { - String routeId = routeDefinition.getId(); + String routeId = routeUpdateParam.getId(); ConfigRouteBase configRouteBase = configRouteBaseMapper.getByColumn("route_id", routeId); boolean doSave = configRouteBase == null; if (doSave) { configRouteBase = new ConfigRouteBase(); configRouteBase.setRouteId(routeId); } - configRouteBase.setStatus((byte) routeDefinition.getStatus()); + configRouteBase.setStatus(routeUpdateParam.getStatus().byteValue()); int i = doSave ? configRouteBaseMapper.save(configRouteBase) : configRouteBaseMapper.update(configRouteBase); if (i > 0) { - this.sendMsg(routeDefinition); + this.sendMsg(configRouteBase); } } catch (Exception e) { log.error("发送msg失败", e); } } - private void sendMsg(GatewayRouteDefinition routeDefinition) { + private void sendMsg(ConfigRouteBase routeDefinition) { RouteConfigDto routeConfigDto = new RouteConfigDto(); - routeConfigDto.setRouteId(routeDefinition.getId()); - routeConfigDto.setStatus(routeDefinition.getStatus()); + routeConfigDto.setRouteId(routeDefinition.getRouteId()); + routeConfigDto.setStatus(routeDefinition.getStatus().intValue()); routeConfigService.sendRouteConfigMsg(routeConfigDto); } diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/ServiceRoutesLoader.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/ServiceRoutesLoader.java index 88dc0e96..421f802d 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/ServiceRoutesLoader.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/ServiceRoutesLoader.java @@ -83,7 +83,6 @@ public class ServiceRoutesLoader { String groupId = NacosConfigs.GROUP_ROUTE; List allInstances = namingService.getAllInstances(serviceName); if (CollectionUtils.isEmpty(allInstances)) { - log.info("{}服务下线,删除路由信息", serviceName); // 如果没有服务列表,则删除所有路由信息 baseRouteCache.remove(serviceName); configService.removeConfig(dataId, groupId); diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/util/RequestUtil.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/util/RequestUtil.java index 04315a71..169d4627 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/util/RequestUtil.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/util/RequestUtil.java @@ -2,6 +2,7 @@ package com.gitee.sop.gatewaycommon.util; import com.alibaba.fastjson.JSON; import com.gitee.sop.gatewaycommon.bean.SopConstants; +import com.netflix.zuul.http.HttpServletRequestWrapper; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; @@ -103,11 +104,14 @@ public class RequestUtil { * @return 返回参数键值对 */ public static Map convertRequestParamsToMap(HttpServletRequest request) { + if (request instanceof HttpServletRequestWrapper) { + request = ((HttpServletRequestWrapper) request).getRequest(); + } Map paramMap = request.getParameterMap(); if (paramMap == null || paramMap.isEmpty()) { return Collections.emptyMap(); } - Map retMap = new HashMap<>(paramMap.size()); + Map retMap = new HashMap<>(paramMap.size() * 2); Set> entrySet = paramMap.entrySet(); diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/param/ZuulParamBuilder.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/param/ZuulParamBuilder.java index d99cb846..032de9d8 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/param/ZuulParamBuilder.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/param/ZuulParamBuilder.java @@ -5,7 +5,6 @@ import com.gitee.sop.gatewaycommon.param.ApiParam; import com.gitee.sop.gatewaycommon.param.BaseParamBuilder; import com.gitee.sop.gatewaycommon.util.RequestUtil; import com.netflix.zuul.context.RequestContext; -import com.netflix.zuul.http.HttpServletRequestWrapper; import lombok.extern.slf4j.Slf4j; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.lang3.StringUtils; @@ -27,9 +26,6 @@ public class ZuulParamBuilder extends BaseParamBuilder { @Override public Map buildRequestParams(RequestContext ctx) { HttpServletRequest request = ctx.getRequest(); - if (request instanceof HttpServletRequestWrapper) { - request = ((HttpServletRequestWrapper) request).getRequest(); - } Map params; if (GET.equalsIgnoreCase(request.getMethod())) { params = RequestUtil.convertRequestParamsToMap(request); diff --git a/sop-common/sop-registry-api/src/main/java/com/gitee/sop/registryapi/bean/HttpTool.java b/sop-common/sop-registry-api/src/main/java/com/gitee/sop/registryapi/bean/HttpTool.java index ddaed44f..3b6d15dc 100644 --- a/sop-common/sop-registry-api/src/main/java/com/gitee/sop/registryapi/bean/HttpTool.java +++ b/sop-common/sop-registry-api/src/main/java/com/gitee/sop/registryapi/bean/HttpTool.java @@ -204,7 +204,7 @@ public class HttpTool { * @return * @throws IOException */ - public String requestFile(String url, Map form, Map header, List files) + public String requestFile(String url, Map form, Map header, List files) throws IOException { // 创建MultipartBody.Builder,用于添加请求的数据 MultipartBody.Builder bodyBuilder = new MultipartBody.Builder(); @@ -217,9 +217,8 @@ public class HttpTool { ); } - Set> entrySet = form.entrySet(); - for (Map.Entry entry : entrySet) { - bodyBuilder.addFormDataPart(entry.getKey(), entry.getValue()); + for (Map.Entry entry : form.entrySet()) { + bodyBuilder.addFormDataPart(entry.getKey(), String.valueOf(entry.getValue())); } RequestBody requestBody = bodyBuilder.build(); diff --git a/sop-test/src/main/java/com/gitee/sop/test/Client.java b/sop-test/src/main/java/com/gitee/sop/test/Client.java index 34e8b595..1a014ab9 100644 --- a/sop-test/src/main/java/com/gitee/sop/test/Client.java +++ b/sop-test/src/main/java/com/gitee/sop/test/Client.java @@ -5,11 +5,15 @@ import com.gitee.sop.test.alipay.AlipayApiException; import com.gitee.sop.test.alipay.AlipaySignature; import lombok.Data; +import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.HashMap; +import java.util.List; import java.util.Map; /** @@ -74,6 +78,7 @@ public class Client { /** * 发送请求 + * * @param requestBuilder 请求信息 * @return 返回结果 */ @@ -84,14 +89,21 @@ public class Client { Map form = requestInfo.getForm(); Map header = requestInfo.getHeader(); String requestUrl = requestInfo.getUrl() != null ? requestInfo.getUrl() : url; + List uploadFileList = requestBuilder.getUploadFileList(); String responseData = null; + // 发送请求 try { - // 发送请求 - if (httpMethod == HttpTool.HTTPMethod.POST && postJson) { - responseData = httpTool.requestJson(requestUrl, JSON.toJSONString(form), header); + // 如果有上传文件 + if (uploadFileList != null && uploadFileList.size() > 0) { + responseData = httpTool.requestFile(url, form, header, uploadFileList); } else { - responseData = httpTool.request(requestUrl, form, header, httpMethod); + if (httpMethod == HttpTool.HTTPMethod.POST && postJson) { + responseData = httpTool.requestJson(requestUrl, JSON.toJSONString(form), header); + } else { + responseData = httpTool.request(requestUrl, form, header, httpMethod); + } } + } catch (IOException e) { throw new RuntimeException(e); } @@ -120,6 +132,7 @@ public class Client { private Map header; private boolean ignoreSign; private boolean postJson; + private List uploadFileList; private Callback callback; /** @@ -210,6 +223,62 @@ public class Client { return this; } + /** + * 添加文件 + * + * @param paramName 表单名称 + * @param file 文件 + * @return 返回RequestBuilder + */ + public RequestBuilder addFile(String paramName, File file) { + try { + HttpTool.UploadFile uploadFile = new HttpTool.UploadFile(paramName, file); + getUploadFileList().add(uploadFile); + } catch (IOException e) { + throw new IllegalArgumentException("上传文件错误", e); + } + return this; + } + + /** + * 添加文件 + * + * @param paramName 表单名称 + * @param fileName 文件名称 + * @param fileInputStream 文件流 + * @return 返回RequestBuilder + */ + public RequestBuilder addFile(String paramName, String fileName, InputStream fileInputStream) { + try { + HttpTool.UploadFile uploadFile = new HttpTool.UploadFile(paramName, fileName, fileInputStream); + getUploadFileList().add(uploadFile); + } catch (IOException e) { + throw new IllegalArgumentException("上传文件错误", e); + } + return this; + } + + /** + * 添加文件 + * + * @param paramName 表单名称 + * @param fileName 文件名称 + * @param fileData 文件数据 + * @return 返回RequestBuilder + */ + public RequestBuilder addFile(String paramName, String fileName, byte[] fileData) { + HttpTool.UploadFile uploadFile = new HttpTool.UploadFile(paramName, fileName, fileData); + getUploadFileList().add(uploadFile); + return this; + } + + private List getUploadFileList() { + if (uploadFileList == null) { + uploadFileList = new ArrayList<>(16); + } + return uploadFileList; + } + /** * 设置请求成功处理 * diff --git a/sop-test/src/main/java/com/gitee/sop/test/HttpTool.java b/sop-test/src/main/java/com/gitee/sop/test/HttpTool.java index 39025dc0..862e0662 100644 --- a/sop-test/src/main/java/com/gitee/sop/test/HttpTool.java +++ b/sop-test/src/main/java/com/gitee/sop/test/HttpTool.java @@ -204,7 +204,7 @@ public class HttpTool { * @return * @throws IOException */ - public String requestFile(String url, Map form, Map header, List files) + public String requestFile(String url, Map form, Map header, List files) throws IOException { // 创建MultipartBody.Builder,用于添加请求的数据 MultipartBody.Builder bodyBuilder = new MultipartBody.Builder(); @@ -217,9 +217,8 @@ public class HttpTool { ); } - Set> entrySet = form.entrySet(); - for (Map.Entry entry : entrySet) { - bodyBuilder.addFormDataPart(entry.getKey(), entry.getValue()); + for (Map.Entry entry : form.entrySet()) { + bodyBuilder.addFormDataPart(entry.getKey(), String.valueOf(entry.getValue())); } RequestBody requestBody = bodyBuilder.build(); diff --git a/sop-test/src/main/resources/file1.txt b/sop-test/src/main/resources/file1.txt new file mode 100644 index 00000000..3b760675 --- /dev/null +++ b/sop-test/src/main/resources/file1.txt @@ -0,0 +1 @@ +file1 content,内容1 \ No newline at end of file diff --git a/sop-test/src/main/resources/file2.txt b/sop-test/src/main/resources/file2.txt new file mode 100644 index 00000000..fd7656f3 --- /dev/null +++ b/sop-test/src/main/resources/file2.txt @@ -0,0 +1 @@ +file2 content,内容2 \ No newline at end of file diff --git a/sop-test/src/test/java/com/gitee/sop/test/AllInOneTest.java b/sop-test/src/test/java/com/gitee/sop/test/AllInOneTest.java index d179e491..bb84bd02 100644 --- a/sop-test/src/test/java/com/gitee/sop/test/AllInOneTest.java +++ b/sop-test/src/test/java/com/gitee/sop/test/AllInOneTest.java @@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import org.junit.Assert; +import java.io.File; import java.util.HashMap; import java.util.Optional; import java.util.concurrent.CountDownLatch; @@ -142,6 +143,23 @@ public class AllInOneTest extends TestBase { client.execute(requestBuilder); } + /** + * 演示文件上传 + */ + public void testFile() { + String root = System.getProperty("user.dir"); + Client.RequestBuilder requestBuilder = new Client.RequestBuilder() + .method("demo.file.upload") + .version("1.0") + .bizContent(new BizContent().add("remark", "test file upload")) + // 添加文件 + .addFile("file1", new File(root + "/src/main/resources/file1.txt")) + .addFile("file2", new File(root + "/src/main/resources/file2.txt")) + ; + + client.execute(requestBuilder); + } + /** * 验证中文乱码问题 */ diff --git a/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/bean/HttpTool.java b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/bean/HttpTool.java index 07362f53..0a4e455d 100644 --- a/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/bean/HttpTool.java +++ b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/bean/HttpTool.java @@ -204,7 +204,7 @@ public class HttpTool { * @return * @throws IOException */ - public String requestFile(String url, Map form, Map header, List files) + public String requestFile(String url, Map form, Map header, List files) throws IOException { // 创建MultipartBody.Builder,用于添加请求的数据 MultipartBody.Builder bodyBuilder = new MultipartBody.Builder(); @@ -217,9 +217,8 @@ public class HttpTool { ); } - Set> entrySet = form.entrySet(); - for (Map.Entry entry : entrySet) { - bodyBuilder.addFormDataPart(entry.getKey(), entry.getValue()); + for (Map.Entry entry : form.entrySet()) { + bodyBuilder.addFormDataPart(entry.getKey(), String.valueOf(entry.getValue())); } RequestBody requestBody = bodyBuilder.build();