PageRenderTime 67ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/dubbo-admin/src/main/java/com/alibaba/dubbo/governance/web/home/module/screen/Restful.java

https://gitlab.com/sxyseo/dubbox
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. */
  8. package com.alibaba.dubbo.governance.web.home.module.screen;
  9. import java.util.Map;
  10. import javax.servlet.ServletOutputStream;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import com.alibaba.dubbo.common.URL;
  15. import com.alibaba.dubbo.governance.web.util.WebConstants;
  16. import com.alibaba.dubbo.registry.common.domain.User;
  17. import com.alibaba.fastjson.JSON;
  18. public abstract class Restful {
  19. @Autowired
  20. private HttpServletResponse response;
  21. @Autowired
  22. HttpServletRequest request;
  23. // @Autowired
  24. // RegistryValidator registryService;
  25. protected String role = null;
  26. protected String operator = null;
  27. protected User currentUser = null;
  28. protected String operatorAddress = null;
  29. protected URL url = null;
  30. public void execute(Map<String, Object> context) throws Exception {
  31. Result result = new Result();
  32. if(request.getParameter("url")!=null){
  33. url = URL.valueOf(URL.decode(request.getParameter("url")));
  34. }
  35. if (context.get(WebConstants.CURRENT_USER_KEY) != null) {
  36. User user = (User) context.get(WebConstants.CURRENT_USER_KEY);
  37. currentUser = user;
  38. operator = user.getUsername();
  39. role = user.getRole();
  40. context.put(WebConstants.CURRENT_USER_KEY, user);
  41. }
  42. operatorAddress = (String) context.get("clientid");
  43. if(operatorAddress==null || operatorAddress.isEmpty()){
  44. operatorAddress = (String) context.get("request.remoteHost");
  45. }
  46. context.put("operator", operator);
  47. context.put("operatorAddress", operatorAddress);
  48. String jsonResult = null;
  49. try {
  50. result = doExecute(context);
  51. result.setStatus("OK");
  52. } catch (IllegalArgumentException t) {
  53. result.setStatus("ERROR");
  54. result.setCode(3);
  55. result.setMessage(t.getMessage());
  56. }
  57. // catch (InvalidRequestException t) {
  58. // result.setStatus("ERROR");
  59. // result.setCode(2);
  60. // result.setMessage(t.getMessage());
  61. // }
  62. catch (Throwable t){
  63. result.setStatus("ERROR");
  64. result.setCode(1);
  65. result.setMessage(t.getMessage());
  66. }
  67. response.setContentType("application/javascript");
  68. ServletOutputStream os = response.getOutputStream();
  69. try {
  70. jsonResult = JSON.toJSONString(result);
  71. os.print(jsonResult);
  72. } catch (Exception e) {
  73. response.setStatus(500);
  74. os.print(e.getMessage());
  75. }finally{
  76. os.flush();
  77. }
  78. }
  79. protected abstract Result doExecute(Map<String, Object> context) throws Exception;
  80. }