/dubbo-admin/src/main/java/com/alibaba/dubbo/governance/web/home/module/screen/Restful.java
Java | 92 lines | 66 code | 12 blank | 14 comment | 5 complexity | 3ff4d4295a65ba53966291028216a021 MD5 | raw file
1/*
2 * Copyright 2011 Alibaba.com All right reserved. This software is the
3 * confidential and proprietary information of Alibaba.com ("Confidential
4 * Information"). You shall not disclose such Confidential Information and shall
5 * use it only in accordance with the terms of the license agreement you entered
6 * into with Alibaba.com.
7 */
8package com.alibaba.dubbo.governance.web.home.module.screen;
9
10import java.util.Map;
11
12import javax.servlet.ServletOutputStream;
13import javax.servlet.http.HttpServletRequest;
14import javax.servlet.http.HttpServletResponse;
15
16import org.springframework.beans.factory.annotation.Autowired;
17
18import com.alibaba.dubbo.common.URL;
19import com.alibaba.dubbo.governance.web.util.WebConstants;
20import com.alibaba.dubbo.registry.common.domain.User;
21import com.alibaba.fastjson.JSON;
22
23public abstract class Restful {
24
25 @Autowired
26 private HttpServletResponse response;
27
28 @Autowired
29 HttpServletRequest request;
30
31// @Autowired
32// RegistryValidator registryService;
33
34 protected String role = null;
35 protected String operator = null;
36 protected User currentUser = null;
37 protected String operatorAddress = null;
38 protected URL url = null;
39
40 public void execute(Map<String, Object> context) throws Exception {
41 Result result = new Result();
42 if(request.getParameter("url")!=null){
43 url = URL.valueOf(URL.decode(request.getParameter("url")));
44 }
45 if (context.get(WebConstants.CURRENT_USER_KEY) != null) {
46 User user = (User) context.get(WebConstants.CURRENT_USER_KEY);
47 currentUser = user;
48 operator = user.getUsername();
49 role = user.getRole();
50 context.put(WebConstants.CURRENT_USER_KEY, user);
51 }
52 operatorAddress = (String) context.get("clientid");
53 if(operatorAddress==null || operatorAddress.isEmpty()){
54 operatorAddress = (String) context.get("request.remoteHost");
55 }
56 context.put("operator", operator);
57 context.put("operatorAddress", operatorAddress);
58 String jsonResult = null;
59 try {
60 result = doExecute(context);
61 result.setStatus("OK");
62 } catch (IllegalArgumentException t) {
63 result.setStatus("ERROR");
64 result.setCode(3);
65 result.setMessage(t.getMessage());
66 }
67// catch (InvalidRequestException t) {
68// result.setStatus("ERROR");
69// result.setCode(2);
70// result.setMessage(t.getMessage());
71// }
72 catch (Throwable t){
73 result.setStatus("ERROR");
74 result.setCode(1);
75 result.setMessage(t.getMessage());
76 }
77 response.setContentType("application/javascript");
78 ServletOutputStream os = response.getOutputStream();
79 try {
80 jsonResult = JSON.toJSONString(result);
81 os.print(jsonResult);
82 } catch (Exception e) {
83 response.setStatus(500);
84 os.print(e.getMessage());
85 }finally{
86 os.flush();
87 }
88 }
89
90 protected abstract Result doExecute(Map<String, Object> context) throws Exception;
91
92}