pull/1/head
tanghc 5 years ago
parent 9194482522
commit 26f7d2843e
  1. 1
      sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/isv/IsvApi.java
  2. 17
      sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/service/RouteApi.java
  3. 1
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/ServiceRoutesLoader.java
  4. 6
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/util/RequestUtil.java
  5. 4
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/param/ZuulParamBuilder.java
  6. 7
      sop-common/sop-registry-api/src/main/java/com/gitee/sop/registryapi/bean/HttpTool.java
  7. 77
      sop-test/src/main/java/com/gitee/sop/test/Client.java
  8. 7
      sop-test/src/main/java/com/gitee/sop/test/HttpTool.java
  9. 1
      sop-test/src/main/resources/file1.txt
  10. 1
      sop-test/src/main/resources/file2.txt
  11. 18
      sop-test/src/test/java/com/gitee/sop/test/AllInOneTest.java
  12. 7
      sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/bean/HttpTool.java

@ -211,7 +211,6 @@ public class IsvApi {
@Api(name = "isv.keys.update") @Api(name = "isv.keys.update")
@ApiDocMethod(description = "修改isv") @ApiDocMethod(description = "修改isv")
@Transactional(rollbackFor = Exception.class)
public void updateIsvKeys(IsvKeysFormUpdate param) { public void updateIsvKeys(IsvKeysFormUpdate param) {
IsvKeys isvKeys = isvKeysMapper.getByColumn("app_key", param.getAppKey()); IsvKeys isvKeys = isvKeysMapper.getByColumn("app_key", param.getAppKey());
if (isvKeys == null) { if (isvKeys == null) {

@ -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.RouteSearchParam;
import com.gitee.sop.adminserver.api.service.param.RouteUpdateParam; import com.gitee.sop.adminserver.api.service.param.RouteUpdateParam;
import com.gitee.sop.adminserver.api.service.result.RouteVO; 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.bean.RouteConfigDto;
import com.gitee.sop.adminserver.common.BizException; import com.gitee.sop.adminserver.common.BizException;
import com.gitee.sop.adminserver.entity.ConfigRouteBase; import com.gitee.sop.adminserver.entity.ConfigRouteBase;
@ -100,7 +99,7 @@ public class RouteApi {
@Api(name = "route.update") @Api(name = "route.update")
@ApiDocMethod(description = "修改路由") @ApiDocMethod(description = "修改路由")
void updateRoute(RouteUpdateParam param) { void updateRoute(RouteUpdateParam param) {
// TODO: 修改路由 this.updateRouteConfig(param);
} }
@Api(name = "route.del") @Api(name = "route.del")
@ -110,32 +109,32 @@ public class RouteApi {
} }
private void updateRouteConfig(GatewayRouteDefinition routeDefinition) { private void updateRouteConfig(RouteUpdateParam routeUpdateParam) {
try { try {
String routeId = routeDefinition.getId(); String routeId = routeUpdateParam.getId();
ConfigRouteBase configRouteBase = configRouteBaseMapper.getByColumn("route_id", routeId); ConfigRouteBase configRouteBase = configRouteBaseMapper.getByColumn("route_id", routeId);
boolean doSave = configRouteBase == null; boolean doSave = configRouteBase == null;
if (doSave) { if (doSave) {
configRouteBase = new ConfigRouteBase(); configRouteBase = new ConfigRouteBase();
configRouteBase.setRouteId(routeId); configRouteBase.setRouteId(routeId);
} }
configRouteBase.setStatus((byte) routeDefinition.getStatus()); configRouteBase.setStatus(routeUpdateParam.getStatus().byteValue());
int i = doSave ? configRouteBaseMapper.save(configRouteBase) int i = doSave ? configRouteBaseMapper.save(configRouteBase)
: configRouteBaseMapper.update(configRouteBase); : configRouteBaseMapper.update(configRouteBase);
if (i > 0) { if (i > 0) {
this.sendMsg(routeDefinition); this.sendMsg(configRouteBase);
} }
} catch (Exception e) { } catch (Exception e) {
log.error("发送msg失败", e); log.error("发送msg失败", e);
} }
} }
private void sendMsg(GatewayRouteDefinition routeDefinition) { private void sendMsg(ConfigRouteBase routeDefinition) {
RouteConfigDto routeConfigDto = new RouteConfigDto(); RouteConfigDto routeConfigDto = new RouteConfigDto();
routeConfigDto.setRouteId(routeDefinition.getId()); routeConfigDto.setRouteId(routeDefinition.getRouteId());
routeConfigDto.setStatus(routeDefinition.getStatus()); routeConfigDto.setStatus(routeDefinition.getStatus().intValue());
routeConfigService.sendRouteConfigMsg(routeConfigDto); routeConfigService.sendRouteConfigMsg(routeConfigDto);
} }

@ -83,7 +83,6 @@ public class ServiceRoutesLoader<T extends TargetRoute> {
String groupId = NacosConfigs.GROUP_ROUTE; String groupId = NacosConfigs.GROUP_ROUTE;
List<Instance> allInstances = namingService.getAllInstances(serviceName); List<Instance> allInstances = namingService.getAllInstances(serviceName);
if (CollectionUtils.isEmpty(allInstances)) { if (CollectionUtils.isEmpty(allInstances)) {
log.info("{}服务下线,删除路由信息", serviceName);
// 如果没有服务列表,则删除所有路由信息 // 如果没有服务列表,则删除所有路由信息
baseRouteCache.remove(serviceName); baseRouteCache.remove(serviceName);
configService.removeConfig(dataId, groupId); configService.removeConfig(dataId, groupId);

@ -2,6 +2,7 @@ package com.gitee.sop.gatewaycommon.util;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.gitee.sop.gatewaycommon.bean.SopConstants; import com.gitee.sop.gatewaycommon.bean.SopConstants;
import com.netflix.zuul.http.HttpServletRequestWrapper;
import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.fileupload.servlet.ServletFileUpload;
@ -103,11 +104,14 @@ public class RequestUtil {
* @return 返回参数键值对 * @return 返回参数键值对
*/ */
public static Map<String, String> convertRequestParamsToMap(HttpServletRequest request) { public static Map<String, String> convertRequestParamsToMap(HttpServletRequest request) {
if (request instanceof HttpServletRequestWrapper) {
request = ((HttpServletRequestWrapper) request).getRequest();
}
Map<String, String[]> paramMap = request.getParameterMap(); Map<String, String[]> paramMap = request.getParameterMap();
if (paramMap == null || paramMap.isEmpty()) { if (paramMap == null || paramMap.isEmpty()) {
return Collections.emptyMap(); return Collections.emptyMap();
} }
Map<String, String> retMap = new HashMap<>(paramMap.size()); Map<String, String> retMap = new HashMap<>(paramMap.size() * 2);
Set<Map.Entry<String, String[]>> entrySet = paramMap.entrySet(); Set<Map.Entry<String, String[]>> entrySet = paramMap.entrySet();

@ -5,7 +5,6 @@ import com.gitee.sop.gatewaycommon.param.ApiParam;
import com.gitee.sop.gatewaycommon.param.BaseParamBuilder; import com.gitee.sop.gatewaycommon.param.BaseParamBuilder;
import com.gitee.sop.gatewaycommon.util.RequestUtil; import com.gitee.sop.gatewaycommon.util.RequestUtil;
import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.http.HttpServletRequestWrapper;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -27,9 +26,6 @@ public class ZuulParamBuilder extends BaseParamBuilder<RequestContext> {
@Override @Override
public Map<String, ?> buildRequestParams(RequestContext ctx) { public Map<String, ?> buildRequestParams(RequestContext ctx) {
HttpServletRequest request = ctx.getRequest(); HttpServletRequest request = ctx.getRequest();
if (request instanceof HttpServletRequestWrapper) {
request = ((HttpServletRequestWrapper) request).getRequest();
}
Map<String, ?> params; Map<String, ?> params;
if (GET.equalsIgnoreCase(request.getMethod())) { if (GET.equalsIgnoreCase(request.getMethod())) {
params = RequestUtil.convertRequestParamsToMap(request); params = RequestUtil.convertRequestParamsToMap(request);

@ -204,7 +204,7 @@ public class HttpTool {
* @return * @return
* @throws IOException * @throws IOException
*/ */
public String requestFile(String url, Map<String, String> form, Map<String, String> header, List<UploadFile> files) public String requestFile(String url, Map<String, ?> form, Map<String, String> header, List<UploadFile> files)
throws IOException { throws IOException {
// 创建MultipartBody.Builder,用于添加请求的数据 // 创建MultipartBody.Builder,用于添加请求的数据
MultipartBody.Builder bodyBuilder = new MultipartBody.Builder(); MultipartBody.Builder bodyBuilder = new MultipartBody.Builder();
@ -217,9 +217,8 @@ public class HttpTool {
); );
} }
Set<Map.Entry<String, String>> entrySet = form.entrySet(); for (Map.Entry<String, ?> entry : form.entrySet()) {
for (Map.Entry<String, String> entry : entrySet) { bodyBuilder.addFormDataPart(entry.getKey(), String.valueOf(entry.getValue()));
bodyBuilder.addFormDataPart(entry.getKey(), entry.getValue());
} }
RequestBody requestBody = bodyBuilder.build(); RequestBody requestBody = bodyBuilder.build();

@ -5,11 +5,15 @@ import com.gitee.sop.test.alipay.AlipayApiException;
import com.gitee.sop.test.alipay.AlipaySignature; import com.gitee.sop.test.alipay.AlipaySignature;
import lombok.Data; import lombok.Data;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
@ -74,6 +78,7 @@ public class Client {
/** /**
* 发送请求 * 发送请求
*
* @param requestBuilder 请求信息 * @param requestBuilder 请求信息
* @return 返回结果 * @return 返回结果
*/ */
@ -84,14 +89,21 @@ public class Client {
Map<String, ?> form = requestInfo.getForm(); Map<String, ?> form = requestInfo.getForm();
Map<String, String> header = requestInfo.getHeader(); Map<String, String> header = requestInfo.getHeader();
String requestUrl = requestInfo.getUrl() != null ? requestInfo.getUrl() : url; String requestUrl = requestInfo.getUrl() != null ? requestInfo.getUrl() : url;
List<HttpTool.UploadFile> uploadFileList = requestBuilder.getUploadFileList();
String responseData = null; String responseData = null;
// 发送请求
try { try {
// 发送请求 // 如果有上传文件
if (httpMethod == HttpTool.HTTPMethod.POST && postJson) { if (uploadFileList != null && uploadFileList.size() > 0) {
responseData = httpTool.requestJson(requestUrl, JSON.toJSONString(form), header); responseData = httpTool.requestFile(url, form, header, uploadFileList);
} else { } 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) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -120,6 +132,7 @@ public class Client {
private Map<String, String> header; private Map<String, String> header;
private boolean ignoreSign; private boolean ignoreSign;
private boolean postJson; private boolean postJson;
private List<HttpTool.UploadFile> uploadFileList;
private Callback callback; private Callback callback;
/** /**
@ -210,6 +223,62 @@ public class Client {
return this; 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<HttpTool.UploadFile> getUploadFileList() {
if (uploadFileList == null) {
uploadFileList = new ArrayList<>(16);
}
return uploadFileList;
}
/** /**
* 设置请求成功处理 * 设置请求成功处理
* *

@ -204,7 +204,7 @@ public class HttpTool {
* @return * @return
* @throws IOException * @throws IOException
*/ */
public String requestFile(String url, Map<String, String> form, Map<String, String> header, List<UploadFile> files) public String requestFile(String url, Map<String, ?> form, Map<String, String> header, List<UploadFile> files)
throws IOException { throws IOException {
// 创建MultipartBody.Builder,用于添加请求的数据 // 创建MultipartBody.Builder,用于添加请求的数据
MultipartBody.Builder bodyBuilder = new MultipartBody.Builder(); MultipartBody.Builder bodyBuilder = new MultipartBody.Builder();
@ -217,9 +217,8 @@ public class HttpTool {
); );
} }
Set<Map.Entry<String, String>> entrySet = form.entrySet(); for (Map.Entry<String, ?> entry : form.entrySet()) {
for (Map.Entry<String, String> entry : entrySet) { bodyBuilder.addFormDataPart(entry.getKey(), String.valueOf(entry.getValue()));
bodyBuilder.addFormDataPart(entry.getKey(), entry.getValue());
} }
RequestBody requestBody = bodyBuilder.build(); RequestBody requestBody = bodyBuilder.build();

@ -0,0 +1 @@
file1 content,内容1

@ -0,0 +1 @@
file2 content,内容2

@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import org.junit.Assert; import org.junit.Assert;
import java.io.File;
import java.util.HashMap; import java.util.HashMap;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
@ -142,6 +143,23 @@ public class AllInOneTest extends TestBase {
client.execute(requestBuilder); 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);
}
/** /**
* 验证中文乱码问题 * 验证中文乱码问题
*/ */

@ -204,7 +204,7 @@ public class HttpTool {
* @return * @return
* @throws IOException * @throws IOException
*/ */
public String requestFile(String url, Map<String, String> form, Map<String, String> header, List<UploadFile> files) public String requestFile(String url, Map<String, ?> form, Map<String, String> header, List<UploadFile> files)
throws IOException { throws IOException {
// 创建MultipartBody.Builder,用于添加请求的数据 // 创建MultipartBody.Builder,用于添加请求的数据
MultipartBody.Builder bodyBuilder = new MultipartBody.Builder(); MultipartBody.Builder bodyBuilder = new MultipartBody.Builder();
@ -217,9 +217,8 @@ public class HttpTool {
); );
} }
Set<Map.Entry<String, String>> entrySet = form.entrySet(); for (Map.Entry<String, ?> entry : form.entrySet()) {
for (Map.Entry<String, String> entry : entrySet) { bodyBuilder.addFormDataPart(entry.getKey(), String.valueOf(entry.getValue()));
bodyBuilder.addFormDataPart(entry.getKey(), entry.getValue());
} }
RequestBody requestBody = bodyBuilder.build(); RequestBody requestBody = bodyBuilder.build();

Loading…
Cancel
Save