Add failure analyzer for incorrect pcap file
This commit is contained in:
@@ -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;
|
||||
|
||||
}
|
||||
@@ -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<PcapFileNotFoundException> {
|
||||
@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<String> 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
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
2
src/main/resources/META-INF/spring.factories
Normal file
2
src/main/resources/META-INF/spring.factories
Normal file
@@ -0,0 +1,2 @@
|
||||
org.springframework.boot.diagnostics.FailureAnalyzer=\
|
||||
ru.serega6531.packmate.exception.analyzer.PcapFileNotFoundFailureAnalyzer
|
||||
Reference in New Issue
Block a user