diff options
| author | zhanghongqing <[email protected]> | 2022-08-09 16:54:16 +0800 |
|---|---|---|
| committer | zhanghongqing <[email protected]> | 2022-08-09 16:54:16 +0800 |
| commit | b3fa11d4b1b5a68d7b04fde5eb6cfbda557927eb (patch) | |
| tree | a49d344e49fc427fbf4cf00aa4963c4d04cd98a4 /src/main/java/com/mesasoft/cn/util | |
| parent | d8a2be0d094ac9ba2d47c81ebf03b3fe6e34a078 (diff) | |
Diffstat (limited to 'src/main/java/com/mesasoft/cn/util')
| -rw-r--r-- | src/main/java/com/mesasoft/cn/util/BeanUtils.java | 77 | ||||
| -rw-r--r-- | src/main/java/com/mesasoft/cn/util/CommonUtils.java | 26 | ||||
| -rw-r--r-- | src/main/java/com/mesasoft/cn/util/ConfigUtils.java | 46 | ||||
| -rw-r--r-- | src/main/java/com/mesasoft/cn/util/Contants.java | 54 | ||||
| -rw-r--r-- | src/main/java/com/mesasoft/cn/util/ControllerUtils.java | 152 | ||||
| -rw-r--r-- | src/main/java/com/mesasoft/cn/util/FileUtils.java | 197 | ||||
| -rw-r--r-- | src/main/java/com/mesasoft/cn/util/MariaDBUtils.java | 80 | ||||
| -rw-r--r-- | src/main/java/com/mesasoft/cn/util/MariaDbBase.java | 84 | ||||
| -rw-r--r-- | src/main/java/com/mesasoft/cn/util/ServiceUtils.java | 59 | ||||
| -rw-r--r-- | src/main/java/com/mesasoft/cn/util/TimeUtils.java | 59 | ||||
| -rw-r--r-- | src/main/java/com/mesasoft/cn/util/ValidationUtils.java | 202 | ||||
| -rw-r--r-- | src/main/java/com/mesasoft/cn/util/Verify.java | 20 |
12 files changed, 1056 insertions, 0 deletions
diff --git a/src/main/java/com/mesasoft/cn/util/BeanUtils.java b/src/main/java/com/mesasoft/cn/util/BeanUtils.java new file mode 100644 index 0000000..152c490 --- /dev/null +++ b/src/main/java/com/mesasoft/cn/util/BeanUtils.java @@ -0,0 +1,77 @@ +package com.mesasoft.cn.util; + +import com.alibaba.fastjson.JSONObject; +import com.zhazhapan.modules.constant.ValueConsts; +import com.zhazhapan.util.Checker; +import com.zhazhapan.util.Formatter; +import com.zhazhapan.util.enums.FieldModifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.reflect.InvocationTargetException; + +/** + * @author pantao + * @since 2018/1/18 + */ +public class BeanUtils { + + private static final String ERROR_JSON = "{\"error\":\"internal error, please try again later\"}"; + + private static Logger logger = LoggerFactory.getLogger(BeanUtils.class); + + private BeanUtils() {} + + /** + * 将权限字符串装换成权限数组 + * + * @param auth 权限字符串 + * + * @return 权限数组 + */ + public static int[] getAuth(String auth) { + int[] a = new int[5]; + if (Checker.isNotEmpty(auth)) { + String[] u = auth.split(ValueConsts.COMMA_SIGN); + int len = Math.min(a.length, u.length); + for (int i = 0; i < len; i++) { + a[i] = Formatter.stringToInt(u[i]); + } + } + return a; + } + + /** + * 将Bean转换成JSON + * + * @param object Bean对象 + * + * @return {@link String} + */ + public static String toPrettyJson(Object object) { + String result; + try { + result = com.zhazhapan.util.BeanUtils.toPrettyJson(object, FieldModifier.PRIVATE); + } catch (IllegalAccessException e) { + result = Formatter.formatJson(ERROR_JSON); + logger.error(e.getMessage()); + } + return result; + } + + /** + * 将类属性装换成JSON(只能转换有get方法的) + * + * @param object 转换的对象 + * + * @return {@link JSONObject} + */ + public static JSONObject beanToJson(Object object) { + try { + return com.zhazhapan.util.BeanUtils.beanToJson(object); + } catch (IllegalAccessException | InvocationTargetException e) { + logger.error(e.getMessage()); + return null; + } + } +} diff --git a/src/main/java/com/mesasoft/cn/util/CommonUtils.java b/src/main/java/com/mesasoft/cn/util/CommonUtils.java new file mode 100644 index 0000000..eebb49b --- /dev/null +++ b/src/main/java/com/mesasoft/cn/util/CommonUtils.java @@ -0,0 +1,26 @@ +package com.mesasoft.cn.util; + +import com.mesasoft.cn.modules.constant.DefaultValues; +import com.zhazhapan.modules.constant.ValueConsts; + +/** + * @author pantao + * @since 2018/1/29 + */ +public class CommonUtils { + + private CommonUtils() {} + + /** + * 将相对路径转换成绝对路径 + * + * @param path 文件路径 + * + * @return {@link String} + */ + public static String checkPath(String path) { + String prefix = DefaultValues.COLON + ValueConsts.SEPARATOR; + return path.startsWith(ValueConsts.SEPARATOR) || path.startsWith(prefix, ValueConsts.ONE_INT) ? path : + DefaultValues.STORAGE_PATH + path; + } +} diff --git a/src/main/java/com/mesasoft/cn/util/ConfigUtils.java b/src/main/java/com/mesasoft/cn/util/ConfigUtils.java new file mode 100644 index 0000000..8aedad0 --- /dev/null +++ b/src/main/java/com/mesasoft/cn/util/ConfigUtils.java @@ -0,0 +1,46 @@ +package com.mesasoft.cn.util; + + +import org.apache.log4j.Logger; + +import java.util.Properties; + +public class ConfigUtils { + private static final Logger LOG = Logger.getLogger(ConfigUtils.class); + private static Properties propCommon = new Properties(); + + public static String getStringProperty(String key) { + return propCommon.getProperty(key); + } + + public static Integer getIntProperty(String key) { + return Integer.parseInt(propCommon.getProperty(key)); + } + + public static Long getLongProperty(String key) { + return Long.parseLong(propCommon.getProperty(key)); + } + + public static Boolean getBooleanProperty(String key) { + return "true".equals(propCommon.getProperty(key).toLowerCase().trim()); + } + + public static String getEffectiveString(String s) { + if (!(s == null)) { + return s.length() == 0 ? null : s; + } else { + return null; + } + } + + static { + try { + propCommon.load(ConfigUtils.class.getClassLoader().getResourceAsStream("sketch.properties")); + + } catch (Exception e) { + propCommon = null; + LOG.error("配置加载失败"); + } + } + +} diff --git a/src/main/java/com/mesasoft/cn/util/Contants.java b/src/main/java/com/mesasoft/cn/util/Contants.java new file mode 100644 index 0000000..2989133 --- /dev/null +++ b/src/main/java/com/mesasoft/cn/util/Contants.java @@ -0,0 +1,54 @@ +package com.mesasoft.cn.util; + +import com.google.common.collect.ImmutableMap; + +import java.util.Map; + +/** + * @description, + * @author, zhq + * @create, 2022-03-18 + **/ +public class Contants { + + public static final Map<String, String> CONTENT_TYPES = ImmutableMap.<String, String>builder() + .put("doc", "application/msword") + .put("bin", "application/octet-stream") + .put("exe", "application/octet-stream") + .put("so", "application/octet-stream") + .put("dll", "application/octet-stream") + .put("pdf", "application/pdf") + .put("ai", "application/postscript") + .put("xls", "application/vnd.ms-excel") + .put("ppt", "application/vnd.ms-powerpoint") + .put("dir", "application/x-director") + .put("js", "application/x-javascript") + .put("swf", "application/x-shockwave-flash") + .put("xhtml", "application/xhtml+xml") + .put("xht", "application/xhtml+xml") + .put("zip", "application/zip") + .put("mid", "audio/midi") + .put("midi", "audio/midi") + .put("mp3", "audio/mpeg") + .put("rm", "audio/x-pn-realaudio") + .put("rpm", "audio/x-pn-realaudio-plugin") + .put("wav", "audio/x-wav") + .put("bmp", "image/bmp") + .put("gif", "image/gif") + .put("jpeg", "image/jpeg") + .put("jpg", "image/jpeg") + .put("png", "image/png") + .put("css", "text/css") + .put("html", "text/html") + .put("htm", "text/html") + .put("txt", "text/plain") + .put("xsl", "text/xml") + .put("xml", "text/xml") + .put("mpeg", "video/mpeg") + .put("mpg", "video/mpeg") + .put("avi", "video/x-msvideo") + .put("movie", "video/x-sgi-movie") + .put(".csv", "text/csv" + ).build(); + +} diff --git a/src/main/java/com/mesasoft/cn/util/ControllerUtils.java b/src/main/java/com/mesasoft/cn/util/ControllerUtils.java new file mode 100644 index 0000000..e551434 --- /dev/null +++ b/src/main/java/com/mesasoft/cn/util/ControllerUtils.java @@ -0,0 +1,152 @@ +package com.mesasoft.cn.util; + +import cn.hutool.core.io.FileUtil; +import cn.hutool.core.io.IoUtil; +import cn.hutool.core.text.UnicodeUtil; +import com.alibaba.fastjson.JSONObject; +import com.mesasoft.cn.modules.constant.DefaultValues; +import com.zhazhapan.util.Checker; +import org.apache.commons.lang3.StringUtils; + +import javax.activation.MimetypesFileTypeMap; +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.*; + +/** + * @author pantao + * @since 2018/1/30 + */ +public class ControllerUtils { + + private ControllerUtils() { + } + + /** + * 获取一个简单的响应状态 + * + * @param isSuccess 是否操作成功 + * @return 响应JSON字符串 + */ + public static String getResponse(boolean isSuccess) { + JSONObject jsonObject = new JSONObject(); + if (isSuccess) { + jsonObject.put("status", "success"); + } else { + jsonObject.put("status", "error"); + } + return jsonObject.toString(); + } + + /** + * 加载本地资源 + * + * @param response 返回的Response + * @param path 资源路径 + * @param download 直接下载 + */ + public static void loadResource2(HttpServletResponse response, String path, boolean download) throws IOException { + if (Checker.isNotEmpty(path)) { + File file = new File(path); + if (download) { + response.setContentType(getContentType(file)+";charset=UTF-8"); + setResponseFileName2(response, file.getName()); + } + FileInputStream in = new FileInputStream(file); + ServletOutputStream os = response.getOutputStream(); + byte[] b; + while (in.available() > 0) { + b = in.available() > 1024 ? new byte[1024] : new byte[in.available()]; + in.read(b, 0, b.length); + os.write(b, 0, b.length); + } + in.close(); + os.flush(); + os.close(); + } else { + response.sendRedirect(DefaultValues.NOT_FOUND_PAGE); + } + } + public static void loadResource(HttpServletResponse response, String path, boolean download) throws IOException { + if (Checker.isNotEmpty(path)) { + File file = new File(path); + if (download) { + response.setContentType(getContentType(file)); + setResponseFileName( response, file.getName()); + response.setCharacterEncoding("UTF-8"); + } + FileInputStream in = new FileInputStream(file); + ServletOutputStream os = response.getOutputStream(); + byte[] b; + while (in.available() > 0) { + b = in.available() > 1024 ? new byte[1024] : new byte[in.available()]; + in.read(b, 0, b.length); + os.write(b, 0, b.length); + } + in.close(); + os.flush(); + os.close(); + } else { + response.sendRedirect(DefaultValues.NOT_FOUND_PAGE); + } + } + public static String getContentType(File file) { + String defContentType = "application/octet-stream"; + String fileName = file.getName(); + String fileTyle=fileName.substring(fileName.lastIndexOf(".")+1,fileName.length()); + if (StringUtils.isNotBlank(fileTyle)) { + String type2 = Contants.CONTENT_TYPES.get(fileTyle); + if (StringUtils.isNotBlank(type2)) { + return type2; + } + } else { + String type1 = new MimetypesFileTypeMap().getContentType(file); + if (StringUtils.isNotBlank(type1)) { + return type1; + } + } + return defContentType; + } + + /** + * 设置响应头的文件名 + * + * @param response {@link HttpServletResponse} + * @param fileName 文件名 + */ + public static void setResponseFileName2(HttpServletResponse response, String fileName) { + response.setHeader("Content-Disposition", "attachment;filename=" + UnicodeUtil.toUnicode(fileName)); + } + public static void setResponseFileName( HttpServletResponse response, String fileName) throws + UnsupportedEncodingException { + response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("UTF-8"), + "ISO-8859-1")); + } + + public static void loadFile(HttpServletRequest request, HttpServletResponse response,String filePath,boolean download) throws IOException { + request.setCharacterEncoding("utf-8"); + // 文件存储路径 + // 从请求中获取文件名 + File file=new File(filePath); + String fileName=file.getName(); + // 创建输出流对象 + ServletOutputStream outputStream = response.getOutputStream(); + //以字节数组的形式读取文件 + byte[] bytes = FileUtil.readBytes(filePath); + // 设置返回内容格式 + response.setContentType("application/octet-stream;charset=UTF-8"); + // 把文件名按UTF-8取出并按ISO8859-1编码,保证弹出窗口中的文件名中文不乱码 + // 中文不要太多,最多支持17个中文,因为header有150个字节限制。 + response.setHeader("filename", UnicodeUtil.toUnicode(fileName)); + // 这一步一定要在读取文件之后进行,否则文件名会乱码,找不到文件 + fileName = new String(fileName.getBytes("UTF-8"),"ISO-8859-1"); + // 设置下载弹窗的文件名和格式(文件名要包括名字和文件格式) + response.setHeader("Content-Disposition", "attachment;filename=" + fileName); + // 返回数据到输出流对象中 + outputStream.write(bytes); + // 关闭流对象 + IoUtil.close(outputStream); + + } +} diff --git a/src/main/java/com/mesasoft/cn/util/FileUtils.java b/src/main/java/com/mesasoft/cn/util/FileUtils.java new file mode 100644 index 0000000..773843d --- /dev/null +++ b/src/main/java/com/mesasoft/cn/util/FileUtils.java @@ -0,0 +1,197 @@ +package com.mesasoft.cn.util; + +import org.apache.log4j.Logger; + +import java.io.*; +import java.util.ArrayList; +import java.util.List; + +/** + * @author yjy + * @version 1.0 + * @date 2021/2/25 6:11 下午 + */ +public class FileUtils { + private static final Logger LOG = Logger.getLogger(FileUtils.class); + + public static List<String> readTxtFileIntoStringArrList(String filePath) + { + List<String> list = new ArrayList<>(); + try + { + String encoding = "GBK"; + File file = new File(filePath); + if (file.isFile() && file.exists()) + { // 判断文件是否存在 + InputStreamReader read = new InputStreamReader( + new FileInputStream(file), encoding); + BufferedReader bufferedReader = new BufferedReader(read); + String lineTxt = null; + + while ((lineTxt = bufferedReader.readLine()) != null) + { + if (!lineTxt.equals("")) { + list.add(lineTxt.trim()); + } + } + bufferedReader.close(); + read.close(); + } + else + { + System.out.println("Can not find file: " + filePath); + } + } + catch (Exception e) + { + System.out.println("Error occurred in Function 'readTxtFileIntoStringArrList'"); + e.printStackTrace(); + } + + return list; + } + + public static List<String> getBatchLineReadIn(BufferedReader bufferedReader, int batchSize){ + List<String> list = new ArrayList<>(); + String lineTxt; + try{ + while ((lineTxt = bufferedReader.readLine()) != null && list.size()<batchSize) + { + if (!lineTxt.equals("")) { + list.add(lineTxt.trim()); + } + } + } catch (IOException e){ + e.printStackTrace(); + } + return list; + } + + public static void createFile(File filePath, String fileName){ + try { + File file = new File(filePath.toString() + "/" + fileName); + + if (!filePath.exists()){ + filePath.mkdirs(); + } + + boolean isCreate = file.createNewFile(); + if (isCreate){ + LOG.info("File " + fileName + " is created."); + } + } catch (IOException e) { + e.printStackTrace(); + } + + } + + public static void createFile(File file){ + try { + boolean isCreate = file.createNewFile(); + if (isCreate){ + LOG.info("File " + file + " is created."); + } + } catch (IOException e) { + e.printStackTrace(); + } + + } + + + public static String readJsonFile(String fileName) { + String jsonStr = ""; + try { + File jsonFile = new File(fileName); + FileReader fileReader = new FileReader(jsonFile); + + Reader reader = new InputStreamReader(new FileInputStream(jsonFile), "utf-8"); + int ch = 0; + StringBuffer sb = new StringBuffer(); + while ((ch = reader.read()) != -1) { + sb.append((char) ch); + } + + fileReader.close(); + reader.close(); + jsonStr = sb.toString(); + return jsonStr; + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } + + public static String getFileName(File file){ + String[] tmp = file.toString().split("/"); + String fileName = tmp[tmp.length-1]; + return fileName; + } + + + public static void writerClose(OutputStreamWriter outWriter, OutputStream outStream) throws IOException { + assert outWriter != null; + outWriter.close(); + outStream.close(); + } + + public static void readerClose(BufferedReader bufferedReader, InputStreamReader inputStreamReader) throws IOException { + assert inputStreamReader != null; + bufferedReader.close(); + inputStreamReader.close(); + } + + //执行cmd命令,获取返回结果 + public static String execCMD(String command) { + StringBuilder sb =new StringBuilder(); + try { + Process process=Runtime.getRuntime().exec(command); + BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(process.getInputStream())); + String line; + while((line=bufferedReader.readLine())!=null) + { + sb.append(line+"\n"); + } + } catch (Exception e) { + return e.toString(); + } + return sb.toString(); + } + + public static Long getFileLineNum(File file){ + Long num = 0L; + if (!file.exists()){ + LOG.error("File not exist: " + file.toString()); + } else { + String res = FileUtils.execCMD("wc -l " + file.toString()); + num = Long.parseLong(res.trim().split(" ")[0]); + } + return num; + } + + public static int getMaxLength(String filePath) { + int lengthDomain = 0; + try { + String encoding = "UTF-8"; + File file = new File(filePath); + if (file.isFile() && file.exists()) { + InputStreamReader read = new InputStreamReader( + new FileInputStream(file), encoding); + BufferedReader bufferedReader = new BufferedReader(read); + String lineTxt = null; + while ((lineTxt = bufferedReader.readLine()) != null) { + String[] split = lineTxt.split("\\."); + if (split.length > lengthDomain) { + lengthDomain = split.length; + } + } + read.close(); + } else { + LOG.error("FilePath is wrong--->{" + filePath + "}<---"); + } + } catch (Exception e) { + LOG.error("Get filePathData error--->{" + e + "}<---"); + e.printStackTrace(); + } + return lengthDomain; + } +} diff --git a/src/main/java/com/mesasoft/cn/util/MariaDBUtils.java b/src/main/java/com/mesasoft/cn/util/MariaDBUtils.java new file mode 100644 index 0000000..a86b363 --- /dev/null +++ b/src/main/java/com/mesasoft/cn/util/MariaDBUtils.java @@ -0,0 +1,80 @@ +package com.mesasoft.cn.util; + +import com.alibaba.druid.pool.DruidDataSourceFactory; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Properties; + +/** + * Druid连接池的工具类 + */ +public class MariaDBUtils { + private static DataSource ds ; + + static{ + try { + Properties pro = new Properties(); +// pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties")); + ds = DruidDataSourceFactory.createDataSource(pro); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * 获取连接 + */ + public static Connection getConnection() throws SQLException { + return ds.getConnection(); + } + + /** + * 释放资源 + */ + public static void close(Statement stmt,Connection conn){ + + close(null,stmt,conn); + } + + + public static void close(ResultSet rs , Statement stmt, Connection conn){ + + + if(rs != null){ + try { + rs.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + + if(stmt != null){ + try { + stmt.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + if(conn != null){ + try { + conn.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + + /** + * 获取连接池方法 + */ + public static DataSource getDataSource(){ + return ds; + } + +}
\ No newline at end of file diff --git a/src/main/java/com/mesasoft/cn/util/MariaDbBase.java b/src/main/java/com/mesasoft/cn/util/MariaDbBase.java new file mode 100644 index 0000000..777ec44 --- /dev/null +++ b/src/main/java/com/mesasoft/cn/util/MariaDbBase.java @@ -0,0 +1,84 @@ +package com.mesasoft.cn.util; + +import com.mesasoft.cn.sketch.config.ApplicationConfig; +import org.apache.log4j.Logger; + +import java.sql.*; +import java.util.Date; +import java.util.Properties; + +/** + * Created with IntelliJ IDEA. + * User: joy + * Date: 2021/12/28 + * Time: 2:56 PM + * Description: No Description + */ +public class MariaDbBase { + + private static final Logger LOG = Logger.getLogger(MariaDbBase.class); + private static final Properties props = new Properties(); + + private final Statement statement; + + public MariaDbBase(Connection conn, Statement stat) { + statement = stat; + } + + /** + * 执行写入sql + */ + public void writeSqlExecute(String sql){ + try { + statement.executeUpdate(sql); + } catch (SQLIntegrityConstraintViolationException e){ + LOG.error("Duplicated entry for key 'PRIMARY'"); + } catch (SQLException exception) { + LOG.error("Sql : " + sql); + exception.printStackTrace(); + } + } + + /** + * 执行查询sql + */ + public ResultSet querySqlExecute(String sql){ + ResultSet set = null; + try { + set = statement.executeQuery(sql); + } catch (SQLException exception) { + exception.printStackTrace(); + } + return set; + } + + + /** + * 获得指定表格、按指定时间字段的过期记录 + * @param tableName 库表名称 + * @param timeColumnName 时间列名 + * @return 查询结果 + */ + public ResultSet getExpiredRecord(String tableName, String timeColumnName){ + Date lastUpdateTime = new Timestamp(getExpiredTime(ApplicationConfig.UPDATE_EXPIRED_DAY).getTime()); + + String resSql = "SELECT *" + + " FROM " + ApplicationConfig.DATABASE + "." + tableName + + " WHERE " + timeColumnName + " < '" + lastUpdateTime + '\''; + + LOG.debug("Update task: expired query sql" + resSql); + + return querySqlExecute(resSql); + } + + /** + * TODO: getUnlabeledRecord() 考虑多个来源的情况 + */ + + /** + * 获得过期时间, 当前时间的expiredRangeDays天之前的日期为过期日期 + */ + public static Date getExpiredTime(int expiredRangeDays){ + return new Timestamp(TimeUtils.getStartOfDay(-expiredRangeDays).getTime()); + } +} diff --git a/src/main/java/com/mesasoft/cn/util/ServiceUtils.java b/src/main/java/com/mesasoft/cn/util/ServiceUtils.java new file mode 100644 index 0000000..2e3b542 --- /dev/null +++ b/src/main/java/com/mesasoft/cn/util/ServiceUtils.java @@ -0,0 +1,59 @@ +package com.mesasoft.cn.util; + +import com.mesasoft.cn.service.ICategoryService; +import com.mesasoft.cn.service.IFileService; +import com.mesasoft.cn.service.IUserService; +import com.zhazhapan.modules.constant.ValueConsts; +import com.zhazhapan.util.Checker; +import com.zhazhapan.util.ReflectUtils; +import org.apache.log4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.lang.reflect.InvocationTargetException; + +/** + * @author pantao + * @since 2018/2/28 + */ +@Component +public class ServiceUtils { + + private static IUserService userService; + + private static IFileService fileService; + + private static ICategoryService categoryService; + + private static Logger logger = Logger.getLogger(ServiceUtils.class); + + @Autowired + public ServiceUtils(IUserService userService, IFileService fileService, ICategoryService categoryService) { + ServiceUtils.userService = userService; + ServiceUtils.fileService = fileService; + ServiceUtils.categoryService = categoryService; + } + + public static int getUserId(String usernameOrEmail) { + return Checker.isEmpty(usernameOrEmail) ? ValueConsts.ZERO_INT : userService.getUserId(usernameOrEmail); + } + + public static long getFileId(String fileName) { + return Checker.isEmpty(fileName) ? ValueConsts.ZERO_INT : fileService.getFileId(fileName); + } + + public static int getCategoryId(String categoryName) { + return Checker.isEmpty(categoryName) ? ValueConsts.ZERO_INT : categoryService.getIdByName(categoryName); + } + + public static Object invokeFileFilter(Object object, String methodName, String user, String file, String + category, int offset) { + try { + return ReflectUtils.invokeMethodUseBasicType(object, methodName, new Object[]{getUserId(user), getFileId + (file), file, getCategoryId(category), offset}); + } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { + logger.error(e.getMessage()); + return null; + } + } +} diff --git a/src/main/java/com/mesasoft/cn/util/TimeUtils.java b/src/main/java/com/mesasoft/cn/util/TimeUtils.java new file mode 100644 index 0000000..5e128dd --- /dev/null +++ b/src/main/java/com/mesasoft/cn/util/TimeUtils.java @@ -0,0 +1,59 @@ +package com.mesasoft.cn.util; + +import java.util.Calendar; +import java.util.Date; + +/** + * @author yjy + * @version 1.0 + * @date 2021/2/25 11:26 上午 + */ +public class TimeUtils { + public static final Long HOUR_TO_MILLISECONDS = 3600000L; + public static final Long DAY_TO_MILLSEDONDS = 86400000L; + public static final Integer SECOND_TO_MILLSEDONDS = 1000; + + + /** + * 获得当前时间小时的起始(0分钟)时间 + */ + public static Date getStartOfHour() { + return getStartOfHour(0); + } + public static Date getStartOfHour(Integer offset) { + Calendar ca = Calendar.getInstance(); + ca.add(Calendar.HOUR, offset); + ca.set(Calendar.MINUTE, 0); + ca.set(Calendar.SECOND, 0); + ca.set(Calendar.MILLISECOND, 0); + return ca.getTime(); + } + + /** + * 获得当前日期的起始(0时)时间 + */ + public static Date getStartOfDay() { + return getStartOfDay(0); + } + public static Date getStartOfDay(Integer bias) { + Calendar ca = Calendar.getInstance(); + ca.add(Calendar.DATE, bias); + ca.set(Calendar.HOUR, -12); + ca.set(Calendar.MINUTE, 0); + ca.set(Calendar.SECOND, 0); + ca.set(Calendar.MILLISECOND, 0); + return ca.getTime(); + } + + public static Date getStartOfMonth() { + Calendar ca = Calendar.getInstance(); + ca.set(Calendar.DATE, 1); + ca.set(Calendar.HOUR, -12); + ca.set(Calendar.MINUTE, 0); + ca.set(Calendar.SECOND, 0); + ca.set(Calendar.MILLISECOND, 0); + return ca.getTime(); + } + +} + diff --git a/src/main/java/com/mesasoft/cn/util/ValidationUtils.java b/src/main/java/com/mesasoft/cn/util/ValidationUtils.java new file mode 100644 index 0000000..013008e --- /dev/null +++ b/src/main/java/com/mesasoft/cn/util/ValidationUtils.java @@ -0,0 +1,202 @@ +package com.mesasoft.cn.util; + +import com.mesasoft.cn.sketch.api.BrightCloud; +import com.mesasoft.cn.sketch.config.ApplicationConfig; +import org.apache.commons.lang3.StringUtils; +import org.apache.log4j.Logger; +import sun.net.util.IPAddressUtil; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Objects; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class ValidationUtils { + private static final Logger LOG = Logger.getLogger(ValidationUtils.class); + + /** + * 获取二级域名 + **/ + public static String getSecDomain(String fqdnOrUrl){ + String filePath = Objects.requireNonNull(BrightCloud.class.getClassLoader() + .getResource(ApplicationConfig.TLD_FILE)).getFile(); + HashMap<String, HashMap<String, String>> maps = readTopDomainFile(filePath); + try { + String[] split = fqdnOrUrl.split("\\."); + String secDomain = null; + for (int i = split.length - 1; i >= 0; i--) { + int maps_index = split.length - (i + 1); + HashMap<String, String> innerMap = maps.get("map_id_" + maps_index); + HashMap<String, String> fullTop = maps.get("full"); + if (!(innerMap.containsKey(split[i]))) { + String strSec = ""; + for (int j = i; j < split.length; j++) { + strSec += (split[j] + "."); + } + secDomain = strSec.substring(0, strSec.length() - 1); + if (fullTop.containsKey(getTopFromSecDomain(secDomain))) { + break; + } else { + while (!fullTop.containsKey(getTopFromSecDomain(secDomain)) && getTopFromSecDomain(secDomain).contains(".")) { + secDomain = getTopFromSecDomain(secDomain); + } + break; + } + } + } + // 右匹配为顶级域名 + if (secDomain == null){ + secDomain = fqdnOrUrl; + } + return secDomain; + } catch (Exception e) { + LOG.error("urlDomain:" + fqdnOrUrl); + e.printStackTrace(); + return "---no---return---"; + } + } + + public static List<String> getSecDomain(List<String> fqdnOrUrls) { + HashMap<String, HashMap<String, String>> maps = readTopDomainFile(ApplicationConfig.TLD_FILE); + List<String> secDomainList = new ArrayList<>(); + for (String oriDomain : fqdnOrUrls) { + String secDomain = getSecDomain(oriDomain); + if (StringUtils.isNotBlank(secDomain) && !("---no---return---".equals(secDomain))) { + secDomainList.add(secDomain); + } else { + System.out.println(oriDomain); + } + } + return secDomainList; + } + + public static String getTopFromSecDomain(String secDomain) { + String quFirstDian = secDomain; + if (secDomain.contains(".")) { + quFirstDian = secDomain.substring(secDomain.indexOf(".")).substring(1); + } + return quFirstDian; + } + + public static HashMap<String, HashMap<String, String>> readTopDomainFile(String filePath) { + HashMap<String, HashMap<String, String>> maps = makeHashMap(filePath); + try { + String encoding = "UTF-8"; + File file = new File(filePath); + if (file.isFile() && file.exists()) { + InputStreamReader read = new InputStreamReader( + new FileInputStream(file), encoding); + BufferedReader bufferedReader = new BufferedReader(read); + String lineTxt = null; + while ((lineTxt = bufferedReader.readLine()) != null) { + HashMap<String, String> fullTop = maps.get("full"); + fullTop.put(lineTxt, lineTxt); + maps.put("full", fullTop); + String[] split = lineTxt.split("\\."); + for (int i = split.length - 1; i >= 0; i--) { + int maps_index = split.length - (i + 1); + HashMap<String, String> innerMap = maps.get("map_id_" + maps_index); + innerMap.put(split[i], split[i]); + maps.put("map_id_" + maps_index, innerMap); + } + } + read.close(); + } else { + LOG.error("TopDomainUtils>=>readTopDomainFile filePath is wrong--->{" + filePath + "}<---"); + } + } catch (Exception e) { + LOG.error("TopDomainUtils>=>readTopDomainFile get filePathData error--->{" + e + "}<---"); + e.printStackTrace(); + } + return maps; + } + + public static HashMap<String, HashMap<String, String>> makeHashMap(String filePath) { + int maxLength = FileUtils.getMaxLength(filePath); + HashMap<String, HashMap<String, String>> maps = new HashMap<String, HashMap<String, String>>(); + for (int i = 0; i < maxLength; i++) { + maps.put("map_id_" + i, new HashMap<String, String>()); + } + maps.put("full", new HashMap<String, String>()); + return maps; + } + + public static List<String> getChecked(List<String> objectList, String type){ + if (type.equals("ip")){ + return getCheckedIps(objectList); + } + if (type.equals("domain")){ + return getCheckedFqdns(objectList); + } + LOG.error("Wrong type to be checked: " + type); + return objectList; + } + + public static List<String> getCheckedFqdns(List<String> fqdns){ + List<String> res = new ArrayList<>(); + for (String fqdn:fqdns){ + //去端口号 + fqdn = fqdn.split(":")[0]; + // 去重 & 校验 + if (isValidDomain(fqdn) && !res.contains(fqdn)){ + res.add(fqdn.toLowerCase()); + } else { + LOG.debug("Bad or duplicated fqdn:" + fqdn); + } + } + return res; + } + + public static List<String> getCheckedIps(List<String> ipList){ + List<String> res = new ArrayList<>(); + for (String ip:ipList){ + //去端口号 + ip = ip.split(":")[0]; + // 去重 & 校验 + if (isValidIp(ip) && !res.contains(ip)){ + res.add(ip.toLowerCase()); + } else { + LOG.debug("Bad or duplicated fqdn:" + ip); + } + } + return res; + } + + public static boolean isValidIp(String ip){ + boolean iPv4LiteralAddress = IPAddressUtil.isIPv4LiteralAddress(ip); + boolean iPv6LiteralAddress = IPAddressUtil.isIPv6LiteralAddress(ip); + return iPv4LiteralAddress || iPv6LiteralAddress; + } + + private static boolean isValidDomain(String str) + { + String regex = "^((?!-)[A-Za-z0-9-_]" + + "{1,63}(?<!-)\\.)" + + "+[A-Za-z]{2,6}"; + Pattern p = Pattern.compile(regex); + + if (str == null) { + return false; + } + Matcher m = p.matcher(str); + return m.matches(); + } + + public static Integer getMatchPattern(String fqdn){ + int match_pattern = 2; + if (fqdn.equals(getSecDomain(fqdn))){ + match_pattern = 1; // 二级域名-右匹配 + } + return match_pattern; + } + + + + +} diff --git a/src/main/java/com/mesasoft/cn/util/Verify.java b/src/main/java/com/mesasoft/cn/util/Verify.java new file mode 100644 index 0000000..8e63ea3 --- /dev/null +++ b/src/main/java/com/mesasoft/cn/util/Verify.java @@ -0,0 +1,20 @@ +package com.mesasoft.cn.util; + +import java.util.regex.Pattern; + +public class Verify { + + + private static final String DOMAIN_NAME_PATTERN = "^((?!-)[A-Za-z0-9-]{1,63}(?<!-)\\.)+[A-Za-z]{2,6}$"; + + private static Pattern pDomainName = Pattern.compile(DOMAIN_NAME_PATTERN); + + public static boolean domainValid(String domainName) { + return pDomainName.matcher(domainName).find(); + } + + public static void main(String[] args) { + boolean b = domainValid("192.168.44.12"); + System.err.println(b); + } +} |
