|
@@ -1,13 +1,24 @@
|
|
|
package org.ssssssss.magicapi.controller;
|
|
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.core.ParameterizedTypeReference;
|
|
|
+import org.springframework.http.HttpEntity;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
+import org.springframework.http.HttpMethod;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
|
import org.ssssssss.magicapi.config.MagicConfiguration;
|
|
|
+import org.ssssssss.magicapi.interceptor.RequestInterceptor;
|
|
|
import org.ssssssss.magicapi.logging.MagicLoggerContext;
|
|
|
import org.ssssssss.magicapi.model.JsonBean;
|
|
|
import org.ssssssss.magicapi.model.Options;
|
|
|
+import org.ssssssss.magicapi.model.SynchronizeRequest;
|
|
|
+import org.ssssssss.magicapi.model.SynchronizeResponse;
|
|
|
+import org.ssssssss.magicapi.provider.ApiServiceProvider;
|
|
|
+import org.ssssssss.magicapi.provider.GroupServiceProvider;
|
|
|
import org.ssssssss.magicapi.utils.MD5Utils;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
@@ -19,8 +30,16 @@ import java.util.stream.Stream;
|
|
|
|
|
|
public class MagicWorkbenchController extends MagicController {
|
|
|
|
|
|
+ private RestTemplate restTemplate = new RestTemplate();
|
|
|
+
|
|
|
+ private ApiServiceProvider agiApiService;
|
|
|
+
|
|
|
+ private GroupServiceProvider groupService;
|
|
|
+
|
|
|
public MagicWorkbenchController(MagicConfiguration configuration) {
|
|
|
super(configuration);
|
|
|
+ this.agiApiService = configuration.getMagicApiService();
|
|
|
+ this.groupService = configuration.getGroupServiceProvider();
|
|
|
}
|
|
|
|
|
|
|
|
@@ -56,4 +75,62 @@ public class MagicWorkbenchController extends MagicController {
|
|
|
public JsonBean<List<Map<String, String>>> options() {
|
|
|
return new JsonBean<>(Stream.of(Options.values()).map(item -> Collections.singletonMap(item.getValue(), item.getName())).collect(Collectors.toList()));
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ private String validateRequest(SynchronizeRequest request) {
|
|
|
+ if (StringUtils.isBlank(request.getMode())) {
|
|
|
+ return "请求参数有误";
|
|
|
+ }
|
|
|
+ if (request.getMode().equals("1") && StringUtils.isBlank(request.getGroupId())) {
|
|
|
+ return "分组id不能为空";
|
|
|
+ }
|
|
|
+ if (request.getMode().equals("2") && (StringUtils.isBlank(request.getApiId()) || StringUtils.isBlank(request.getFunctionId()))) {
|
|
|
+ return "函数id或接口id不能同时为空";
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping("/synchronize")
|
|
|
+ @ResponseBody
|
|
|
+ public JsonBean<SynchronizeResponse> synchronize(SynchronizeRequest synchronizeRequest, HttpServletRequest request) {
|
|
|
+ if (!allowVisit(request, RequestInterceptor.Authorization.SYNC)) {
|
|
|
+ return new JsonBean<>(-10, "无权限执行同步方法");
|
|
|
+ }
|
|
|
+ String message = validateRequest(synchronizeRequest);
|
|
|
+ if (message == null) {
|
|
|
+ List<SynchronizeRequest.Info> infos = agiApiService.listForSync(null, synchronizeRequest.getApiId());
|
|
|
+ infos.forEach(it -> it.setGroupPath(groupService.getFullPath(it.getGroupId())));
|
|
|
+ synchronizeRequest.setInfos(infos);
|
|
|
+ return request(synchronizeRequest);
|
|
|
+ }
|
|
|
+ return new JsonBean<>(0, message);
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping("/synchronize/pull")
|
|
|
+ @ResponseBody
|
|
|
+ public JsonBean<Void> pull(SynchronizeRequest synchronizeRequest, HttpServletRequest request) {
|
|
|
+ if (!allowVisit(request, RequestInterceptor.Authorization.PULL)) {
|
|
|
+ return new JsonBean<>(-10, "无权限执行拉取方法");
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping("/synchronize/push")
|
|
|
+ @ResponseBody
|
|
|
+ public JsonBean<Void> push(SynchronizeRequest synchronizeRequest, HttpServletRequest request) {
|
|
|
+ if (!allowVisit(request, RequestInterceptor.Authorization.PUSH)) {
|
|
|
+ return new JsonBean<>(-10, "无权限执行推送方法");
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private JsonBean<SynchronizeResponse> request(SynchronizeRequest request) {
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
|
|
|
+ String requestURL = String.format("%s/_synchronize?secret=%s", request.getRemote(), request.getSecret());
|
|
|
+ HttpEntity<Object> entity = new HttpEntity<>(request, headers);
|
|
|
+ return restTemplate.exchange(requestURL, HttpMethod.POST, entity, new ParameterizedTypeReference<JsonBean<SynchronizeResponse>>() {
|
|
|
+ }).getBody();
|
|
|
+ }
|
|
|
+
|
|
|
}
|