MainServlet.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 java.io.IOException;
  14. import java.util.HashSet;
  15. import java.util.Set;
  16. import javax.servlet.ServletException;
  17. import javax.servlet.http.HttpServlet;
  18. import javax.servlet.http.HttpServletRequest;
  19. import javax.servlet.http.HttpServletResponse;
  20. import net.sf.json.JSONArray;
  21. import com.eos.data.datacontext.UserObject;
  22. import com.primeton.dgs.kernel.core.common.ActionHelper;
  23. import com.primeton.dgs.kernel.core.common.ActionResult;
  24. import com.primeton.dgs.kernel.core.util.ExUtils;
  25. import com.primeton.licensemanager.checklicense.VerifyHLicenseException;
  26. import org.apache.commons.lang.StringUtils;
  27. import org.slf4j.Logger;
  28. import org.slf4j.LoggerFactory;
  29. import org.springframework.util.AntPathMatcher;
  30. import org.springframework.util.PathMatcher;
  31. /**
  32. * mvc主控 Servlet
  33. *
  34. * @author user
  35. * @version 1.0 2006-9-18
  36. */
  37. public class MainServlet extends HttpServlet {
  38. private static final long serialVersionUID = 1L;
  39. private Logger log = LoggerFactory.getLogger(MainServlet.class);
  40. public static final String ENCODE = "UTF-8";
  41. String loginPath = "/login.do";
  42. /**
  43. * 无需认证即可访问的白名单
  44. */
  45. protected Set<String> authWriteList = new HashSet<>();
  46. /**
  47. * Initialize global variables
  48. */
  49. public void init() throws ServletException {
  50. log.info("MainServlet init.");
  51. }
  52. /**
  53. * Process the HTTP Get request
  54. */
  55. public void doGet(HttpServletRequest request, HttpServletResponse response)
  56. throws IOException, ServletException {
  57. perform(request, response);
  58. }
  59. /**
  60. * Process the HTTP Post request
  61. */
  62. public void doPost(HttpServletRequest request, HttpServletResponse response)
  63. throws IOException, ServletException {
  64. perform(request, response);
  65. }
  66. /**
  67. * Process the HTTP request
  68. */
  69. public void perform(HttpServletRequest request, HttpServletResponse response)
  70. throws ServletException, IOException {
  71. response.setCharacterEncoding(ENCODE);
  72. String next = "";
  73. try {
  74. String path = request.getServletPath();
  75. String name = request.getParameter("invoke");
  76. // 获取登录信息
  77. Object userObject = request.getSession().getAttribute("userObject");
  78. // 登录动作,不要拦截
  79. //解决当session失效时,请求报错,后台报空指针,页面跳转到非登录页面
  80. Object userProfile = request.getSession().getAttribute("com.primeton.dgs.workspace.system.common.UserProfile");
  81. // 检查是否已经登录
  82. if ((null == userObject && null == userProfile) || name == null) {
  83. // 未登录,是否在白名单
  84. String resourcePath = request.getRequestURI().replaceAll(request.getContextPath(), "");
  85. boolean cross = false;
  86. PathMatcher matcher = new AntPathMatcher();
  87. for (String s : authWriteList) {
  88. cross = matcher.match(s, resourcePath);
  89. if(cross) {
  90. // 一旦通过一个,则进行下一步校验
  91. break;
  92. }
  93. }
  94. // name is null 或者 不通过
  95. if(name == null || !cross) {
  96. dispatch(request, response, WebViewer.LONGIN_PAGE);
  97. return;
  98. }
  99. }
  100. request.setAttribute("invoke", name);
  101. AbstractCommand cmd = CommandFactory.getInstance().getCommand(path);
  102. cmd.init(request, response, getServletConfig());
  103. next = cmd.execute();
  104. if (null != next) {
  105. dispatch(request, response, next);
  106. }
  107. } catch (VerifyHLicenseException e) {
  108. if (isExt(request)) {
  109. performOnError(request, response, e);
  110. return;
  111. }
  112. request.setAttribute("javax.servlet.jspException", e);
  113. dispatch(request, response, WebViewer.LICENSE_PAGE);
  114. } catch (Exception e) {
  115. log.error("MainServlet catch an error:", e);
  116. if (isExt(request)) {
  117. performOnError(request, response, e);
  118. return;
  119. }
  120. request.setAttribute("_CommandExecuteException_",
  121. ExUtils.getMessage(request, e));
  122. request.setAttribute("javax.servlet.jspException", e);
  123. dispatch(request, response, WebViewer.VIEW_ERROR);
  124. }
  125. }
  126. public void performOnError(HttpServletRequest request,
  127. HttpServletResponse response, Exception e) throws ServletException,
  128. IOException {
  129. String msg = ExUtils.getMessage(request, e);
  130. ActionResult rs = new ActionResult(false, msg);
  131. rs.put("list", new JSONArray());
  132. try {
  133. if (isReturnXml(request)) {
  134. ActionHelper.outputXmlList(response, null, 0, rs);
  135. } else {
  136. ActionHelper.output(response, rs);
  137. }
  138. } catch (Exception er) {
  139. throw new ServletException(er);
  140. }
  141. }
  142. public void dispatch(HttpServletRequest request,
  143. HttpServletResponse response, String page) throws ServletException,
  144. IOException {
  145. log.info("Command dispath request to: " + page);
  146. if (page.toLowerCase().startsWith("http:")
  147. || page.toLowerCase().startsWith("https:")) {
  148. response.sendRedirect(page);
  149. } else {
  150. request.getRequestDispatcher(page).forward(request, response);
  151. }
  152. }
  153. /**
  154. * 添加排除
  155. * @param s
  156. * @return
  157. */
  158. public boolean addExclude(String s) {
  159. if(StringUtils.isBlank(s)) {
  160. return false;
  161. }
  162. return authWriteList.add(StringUtils.trim(s));
  163. }
  164. public static boolean isExt(HttpServletRequest request) {
  165. return "Ext".equalsIgnoreCase(request.getHeader("Request-By"));
  166. }
  167. public static boolean isReturnXml(HttpServletRequest request) {
  168. return "XML".equalsIgnoreCase(request.getHeader("Response-By"));
  169. }
  170. }