RestMainServlet.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright 2009 by primedata Corporation. Address:TianChuang Technology Building, CaiHeFang
  3. * Road,Haidian District, Beijing
  4. *
  5. * All rights reserved.
  6. *
  7. * This software is the confidential and proprietary information of primedata
  8. * Corporation ("Confidential Information"). You shall not disclose such
  9. * Confidential Information and shall use it only in accordance with the terms
  10. * of the license agreement you entered into with primedata.
  11. */
  12. package com.primeton.dgs.kernel.core.web;
  13. import com.primeton.dgs.kernel.core.util.ExUtils;
  14. import com.primeton.licensemanager.checklicense.VerifyHLicenseException;
  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17. import org.springframework.util.AntPathMatcher;
  18. import org.springframework.util.PathMatcher;
  19. import javax.servlet.ServletException;
  20. import javax.servlet.http.HttpServletRequest;
  21. import javax.servlet.http.HttpServletResponse;
  22. import java.io.IOException;
  23. import java.util.Map;
  24. /**
  25. * mvc主控 Servlet, REstFull Api 支持
  26. *
  27. * @author zhaopx
  28. * @version 7.1.0 2020-9-10
  29. */
  30. public class RestMainServlet extends MainServlet {
  31. private static final long serialVersionUID = 1L;
  32. private Logger log = LoggerFactory.getLogger(RestMainServlet.class);
  33. /**
  34. * Process the HTTP request
  35. */
  36. public void perform(HttpServletRequest request, HttpServletResponse response, Map<String, Object> payload)
  37. throws ServletException, IOException {
  38. response.setCharacterEncoding(ENCODE);
  39. String next = "";
  40. try {
  41. String path = payload.get("servletPath") + ".do";
  42. String name = (String) payload.get("invoke");
  43. // 获取登录信息
  44. Object userObject = request.getSession().getAttribute("userObject");
  45. //解决当session失效时,请求报错,后台报空指针,页面跳转到非登录页面
  46. Object userProfile = request.getSession().getAttribute("com.primeton.dgs.workspace.system.common.UserProfile");
  47. // 检查是否已经登录
  48. if ((null == userObject && null == userProfile) || name == null) {
  49. // 未登录,是否在白名单
  50. String resourcePath = request.getRequestURI().replaceAll(request.getContextPath(), "");
  51. boolean cross = false;
  52. PathMatcher matcher = new AntPathMatcher();
  53. for (String s : authWriteList) {
  54. cross = matcher.match(s, resourcePath);
  55. if(cross) {
  56. // 一旦通过一个,则进行下一步校验
  57. break;
  58. }
  59. }
  60. // name is null 或者 不通过
  61. if(name == null || !cross) {
  62. dispatch(request, response, WebViewer.LONGIN_PAGE);
  63. return;
  64. }
  65. }
  66. // 要执行的函数
  67. request.setAttribute("invoke", name);
  68. // 合并参数
  69. MetaCubeHttpServletRequestWrapper requestWrapper = new MetaCubeHttpServletRequestWrapper(request, payload);
  70. //获取 bean
  71. AbstractCommand cmd = CommandFactory.getInstance().getCommand(path);
  72. cmd.init(requestWrapper, response, getServletConfig());
  73. next = cmd.execute();
  74. if (null != next) {
  75. dispatch(requestWrapper, response, next);
  76. }
  77. } catch (VerifyHLicenseException e) {
  78. if (isExt(request)) {
  79. performOnError(request, response, e);
  80. return;
  81. }
  82. request.setAttribute("javax.servlet.jspException", e);
  83. dispatch(request, response, WebViewer.LICENSE_PAGE);
  84. } catch (Exception e) {
  85. log.error("MainServlet catch an error:", e);
  86. if (isExt(request)) {
  87. performOnError(request, response, e);
  88. return;
  89. }
  90. request.setAttribute("_CommandExecuteException_",
  91. ExUtils.getMessage(request, e));
  92. request.setAttribute("javax.servlet.jspException", e);
  93. dispatch(request, response, WebViewer.VIEW_ERROR);
  94. }
  95. }
  96. }