-
Notifications
You must be signed in to change notification settings - Fork 374
/
Copy pathConfController.java
253 lines (211 loc) · 6.93 KB
/
ConfController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package com.xxl.conf.admin.controller;
import com.xxl.conf.admin.controller.annotation.PermessionLimit;
import com.xxl.conf.admin.core.model.XxlConfNode;
import com.xxl.conf.admin.core.model.XxlConfProject;
import com.xxl.conf.admin.core.model.XxlConfUser;
import com.xxl.conf.admin.core.util.JacksonUtil;
import com.xxl.conf.admin.core.util.ReturnT;
import com.xxl.conf.admin.dao.XxlConfProjectDao;
import com.xxl.conf.admin.service.IXxlConfNodeService;
import com.xxl.conf.admin.service.impl.LoginService;
import com.xxl.conf.core.model.XxlConfParamVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.async.DeferredResult;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import static com.xxl.conf.admin.controller.interceptor.EnvInterceptor.CURRENT_ENV;
/**
* 配置管理
*
* @author xuxueli
*/
@Controller
@RequestMapping("/conf")
public class ConfController {
@Resource
private XxlConfProjectDao xxlConfProjectDao;
@Resource
private IXxlConfNodeService xxlConfNodeService;
@RequestMapping("")
public String index(HttpServletRequest request, Model model, String appname){
List<XxlConfProject> list = xxlConfProjectDao.findAll();
if (list==null || list.size()==0) {
throw new RuntimeException("系统异常,无可用项目");
}
XxlConfProject project = list.get(0);
for (XxlConfProject item: list) {
if (item.getAppname().equals(appname)) {
project = item;
}
}
boolean ifHasProjectPermission = xxlConfNodeService.ifHasProjectPermission(
(XxlConfUser) request.getAttribute(LoginService.LOGIN_IDENTITY),
(String) request.getAttribute(CURRENT_ENV),
project.getAppname());
model.addAttribute("ProjectList", list);
model.addAttribute("project", project);
model.addAttribute("ifHasProjectPermission", ifHasProjectPermission);
return "conf/conf.index";
}
@RequestMapping("/pageList")
@ResponseBody
public Map<String, Object> pageList(HttpServletRequest request,
@RequestParam(required = false, defaultValue = "0") int start,
@RequestParam(required = false, defaultValue = "10") int length,
String appname,
String key) {
XxlConfUser xxlConfUser = (XxlConfUser) request.getAttribute(LoginService.LOGIN_IDENTITY);
String loginEnv = (String) request.getAttribute(CURRENT_ENV);
return xxlConfNodeService.pageList(start, length, appname, key, xxlConfUser, loginEnv);
}
/**
* get
* @return
*/
@RequestMapping("/delete")
@ResponseBody
public ReturnT<String> delete(HttpServletRequest request, String key){
XxlConfUser xxlConfUser = (XxlConfUser) request.getAttribute(LoginService.LOGIN_IDENTITY);
String loginEnv = (String) request.getAttribute(CURRENT_ENV);
return xxlConfNodeService.delete(key, xxlConfUser, loginEnv);
}
/**
* create/update
* @return
*/
@RequestMapping("/add")
@ResponseBody
public ReturnT<String> add(HttpServletRequest request, XxlConfNode xxlConfNode){
XxlConfUser xxlConfUser = (XxlConfUser) request.getAttribute(LoginService.LOGIN_IDENTITY);
String loginEnv = (String) request.getAttribute(CURRENT_ENV);
// fill env
xxlConfNode.setEnv(loginEnv);
return xxlConfNodeService.add(xxlConfNode, xxlConfUser, loginEnv);
}
/**
* create/update
* @return
*/
@RequestMapping("/update")
@ResponseBody
public ReturnT<String> update(HttpServletRequest request, XxlConfNode xxlConfNode){
XxlConfUser xxlConfUser = (XxlConfUser) request.getAttribute(LoginService.LOGIN_IDENTITY);
String loginEnv = (String) request.getAttribute(CURRENT_ENV);
// fill env
xxlConfNode.setEnv(loginEnv);
return xxlConfNodeService.update(xxlConfNode, xxlConfUser, loginEnv);
}
/*@RequestMapping("/syncConf")
@ResponseBody
public ReturnT<String> syncConf(HttpServletRequest request,
String appname) {
XxlConfUser xxlConfUser = (XxlConfUser) request.getAttribute(LoginService.LOGIN_IDENTITY);
String loginEnv = (String) request.getAttribute(CURRENT_ENV);
return xxlConfNodeService.syncConf(appname, xxlConfUser, loginEnv);
}*/
// ---------------------- rest api ----------------------
@Value("${xxl.conf.access.token}")
private String accessToken;
/**
* 配置查询 API
*
* 说明:查询配置数据;
*
* ------
* 地址格式:{配置中心跟地址}/find
*
* 请求参数说明:
* 1、accessToken:请求令牌;
* 2、env:环境标识
* 3、keys:配置Key列表
*
* 请求数据格式如下,放置在 RequestBody 中,JSON格式:
*
* {
* "accessToken" : "xx",
* "env" : "xx",
* "keys" : [
* "key01",
* "key02"
* ]
* }
*
* @param data
* @return
*/
@RequestMapping("/find")
@ResponseBody
@PermessionLimit(limit = false)
public ReturnT<Map<String, String>> find(@RequestBody(required = false) String data){
// parse data
XxlConfParamVO confParamVO = null;
try {
confParamVO = (XxlConfParamVO) JacksonUtil.readValue(data, XxlConfParamVO.class);
} catch (Exception e) { }
// parse param
String accessToken = null;
String env = null;
List<String> keys = null;
if (confParamVO != null) {
accessToken = confParamVO.getAccessToken();
env = confParamVO.getEnv();
keys = confParamVO.getKeys();
}
return xxlConfNodeService.find(accessToken, env, keys);
}
/**
* 配置监控 API
*
* 说明:long-polling 接口,主动阻塞一段时间(默认30s);直至阻塞超时或配置信息变动时响应;
*
* ------
* 地址格式:{配置中心跟地址}/monitor
*
* 请求参数说明:
* 1、accessToken:请求令牌;
* 2、env:环境标识
* 3、keys:配置Key列表
*
* 请求数据格式如下,放置在 RequestBody 中,JSON格式:
*
* {
* "accessToken" : "xx",
* "env" : "xx",
* "keys" : [
* "key01",
* "key02"
* ]
* }
*
* @param data
* @return
*/
@RequestMapping("/monitor")
@ResponseBody
@PermessionLimit(limit = false)
public DeferredResult<ReturnT<String>> monitor(@RequestBody(required = false) String data){
// parse data
XxlConfParamVO confParamVO = null;
try {
confParamVO = (XxlConfParamVO) JacksonUtil.readValue(data, XxlConfParamVO.class);
} catch (Exception e) { }
// parse param
String accessToken = null;
String env = null;
List<String> keys = null;
if (confParamVO != null) {
accessToken = confParamVO.getAccessToken();
env = confParamVO.getEnv();
keys = confParamVO.getKeys();
}
return xxlConfNodeService.monitor(accessToken, env, keys);
}
}