|
|
@@ -0,0 +1,331 @@
|
|
|
+package org.jeecg.modules.api.sys;
|
|
|
+
|
|
|
+import net.sf.json.JSONArray;
|
|
|
+import net.sf.json.JSONObject;
|
|
|
+
|
|
|
+import org.jeecg.common.util.IPUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * app端Controller基类
|
|
|
+ *
|
|
|
+ * @author zwq 2019-03-18
|
|
|
+ */
|
|
|
+public class BaseAppController {
|
|
|
+
|
|
|
+ private static Logger logger = LoggerFactory.getLogger(BaseAppController.class);
|
|
|
+ // 日志时需要忽略的字段
|
|
|
+ private static final HashSet<String> paramsToOmit = new HashSet<String>();
|
|
|
+
|
|
|
+ static {
|
|
|
+ paramsToOmit.add("loginPassword");
|
|
|
+ paramsToOmit.add("loginPasswordChk");
|
|
|
+ paramsToOmit.add("password");
|
|
|
+ paramsToOmit.add("oldPassword");
|
|
|
+ paramsToOmit.add("newPassword");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static final String DATA = "data";
|
|
|
+ public static final String ERROR = "error";
|
|
|
+ public static final String STATUS = "status";
|
|
|
+ public static final String ERRORMSG = "errmsg";
|
|
|
+ public static final String ERRORCODE = "errcode";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将request里面访问的所有参数转成HashMap
|
|
|
+ *
|
|
|
+ * @param request
|
|
|
+ * @return HashMap<String , String>
|
|
|
+ */
|
|
|
+ @SuppressWarnings("rawtypes")
|
|
|
+ protected HashMap<String, String> findRequestMap(HttpServletRequest request) {
|
|
|
+ HashMap<String, String> requestMap = new HashMap<String, String>();
|
|
|
+ // Map requestParams = request.getParameterMap();
|
|
|
+ if (request.getMethod().equals("GET")) {
|
|
|
+ Map requestParams = request.getParameterMap();
|
|
|
+ for (Iterator iter = requestParams.keySet().iterator(); iter
|
|
|
+ .hasNext(); ) {
|
|
|
+ String name = (String) iter.next();
|
|
|
+ String[] values = (String[]) requestParams.get(name);
|
|
|
+ String valueStr = "";
|
|
|
+ for (int i = 0; i < values.length; i++) {
|
|
|
+ valueStr = (i == values.length - 1) ? valueStr + values[i]
|
|
|
+ : valueStr + values[i] + ",";
|
|
|
+ }
|
|
|
+ requestMap.put(name, valueStr);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ String jsonString = null;
|
|
|
+ try {
|
|
|
+ jsonString = getRequestJsonString(request);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ if (jsonString.contains("&")) {
|
|
|
+ String[] strs = jsonString.split("&");
|
|
|
+ for (int i = 0; i < strs.length; i++) {
|
|
|
+ String json = strs[i];
|
|
|
+ String[] strs1 = json.split("=");
|
|
|
+
|
|
|
+ if (strs1.length == 1) {
|
|
|
+ requestMap.put(strs1[0].trim(), "");
|
|
|
+ } else {
|
|
|
+ requestMap.put(strs1[0].trim(), strs1[1].trim());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ JSONObject jsonObject = JSONObject.fromObject(jsonString);
|
|
|
+
|
|
|
+ Iterator<String> keys = jsonObject.keys();// 定义迭代器
|
|
|
+
|
|
|
+ String key = null;
|
|
|
+ String value = null;
|
|
|
+
|
|
|
+ while (keys.hasNext()) {
|
|
|
+ key = keys.next().toString();
|
|
|
+ value = jsonObject.get(key).toString();
|
|
|
+
|
|
|
+ requestMap.put(key.trim(), value.trim());
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return requestMap;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /***
|
|
|
+ * 获取 request 中 json 字符串的内容
|
|
|
+ *
|
|
|
+ * @param request
|
|
|
+ * @return : <code>byte[]</code>
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public String getRequestJsonString(HttpServletRequest request)
|
|
|
+ throws IOException {
|
|
|
+ String submitMehtod = request.getMethod();
|
|
|
+ System.out.println("submitMehtod ============>" + submitMehtod);
|
|
|
+ // GET
|
|
|
+ if (submitMehtod.equals("GET")) {
|
|
|
+ return new String(request.getQueryString().getBytes("iso-8859-1"),
|
|
|
+ "utf-8").replaceAll("%22", "\"").replaceAll("\\+", "%2B");
|
|
|
+ // POST
|
|
|
+ } else {
|
|
|
+ return getRequestPostStr(request);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 描述:获取 post 请求的 byte[] 数组
|
|
|
+ *
|
|
|
+ * <pre>
|
|
|
+ * 举例:
|
|
|
+ * </pre>
|
|
|
+ *
|
|
|
+ * @param request
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public byte[] getRequestPostBytes(HttpServletRequest request)
|
|
|
+ throws IOException {
|
|
|
+ int contentLength = request.getContentLength();
|
|
|
+ if (contentLength < 0) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ byte buffer[] = new byte[contentLength];
|
|
|
+ for (int i = 0; i < contentLength; ) {
|
|
|
+
|
|
|
+ int readlen = request.getInputStream().read(buffer, i,
|
|
|
+ contentLength - i);
|
|
|
+ if (readlen == -1) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ i += readlen;
|
|
|
+ }
|
|
|
+ return buffer;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 描述:获取 post 请求内容
|
|
|
+ *
|
|
|
+ * <pre>
|
|
|
+ * 举例:
|
|
|
+ * </pre>
|
|
|
+ *
|
|
|
+ * @param request
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public String getRequestPostStr(HttpServletRequest request)
|
|
|
+ throws IOException {
|
|
|
+ byte buffer[] = getRequestPostBytes(request);
|
|
|
+ String charEncoding = request.getCharacterEncoding();
|
|
|
+ if (charEncoding == null) {
|
|
|
+ charEncoding = "UTF-8";
|
|
|
+ }
|
|
|
+ return new String(buffer, charEncoding);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 输出map正确消息
|
|
|
+ public Map<String, Object> successResult(int errorCode, String errorMsg,
|
|
|
+ Object data) {
|
|
|
+ Map<String, Object> map = new HashMap<String, Object>();
|
|
|
+ map.put(ERRORCODE, errorCode);
|
|
|
+ map.put(ERRORMSG, errorMsg);
|
|
|
+ map.put(DATA, data);
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 输出map错误消息
|
|
|
+ public Map<String, Object> errorResult(int errorCode, String errorMsg) {
|
|
|
+ Map<String, Object> map = new HashMap<String, Object>();
|
|
|
+ map.put(ERRORCODE, errorCode);
|
|
|
+ map.put(ERRORMSG, errorMsg);
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ // AJAX输出文本,返回null
|
|
|
+ public String ajaxText(String text, HttpServletResponse response) {
|
|
|
+ return ajax(text, "text/plain", response);
|
|
|
+ }
|
|
|
+
|
|
|
+ // AJAX输出HTML,返回null
|
|
|
+ public String ajaxHtml(String html, HttpServletResponse response) {
|
|
|
+ return ajax(html, "text/html", response);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据字符串输出JSON,返回null
|
|
|
+ public String ajaxJson(String jsonString, HttpServletResponse response) {
|
|
|
+ return ajax(jsonString, "text/html", response);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据Map输出JSON,返回null
|
|
|
+ public String ajaxJson(Map<String, Object> jsonMap,
|
|
|
+ HttpServletResponse response) {
|
|
|
+ JSONObject jsonObject = JSONObject.fromObject(jsonMap);
|
|
|
+ return ajax(jsonObject.toString(), "text/html", response);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据HashMap输出JSON,返回null
|
|
|
+ public String ajaxJson(HashMap<String, String> jsonMap,
|
|
|
+ HttpServletResponse response) {
|
|
|
+ JSONObject jsonObject = JSONObject.fromObject(jsonMap);
|
|
|
+ return ajax(jsonObject.toString(), "text/html", response);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据List输出JSON,返回null
|
|
|
+ public String ajaxJson(List<HashMap<String, String>> list,
|
|
|
+ HttpServletResponse response) {
|
|
|
+ JSONArray jsonArray = JSONArray.fromObject(list);
|
|
|
+ return ajax(jsonArray.toString(), "text/html", response);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据List输出JSON,返回null
|
|
|
+ public String ajaxJsonListObject(List<Map<String, Object>> list,
|
|
|
+ HttpServletResponse response) {
|
|
|
+ JSONArray jsonArray = JSONArray.fromObject(list);
|
|
|
+ return ajax(jsonArray.toString(), "text/html", response);
|
|
|
+ }
|
|
|
+
|
|
|
+ // AJAX输出,返回null
|
|
|
+ public String ajax(String content, String type, HttpServletResponse response) {
|
|
|
+ try {
|
|
|
+ response.setContentType(type + ";charset=UTF-8");
|
|
|
+ response.setHeader("Pragma", "No-cache");
|
|
|
+ response.setHeader("Cache-Control", "no-cache");
|
|
|
+ response.setDateHeader("Expires", 0);
|
|
|
+ response.getWriter().write(content);
|
|
|
+ response.getWriter().flush();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 给重要的功能入口加日志功能
|
|
|
+ *
|
|
|
+ * @param request
|
|
|
+ * @param action 动作
|
|
|
+ */
|
|
|
+ protected void logInfo(HttpServletRequest request, String action) {
|
|
|
+ StringBuilder sb = new StringBuilder("实际调用方法:")
|
|
|
+ .append(Thread.currentThread().getStackTrace()[2]
|
|
|
+ .getClassName())
|
|
|
+ .append(".")
|
|
|
+ .append(Thread.currentThread().getStackTrace()[2]
|
|
|
+ .getMethodName())
|
|
|
+ .append('(')
|
|
|
+ .append(Thread.currentThread().getStackTrace()[2]
|
|
|
+ .getLineNumber()).append(')').append(",IP地址")
|
|
|
+ .append(IPUtils.getIpAddr(request)).append(") —— 动作:")
|
|
|
+ .append(action).append(" 请求参数是|").append(getParams(request));
|
|
|
+ logger.info(sb.toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 记录错误日志
|
|
|
+ *
|
|
|
+ * @param request
|
|
|
+ * @param message 错误信息
|
|
|
+ */
|
|
|
+ protected void logError(HttpServletRequest request, String message) {
|
|
|
+ logError(request, message, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected void logError(HttpServletRequest request, String message,
|
|
|
+ Exception e) {
|
|
|
+ StringBuilder sb = new StringBuilder("实际调用方法:")
|
|
|
+ .append(Thread.currentThread().getStackTrace()[2]
|
|
|
+ .getClassName())
|
|
|
+ .append(".")
|
|
|
+ .append(Thread.currentThread().getStackTrace()[2]
|
|
|
+ .getMethodName())
|
|
|
+ .append('(')
|
|
|
+ .append(Thread.currentThread().getStackTrace()[2]
|
|
|
+ .getLineNumber()).append(')').append("错误信息:")
|
|
|
+ .append(message).append(",IP地址").append(IPUtils.getIpAddr(request))
|
|
|
+ .append(" 请求参数是|").append(getParams(request));
|
|
|
+ sb.append("||异常信息:").append(message);
|
|
|
+ logger.error(sb.toString(), e);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到请求参数
|
|
|
+ *
|
|
|
+ * @param request
|
|
|
+ */
|
|
|
+ @SuppressWarnings("rawtypes")
|
|
|
+ protected String getParams(HttpServletRequest request) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ Map params = request.getParameterMap();
|
|
|
+ for (Object key : params.keySet()) {
|
|
|
+ Object[] objs = null;
|
|
|
+ if (null != params.get(key))
|
|
|
+ objs = (Object[]) params.get(key);
|
|
|
+
|
|
|
+ sb.append(key).append(":");
|
|
|
+ if (objs != null) {
|
|
|
+ // 密码等不能写入日志!!
|
|
|
+ if (paramsToOmit.contains(key)) {
|
|
|
+ sb.append("*****/");
|
|
|
+ } else {
|
|
|
+ for (Object object : objs) {
|
|
|
+ sb.append(object).append("/");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sb.append(",");
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|