diff options
| author | wanghao <[email protected]> | 2023-03-22 15:13:22 +0800 |
|---|---|---|
| committer | wangwei <[email protected]> | 2023-03-27 17:16:51 +0800 |
| commit | 618c7636c039705797d6afb327852e9faa85747c (patch) | |
| tree | 06bfd2b04b067d1bc075881d35f8f22cb64fc242 | |
| parent | ce7831eff7c32b41a8e23f8da8830af5466b9892 (diff) | |
fix(DSL):GAL-300 查询网关自动识别字符串日期格式
12 files changed, 352 insertions, 164 deletions
@@ -62,7 +62,7 @@ <jsqlparser.version>4.4</jsqlparser.version> <google.gson.version>2.8.6</google.gson.version> <hbase.client.version>2.2.3</hbase.client.version> - <hutool.version>5.5.4</hutool.version> + <hutool.version>5.6.1</hutool.version> <json.path.version>2.5.0</json.path.version> <docker.build>192.168.40.153</docker.build> diff --git a/src/main/java/com/mesalab/knowledge/service/KnowledgeService.java b/src/main/java/com/mesalab/knowledge/service/KnowledgeService.java index d03b1dc4..b7f22e85 100644 --- a/src/main/java/com/mesalab/knowledge/service/KnowledgeService.java +++ b/src/main/java/com/mesalab/knowledge/service/KnowledgeService.java @@ -1,5 +1,7 @@ package com.mesalab.knowledge.service; +import cn.hutool.core.date.DateException; +import cn.hutool.core.date.DateUtil; import cn.hutool.log.Log; import cn.hutool.log.LogFactory; import com.fasterxml.jackson.databind.JsonNode; @@ -55,9 +57,6 @@ import static java.util.Objects.*; @Service public class KnowledgeService { - public static final Pattern strFormatDateTime = Pattern.compile("\\d{4}-\\d{2}-\\d{2}\\s+\\d{2}:\\d{2}:\\d{2}|" + - "(\\d{4})-(\\d{1,2})-(\\d{1,2})T(\\d{1,2}):(\\d{1,2}):(\\d{1,2})\\+|\\-(\\d{1,2}):(\\d{1,2})|" + - "(\\d{4})-(\\d{1,2})-(\\d{1,2})T(\\d{1,2}):(\\d{1,2}):(\\d{1,2})Z", Pattern.CASE_INSENSITIVE); private static final Log log = LogFactory.get(); @Value("${arango.maxrows}") private String maxrows;//查询最大条数 @@ -145,24 +144,19 @@ public class KnowledgeService { }); } if (!ObjectUtils.isEmpty(intervals)) { - List<String> times = Splitter.on("/").splitToList(intervals.get(0)); - if (ObjectUtils.isEmpty(times) || times.size() != 2){ - throw new BusinessException(ResultStatusEnum.BAD_REQUEST.getCode(), ResultCodeEnum.PARAMETER_ERROR.getCode(), - String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(), KnowLedgeErrorMessage.INTERVALS_IS_INVALID)); - } - for (String dateTimeStr : times) { - if (!strFormatDateTime.matcher(dateTimeStr).find()) { - throw new QGWBusinessException(ResultStatusEnum.BAD_REQUEST.getCode(), ResultCodeEnum.PARAMETER_ERROR.getCode(), - String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(),QGWMessageConst.TIME_FORMAT_ERROR)); + try { + List<String> times = Splitter.on("/").splitToList(intervals.get(0)); + if (ObjectUtils.isEmpty(times) || times.size() != 2) { + throw new BusinessException(ResultStatusEnum.BAD_REQUEST.getCode(), ResultCodeEnum.PARAMETER_ERROR.getCode(), + String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(), KnowLedgeErrorMessage.INTERVALS_IS_INVALID)); } + for (String dateTimeStr : times) { + DateUtil.parse(dateTimeStr); + } + } catch (DateException e) { + throw new QGWBusinessException(ResultStatusEnum.BAD_REQUEST.getCode(), ResultCodeEnum.PARAMETER_ERROR.getCode(), + String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(), QGWMessageConst.TIME_FORMAT_ERROR)); } - -// Date start = DateUtils.convertStringToDate(times.get(0), DateUtils.YYYY_MM_DD_HH24_MM_SS); -// Date end = DateUtils.convertStringToDate(times.get(1), DateUtils.YYYY_MM_DD_HH24_MM_SS); -// if (!start.before(end)){ -// throw new BusinessException(ResultStatusEnum.BAD_REQUEST.getCode(), ResultCodeEnum.PARAMETER_ERROR.getCode(), -// String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(), KnowLedgeErrorMessage.TIME_ERROR)); -// } } if (nonNull(limit)) { List<String> limits = Splitter.on(",").trimResults().omitEmptyStrings().splitToList(limit); diff --git a/src/main/java/com/mesalab/knowledge/strategy/FqdnProviderImpl.java b/src/main/java/com/mesalab/knowledge/strategy/FqdnProviderImpl.java index b2f58c7f..51826a05 100644 --- a/src/main/java/com/mesalab/knowledge/strategy/FqdnProviderImpl.java +++ b/src/main/java/com/mesalab/knowledge/strategy/FqdnProviderImpl.java @@ -1,5 +1,7 @@ package com.mesalab.knowledge.strategy; +import cn.hutool.core.date.DateException; +import cn.hutool.core.date.DateUtil; import cn.hutool.log.Log; import cn.hutool.log.LogFactory; import com.fasterxml.jackson.core.type.TypeReference; @@ -19,6 +21,7 @@ import com.mesalab.knowledge.entity.arango.IpLearningPath; import com.mesalab.knowledge.enums.RangeEnum; import com.mesalab.knowledge.exception.KnowLedgeErrorCode; import com.mesalab.knowledge.exception.KnowLedgeErrorMessage; +import com.mesalab.qgw.constant.QGWMessageConst; import com.zdjizhi.utils.DateUtils; import com.zdjizhi.utils.StringUtil; import org.apache.commons.lang3.EnumUtils; @@ -42,7 +45,6 @@ import java.util.stream.Stream; public class FqdnProviderImpl implements QueryProvider { private static final Log log = LogFactory.get(); - public static final Pattern strFormatDateTime = Pattern.compile("\\d{4}-\\d{2}-\\d{2}\\s+\\d{2}:\\d{2}:\\d{2}", Pattern.CASE_INSENSITIVE); private static final String depthregex = "^[1-9]\\d*$"; //depth 取值范围大于0 private static final String SSL = "SSL"; private static final String TLS = "TLS"; @@ -111,24 +113,17 @@ public class FqdnProviderImpl implements QueryProvider { likeFilter.append(Match.like(matches, (m) -> m != null && "FQDN_NAME".equals(m.getFieldKey()) && !ObjectUtils.isEmpty(m.getFieldValues()), ranges).replace(" doc._key like \"", " e._from like \"FQDN/")); //2.构建查询语句 intervals 时间 - List<String> intervals = parameters.getIntervals(); - if(!ObjectUtils.isEmpty(intervals)){ - String[] times = intervals.get(0).split("/"); - long startTime; - long endTime; - String start = times[0]; - String end = times[1]; - if (strFormatDateTime.matcher(start).find()){ - startTime = DateUtils.convertStringToTimestamp(start, "yyyy-MM-dd HH:mm:ss"); - } else { - startTime = ISODateTimeFormat.dateTimeNoMillis().parseMillis(start)/1000; + try { + List<String> intervals = parameters.getIntervals(); + if (!ObjectUtils.isEmpty(intervals)) { + String[] times = intervals.get(0).split("/"); + long startTime = DateUtil.parse(times[0]).toTimestamp().getTime() / 1000; + long endTime = DateUtil.parse(times[1]).toTimestamp().getTime() / 1000; + intervalsb.append(" and e.LAST_FOUND_TIME >= ").append(startTime).append(" and ").append("e.LAST_FOUND_TIME < ").append(endTime); } - if (strFormatDateTime.matcher(end).find()){ - endTime = DateUtils.convertStringToTimestamp(end, "yyyy-MM-dd HH:mm:ss"); - } else { - endTime = ISODateTimeFormat.dateTimeNoMillis().parseMillis(end)/1000; - } - intervalsb.append(" and e.LAST_FOUND_TIME >= ").append(startTime).append(" and ").append("e.LAST_FOUND_TIME < ").append(endTime); + } catch (DateException e) { + throw new BusinessException(ResultStatusEnum.BAD_REQUEST.getCode(), ResultCodeEnum.PARAMETER_ERROR.getCode(), + String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(), QGWMessageConst.TIME_FORMAT_ERROR)); } } diff --git a/src/main/java/com/mesalab/knowledge/strategy/SubscriberIdProviderImpl.java b/src/main/java/com/mesalab/knowledge/strategy/SubscriberIdProviderImpl.java index 85006edc..8134bfc0 100644 --- a/src/main/java/com/mesalab/knowledge/strategy/SubscriberIdProviderImpl.java +++ b/src/main/java/com/mesalab/knowledge/strategy/SubscriberIdProviderImpl.java @@ -1,16 +1,22 @@ package com.mesalab.knowledge.strategy; +import cn.hutool.core.date.DateException; +import cn.hutool.core.date.DateUtil; import cn.hutool.log.Log; import cn.hutool.log.LogFactory; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.Lists; import com.google.common.collect.Maps; +import com.mesalab.common.enums.ResultCodeEnum; +import com.mesalab.common.enums.ResultStatusEnum; +import com.mesalab.common.exception.BusinessException; import com.mesalab.knowledge.entity.DSLObject; import com.mesalab.knowledge.entity.Match; import com.mesalab.knowledge.entity.Parameters; import com.mesalab.knowledge.entity.Range; import com.mesalab.knowledge.enums.RangeEnum; +import com.mesalab.qgw.constant.QGWMessageConst; import com.zdjizhi.utils.DateUtils; import com.zdjizhi.utils.StringUtil; import org.joda.time.format.ISODateTimeFormat; @@ -33,7 +39,6 @@ import java.util.regex.Pattern; public class SubscriberIdProviderImpl implements QueryProvider { private static final Log log = LogFactory.get(); - public static final Pattern strFormatDateTime = Pattern.compile("\\d{4}-\\d{2}-\\d{2}\\s+\\d{2}:\\d{2}:\\d{2}", Pattern.CASE_INSENSITIVE); @Value("${arango.maxrows}") private String limit;//查询最大条数 @@ -57,24 +62,17 @@ public class SubscriberIdProviderImpl implements QueryProvider { limit = ObjectUtils.isEmpty(parameters.getLimit()) ? limit : parameters.getLimit(); //构建查询语句 intervals 时间 - List<String> intervals = parameters.getIntervals(); - if(!ObjectUtils.isEmpty(intervals)){ - String[] times = intervals.get(0).split("/"); - long startTime; - long endTime; - String start = times[0]; - String end = times[1]; - if (strFormatDateTime.matcher(start).find()){ - startTime = DateUtils.convertStringToTimestamp(start, "yyyy-MM-dd HH:mm:ss"); - } else { - startTime = ISODateTimeFormat.dateTimeNoMillis().parseMillis(start)/1000; + try { + List<String> intervals = parameters.getIntervals(); + if (!ObjectUtils.isEmpty(intervals)) { + String[] times = intervals.get(0).split("/"); + long startTime = DateUtil.parse(times[0]).toTimestamp().getTime() / 1000; + long endTime = DateUtil.parse(times[1]).toTimestamp().getTime() / 1000; + intervalsb.append(" filter e.LAST_FOUND_TIME >= ").append(startTime).append(" and ").append("e.LAST_FOUND_TIME < ").append(endTime); } - if (strFormatDateTime.matcher(end).find()){ - endTime = DateUtils.convertStringToTimestamp(end, "yyyy-MM-dd HH:mm:ss"); - } else { - endTime = ISODateTimeFormat.dateTimeNoMillis().parseMillis(end)/1000; - } - intervalsb.append(" filter e.LAST_FOUND_TIME >= ").append(startTime).append(" and ").append("e.LAST_FOUND_TIME < ").append(endTime); + } catch (DateException e) { + throw new BusinessException(ResultStatusEnum.BAD_REQUEST.getCode(), ResultCodeEnum.PARAMETER_ERROR.getCode(), + String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(), QGWMessageConst.TIME_FORMAT_ERROR)); } } diff --git a/src/main/java/com/mesalab/network/dsl/DSLValidate.java b/src/main/java/com/mesalab/network/dsl/DSLValidate.java index bbb53828..02841774 100644 --- a/src/main/java/com/mesalab/network/dsl/DSLValidate.java +++ b/src/main/java/com/mesalab/network/dsl/DSLValidate.java @@ -1,5 +1,7 @@ package com.mesalab.network.dsl; +import cn.hutool.core.date.DateException; +import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.NumberUtil; import com.mesalab.cn.constant.ErrorMessage; import com.mesalab.common.enums.ResultCodeEnum; @@ -9,6 +11,7 @@ import com.mesalab.knowledge.enums.MatchEnum; import com.mesalab.knowledge.enums.RangeEnum; import com.mesalab.network.exception.NWErrorCode; import com.mesalab.network.exception.NWErrorMessage; +import com.mesalab.qgw.exception.QGWBusinessException; import com.zdjizhi.utils.StringUtil; import org.apache.commons.lang.Validate; import org.apache.commons.lang3.EnumUtils; @@ -31,10 +34,6 @@ public class DSLValidate { public static final Pattern periodOfPT = Pattern.compile("PT(\\d+)[SMH]", Pattern.CASE_INSENSITIVE); public static final Pattern periodOfP = Pattern.compile("P(\\d+)[DWMY]", Pattern.CASE_INSENSITIVE); - public static final Pattern strFormatDateTime = Pattern.compile("\\d{4}-\\d{2}-\\d{2}\\s+\\d{2}:\\d{2}:\\d{2}|" + - "(\\d{4})-(\\d{1,2})-(\\d{1,2})T(\\d{1,2}):(\\d{1,2}):(\\d{1,2})\\+|\\-(\\d{1,2}):(\\d{1,2})|" + - "(\\d{4})-(\\d{1,2})-(\\d{1,2})T(\\d{1,2}):(\\d{1,2}):(\\d{1,2})Z", Pattern.CASE_INSENSITIVE); - public void executeValidate(DSLObject dslObject) throws BusinessException { if (StringUtil.isEmpty(dslObject)) { @@ -129,35 +128,27 @@ public class DSLValidate { * @param intervals */ private boolean isValidIntervals(List<String> intervals) { - if (CollectionUtils.isEmpty(intervals)) { - return true; - } - if (intervals.size() != 1) { - throw new BusinessException(ResultStatusEnum.BAD_REQUEST.getCode(),ResultCodeEnum.PARAMETER_ERROR.getCode(), - String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(),NWErrorMessage.INTERVALS_PARAM_ERROR)); - } - String[] split = intervals.get(0).split("/"); - if (split.length != 2) { - throw new BusinessException(ResultStatusEnum.BAD_REQUEST.getCode(),ResultCodeEnum.PARAMETER_ERROR.getCode(), - String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(),NWErrorMessage.INTERVALS_PARAM_ERROR)); - } - for (String dateTimeStr : split) { - if (!strFormatDateTime.matcher(dateTimeStr).find()) { - throw new BusinessException(ResultStatusEnum.BAD_REQUEST.getCode(),ResultCodeEnum.PARAMETER_ERROR.getCode(), - String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(),NWErrorMessage.TIME_FORMAT_ERROR)); + try { + if (CollectionUtils.isEmpty(intervals)) { + return true; + } + if (intervals.size() != 1) { + throw new BusinessException(ResultStatusEnum.BAD_REQUEST.getCode(), ResultCodeEnum.PARAMETER_ERROR.getCode(), + String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(), NWErrorMessage.INTERVALS_PARAM_ERROR)); + } + String[] split = intervals.get(0).split("/"); + if (split.length != 2) { + throw new BusinessException(ResultStatusEnum.BAD_REQUEST.getCode(), ResultCodeEnum.PARAMETER_ERROR.getCode(), + String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(), NWErrorMessage.INTERVALS_PARAM_ERROR)); } + for (String dateTimeStr : split) { + DateUtil.parse(dateTimeStr); + } + return true; + } catch (DateException e) { + throw new QGWBusinessException(ResultStatusEnum.BAD_REQUEST.getCode(), ResultCodeEnum.PARAMETER_ERROR.getCode(), + String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(), NWErrorMessage.TIME_FORMAT_ERROR)); } -// try { -// DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"); -// if (dateTimeFormatter.parseMillis(split[1]) < dateTimeFormatter.parseMillis(split[0])) { -// throw new BusinessException(ResultStatusEnum.BAD_REQUEST.getCode(),ResultCodeEnum.PARAMETER_ERROR.getCode(), -// String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(),NWErrorMessage.INTERVALS_VALUE_ERROR)); -// } -// } catch (RuntimeException e) { -// throw new BusinessException(ResultStatusEnum.BAD_REQUEST.getCode(),ResultCodeEnum.PARAMETER_ERROR.getCode(), -// String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(),NWErrorMessage.INTERVALS_VALUE_ERROR)); -// } - return true; } } diff --git a/src/main/java/com/mesalab/network/exception/NWErrorMessage.java b/src/main/java/com/mesalab/network/exception/NWErrorMessage.java index 05ac6943..97392bc8 100644 --- a/src/main/java/com/mesalab/network/exception/NWErrorMessage.java +++ b/src/main/java/com/mesalab/network/exception/NWErrorMessage.java @@ -20,7 +20,7 @@ public class NWErrorMessage { public static String MATCH_FIELD_VALUES_ERROR = "Match fieldValues cannot startWith '*' or endWith '$'"; public static String RANGE_TYPE_ERROR = "range type is illegal"; public static String INTERVALS_PARAM_ERROR = "DSLObject.query.query.Intervals is invalid"; - public static String TIME_FORMAT_ERROR = "Time format should be:[yyyy-MM-dd HH:mm:ss,yyyy-MM-dd'T'HH:mm:ssXXX]"; + public static String TIME_FORMAT_ERROR = "Time format should follow ISO 8601 standard."; public static String INTERVALS_VALUE_ERROR = "Intervals value should be [start, end]"; diff --git a/src/main/java/com/mesalab/qgw/constant/QGWMessageConst.java b/src/main/java/com/mesalab/qgw/constant/QGWMessageConst.java index ec33c45d..06bd6744 100644 --- a/src/main/java/com/mesalab/qgw/constant/QGWMessageConst.java +++ b/src/main/java/com/mesalab/qgw/constant/QGWMessageConst.java @@ -97,7 +97,7 @@ public class QGWMessageConst { public static final String MATCHING_SIGN = "error in matching sign. begin with * and do not end with * for suffix matching, end with * and do not begin with $ or * for prefix matching, begin with $ and do not end with * for exactly matching, begin with * and end with * or no wildcard for substring matching"; - public static final String TIME_FORMAT_ERROR = "Time format should be: [yyyy-MM-dd HH:mm:ss,yyyy-MM-dd'T'HH:mm:ssXXX]"; + public static final String TIME_FORMAT_ERROR = "Time format should follow ISO 8601 standard."; public static final String QUERY_TYPE_NOT_NULL = "The param must contain query.type."; diff --git a/src/main/java/com/mesalab/services/common/dsl/ComDSLParse.java b/src/main/java/com/mesalab/services/common/dsl/ComDSLParse.java index 0282a1ea..032f2a78 100644 --- a/src/main/java/com/mesalab/services/common/dsl/ComDSLParse.java +++ b/src/main/java/com/mesalab/services/common/dsl/ComDSLParse.java @@ -1,12 +1,18 @@ package com.mesalab.services.common.dsl; +import cn.hutool.core.date.DateException; +import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.crypto.digest.DigestUtil; import com.google.common.base.Joiner; import com.google.common.base.Splitter; import com.mesalab.cn.entity.pojo.DSLObject; import com.mesalab.cn.entity.pojo.DSLParser; +import com.mesalab.common.enums.ResultCodeEnum; +import com.mesalab.common.enums.ResultStatusEnum; +import com.mesalab.common.exception.BusinessException; import com.mesalab.knowledge.enums.MatchEnum; +import com.mesalab.qgw.constant.QGWMessageConst; import com.zdjizhi.utils.DateUtils; import com.zdjizhi.utils.StringUtil; import org.joda.time.format.ISODateTimeFormat; @@ -20,32 +26,22 @@ import java.util.regex.Pattern; import static java.util.stream.Collectors.joining; public class ComDSLParse extends DSLParser { - - public static final Pattern strFormatDateTime = Pattern.compile("\\d{4}-\\d{2}-\\d{2}\\s+\\d{2}:\\d{2}:\\d{2}", Pattern.CASE_INSENSITIVE); - public static String parseInterval(ComDSLObject.Query query, String defField, boolean format) { - if (ObjectUtil.isEmpty(query) || ObjectUtil.isEmpty(query.getParameters()) || ObjectUtil.isEmpty(query.getParameters().getIntervals())) { - return StringUtil.EMPTY; - } - List<String> intervals = Splitter.on("/").omitEmptyStrings().splitToList(query.getParameters().getIntervals().get(0)); - if (format) { - long startTime; - long endTime; - String start = intervals.get(0); - String end = intervals.get(1); - if (strFormatDateTime.matcher(start).find()){ - startTime = DateUtils.convertStringToTimestamp(start, "yyyy-MM-dd HH:mm:ss"); - } else { - startTime = ISODateTimeFormat.dateTimeNoMillis().parseMillis(start)/1000; + try { + if (ObjectUtil.isEmpty(query) || ObjectUtil.isEmpty(query.getParameters()) || ObjectUtil.isEmpty(query.getParameters().getIntervals())) { + return StringUtil.EMPTY; } - if (strFormatDateTime.matcher(end).find()){ - endTime = DateUtils.convertStringToTimestamp(end, "yyyy-MM-dd HH:mm:ss"); - } else { - endTime = ISODateTimeFormat.dateTimeNoMillis().parseMillis(end)/1000; + List<String> intervals = Splitter.on("/").omitEmptyStrings().splitToList(query.getParameters().getIntervals().get(0)); + if (format) { + long startTime = DateUtil.parse(intervals.get(0)).toTimestamp().getTime() / 1000; + long endTime = DateUtil.parse(intervals.get(1)).toTimestamp().getTime() / 1000; + return Joiner.on(" ").join(" AND ", defField, ">=", startTime, " AND ", defField, "<", endTime); } - return Joiner.on(" ").join(" AND ", defField, ">=", startTime, " AND ", defField, "<", endTime); + return Joiner.on(" ").join(" AND ", defField, ">=", intervals.get(0), " AND ", defField, "<", intervals.get(1)); + } catch (DateException e) { + throw new BusinessException(ResultStatusEnum.BAD_REQUEST.getCode(), ResultCodeEnum.PARAMETER_ERROR.getCode(), + String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(), QGWMessageConst.TIME_FORMAT_ERROR)); } - return Joiner.on(" ").join(" AND ", defField, ">=", intervals.get(0), " AND ", defField, "<", intervals.get(1)); } public static String parseMath(List<ComDSLObject.Query.FilterBean> matches) { diff --git a/src/main/java/com/mesalab/services/common/dsl/ComDSLValidate.java b/src/main/java/com/mesalab/services/common/dsl/ComDSLValidate.java index 89fb3231..ad920518 100644 --- a/src/main/java/com/mesalab/services/common/dsl/ComDSLValidate.java +++ b/src/main/java/com/mesalab/services/common/dsl/ComDSLValidate.java @@ -1,5 +1,7 @@ package com.mesalab.services.common.dsl; +import cn.hutool.core.date.DateException; +import cn.hutool.core.date.DateUtil; import com.mesalab.common.enums.ResultCodeEnum; import com.mesalab.common.enums.ResultStatusEnum; import com.mesalab.knowledge.enums.MatchEnum; @@ -24,9 +26,6 @@ public class ComDSLValidate { public static final Pattern periodOfPT = Pattern.compile("PT(\\d+)[SMH]", Pattern.CASE_INSENSITIVE); public static final Pattern periodOfP = Pattern.compile("P(\\d+)[DWMY]", Pattern.CASE_INSENSITIVE); - public static final Pattern strFormatDateTime = Pattern.compile("\\d{4}-\\d{2}-\\d{2}\\s+\\d{2}:\\d{2}:\\d{2}|" + - "(\\d{4})-(\\d{1,2})-(\\d{1,2})T(\\d{1,2}):(\\d{1,2}):(\\d{1,2})\\+|\\-(\\d{1,2}):(\\d{1,2})|" + - "(\\d{4})-(\\d{1,2})-(\\d{1,2})T(\\d{1,2}):(\\d{1,2}):(\\d{1,2})Z", Pattern.CASE_INSENSITIVE); public void validation(ComDSLObject dslObject) throws QGWBusinessException { @@ -37,15 +36,15 @@ public class ComDSLValidate { ComDSLObject.Query query; if (StringUtil.isEmpty(query = dslObject.getQuery())) { throw new QGWBusinessException(ResultStatusEnum.BAD_REQUEST.getCode(), ResultCodeEnum.PARAMETER_ERROR.getCode(), - String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(),QGWMessageConst.QUERY_IS_INVALID)); + String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(), QGWMessageConst.QUERY_IS_INVALID)); } if (StringUtil.isEmpty(query.getDataEngine())) { throw new QGWBusinessException(ResultStatusEnum.BAD_REQUEST.getCode(), ResultCodeEnum.PARAMETER_ERROR.getCode(), - String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(),QGWMessageConst.DATA_ENGINE_IS_INVALID)); + String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(), QGWMessageConst.DATA_ENGINE_IS_INVALID)); } if (StringUtil.isEmpty(query.getDataSource())) { throw new QGWBusinessException(ResultStatusEnum.BAD_REQUEST.getCode(), ResultCodeEnum.PARAMETER_ERROR.getCode(), - String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(),QGWMessageConst.DATASOURCE_IS_INVALID)); + String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(), QGWMessageConst.DATASOURCE_IS_INVALID)); } ComDSLObject.Query.QueryBean parameters; if (StringUtil.isEmpty(parameters = query.getParameters())) { @@ -53,23 +52,23 @@ public class ComDSLValidate { } if (!isValidGranularity(parameters.getGranularity())) { throw new QGWBusinessException(ResultStatusEnum.BAD_REQUEST.getCode(), ResultCodeEnum.PARAMETER_ERROR.getCode(), - String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(),QGWMessageConst.GRANULARITY_VALUE_IS_INVALID)); + String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(), QGWMessageConst.GRANULARITY_VALUE_IS_INVALID)); } if (!isValidMatch(parameters.getMatch())) { throw new QGWBusinessException(ResultStatusEnum.BAD_REQUEST.getCode(), ResultCodeEnum.PARAMETER_ERROR.getCode(), - String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(),QGWMessageConst.MATCH_TYPE)); + String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(), QGWMessageConst.MATCH_TYPE)); } if (!isValidRange(parameters.getRange())) { throw new QGWBusinessException(ResultStatusEnum.BAD_REQUEST.getCode(), ResultCodeEnum.PARAMETER_ERROR.getCode(), - String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(),QGWMessageConst.INTERVALS_PARAM_ERROR)); + String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(), QGWMessageConst.INTERVALS_PARAM_ERROR)); } if (!isValidIntervals(parameters.getIntervals())) { throw new QGWBusinessException(ResultStatusEnum.BAD_REQUEST.getCode(), ResultCodeEnum.PARAMETER_ERROR.getCode(), - String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(),QGWMessageConst.INTERVALS_PARAM_ERROR)); + String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(), QGWMessageConst.INTERVALS_PARAM_ERROR)); } if (!isValidSort(parameters.getSort())) { throw new QGWBusinessException(ResultStatusEnum.BAD_REQUEST.getCode(), ResultCodeEnum.PARAMETER_ERROR.getCode(), - String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(),QGWMessageConst.INTERVALS_PARAM_ERROR)); + String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(), QGWMessageConst.INTERVALS_PARAM_ERROR)); } } @@ -92,7 +91,7 @@ public class ComDSLValidate { String fieldKey = sortBean.getFieldKey(); if (StringUtil.isBlank(fieldKey)) { throw new QGWBusinessException(ResultStatusEnum.BAD_REQUEST.getCode(), ResultCodeEnum.PARAMETER_ERROR.getCode(), - String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(),QGWMessageConst.SORT_FIELD_VALUES)); + String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(), QGWMessageConst.SORT_FIELD_VALUES)); } } return true; @@ -104,15 +103,15 @@ public class ComDSLValidate { } for (ComDSLObject.Query.FilterBean rangeBean : rangeList) { - if (!EnumUtils.isValidEnum(RangeEnum.class, StringUtil.upperCase(rangeBean.getType()))){ + if (!EnumUtils.isValidEnum(RangeEnum.class, StringUtil.upperCase(rangeBean.getType()))) { throw new QGWBusinessException(ResultStatusEnum.BAD_REQUEST.getCode(), ResultCodeEnum.PARAMETER_ERROR.getCode(), - String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(),QGWMessageConst.RANGE_TYPE_IS_INVALID)); + String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(), QGWMessageConst.RANGE_TYPE_IS_INVALID)); } String fieldKey = rangeBean.getFieldKey(); if (StringUtil.isBlank(fieldKey)) { throw new QGWBusinessException(ResultStatusEnum.BAD_REQUEST.getCode(), ResultCodeEnum.PARAMETER_ERROR.getCode(), - String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(),QGWMessageConst.RANGE_FIELD_VALUES)); + String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(), QGWMessageConst.RANGE_FIELD_VALUES)); } } return true; @@ -123,57 +122,44 @@ public class ComDSLValidate { return true; } for (ComDSLObject.Query.FilterBean matchBean : matchList) { - if (!EnumUtils.isValidEnum(MatchEnum.class, StringUtil.upperCase(matchBean.getType()))){ + if (!EnumUtils.isValidEnum(MatchEnum.class, StringUtil.upperCase(matchBean.getType()))) { throw new QGWBusinessException(ResultStatusEnum.BAD_REQUEST.getCode(), ResultCodeEnum.PARAMETER_ERROR.getCode(), - String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(),QGWMessageConst.MATCH_TYPE)); + String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(), QGWMessageConst.MATCH_TYPE)); } if (MatchEnum.REGEX.getType().equals(matchBean.getType())) { matchBean.getFieldValues().forEach(mv -> { - if (String.valueOf(mv).startsWith("$") && String.valueOf(mv).endsWith("*")){ + if (String.valueOf(mv).startsWith("$") && String.valueOf(mv).endsWith("*")) { throw new QGWBusinessException(ResultStatusEnum.BAD_REQUEST.getCode(), ResultCodeEnum.PARAMETER_ERROR.getCode(), - String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(),QGWMessageConst.MATCHING_SIGN)); + String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(), QGWMessageConst.MATCHING_SIGN)); } }); } - - /*for (String fieldValue : matchBean.getFieldValues()) { - if (fieldValue.startsWith("*") || fieldValue.endsWith("$")) { - throw new QGWBusinessException(ResultStatusEnum.FAIL.getCode(), ResultCodeEnum.PARAM_SYNTAX_ERROR.getCode(), "Match fieldValues cannot startWith '*' or endWith '$'"); - } - }*/ } return true; } private boolean isValidIntervals(List<String> intervalsBean) { - if (CollectionUtils.isEmpty(intervalsBean)) { - return true; - } - if (intervalsBean.size() != 1) { - throw new QGWBusinessException(ResultStatusEnum.BAD_REQUEST.getCode(), ResultCodeEnum.PARAMETER_ERROR.getCode(), - String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(),QGWMessageConst.INTERVALS_PARAM_ERROR)); - } - String[] split = intervalsBean.get(0).split("/"); - if (split.length != 2) { - throw new QGWBusinessException(ResultStatusEnum.BAD_REQUEST.getCode(), ResultCodeEnum.PARAMETER_ERROR.getCode(), - String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(),QGWMessageConst.INTERVALS_PARAM_ERROR)); - } - for (String dateTimeStr : split) { - if (!strFormatDateTime.matcher(dateTimeStr).find()) { + try { + if (CollectionUtils.isEmpty(intervalsBean)) { + return true; + } + if (intervalsBean.size() != 1) { + throw new QGWBusinessException(ResultStatusEnum.BAD_REQUEST.getCode(), ResultCodeEnum.PARAMETER_ERROR.getCode(), + String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(), QGWMessageConst.INTERVALS_PARAM_ERROR)); + } + String[] split = intervalsBean.get(0).split("/"); + if (split.length != 2) { throw new QGWBusinessException(ResultStatusEnum.BAD_REQUEST.getCode(), ResultCodeEnum.PARAMETER_ERROR.getCode(), - String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(),QGWMessageConst.TIME_FORMAT_ERROR)); + String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(), QGWMessageConst.INTERVALS_PARAM_ERROR)); + } + for (String dateTimeStr : split) { + DateUtil.parse(dateTimeStr); } + return true; + } catch (DateException e) { + throw new QGWBusinessException(ResultStatusEnum.BAD_REQUEST.getCode(), ResultCodeEnum.PARAMETER_ERROR.getCode(), + String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(), QGWMessageConst.TIME_FORMAT_ERROR)); } -// try { -// DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"); -// if (dateTimeFormatter.parseMillis(split[1]) < dateTimeFormatter.parseMillis(split[0])) { -// throw new RuntimeException("Intervals value should be [start, end]"); -// } -// } catch (RuntimeException e) { -// throw new QGWBusinessException(ResultStatusEnum.BAD_REQUEST.getCode(), ResultCodeEnum.PARAMETER_ERROR.getCode(), -// String.format(ResultCodeEnum.PARAMETER_ERROR.getMessage(),e.getMessage())); -// } - return true; } } diff --git a/src/test/java/com/mesalab/qgw/service/DateFormatTest.java b/src/test/java/com/mesalab/qgw/service/DateFormatTest.java new file mode 100644 index 00000000..fac9e100 --- /dev/null +++ b/src/test/java/com/mesalab/qgw/service/DateFormatTest.java @@ -0,0 +1,37 @@ +package com.mesalab.qgw.service; + +import cn.hutool.core.date.DateException; +import cn.hutool.core.date.DateUtil; +import cn.hutool.log.Log; +import cn.hutool.log.LogFactory; +import com.google.common.collect.Lists; +import com.mesalab.GalaxyQGWApplicationTests; +import org.junit.Test; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; + +import java.util.ArrayList; + + +@EnableAutoConfiguration +public class DateFormatTest extends GalaxyQGWApplicationTests { + private static final Log log = LogFactory.get(); + private final static ArrayList<String> dateTimeStrList = Lists.newArrayList(); + + static { + dateTimeStrList.add("2021-08-13T14:20:18.992847200-04:00"); + dateTimeStrList.add("2023-03-15T01:57:58.865Z"); + dateTimeStrList.add("2018-10-01T10:00:00Z"); + dateTimeStrList.add("2023-03-15T01:57:58.865+08:00"); + } + + @Test + public void testDateParser() { + try { + for (String dateTimeStr : dateTimeStrList) { + log.info(String.valueOf(DateUtil.parse(dateTimeStr))); + } + } catch (DateException e) { + throw new DateException(e); + } + } +} diff --git a/src/test/resources/parameters/entityTest.json b/src/test/resources/parameters/entityTest.json index 668a1605..fea56d20 100644 --- a/src/test/resources/parameters/entityTest.json +++ b/src/test/resources/parameters/entityTest.json @@ -25,7 +25,7 @@ } ], "intervals": [ - "2020-08-15 00:00:00/2022-08-15 00:30:00" + "2020-08-15T00:00:00.865Z/2022-08-15T00:30:00.865Z" ] } } @@ -47,7 +47,7 @@ } ], "intervals": [ - "2020-08-15 00:00:00/2022-08-16 00:00:00" + "2020-08-15T00:00:00Z/2022-08-16T00:00:00Z" ] } } @@ -69,7 +69,7 @@ } ], "intervals": [ - "2020-08-15 00:00:00/2022-08-16 00:00:00" + "2020-08-15T00:00:00.865+08:00/2022-08-16T00:00:00.865+08:00" ] } } diff --git a/src/test/resources/parameters/networkMonitorTest.json b/src/test/resources/parameters/networkMonitorTest.json new file mode 100644 index 00000000..f2149c20 --- /dev/null +++ b/src/test/resources/parameters/networkMonitorTest.json @@ -0,0 +1,191 @@ +{ + "appDataSummary": { + "clientId": null, + "query": { + "queryType": "appDataSummary", + "dataSource": "traffic_protocol_stat_log", + "limit": "100", + "offset": "10", + "parameters": { + "match": [ + { + "type": "exactly", + "fieldKey": "data_center", + "fieldValues": [ + "dataCenterValue" + ] + }, + { + "type": "exactly", + "fieldKey": "device_group", + "fieldValues": [ + "deviceGroupValue1", + "deviceGroupValue2" + ] + }, + { + "type": "prefix", + "fieldKey": "app_name", + "fieldValues": [ + "Google" + ] + } + ], + "range": [ + { + "type": "eq", + "fieldKey": "vsys_id", + "fieldValues": [ + 1, + 2 + ] + } + ], + "intervals": [ + "2022-11-12 00:00:00/2022-11-12 01:00:00" + ] + } + } + }, + "internalIPDataSummary": { + "clientId": null, + "query": { + "queryType": "internalIPDataSummary", + "dataSource": "session_record", + "limit": "10", + "offset": "20", + "parameters": { + "match": [ + { + "type": "exactly", + "fieldKey": "app_name", + "fieldValues": [ + "Google" + ] + }, + { + "type": "exactly", + "fieldKey": "data_center", + "fieldValues": [ + "dataCenterValue" + ] + }, + { + "type": "exactly", + "fieldKey": "device_group", + "fieldValues": [ + "deviceGroupValue1", + "deviceGroupValue2" + ] + } + ], + "range": [ + { + "type": "eq", + "fieldKey": "vsys_id", + "fieldValues": [ + 1, + 2 + ] + } + ], + "intervals": [ + "2022-11-12 00:00:00/2022-11-12 01:00:00" + ] + } + } + }, + "appDataRateSummary": { + "clientId": null, + "query": { + "queryType": "appDataRateSummary", + "dataSource": "traffic_protocol_stat_log", + "parameters": { + "granularity": "PT30S", + "match": [ + { + "type": "exactly", + "fieldKey": "app_name", + "fieldValues": [ + "Freegate" + ] + }, + { + "type": "exactly", + "fieldKey": "data_center", + "fieldValues": [ + "dataCenterValue" + ] + }, + { + "type": "exactly", + "fieldKey": "device_group", + "fieldValues": [ + "deviceGroupValue1", + "deviceGroupValue2" + ] + } + ], + "range": [ + { + "type": "eq", + "fieldKey": "vsys_id", + "fieldValues": [ + 1, + 2 + ] + } + ], + "intervals": [ + "2022-10-28 00:00:00/2022-12-28 01:00:00" + ] + } + } + }, + "appTrafficSummary": { + "clientId": null, + "query": { + "queryType": "appTrafficSummary", + "dataSource": "traffic_protocol_stat_log", + "parameters": { + "match": [ + { + "type": "exactly", + "fieldKey": "app_name", + "fieldValues": [ + "Freegate" + ] + }, + { + "type": "exactly", + "fieldKey": "data_center", + "fieldValues": [ + "dataCenterValue" + ] + }, + { + "type": "exactly", + "fieldKey": "device_group", + "fieldValues": [ + "deviceGroupValue1", + "deviceGroupValue2" + ] + } + ], + "range": [ + { + "type": "eq", + "fieldKey": "vsys_id", + "fieldValues": [ + 1, + 2 + ] + } + ], + "intervals": [ + "2022-10-28 00:00:00/2022-12-28 01:00:00" + ] + } + } + } +}
\ No newline at end of file |
