Browse Source

增加url参数

gaoyu 3 years ago
parent
commit
ea6873a336

+ 100 - 94
apm-common/src/main/java/com/persagy/apm/common/handler/PoemsRequestBodyWrapper.java

@@ -27,98 +27,104 @@ import java.util.Map;
  * @version V1.0 2021/9/9 8:18 下午
  * @version V1.0 2021/9/9 8:18 下午
  */
  */
 public class PoemsRequestBodyWrapper extends HttpServletRequestWrapper {
 public class PoemsRequestBodyWrapper extends HttpServletRequestWrapper {
-    /**
+	/**
-     * 存储body转换后的字符串
+	 * 存储body转换后的字符串
-     */
+	 */
-    private String reqBodyStr;
+	private String reqBodyStr;
-
+
-    public PoemsRequestBodyWrapper(HttpServletRequest request) throws IOException {
+	public PoemsRequestBodyWrapper(HttpServletRequest request) throws IOException {
-        super(request);
+		super(request);
-        // 初始化PoemsContext
+		// 初始化PoemsContext
-        initPoemsContext();
+		initPoemsContext(request);
-    }
+	}
-
+
-    /**
+	/**
-     * 初始化PoemsContext
+	 * 初始化PoemsContext
-     *
+	 *
-     * @author lixing
+	 * @author lixing
-     * @version V1.0 2021/9/9 8:17 下午
+	 * @version V1.0 2021/9/9 8:17 下午
-     */
+	 * @param request
-    private void initPoemsContext() throws IOException{
+	 */
-        //从输入流中取出body串, 如果为空,直接返回
+	private void initPoemsContext(HttpServletRequest request) throws IOException {
-        reqBodyStr = IOUtils.toString(super.getInputStream(), "utf-8");
+		// 从输入流中取出body串, 如果为空,直接返回
-        if (StringUtils.isEmpty(reqBodyStr)) {
+		reqBodyStr = IOUtils.toString(super.getInputStream(), "utf-8");
-            return;
+		if (StringUtils.isEmpty(reqBodyStr)) {
-        }
+			return;
-
+		}
-        //reqBodyStr转为Map对象
+
-        Map<String, Object> paramMap = new ObjectMapper().readValue(reqBodyStr, new TypeReference<HashMap<String, Object>>() {
+		String reqUserId = request.getParameter("userId");
-        });
+		String reqGroupCode = request.getParameter("groupCode");
-        String userId = (String) paramMap.get("userId");
+		//String reqGrojectId = request.getParameter("projectId");
-        String loginDevice = (String) paramMap.get("loginDevice");
+		String reqPd="1";
-        String pd = (String) paramMap.get("pd");
+		// reqBodyStr转为Map对象
-        String groupCode = (String) paramMap.get("groupCode");
+		Map<String, Object> paramMap = new ObjectMapper().readValue(reqBodyStr,
-        String projectId = (String) paramMap.get("projectId");
+				new TypeReference<HashMap<String, Object>>() {
-
+				});
-        if (StringUtils.isBlank(groupCode)) {
+		String userId = (String) paramMap.get("userId");
-
+		String loginDevice = (String) paramMap.get("loginDevice");
-            throw new IllegalArgumentException(super.getRequestURI() + ": 请求体中缺少groupCode");
+		String pd = (String) paramMap.get("pd");
-        }
+		String groupCode = (String) paramMap.get("groupCode");
-        if (StringUtils.isBlank(userId)) {
+		String projectId = (String) paramMap.get("projectId");
-            throw new IllegalArgumentException(super.getRequestURI() + ": 请求体中缺少userId");
+
-        }
+		if (StringUtils.isBlank(groupCode) && StringUtils.isBlank(reqGroupCode)) {
-        if (StringUtils.isBlank(pd)) {
+			throw new IllegalArgumentException(super.getRequestURI() + ": 请求体或请求参数中缺少groupCode");
-            throw new IllegalArgumentException(super.getRequestURI() + ": 请求体中缺少pd");
+		}
-        }
+		if (StringUtils.isBlank(userId)  && StringUtils.isBlank(reqUserId)) {
-        PoemsContext.setContext(userId, loginDevice, pd, groupCode, projectId);
+			throw new IllegalArgumentException(super.getRequestURI() + ": 请求体或请求参数中缺少userId");
-    }
+		}
-
+		if (StringUtils.isBlank(pd) &&StringUtils.isBlank(reqPd) ) {
-    @Override
+			throw new IllegalArgumentException(super.getRequestURI() + ": 请求体或请求参数中缺少pd ");
-    public BufferedReader getReader() throws IOException {
+		}
-        return new BufferedReader(new InputStreamReader(this.getInputStream()));
+		PoemsContext.setContext(userId, loginDevice, pd, groupCode, projectId);
-    }
+	}
-
+
-    @Override
+	@Override
-    public ServletInputStream getInputStream() throws IOException {
+	public BufferedReader getReader() throws IOException {
-        //非json类型,直接返回
+		return new BufferedReader(new InputStreamReader(this.getInputStream()));
-        if (super.getHeader(HttpHeaders.CONTENT_TYPE) == null ||
+	}
-                !super.getHeader(HttpHeaders.CONTENT_TYPE).contains(MediaType.APPLICATION_JSON_VALUE)) {
+
-            return super.getInputStream();
+	@Override
-        }
+	public ServletInputStream getInputStream() throws IOException {
-
+		// 非json类型,直接返回
-        if (StringUtils.isEmpty(reqBodyStr)) {
+		if (super.getHeader(HttpHeaders.CONTENT_TYPE) == null
-            return super.getInputStream();
+				|| !super.getHeader(HttpHeaders.CONTENT_TYPE).contains(MediaType.APPLICATION_JSON_VALUE)) {
-        }
+			return super.getInputStream();
-
+		}
-        //reqBodyStr转为Map对象
+
-        Map<String, Object> paramMap = new ObjectMapper().readValue(reqBodyStr, new TypeReference<HashMap<String, Object>>() {
+		if (StringUtils.isEmpty(reqBodyStr)) {
-        });
+			return super.getInputStream();
-
+		}
-        //重新构造一个输入流对象
+
-        byte[] bytes = JSON.toJSONString(paramMap).getBytes("utf-8");
+		// reqBodyStr转为Map对象
-        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
+		Map<String, Object> paramMap = new ObjectMapper().readValue(reqBodyStr,
-
+				new TypeReference<HashMap<String, Object>>() {
-        return new ServletInputStream() {
+				});
-
+
-            @Override
+		// 重新构造一个输入流对象
-            public boolean isFinished() {
+		byte[] bytes = JSON.toJSONString(paramMap).getBytes("utf-8");
-                return false;
+		ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
-            }
+
-
+		return new ServletInputStream() {
-            @Override
+
-            public boolean isReady() {
+			@Override
-                return false;
+			public boolean isFinished() {
-            }
+				return false;
-
+			}
-            @Override
+
-            public void setReadListener(ReadListener listener) {
+			@Override
-
+			public boolean isReady() {
-            }
+				return false;
-
+			}
-            @Override
+
-            public int read() throws IOException {
+			@Override
-                return bais.read();
+			public void setReadListener(ReadListener listener) {
-            }
+
-        };
+			}
-    }
+
+			@Override
+			public int read() throws IOException {
+				return bais.read();
+			}
+		};
+	}
 }
 }