MetaCubeArthNewSupport.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package com.primeton.dqms.boot.configure;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.google.common.net.MediaType;
  5. import com.primeton.dgs.kernel.core.web.RestMainServlet;
  6. import lombok.NonNull;
  7. import org.apache.commons.io.IOUtils;
  8. import org.apache.commons.lang.StringUtils;
  9. import org.gocom.coframe.model.User;
  10. import org.gocom.coframe.sdk.CofContext;
  11. import org.gocom.coframe.sdk.model.CofUser;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. import org.springframework.beans.factory.annotation.Value;
  15. import org.springframework.stereotype.Component;
  16. import javax.servlet.ServletException;
  17. import javax.servlet.annotation.WebServlet;
  18. import javax.servlet.http.HttpServlet;
  19. import javax.servlet.http.HttpServletRequest;
  20. import javax.servlet.http.HttpServletResponse;
  21. import java.io.BufferedReader;
  22. import java.io.IOException;
  23. import java.util.HashMap;
  24. import java.util.Map;
  25. import java.util.Optional;
  26. /**
  27. *
  28. * SpringBoot 下旧的元数据、数据质量架构支持
  29. *
  30. * <pre>
  31. *
  32. * Created by zhaopx.
  33. * User: zhaopx
  34. * Date: 2020/8/7
  35. * Time: 10:21
  36. *
  37. * </pre>
  38. *
  39. * @author zhaopx
  40. */
  41. @WebServlet(name = "NewMetaCubeServlet", urlPatterns = MetaCubeArthNewSupport.API_PREFIX_PATTERN)
  42. @Component
  43. public class MetaCubeArthNewSupport extends HttpServlet {
  44. private Logger log = LoggerFactory.getLogger(MetaCubeArthNewSupport.class);
  45. /**
  46. * 拦截的 API 前缀
  47. */
  48. public final static String API_PREFIX_PATTERN = "/appsapi/*";
  49. /**
  50. * api 前缀
  51. */
  52. private String apiPrefix = "/" + API_PREFIX_PATTERN.split("/")[1] + "/";
  53. @Value("${coframe.auth.exclude-paths}")
  54. private String authExcudes;
  55. /**
  56. * 转发 Servlet
  57. */
  58. final RestMainServlet mainServlet;
  59. public MetaCubeArthNewSupport() {
  60. mainServlet = new RestMainServlet();
  61. }
  62. @Override
  63. public void init() throws ServletException {
  64. String excludes = Optional.ofNullable(authExcudes).orElse("login");
  65. String[] splits = excludes.split(",");
  66. for (String split : splits) {
  67. mainServlet.addExclude(split);
  68. }
  69. mainServlet.init();
  70. log.info("auth exclude urls: " + authExcudes);
  71. }
  72. @Override
  73. public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
  74. verifySession(request);
  75. BufferedReader reader = request.getReader();
  76. String header = StringUtils.trimToNull(request.getHeader("Content-Type"));
  77. MediaType mediaType = MediaType.parse(Optional.ofNullable(header).orElse("text/html"));
  78. if("application".equalsIgnoreCase(mediaType.type()) || "json".equalsIgnoreCase(mediaType.subtype())) {
  79. reader.mark(0);
  80. String json = IOUtils.toString(reader);
  81. reader.reset();
  82. if(StringUtils.isBlank(json)) {
  83. json = "{}";
  84. }
  85. JSONObject jsonObject = JSON.parseObject(json);
  86. jsonObject.putAll(parseUrlParams(request.getRequestURI()));
  87. mainServlet.perform(request, response, jsonObject);
  88. return;
  89. }
  90. mainServlet.doGet(request, response);
  91. }
  92. @Override
  93. public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
  94. verifySession(request);
  95. BufferedReader reader = request.getReader();
  96. String header = StringUtils.trimToNull(request.getHeader("Content-Type"));
  97. MediaType mediaType = MediaType.parse(Optional.ofNullable(header).orElse("text/html"));
  98. if("application".equalsIgnoreCase(mediaType.type()) || "json".equalsIgnoreCase(mediaType.subtype())) {
  99. reader.mark(0);
  100. String json = IOUtils.toString(reader);
  101. reader.reset();
  102. if(StringUtils.isBlank(json)) {
  103. json = "{}";
  104. }
  105. JSONObject jsonObject = JSON.parseObject(json);
  106. jsonObject.putAll(parseUrlParams(request.getRequestURI()));
  107. mainServlet.perform(request, response, jsonObject);
  108. return;
  109. }
  110. mainServlet.doPost(request, response);
  111. }
  112. @NonNull
  113. private Map<String, String> parseUrlParams(String url) {
  114. final HashMap<String, String> params = new HashMap<>();
  115. if(url.contains(apiPrefix)) {
  116. String resourceUrl = url.substring(url.indexOf(apiPrefix));
  117. String[] splits = resourceUrl.split("/");
  118. if(splits.length >= 4){
  119. params.put("servletPath", splits[2]);
  120. params.put("invoke", splits[3]);
  121. }
  122. }
  123. return params;
  124. }
  125. /**
  126. * 判断登录,检验 Session。 如果已经登录,则从 session 中获取到登录信息
  127. * @param request
  128. */
  129. private void verifySession(HttpServletRequest request) {
  130. CofUser cofUser = (CofUser) CofContext.getContext().get("loginedUser", CofUser.class);
  131. if(cofUser == null) {
  132. return;
  133. }
  134. Object userObject = request.getSession().getAttribute("userObject");
  135. if(userObject == null) {
  136. User user = new User();
  137. user.setId(cofUser.getId());
  138. user.setName(cofUser.getName());
  139. user.setTenantId(cofUser.getTenantId());
  140. request.getSession().setAttribute("userObject", user);
  141. }
  142. }
  143. }