pull/3/head
tanghc 4 years ago
parent 294b58a7f6
commit 8e0677a86e
  1. 20
      sop-website/src/main/java/com/gitee/sop/websiteserver/manager/DocManagerImpl.java
  2. 83
      sop-website/src/main/java/com/gitee/sop/websiteserver/manager/EasyopenDocParser.java

@ -23,16 +23,14 @@ import java.util.function.Consumer;
public class DocManagerImpl implements DocManager {
// key:title
private Map<String, DocInfo> docDefinitionMap = new HashMap<>();
private static final Map<String, DocInfo> docDefinitionMap = new HashMap<>();
/**
* KEY:serviceId, value: md5
*/
private Map<String, String> serviceIdMd5Map = new HashMap<>();
private static final Map<String, String> serviceIdMd5Map = new HashMap<>();
private DocParser swaggerDocParser = new SwaggerDocParser();
private DocParser easyopenDocParser = new EasyopenDocParser();
private static final DocParser swaggerDocParser = new SwaggerDocParser();
@Override
public void addDocInfo(String serviceId, String docInfoJson, Consumer<DocInfo> callback) {
@ -43,22 +41,12 @@ public class DocManagerImpl implements DocManager {
}
serviceIdMd5Map.put(serviceId, newMd5);
JSONObject docRoot = JSON.parseObject(docInfoJson, Feature.OrderedField, Feature.DisableCircularReferenceDetect);
DocParser docParser = this.buildDocParser(docRoot);
DocInfo docInfo = docParser.parseJson(docRoot);
DocInfo docInfo = swaggerDocParser.parseJson(docRoot);
docInfo.setServiceId(serviceId);
docDefinitionMap.put(docInfo.getTitle(), docInfo);
callback.accept(docInfo);
}
protected DocParser buildDocParser(JSONObject rootDoc) {
Object easyopen = rootDoc.get("easyopen");
if (easyopen != null) {
return easyopenDocParser;
} else {
return swaggerDocParser;
}
}
@Override
public DocInfo getByTitle(String title) {
return docDefinitionMap.get(title);

@ -1,83 +0,0 @@
package com.gitee.sop.websiteserver.manager;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gitee.sop.websiteserver.bean.DocInfo;
import com.gitee.sop.websiteserver.bean.DocItem;
import com.gitee.sop.websiteserver.bean.DocModule;
import com.gitee.sop.websiteserver.bean.DocParameter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author tanghc
*/
public class EasyopenDocParser implements DocParser {
@Override
public DocInfo parseJson(JSONObject docRoot) {
String title = docRoot.getString("title");
List<DocItem> docItems = new ArrayList<>();
JSONArray apiModules = docRoot.getJSONArray("apiModules");
for (int i = 0; i < apiModules.size(); i++) {
JSONObject module = apiModules.getJSONObject(i);
JSONArray moduleItems = module.getJSONArray("moduleItems");
for (int k = 0; k < moduleItems.size(); k++) {
JSONObject docInfo = moduleItems.getJSONObject(k);
DocItem docItem = buildDocItem(docInfo);
docItem.setModule(module.getString("name"));
docItems.add(docItem);
}
}
List<DocModule> docModuleList = docItems.stream()
.collect(Collectors.groupingBy(DocItem::getModule))
.entrySet()
.stream()
.map(entry -> {
DocModule docModule = new DocModule();
docModule.setModule(entry.getKey());
docModule.setDocItems(entry.getValue());
return docModule;
})
.collect(Collectors.toList());
DocInfo docInfo = new DocInfo();
docInfo.setTitle(title);
docInfo.setDocModuleList(docModuleList);
return docInfo;
}
protected DocItem buildDocItem(JSONObject docInfo) {
DocItem docItem = new DocItem();
docItem.setName(docInfo.getString("name"));
docItem.setVersion(docInfo.getString("version"));
docItem.setSummary(docInfo.getString("description"));
docItem.setDescription(docInfo.getString("description"));
List<DocParameter> docParameterList = this.buildParameterList(docInfo, "paramDefinitions");
docItem.setRequestParameters(docParameterList);
List<DocParameter> responseParameterList = this.buildParameterList(docInfo, "resultDefinitions");
docItem.setResponseParameters(responseParameterList);
return docItem;
}
protected List<DocParameter> buildParameterList(JSONObject docInfo, String key) {
JSONArray params = docInfo.getJSONArray(key);
if (params == null) {
return Collections.emptyList();
}
List<DocParameter> docParameterList = new ArrayList<>();
for (int i = 0; i < params.size(); i++) {
JSONObject jsonObject = params.getJSONObject(i);
DocParameter docParameter = jsonObject.toJavaObject(DocParameter.class);
docParameter.setType(jsonObject.getString("dataType"));
docParameterList.add(docParameter);
}
return docParameterList;
}
}
Loading…
Cancel
Save