package com.primeton.dqms.boot.configure; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.google.common.net.MediaType; import com.primeton.dgs.kernel.core.web.RestMainServlet; import lombok.NonNull; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.gocom.coframe.model.User; import org.gocom.coframe.sdk.CofContext; import org.gocom.coframe.sdk.model.CofUser; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.BufferedReader; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Optional; /** * * SpringBoot 下旧的元数据、数据质量架构支持 * *
 *
 * Created by zhaopx.
 * User: zhaopx
 * Date: 2020/8/7
 * Time: 10:21
 *
 * 
* * @author zhaopx */ @WebServlet(name = "NewMetaCubeServlet", urlPatterns = MetaCubeArthNewSupport.API_PREFIX_PATTERN) @Component public class MetaCubeArthNewSupport extends HttpServlet { private Logger log = LoggerFactory.getLogger(MetaCubeArthNewSupport.class); /** * 拦截的 API 前缀 */ public final static String API_PREFIX_PATTERN = "/appsapi/*"; /** * api 前缀 */ private String apiPrefix = "/" + API_PREFIX_PATTERN.split("/")[1] + "/"; @Value("${coframe.auth.exclude-paths}") private String authExcudes; /** * 转发 Servlet */ final RestMainServlet mainServlet; public MetaCubeArthNewSupport() { mainServlet = new RestMainServlet(); } @Override public void init() throws ServletException { String excludes = Optional.ofNullable(authExcudes).orElse("login"); String[] splits = excludes.split(","); for (String split : splits) { mainServlet.addExclude(split); } mainServlet.init(); log.info("auth exclude urls: " + authExcudes); } @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { verifySession(request); BufferedReader reader = request.getReader(); String header = StringUtils.trimToNull(request.getHeader("Content-Type")); MediaType mediaType = MediaType.parse(Optional.ofNullable(header).orElse("text/html")); if("application".equalsIgnoreCase(mediaType.type()) || "json".equalsIgnoreCase(mediaType.subtype())) { reader.mark(0); String json = IOUtils.toString(reader); reader.reset(); if(StringUtils.isBlank(json)) { json = "{}"; } JSONObject jsonObject = JSON.parseObject(json); jsonObject.putAll(parseUrlParams(request.getRequestURI())); mainServlet.perform(request, response, jsonObject); return; } mainServlet.doGet(request, response); } @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { verifySession(request); BufferedReader reader = request.getReader(); String header = StringUtils.trimToNull(request.getHeader("Content-Type")); MediaType mediaType = MediaType.parse(Optional.ofNullable(header).orElse("text/html")); if("application".equalsIgnoreCase(mediaType.type()) || "json".equalsIgnoreCase(mediaType.subtype())) { reader.mark(0); String json = IOUtils.toString(reader); reader.reset(); if(StringUtils.isBlank(json)) { json = "{}"; } JSONObject jsonObject = JSON.parseObject(json); jsonObject.putAll(parseUrlParams(request.getRequestURI())); mainServlet.perform(request, response, jsonObject); return; } mainServlet.doPost(request, response); } @NonNull private Map parseUrlParams(String url) { final HashMap params = new HashMap<>(); if(url.contains(apiPrefix)) { String resourceUrl = url.substring(url.indexOf(apiPrefix)); String[] splits = resourceUrl.split("/"); if(splits.length >= 4){ params.put("servletPath", splits[2]); params.put("invoke", splits[3]); } } return params; } /** * 判断登录,检验 Session。 如果已经登录,则从 session 中获取到登录信息 * @param request */ private void verifySession(HttpServletRequest request) { CofUser cofUser = (CofUser) CofContext.getContext().get("loginedUser", CofUser.class); if(cofUser == null) { return; } Object userObject = request.getSession().getAttribute("userObject"); if(userObject == null) { User user = new User(); user.setId(cofUser.getId()); user.setName(cofUser.getName()); user.setTenantId(cofUser.getTenantId()); request.getSession().setAttribute("userObject", user); } } }