|
@@ -1,27 +1,5 @@
|
|
package cn.com.qmth.examcloud.web.support;
|
|
package cn.com.qmth.examcloud.web.support;
|
|
|
|
|
|
-import java.io.BufferedOutputStream;
|
|
|
|
-import java.io.File;
|
|
|
|
-import java.io.FileInputStream;
|
|
|
|
-import java.io.IOException;
|
|
|
|
-import java.io.InputStream;
|
|
|
|
-import java.io.OutputStream;
|
|
|
|
-import java.lang.reflect.Field;
|
|
|
|
-import java.net.URLEncoder;
|
|
|
|
-import java.util.List;
|
|
|
|
-import java.util.Map;
|
|
|
|
-import java.util.Set;
|
|
|
|
-
|
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
|
-
|
|
|
|
-import org.apache.commons.io.IOUtils;
|
|
|
|
-import org.apache.commons.lang3.StringUtils;
|
|
|
|
-import org.springframework.web.context.request.RequestContextHolder;
|
|
|
|
-import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
-
|
|
|
|
-import com.google.common.collect.Lists;
|
|
|
|
-
|
|
|
|
import cn.com.qmth.examcloud.api.commons.security.bean.Role;
|
|
import cn.com.qmth.examcloud.api.commons.security.bean.Role;
|
|
import cn.com.qmth.examcloud.api.commons.security.bean.User;
|
|
import cn.com.qmth.examcloud.api.commons.security.bean.User;
|
|
import cn.com.qmth.examcloud.api.commons.security.enums.RoleMeta;
|
|
import cn.com.qmth.examcloud.api.commons.security.enums.RoleMeta;
|
|
@@ -31,325 +9,342 @@ import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
|
|
import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
|
|
import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
|
|
import cn.com.qmth.examcloud.commons.util.ObjectUtil;
|
|
import cn.com.qmth.examcloud.commons.util.ObjectUtil;
|
|
import cn.com.qmth.examcloud.web.enums.HttpServletRequestAttribute;
|
|
import cn.com.qmth.examcloud.web.enums.HttpServletRequestAttribute;
|
|
|
|
+import com.google.common.collect.Lists;
|
|
|
|
+import org.apache.commons.io.IOUtils;
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
+
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
+import java.io.*;
|
|
|
|
+import java.lang.reflect.Field;
|
|
|
|
+import java.net.URLEncoder;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.Set;
|
|
|
|
|
|
/**
|
|
/**
|
|
* contorller 基类
|
|
* contorller 基类
|
|
- *
|
|
|
|
|
|
+ *
|
|
* @author wang wei
|
|
* @author wang wei
|
|
* @date 2018年4月4日
|
|
* @date 2018年4月4日
|
|
*/
|
|
*/
|
|
public abstract class ControllerSupport {
|
|
public abstract class ControllerSupport {
|
|
|
|
|
|
- /**
|
|
|
|
- * controller 统一业务日志对象
|
|
|
|
- */
|
|
|
|
- protected ExamCloudLog log = ExamCloudLogFactory.getLog(this.getClass());
|
|
|
|
|
|
+ /**
|
|
|
|
+ * controller 统一业务日志对象
|
|
|
|
+ */
|
|
|
|
+ protected ExamCloudLog log = ExamCloudLogFactory.getLog(this.getClass());
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取接入用户
|
|
|
|
+ *
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ protected User getAccessUser() {
|
|
|
|
+ User accessUser = (User) ServletUtil.getRequest()
|
|
|
|
+ .getAttribute(HttpServletRequestAttribute.$_ACCESS_USER.name());
|
|
|
|
+ if (null == accessUser) {
|
|
|
|
+ throw new StatusException("252", "请重新登陆");
|
|
|
|
+ }
|
|
|
|
+ return accessUser;
|
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
|
- * 获取接入用户
|
|
|
|
- *
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- protected User getAccessUser() {
|
|
|
|
- User accessUser = (User) ServletUtil.getRequest()
|
|
|
|
- .getAttribute(HttpServletRequestAttribute.$_ACCESS_USER.name());
|
|
|
|
- if (null == accessUser) {
|
|
|
|
- throw new StatusException("252", "请重新登陆");
|
|
|
|
- }
|
|
|
|
- return accessUser;
|
|
|
|
- }
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 设置http响应码总是为200
|
|
|
|
+ *
|
|
|
|
+ * @author WANGWEI
|
|
|
|
+ */
|
|
|
|
+ protected void setAlwaysOKResponse() {
|
|
|
|
+ ServletUtil.getRequest().setAttribute(HttpServletRequestAttribute.$_ALWAYS_OK.name(), true);
|
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
|
- * 设置http响应码总是为200
|
|
|
|
- *
|
|
|
|
- * @author WANGWEI
|
|
|
|
- */
|
|
|
|
- protected void setAlwaysOKResponse() {
|
|
|
|
- ServletUtil.getRequest().setAttribute(HttpServletRequestAttribute.$_ALWAYS_OK.name(), true);
|
|
|
|
- }
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 获取企业的顶级机构(非session状态)
|
|
|
|
+ *
|
|
|
|
+ * @return
|
|
|
|
+ * @author WANGWEI
|
|
|
|
+ */
|
|
|
|
+ protected Long getEnterpriseRootOrgId() {
|
|
|
|
+ Long rootOrgId = (Long) getRequest()
|
|
|
|
+ .getAttribute(HttpServletRequestAttribute.$_ENTERPRISE_ROOT_ORG_ID.name());
|
|
|
|
+ if (null == rootOrgId) {
|
|
|
|
+ throw new StatusException("280", "安全接入的顶级机构ID为空");
|
|
|
|
+ }
|
|
|
|
+ return rootOrgId;
|
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
|
- * 获取企业的顶级机构(非session状态)
|
|
|
|
- *
|
|
|
|
- * @author WANGWEI
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- protected Long getEnterpriseRootOrgId() {
|
|
|
|
- Long rootOrgId = (Long) getRequest()
|
|
|
|
- .getAttribute(HttpServletRequestAttribute.$_ENTERPRISE_ROOT_ORG_ID.name());
|
|
|
|
- if (null == rootOrgId) {
|
|
|
|
- throw new StatusException("280", "安全接入的顶级机构ID为空");
|
|
|
|
- }
|
|
|
|
- return rootOrgId;
|
|
|
|
- }
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 获取顶级机构(session状态)
|
|
|
|
+ *
|
|
|
|
+ * @return
|
|
|
|
+ * @author WANGWEI
|
|
|
|
+ */
|
|
|
|
+ protected Long getRootOrgId() {
|
|
|
|
+ Long rootOrgId = getAccessUser().getRootOrgId();
|
|
|
|
+ return rootOrgId;
|
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
|
- * 获取顶级机构(session状态)
|
|
|
|
- *
|
|
|
|
- * @author WANGWEI
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- protected Long getRootOrgId() {
|
|
|
|
- Long rootOrgId = getAccessUser().getRootOrgId();
|
|
|
|
- return rootOrgId;
|
|
|
|
- }
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 判断是否超级管理员
|
|
|
|
+ *
|
|
|
|
+ * @return
|
|
|
|
+ * @author WANGWEI
|
|
|
|
+ */
|
|
|
|
+ protected boolean isSuperAdmin() {
|
|
|
|
+ User accessUser = getAccessUser();
|
|
|
|
+ List<Role> roleList = accessUser.getRoleList();
|
|
|
|
+ for (Role role : roleList) {
|
|
|
|
+ if (role.getRoleCode().equals(RoleMeta.SUPER_ADMIN.name())) {
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
|
- * 判断是否超级管理员
|
|
|
|
- *
|
|
|
|
- * @author WANGWEI
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- protected boolean isSuperAdmin() {
|
|
|
|
- User accessUser = getAccessUser();
|
|
|
|
- List<Role> roleList = accessUser.getRoleList();
|
|
|
|
- for (Role role : roleList) {
|
|
|
|
- if (role.getRoleCode().equals(RoleMeta.SUPER_ADMIN.name())) {
|
|
|
|
- return true;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 验证顶级机构隔离
|
|
|
|
+ *
|
|
|
|
+ * @param rootOrgId
|
|
|
|
+ * @author WANGWEI
|
|
|
|
+ */
|
|
|
|
+ protected void validateRootOrgIsolation(Long rootOrgId) {
|
|
|
|
+ if ((!isSuperAdmin()) && (!rootOrgId.equals(getRootOrgId()))) {
|
|
|
|
+ throw new StatusException("250", "非法请求");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
|
- * 验证顶级机构隔离
|
|
|
|
- *
|
|
|
|
- * @author WANGWEI
|
|
|
|
- * @param rootOrgId
|
|
|
|
- */
|
|
|
|
- protected void validateRootOrgIsolation(Long rootOrgId) {
|
|
|
|
- if ((!isSuperAdmin()) && (!rootOrgId.equals(getRootOrgId()))) {
|
|
|
|
- throw new StatusException("250", "非法请求");
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 获取接入用户的角色ID集合
|
|
|
|
+ *
|
|
|
|
+ * @return
|
|
|
|
+ * @author WANGWEI
|
|
|
|
+ */
|
|
|
|
+ protected List<Long> getAccessUserRoleIdList() {
|
|
|
|
+ List<Role> roleList = getAccessUser().getRoleList();
|
|
|
|
+ List<Long> roleIdList = Lists.newArrayList();
|
|
|
|
+ for (Role cur : roleList) {
|
|
|
|
+ roleIdList.add(cur.getRoleId());
|
|
|
|
+ }
|
|
|
|
+ return roleIdList;
|
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
|
- * 获取接入用户的角色ID集合
|
|
|
|
- *
|
|
|
|
- * @author WANGWEI
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- protected List<Long> getAccessUserRoleIdList() {
|
|
|
|
- List<Role> roleList = getAccessUser().getRoleList();
|
|
|
|
- List<Long> roleIdList = Lists.newArrayList();
|
|
|
|
- for (Role cur : roleList) {
|
|
|
|
- roleIdList.add(cur.getRoleId());
|
|
|
|
- }
|
|
|
|
- return roleIdList;
|
|
|
|
- }
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 判断用户是否拥有指定的角色
|
|
|
|
+ *
|
|
|
|
+ * @param role
|
|
|
|
+ * @return
|
|
|
|
+ * @author WANGWEI
|
|
|
|
+ */
|
|
|
|
+ protected Boolean hasRole(RoleMeta role) {
|
|
|
|
+ List<Role> roleList = getAccessUser().getRoleList();
|
|
|
|
+ for (Role cur : roleList) {
|
|
|
|
+ if (cur.getRoleCode().equals(role.name())) {
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
|
- * 判断用户是否拥有指定的角色
|
|
|
|
- *
|
|
|
|
- * @author WANGWEI
|
|
|
|
- * @param role
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- protected Boolean hasRole(RoleMeta role) {
|
|
|
|
- List<Role> roleList = getAccessUser().getRoleList();
|
|
|
|
- for (Role cur : roleList) {
|
|
|
|
- if (cur.getRoleCode().equals(role.name())) {
|
|
|
|
- return true;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 获取request对象
|
|
|
|
+ *
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ protected HttpServletRequest getRequest() {
|
|
|
|
+ ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
|
|
|
|
+ .getRequestAttributes();
|
|
|
|
+ HttpServletRequest request = requestAttributes.getRequest();
|
|
|
|
+ return request;
|
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
|
- * 获取request对象
|
|
|
|
- *
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- protected HttpServletRequest getRequest() {
|
|
|
|
- ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
|
|
|
|
- .getRequestAttributes();
|
|
|
|
- HttpServletRequest request = requestAttributes.getRequest();
|
|
|
|
- return request;
|
|
|
|
- }
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 获取response对象
|
|
|
|
+ *
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ protected HttpServletResponse getResponse() {
|
|
|
|
+ ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
|
|
|
|
+ .getRequestAttributes();
|
|
|
|
+ HttpServletResponse response = requestAttributes.getResponse();
|
|
|
|
+ return response;
|
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
|
- * 获取response对象
|
|
|
|
- *
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- protected HttpServletResponse getResponse() {
|
|
|
|
- ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
|
|
|
|
- .getRequestAttributes();
|
|
|
|
- HttpServletResponse response = requestAttributes.getResponse();
|
|
|
|
- return response;
|
|
|
|
- }
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 文件导出
|
|
|
|
+ *
|
|
|
|
+ * @param fileName
|
|
|
|
+ * @param file
|
|
|
|
+ * @author WANGWEI
|
|
|
|
+ */
|
|
|
|
+ protected void exportFile(String fileName, File file) {
|
|
|
|
+ OutputStream out = null;
|
|
|
|
+ InputStream in = null;
|
|
|
|
+ try {
|
|
|
|
+ in = new FileInputStream(file);
|
|
|
|
+ fileName = URLEncoder.encode(fileName, "UTF-8");
|
|
|
|
+ HttpServletResponse response = getResponse();
|
|
|
|
+ response.reset();
|
|
|
|
+ response.setHeader("Content-Disposition", "inline; filename=" + fileName);
|
|
|
|
+ response.addHeader("Content-Length", "" + file.length());
|
|
|
|
+ response.setContentType("application/octet-stream;charset=UTF-8");
|
|
|
|
+ out = new BufferedOutputStream(response.getOutputStream());
|
|
|
|
+ IOUtils.copy(in, out);
|
|
|
|
+ out.flush();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new ExamCloudRuntimeException(e);
|
|
|
|
+ } finally {
|
|
|
|
+ IOUtils.closeQuietly(out);
|
|
|
|
+ IOUtils.closeQuietly(in);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
|
- * 文件导出
|
|
|
|
- *
|
|
|
|
- * @author WANGWEI
|
|
|
|
- * @param fileName
|
|
|
|
- * @param file
|
|
|
|
- */
|
|
|
|
- protected void exportFile(String fileName, File file) {
|
|
|
|
- OutputStream out = null;
|
|
|
|
- InputStream in = null;
|
|
|
|
- try {
|
|
|
|
- in = new FileInputStream(file);
|
|
|
|
- fileName = URLEncoder.encode(fileName, "UTF-8");
|
|
|
|
- HttpServletResponse response = getResponse();
|
|
|
|
- response.reset();
|
|
|
|
- response.setHeader("Content-Disposition", "inline; filename=" + fileName);
|
|
|
|
- response.addHeader("Content-Length", "" + file.length());
|
|
|
|
- response.setContentType("application/octet-stream;charset=UTF-8");
|
|
|
|
- out = new BufferedOutputStream(response.getOutputStream());
|
|
|
|
- IOUtils.copy(in, out);
|
|
|
|
- out.flush();
|
|
|
|
- } catch (IOException e) {
|
|
|
|
- throw new ExamCloudRuntimeException(e);
|
|
|
|
- } finally {
|
|
|
|
- IOUtils.closeQuietly(out);
|
|
|
|
- IOUtils.closeQuietly(in);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 文件导出
|
|
|
|
+ *
|
|
|
|
+ * @param fileName
|
|
|
|
+ * @param bytes
|
|
|
|
+ * @author WANGWEI
|
|
|
|
+ */
|
|
|
|
+ protected void exportFile(String fileName, byte[] bytes) {
|
|
|
|
+ OutputStream out = null;
|
|
|
|
+ try {
|
|
|
|
+ fileName = URLEncoder.encode(fileName, "UTF-8");
|
|
|
|
+ HttpServletResponse response = getResponse();
|
|
|
|
+ response.reset();
|
|
|
|
+ response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
|
|
|
|
+ response.addHeader("Content-Length", "" + bytes.length);
|
|
|
|
+ response.setContentType("application/octet-stream;charset=UTF-8");
|
|
|
|
+ out = new BufferedOutputStream(response.getOutputStream());
|
|
|
|
+ out.write(bytes);
|
|
|
|
+ out.flush();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new ExamCloudRuntimeException(e);
|
|
|
|
+ } finally {
|
|
|
|
+ IOUtils.closeQuietly(out);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
|
- * 文件导出
|
|
|
|
- *
|
|
|
|
- * @author WANGWEI
|
|
|
|
- * @param fileName
|
|
|
|
- * @param bytes
|
|
|
|
- */
|
|
|
|
- protected void exportFile(String fileName, byte[] bytes) {
|
|
|
|
- OutputStream out = null;
|
|
|
|
- try {
|
|
|
|
- fileName = URLEncoder.encode(fileName, "UTF-8");
|
|
|
|
- HttpServletResponse response = getResponse();
|
|
|
|
- response.reset();
|
|
|
|
- response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
|
|
|
|
- response.addHeader("Content-Length", "" + bytes.length);
|
|
|
|
- response.setContentType("application/octet-stream;charset=UTF-8");
|
|
|
|
- out = new BufferedOutputStream(response.getOutputStream());
|
|
|
|
- out.write(bytes);
|
|
|
|
- out.flush();
|
|
|
|
- } catch (IOException e) {
|
|
|
|
- throw new ExamCloudRuntimeException(e);
|
|
|
|
- } finally {
|
|
|
|
- IOUtils.closeQuietly(out);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 转换为数据库模糊查询匹配模式
|
|
|
|
+ */
|
|
|
|
+ protected String toSqlSearchPattern(String column) {
|
|
|
|
+ if (StringUtils.isBlank(column)) {
|
|
|
|
+ return "%";
|
|
|
|
+ }
|
|
|
|
+ return "%" + column.trim() + "%";
|
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
|
- * 转换为数据库模糊查询匹配模式
|
|
|
|
- *
|
|
|
|
- * @author WANGWEI
|
|
|
|
- * @param column
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- protected String toSqlSearchPattern(String column) {
|
|
|
|
- if (StringUtils.isBlank(column)) {
|
|
|
|
- return "%";
|
|
|
|
- } else {
|
|
|
|
- column = column.trim().replaceAll("\\s", "%");
|
|
|
|
- column = "%" + column + "%";
|
|
|
|
- return column;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 转换为数据库模糊查询匹配模式 (左边全匹配、右边模糊匹配)
|
|
|
|
+ */
|
|
|
|
+ protected String toSqlRightLike(String column) {
|
|
|
|
+ if (StringUtils.isBlank(column)) {
|
|
|
|
+ return "%";
|
|
|
|
+ }
|
|
|
|
+ return column.trim() + "%";
|
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
|
- * 获取非空参数
|
|
|
|
- *
|
|
|
|
- * @author WANGWEI
|
|
|
|
- * @param paramName
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- protected String getRequiredStringParam(String paramName) {
|
|
|
|
- String value = getRequest().getParameter(paramName);
|
|
|
|
- if (StringUtils.isBlank(value)) {
|
|
|
|
- throw new StatusException("520", "param is missing. paramName=" + paramName);
|
|
|
|
- } else {
|
|
|
|
- return value.trim();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 获取非空参数
|
|
|
|
+ *
|
|
|
|
+ * @param paramName
|
|
|
|
+ * @return
|
|
|
|
+ * @author WANGWEI
|
|
|
|
+ */
|
|
|
|
+ protected String getRequiredStringParam(String paramName) {
|
|
|
|
+ String value = getRequest().getParameter(paramName);
|
|
|
|
+ if (StringUtils.isBlank(value)) {
|
|
|
|
+ throw new StatusException("520", "param is missing. paramName=" + paramName);
|
|
|
|
+ } else {
|
|
|
|
+ return value.trim();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
|
- * 获取参数
|
|
|
|
- *
|
|
|
|
- * @author WANGWEI
|
|
|
|
- * @param paramName
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- protected String getStringParam(String paramName) {
|
|
|
|
- String value = getRequest().getParameter(paramName);
|
|
|
|
- if (StringUtils.isBlank(value)) {
|
|
|
|
- return null;
|
|
|
|
- } else {
|
|
|
|
- return value.trim();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 获取参数
|
|
|
|
+ *
|
|
|
|
+ * @param paramName
|
|
|
|
+ * @return
|
|
|
|
+ * @author WANGWEI
|
|
|
|
+ */
|
|
|
|
+ protected String getStringParam(String paramName) {
|
|
|
|
+ String value = getRequest().getParameter(paramName);
|
|
|
|
+ if (StringUtils.isBlank(value)) {
|
|
|
|
+ return null;
|
|
|
|
+ } else {
|
|
|
|
+ return value.trim();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
|
- * 参数trim.<br>
|
|
|
|
- * set null if blank.
|
|
|
|
- *
|
|
|
|
- * @author WANGWEI
|
|
|
|
- * @param bean
|
|
|
|
- */
|
|
|
|
- protected void trim(Object bean) {
|
|
|
|
- trim(bean, false);
|
|
|
|
- }
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 参数trim.<br>
|
|
|
|
+ * set null if blank.
|
|
|
|
+ *
|
|
|
|
+ * @param bean
|
|
|
|
+ * @author WANGWEI
|
|
|
|
+ */
|
|
|
|
+ protected void trim(Object bean) {
|
|
|
|
+ trim(bean, false);
|
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
|
- * 参数trim
|
|
|
|
- *
|
|
|
|
- * @author WANGWEI
|
|
|
|
- * @param bean
|
|
|
|
- * @param nullIfBlank
|
|
|
|
- */
|
|
|
|
- protected void trim(Object bean, boolean nullIfBlank) {
|
|
|
|
- if (null == bean) {
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- Class<? extends Object> clazz = bean.getClass();
|
|
|
|
- if (clazz.equals(Map.class)) {
|
|
|
|
- return;
|
|
|
|
- } else if (clazz.equals(List.class)) {
|
|
|
|
- return;
|
|
|
|
- } else if (clazz.equals(Set.class)) {
|
|
|
|
- return;
|
|
|
|
- } else if (clazz.isEnum()) {
|
|
|
|
- return;
|
|
|
|
- } else if (ObjectUtil.isBaseDataType(clazz)) {
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 参数trim
|
|
|
|
+ *
|
|
|
|
+ * @param bean
|
|
|
|
+ * @param nullIfBlank
|
|
|
|
+ * @author WANGWEI
|
|
|
|
+ */
|
|
|
|
+ protected void trim(Object bean, boolean nullIfBlank) {
|
|
|
|
+ if (null == bean) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ Class<? extends Object> clazz = bean.getClass();
|
|
|
|
+ if (clazz.equals(Map.class)) {
|
|
|
|
+ return;
|
|
|
|
+ } else if (clazz.equals(List.class)) {
|
|
|
|
+ return;
|
|
|
|
+ } else if (clazz.equals(Set.class)) {
|
|
|
|
+ return;
|
|
|
|
+ } else if (clazz.isEnum()) {
|
|
|
|
+ return;
|
|
|
|
+ } else if (ObjectUtil.isBaseDataType(clazz)) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
|
|
- Field[] fields = clazz.getDeclaredFields();
|
|
|
|
- try {
|
|
|
|
- for (int i = 0; i < fields.length; i++) {
|
|
|
|
- Field f = fields[i];
|
|
|
|
- f.setAccessible(true);
|
|
|
|
- Object value = f.get(bean);
|
|
|
|
- if (null == value) {
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
- if (f.getType().equals(String.class)) {
|
|
|
|
- if (null != value) {
|
|
|
|
- String s = (String) value;
|
|
|
|
- s = s.trim();
|
|
|
|
- if (nullIfBlank && StringUtils.isBlank(s)) {
|
|
|
|
- s = null;
|
|
|
|
- }
|
|
|
|
- f.set(bean, s);
|
|
|
|
- }
|
|
|
|
- } else if (f.getType().equals(Map.class)) {
|
|
|
|
- } else if (f.getType().equals(List.class)) {
|
|
|
|
- } else if (f.getType().equals(Set.class)) {
|
|
|
|
- } else if (f.getType().isEnum()) {
|
|
|
|
- } else if (ObjectUtil.isBaseDataType(f.getType())) {
|
|
|
|
- } else {
|
|
|
|
- trim(value);
|
|
|
|
- }
|
|
|
|
|
|
+ Field[] fields = clazz.getDeclaredFields();
|
|
|
|
+ try {
|
|
|
|
+ for (int i = 0; i < fields.length; i++) {
|
|
|
|
+ Field f = fields[i];
|
|
|
|
+ f.setAccessible(true);
|
|
|
|
+ Object value = f.get(bean);
|
|
|
|
+ if (null == value) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ if (f.getType().equals(String.class)) {
|
|
|
|
+ if (null != value) {
|
|
|
|
+ String s = (String) value;
|
|
|
|
+ s = s.trim();
|
|
|
|
+ if (nullIfBlank && StringUtils.isBlank(s)) {
|
|
|
|
+ s = null;
|
|
|
|
+ }
|
|
|
|
+ f.set(bean, s);
|
|
|
|
+ }
|
|
|
|
+ } else if (f.getType().equals(Map.class)) {
|
|
|
|
+ } else if (f.getType().equals(List.class)) {
|
|
|
|
+ } else if (f.getType().equals(Set.class)) {
|
|
|
|
+ } else if (f.getType().isEnum()) {
|
|
|
|
+ } else if (ObjectUtil.isBaseDataType(f.getType())) {
|
|
|
|
+ } else {
|
|
|
|
+ trim(value);
|
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- throw new ExamCloudRuntimeException(e);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new ExamCloudRuntimeException(e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
}
|
|
}
|