diff options
| -rw-r--r-- | src/main/java/com/example/nis/Scheduled/Entity.java | 41 | ||||
| -rw-r--r-- | src/main/java/com/example/nis/common/TsgServiceImpl.java | 39 | ||||
| -rw-r--r-- | src/main/resources/application.yml | 2 |
3 files changed, 79 insertions, 3 deletions
diff --git a/src/main/java/com/example/nis/Scheduled/Entity.java b/src/main/java/com/example/nis/Scheduled/Entity.java index a14075b..912bbe7 100644 --- a/src/main/java/com/example/nis/Scheduled/Entity.java +++ b/src/main/java/com/example/nis/Scheduled/Entity.java @@ -24,10 +24,10 @@ import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.RequestBody; import sun.net.util.IPAddressUtil; +import java.text.DateFormat; +import java.text.SimpleDateFormat; import java.time.format.DateTimeFormatter; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; +import java.util.*; /** * TODO @@ -55,6 +55,12 @@ public class Entity { private Boolean deleteEnable; @Value("${entity.delete.offset}") private Integer deleteOffset; + @Value("${entity.delete.option}") + private String deleteOption; + + final private static String OPTION_DELETE = "DELETE"; + final private static String OPTION_PUT = "PUT"; + @Scheduled(cron = "${entity.update.cron}") public ResponseData scheduledExecutorOfUpdate() { @@ -106,6 +112,35 @@ public class Entity { log.warn("delete schedule disable"); return ResponseData.ok(); } + if (OPTION_DELETE.equalsIgnoreCase(deleteOption)) { + Map<String, Object> body = new HashMap<>(16); + DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); + df.setTimeZone(TimeZone.getTimeZone("UTC")); + String utcDate = df.format(DateUtil.offsetSecond(new Date(), deleteOffset)); + body.put("deleteItemsByLtTime", utcDate); + body.put(Constant.TSG_ITEM_TYPE, Constant.TSG_ITEM_IP); + body.put(Constant.TSG_VSYS_ID, 1); + List<Object> objectIds = new ArrayList<>(); + objectIds.add(ipObjectId); + body.put(Constant.TSG_OBJECT_IDS, objectIds); + return optionDelete(JSONUtil.toJsonStr(body)); + } else if (OPTION_PUT.equalsIgnoreCase(deleteOption)) { + return optionPUT(start); + } else { + log.error("delete option error"); + return ResponseData.error("delete option error"); + } + } + + private ResponseData optionDelete(String body) { + try { + return tsgService.deleteExpireItemsOfObject(body) ? ResponseData.ok() : ResponseData.error("delete failed"); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + private ResponseData optionPUT(long start) { ResponseData responseData = getDeleteByIpObjectId(ipObjectId); if (!Code.SUCCESS.getCode().equals(responseData.get("code"))) { String msg = String.valueOf(responseData.get("msg")); diff --git a/src/main/java/com/example/nis/common/TsgServiceImpl.java b/src/main/java/com/example/nis/common/TsgServiceImpl.java index c835f98..7934b7a 100644 --- a/src/main/java/com/example/nis/common/TsgServiceImpl.java +++ b/src/main/java/com/example/nis/common/TsgServiceImpl.java @@ -141,4 +141,43 @@ public class TsgServiceImpl { return result; } + /** + * Desc: 删除制定object id的对象过期数据 + * + * @param + * @return {@link boolean} + * @created by wWei + * @date 2023/2/17 17:55 + */ + public boolean deleteExpireItemsOfObject(String body) throws Exception { + boolean result; + try { + String token = ToTsgSystemUtil.sendLoginRequest(); + long start = System.currentTimeMillis(); + log.info("start delete, Body params: {}", body); + String result1 = HttpRequest.delete(ToTsgSystemUtil.TSG_URL + "v1/policy/items") + .header(Header.AUTHORIZATION, token) + .header(Header.CONTENT_TYPE, Constant.TSG_APPLICATION_JSON) + .body(body) + .timeout(httpTimeOut) + .execute().body(); + if (StringUtils.isNotBlank(result1)) { + JSONObject jsonObject = JSONUtil.parseObj(result1); + if (Constant.TSG_SUCCESS_CODE.equals(jsonObject.get(Constant.TSG_CODE).toString())) { + result = true; + log.info("object items delete success,cost: {} millisecond", System.currentTimeMillis() - start); + } else { + log.error("object items delete failed, Response:{}", result1); + throw new RuntimeException("object items delete failed."); + } + } else { + log.error("object items delete failed, Response:{}", result1); + throw new RuntimeException("object items delete failed."); + } + } catch (Exception e) { + log.error("object items delete failed!", e); + throw e; + } + return result; + } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index a12bb49..b19933b 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -37,6 +37,8 @@ entity: #Plan B sql: SELECT common_client_ip as client_ip, count(DISTINCT common_server_ip) as num FROM security_event WHERE common_recv_time > now() - 10 * 60 AND common_recv_time <= now() AND notEmpty(common_client_ip) AND common_policy_id = '97810' GROUP BY client_ip HAVING num >= 10 sql: SELECT DISTINCT client_ip FROM psiphon_client_detection_event WHERE event_time >= now()-600 AND event_time < now() AND notEmpty(client_ip) delete: + # DELETE、PUT + option: DELETE cron: 0 0/5 * * * ? enable: true #偏移秒数,正数向未来偏移,负数向历史偏移 |
