xm
2024-06-14 722af26bc6fec32bb289b1df51a9016a4935610f
提交 | 用户 | 时间
722af2 1 package com.xxl.job.admin.controller;
X 2
3 import com.xxl.job.admin.controller.annotation.PermissionLimit;
4 import com.xxl.job.admin.core.conf.XxlJobAdminConfig;
5 import com.xxl.job.core.biz.AdminBiz;
6 import com.xxl.job.core.biz.model.HandleCallbackParam;
7 import com.xxl.job.core.biz.model.RegistryParam;
8 import com.xxl.job.core.biz.model.ReturnT;
9 import com.xxl.job.core.util.GsonTool;
10 import com.xxl.job.core.util.XxlJobRemotingUtil;
11 import org.springframework.stereotype.Controller;
12 import org.springframework.web.bind.annotation.PathVariable;
13 import org.springframework.web.bind.annotation.RequestBody;
14 import org.springframework.web.bind.annotation.RequestMapping;
15 import org.springframework.web.bind.annotation.ResponseBody;
16
17 import javax.annotation.Resource;
18 import javax.servlet.http.HttpServletRequest;
19 import java.util.List;
20
21 /**
22  * Created by xuxueli on 17/5/10.
23  */
24 @Controller
25 @RequestMapping("/api")
26 public class JobApiController {
27
28     @Resource
29     private AdminBiz adminBiz;
30
31     /**
32      * api
33      *
34      * @param uri
35      * @param data
36      * @return
37      */
38     @RequestMapping("/{uri}")
39     @ResponseBody
40     @PermissionLimit(limit = false)
41     public ReturnT<String> api(HttpServletRequest request, @PathVariable("uri") String uri, @RequestBody(required = false) String data) {
42
43         // valid
44         if (!"POST".equalsIgnoreCase(request.getMethod())) {
45             return new ReturnT<String>(ReturnT.FAIL_CODE, "invalid request, HttpMethod not support.");
46         }
47         if (uri == null || uri.trim().length() == 0) {
48             return new ReturnT<String>(ReturnT.FAIL_CODE, "invalid request, uri-mapping empty.");
49         }
50         if (XxlJobAdminConfig.getAdminConfig().getAccessToken() != null
51             && XxlJobAdminConfig.getAdminConfig().getAccessToken().trim().length() > 0
52             && !XxlJobAdminConfig.getAdminConfig().getAccessToken().equals(request.getHeader(XxlJobRemotingUtil.XXL_JOB_ACCESS_TOKEN))) {
53             return new ReturnT<String>(ReturnT.FAIL_CODE, "The access token is wrong.");
54         }
55
56         // services mapping
57         if ("callback".equals(uri)) {
58             List<HandleCallbackParam> callbackParamList = GsonTool.fromJson(data, List.class, HandleCallbackParam.class);
59             return adminBiz.callback(callbackParamList);
60         } else if ("registry".equals(uri)) {
61             RegistryParam registryParam = GsonTool.fromJson(data, RegistryParam.class);
62             return adminBiz.registry(registryParam);
63         } else if ("registryRemove".equals(uri)) {
64             RegistryParam registryParam = GsonTool.fromJson(data, RegistryParam.class);
65             return adminBiz.registryRemove(registryParam);
66         } else {
67             return new ReturnT<String>(ReturnT.FAIL_CODE, "invalid request, uri-mapping(" + uri + ") not found.");
68         }
69
70     }
71
72 }