Добавлена возможность задать направление паттерна

This commit is contained in:
serega6531
2019-09-23 18:00:20 +03:00
parent 88c0ded256
commit 8d5b6072da
5 changed files with 17 additions and 3 deletions

View File

@@ -34,6 +34,8 @@ public class Pattern {
private boolean isRegex;
private PatternType type;
@ManyToMany(mappedBy = "foundPatterns", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JsonIgnore
private List<Stream> matchedStreams;

View File

@@ -0,0 +1,5 @@
package ru.serega6531.packmate.model;
public enum PatternType {
INPUT, OUTPUT, BOTH
}

View File

@@ -2,6 +2,12 @@ package ru.serega6531.packmate.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.serega6531.packmate.model.Pattern;
import ru.serega6531.packmate.model.PatternType;
import java.util.List;
public interface PatternRepository extends JpaRepository<Pattern, Integer> {
List<Pattern> findAllByTypeEqualsOrTypeEquals(PatternType type, PatternType both);
}

View File

@@ -7,6 +7,7 @@ import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ru.serega6531.packmate.model.Pattern;
import ru.serega6531.packmate.model.PatternType;
import ru.serega6531.packmate.model.Stream;
import ru.serega6531.packmate.repository.PatternRepository;
@@ -35,10 +36,10 @@ public class PatternService {
return repository.findAll();
}
public List<Pattern> findMatching(byte[] bytes) {
public List<Pattern> findMatching(byte[] bytes, boolean incoming) {
String content = new String(bytes);
return findAll().stream()
return repository.findAllByTypeEqualsOrTypeEquals(incoming ? PatternType.INPUT : PatternType.OUTPUT, PatternType.BOTH).stream()
.filter(pattern -> matches(pattern, content))
.collect(Collectors.toList());
}

View File

@@ -165,7 +165,7 @@ public class StreamService {
for (ru.serega6531.packmate.model.Packet packet : packets) {
packet.setStream(savedStream);
savedPackets.add(packetService.save(packet));
matches.addAll(patternService.findMatching(packet.getContent()));
matches.addAll(patternService.findMatching(packet.getContent(), packet.isIncoming()));
}
savedStream.setFoundPatterns(new ArrayList<>(matches));