From eef33308a59070b220fcccd6a2ca1439b46001e5 Mon Sep 17 00:00:00 2001 From: Sergey Shkurov Date: Mon, 24 Apr 2023 02:20:21 +0300 Subject: [PATCH] Add failure analyzer for incorrect pcap file --- .../exception/PcapFileNotFoundException.java | 15 +++++++ .../PcapFileNotFoundFailureAnalyzer.java | 44 +++++++++++++++++++ .../packmate/pcap/FilePcapWorker.java | 8 ++-- src/main/resources/META-INF/spring.factories | 2 + 4 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 src/main/java/ru/serega6531/packmate/exception/PcapFileNotFoundException.java create mode 100644 src/main/java/ru/serega6531/packmate/exception/analyzer/PcapFileNotFoundFailureAnalyzer.java create mode 100644 src/main/resources/META-INF/spring.factories diff --git a/src/main/java/ru/serega6531/packmate/exception/PcapFileNotFoundException.java b/src/main/java/ru/serega6531/packmate/exception/PcapFileNotFoundException.java new file mode 100644 index 0000000..567afb5 --- /dev/null +++ b/src/main/java/ru/serega6531/packmate/exception/PcapFileNotFoundException.java @@ -0,0 +1,15 @@ +package ru.serega6531.packmate.exception; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +import java.io.File; + +@EqualsAndHashCode(callSuper = true) +@Data +public class PcapFileNotFoundException extends RuntimeException { + + private final File file; + private final File directory; + +} diff --git a/src/main/java/ru/serega6531/packmate/exception/analyzer/PcapFileNotFoundFailureAnalyzer.java b/src/main/java/ru/serega6531/packmate/exception/analyzer/PcapFileNotFoundFailureAnalyzer.java new file mode 100644 index 0000000..a7679ef --- /dev/null +++ b/src/main/java/ru/serega6531/packmate/exception/analyzer/PcapFileNotFoundFailureAnalyzer.java @@ -0,0 +1,44 @@ +package ru.serega6531.packmate.exception.analyzer; + +import org.springframework.boot.diagnostics.AbstractFailureAnalyzer; +import org.springframework.boot.diagnostics.FailureAnalysis; +import ru.serega6531.packmate.exception.PcapFileNotFoundException; + +import java.io.File; +import java.util.Arrays; +import java.util.List; + +public class PcapFileNotFoundFailureAnalyzer extends AbstractFailureAnalyzer { + @Override + protected FailureAnalysis analyze(Throwable rootFailure, PcapFileNotFoundException cause) { + String description = "The file " + cause.getFile().getAbsolutePath() + " was not found"; + String existingFilesMessage; + + + File[] existingFiles = cause.getDirectory().listFiles(); + + if (existingFiles == null) { + return new FailureAnalysis( + description, + "Make sure you've put the pcap file to the ./pcaps directory, not the root directory. " + + "The directory currently does not exist", + cause + ); + } + + + if (existingFiles.length == 0) { + existingFilesMessage = "The pcaps directory is currently empty"; + } else { + List existingFilesNames = Arrays.stream(existingFiles).map(File::getName).toList(); + existingFilesMessage = "The files present in " + cause.getDirectory().getAbsolutePath() + " are: " + existingFilesNames; + } + + return new FailureAnalysis( + description, + "Please verify the file name. Make sure you've put the pcap file to the ./pcaps directory, not the root directory.\n" + + existingFilesMessage, + cause + ); + } +} diff --git a/src/main/java/ru/serega6531/packmate/pcap/FilePcapWorker.java b/src/main/java/ru/serega6531/packmate/pcap/FilePcapWorker.java index 6eb2b2a..c37fe5c 100644 --- a/src/main/java/ru/serega6531/packmate/pcap/FilePcapWorker.java +++ b/src/main/java/ru/serega6531/packmate/pcap/FilePcapWorker.java @@ -6,6 +6,7 @@ import org.apache.tomcat.util.threads.InlineExecutorService; import org.pcap4j.core.PcapNativeException; import org.pcap4j.core.Pcaps; import org.pcap4j.packet.Packet; +import ru.serega6531.packmate.exception.PcapFileNotFoundException; import ru.serega6531.packmate.model.enums.Protocol; import ru.serega6531.packmate.model.enums.SubscriptionMessageType; import ru.serega6531.packmate.model.pojo.SubscriptionMessage; @@ -16,7 +17,6 @@ import ru.serega6531.packmate.service.SubscriptionService; import java.io.EOFException; import java.io.File; import java.net.UnknownHostException; -import java.util.Arrays; @Slf4j public class FilePcapWorker extends AbstractPcapWorker { @@ -32,10 +32,10 @@ public class FilePcapWorker extends AbstractPcapWorker { super(servicesService, streamService, localIpString); this.subscriptionService = subscriptionService; - file = new File("pcaps", filename); + File directory = new File("pcaps"); + file = new File(directory, filename); if (!file.exists()) { - log.info("Existing files: " + Arrays.toString(new File("pcaps").listFiles())); - throw new IllegalArgumentException("File " + file.getAbsolutePath() + " does not exist"); + throw new PcapFileNotFoundException(file, directory); } processorExecutorService = new InlineExecutorService(); diff --git a/src/main/resources/META-INF/spring.factories b/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..a451ddf --- /dev/null +++ b/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +org.springframework.boot.diagnostics.FailureAnalyzer=\ + ru.serega6531.packmate.exception.analyzer.PcapFileNotFoundFailureAnalyzer \ No newline at end of file