From d2df0817f8aaae49085f49ab786cefd027fefc90 Mon Sep 17 00:00:00 2001 From: Romuald Lemesle Date: Thu, 26 Dec 2024 17:24:57 +0100 Subject: [PATCH] [backend] Refacto --- .../java/io/openbas/executors/Executor.java | 8 +------- .../java/io/openbas/executors/Injector.java | 20 +++++++------------ .../java/io/openbas/utils/InjectionUtils.java | 19 ++++++++++++++++++ 3 files changed, 27 insertions(+), 20 deletions(-) create mode 100644 openbas-api/src/main/java/io/openbas/utils/InjectionUtils.java diff --git a/openbas-api/src/main/java/io/openbas/executors/Executor.java b/openbas-api/src/main/java/io/openbas/executors/Executor.java index 4987ee4b06..25c40522e9 100644 --- a/openbas-api/src/main/java/io/openbas/executors/Executor.java +++ b/openbas-api/src/main/java/io/openbas/executors/Executor.java @@ -1,6 +1,7 @@ package io.openbas.executors; import static io.openbas.database.model.InjectStatusExecution.traceInfo; +import static io.openbas.utils.InjectionUtils.isInInjectableRange; import com.fasterxml.jackson.databind.ObjectMapper; import io.openbas.asset.QueueService; @@ -155,11 +156,4 @@ public InjectStatus execute(ExecutableInject executableInject) { + injectorContract.getInjector().getType())); } - // region utils - private boolean isInInjectableRange(Injection injection) { - Instant now = Instant.now(); - Instant start = now.minus(Duration.parse("PT1H")); - Instant injectWhen = injection.getDate().orElseThrow(); - return injectWhen.isAfter(start) && injectWhen.isBefore(now); - } } diff --git a/openbas-api/src/main/java/io/openbas/executors/Injector.java b/openbas-api/src/main/java/io/openbas/executors/Injector.java index 6df7393ce0..e591cea565 100644 --- a/openbas-api/src/main/java/io/openbas/executors/Injector.java +++ b/openbas-api/src/main/java/io/openbas/executors/Injector.java @@ -1,7 +1,5 @@ package io.openbas.executors; -import static io.openbas.database.model.InjectStatusExecution.traceError; - import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import io.openbas.database.model.*; @@ -11,15 +9,17 @@ import io.openbas.service.FileService; import jakarta.annotation.Resource; import jakarta.validation.constraints.NotNull; +import org.apache.commons.io.IOUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.transaction.annotation.Transactional; + import java.io.InputStream; -import java.time.Duration; -import java.time.Instant; import java.util.ArrayList; import java.util.List; import java.util.Optional; -import org.apache.commons.io.IOUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.transaction.annotation.Transactional; + +import static io.openbas.database.model.InjectStatusExecution.traceError; +import static io.openbas.utils.InjectionUtils.isInInjectableRange; public abstract class Injector { @@ -73,12 +73,6 @@ public Execution executeInjection(ExecutableInject executableInject) { } // region utils - private boolean isInInjectableRange(Injection injection) { - Instant now = Instant.now(); - Instant start = now.minus(Duration.parse("PT1H")); - Instant injectWhen = injection.getDate().orElseThrow(); - return injectWhen.isAfter(start) && injectWhen.isBefore(now); - } public T contentConvert( @NotNull final ExecutableInject injection, @NotNull final Class converter) diff --git a/openbas-api/src/main/java/io/openbas/utils/InjectionUtils.java b/openbas-api/src/main/java/io/openbas/utils/InjectionUtils.java new file mode 100644 index 0000000000..764422e369 --- /dev/null +++ b/openbas-api/src/main/java/io/openbas/utils/InjectionUtils.java @@ -0,0 +1,19 @@ +package io.openbas.utils; + +import io.openbas.database.model.Injection; + +import java.time.Duration; +import java.time.Instant; + +public class InjectionUtils { + + private InjectionUtils() {} + + public static boolean isInInjectableRange(Injection injection) { + Instant now = Instant.now(); + Instant start = now.minus(Duration.parse("PT2M")); + Instant injectWhen = injection.getDate().orElseThrow(); + return injectWhen.isAfter(start) && injectWhen.isBefore(now); + } + +}