各种细节打磨优化,增加提示

This commit is contained in:
暮光:城中城
2019-02-02 21:17:44 +08:00
committed by zhanghongli
parent 15cfa7e2d1
commit e96418e346
13 changed files with 112 additions and 63 deletions

View File

@@ -26,7 +26,6 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
/**
@@ -126,9 +125,11 @@ public class MgDocumentController {
}
locationList.addAll(locationListStorage);
this.storageSwaggerLocationList(locationListStorage);
AtomicInteger idIndex = new AtomicInteger(1);
resourcesSet.forEach(val -> val.setId(idIndex.getAndIncrement()));
resourcesSet.forEach(val -> {
if (val.getId() == null) {
val.setId(storageService.getNextId());
}
});
storageService.put(StorageKeys.SWAGGER_RESOURCES_LIST, JSON.toJSONString(resourcesSet));
}
List<String> swaggerResourceStrList = new LinkedList<>();
@@ -316,15 +317,18 @@ public class MgDocumentController {
SwaggerResourcesInfoVo resourcesInfoVo = new SwaggerResourcesInfoVo(resourcesUrl, resourceList);
resourcesInfoVo.setRewriteDomainUrl(rewriteDomainUrl);
resourcesList.add(resourcesInfoVo);
AtomicInteger idIndex = new AtomicInteger(1);
resourcesList.forEach(val -> val.setId(idIndex.getAndIncrement()));
resourcesList.forEach(val -> {
if (val.getId() == null) {
val.setId(storageService.getNextId());
}
});
}
} catch (Exception e) {
logger.error("获取文档失败:{}{}", resourcesUrl, e.getMessage());
return DocResponseJson.warn("该地址查找文档失败");
}
// 去重
resourcesList = resourcesList.stream().distinct().collect(Collectors.toList());
resourcesList = resourcesList.stream().distinct().sorted(Comparator.comparing(SwaggerResourcesInfoVo::getId)).collect(Collectors.toList());
storageService.put(StorageKeys.SWAGGER_RESOURCES_LIST, JSON.toJSONString(resourcesList));
return DocResponseJson.ok();
}
@@ -366,20 +370,16 @@ public class MgDocumentController {
*
* @author 暮光:城中城
* @since 2018年8月21日
* @param docUrl 文档地址
* @param location 文档地址
* @return 删除结果
*/
@Deprecated
@PostMapping(value = "/deleteSwaggerDoc")
public ResponseJson<Object> deleteSwaggerDoc(String docUrl) {
// String swaggerDocsDeleteStr = storageService.get(StorageKeys.SWAGGER_DOCS_DELETE_LIST);
// Set<String> swaggerDocsDeleteSet = new HashSet<>();
// if (StringUtils.isNotBlank(swaggerDocsDeleteStr)) {
// List<String> swaggerDocsDeleteList = JSON.parseArray(swaggerDocsDeleteStr, String.class);
// swaggerDocsDeleteSet.addAll(swaggerDocsDeleteList);
// }
// swaggerDocsDeleteSet.add(docUrl);
// storageService.put(StorageKeys.SWAGGER_DOCS_DELETE_LIST, JSON.toJSONString(swaggerDocsDeleteSet));
public ResponseJson<Object> deleteSwaggerDoc(String location) {
List<LocationListVo> locationList = this.getLocationSet();
String locationDel = this.encodeUrlParam(location);
locationList = locationList.stream().filter(val -> !Objects.equals(val.getLocation(), locationDel)).collect(Collectors.toList());
this.storageSwaggerLocationList(locationList);
return DocResponseJson.ok();
}
@@ -405,15 +405,16 @@ public class MgDocumentController {
List<LocationListVo> locationList = this.getLocationSet();
// 组装新的对象
LocationListVo locationListVo = new LocationListVo(locationUrl, "");
locationListVo.setRewriteDomainUrl(rewriteDomainUrl);
locationListVo.setOpenVisit(openVisit);
// 如果旧的不为空使用旧的uuid
for (LocationListVo location : locationList) {
if (Objects.equals(location.getLocation(), oldUrl) && StringUtils.isNotBlank(location.getUuid())) {
locationListVo.setUuid(location.getUuid());
locationListVo = location;
break;
}
}
locationListVo.setLocation(locationUrl);
locationListVo.setRewriteDomainUrl(rewriteDomainUrl);
locationListVo.setOpenVisit(openVisit);
// 去除旧的,加入新的
locationList = locationList.stream().filter(val -> !Objects.equals(val.getLocation(), oldUrl)).collect(Collectors.toList());
locationList.add(locationListVo);
@@ -431,14 +432,18 @@ public class MgDocumentController {
*/
private void addSwaggerLocationList(List<SwaggerResource> resourceList, String resourcesUrl, String rewriteDomainUrl, Integer openVisit) {
List<LocationListVo> locationList = this.getLocationSet();
Map<String, LocationListVo> locationListVoMap = locationList.stream().collect(Collectors.toMap(LocationListVo::getLocation, val -> val));
// 加入到location列表
String resourcesDomain = resourcesUrl.substring(0, resourcesUrl.lastIndexOf("/") + 1);
for (SwaggerResource swaggerResource : resourceList) {
String location = this.getLocationUrl(resourcesDomain, swaggerResource.getLocation(), swaggerResource.getName());
LocationListVo locationListVo = new LocationListVo(location, resourcesUrl);
LocationListVo locationListVo = locationListVoMap.get(location);
if (locationListVo == null) {
locationListVo = new LocationListVo(location, resourcesUrl);
locationList.add(locationListVo);
}
locationListVo.setRewriteDomainUrl(rewriteDomainUrl);
locationListVo.setOpenVisit(openVisit);
locationList.add(locationListVo);
}
this.storageSwaggerLocationList(locationList);
}
@@ -447,9 +452,13 @@ public class MgDocumentController {
* 保存location列表
*/
private void storageSwaggerLocationList(List<LocationListVo> locationSet) {
AtomicInteger idIndex = new AtomicInteger(1);
locationSet = locationSet.stream().distinct().collect(Collectors.toList());
locationSet.forEach(val -> val.setId(idIndex.getAndIncrement()));
locationSet.forEach(val -> {
if (val.getId() == null) {
val.setId(storageService.getNextId());
}
});
locationSet = locationSet.stream().sorted(Comparator.comparing(LocationListVo::getId)).collect(Collectors.toList());
storageService.put(StorageKeys.SWAGGER_LOCATION_LIST, JSON.toJSONString(locationSet));
}

View File

@@ -42,7 +42,7 @@ public class MgHttpRequestController {
}
paramUrl = paramUrl.replace("http://", "").replace("https://", "");
String regexStr = paramUrl.substring(0, paramUrl.indexOf("/"));
long inWhiteList = whiteDomain.stream().filter(val -> regexStr.matches(val)).count();
long inWhiteList = whiteDomain.stream().filter(regexStr::matches).count();
if (inWhiteList <= 0) {
return DocResponseJson.warn("该域名不在白名单内,不能代理请求");
}

View File

@@ -25,15 +25,12 @@ public class LocationListVo {
this.uuid = RandomUtil.simpleUUID();
// 修正名字
int indexGroup = location.indexOf("group=");
int indexV2 = location.indexOf("/v2");
if (indexGroup >= 0 && location.length() > indexGroup) {
try {
this.name = URLDecoder.decode(location.substring(indexGroup + 6), "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
} else if (indexV2 >= 0) {
this.name = location.substring(indexV2 + 3);
}
}

View File

@@ -2,7 +2,7 @@ package com.zyplayer.doc.swagger.framework.constant;
/**
* 存储数据的KEY常量类
*
*
* @author 暮光:城中城
* @since 2018年8月21日
*/
@@ -15,4 +15,6 @@ public class StorageKeys {
public static final String SWAGGER_LOCATION_LIST = "swagger-location-list";
// 文档的离线数据key
public static final String SWAGGER_OFFLINE_DOC_START = "swagger-offline-doc-";
}
// 自增ID的key
public static final String SWAGGER_ID_WORKER = "swagger-id-worker";
}

View File

@@ -1,5 +1,8 @@
package com.zyplayer.doc.swagger.framework.service;
import com.zyplayer.doc.swagger.framework.constant.StorageKeys;
import org.apache.commons.lang.math.NumberUtils;
import java.util.List;
/**
@@ -52,4 +55,17 @@ public interface MgStorageService {
*/
List<String> getProxyRequestWhiteDomain();
/**
* 获取一个自增的ID
* @author 暮光:城中城
* @since 2019年1月27日
*/
default Integer getNextId() {
synchronized (StorageKeys.SWAGGER_ID_WORKER) {
String idWorker = this.get(StorageKeys.SWAGGER_ID_WORKER);
Integer nextId = NumberUtils.toInt(idWorker, 1);
this.put(StorageKeys.SWAGGER_ID_WORKER, String.valueOf(nextId + 1));
return nextId;
}
}
}