Работа над добавлением поиска паттернов
This commit is contained in:
@@ -1,12 +1,15 @@
|
||||
package ru.serega6531.packmate.service;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.serega6531.packmate.model.Pattern;
|
||||
import ru.serega6531.packmate.repository.PatternRepository;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
@@ -23,6 +26,14 @@ public class PatternService {
|
||||
return repository.findAll();
|
||||
}
|
||||
|
||||
public List<Pattern> findMatching(byte[] bytes) {
|
||||
String content = new String(bytes);
|
||||
|
||||
return findAll().stream()
|
||||
.filter(pattern -> StringUtils.containsIgnoreCase(content, pattern.getValue()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public void deleteById(int id) {
|
||||
repository.deleteById(id);
|
||||
}
|
||||
|
||||
@@ -3,24 +3,39 @@ package ru.serega6531.packmate.service;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.serega6531.packmate.model.Pattern;
|
||||
import ru.serega6531.packmate.model.Stream;
|
||||
import ru.serega6531.packmate.repository.StreamRepository;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class StreamService {
|
||||
|
||||
private final StreamRepository repository;
|
||||
private final PatternService patternService;
|
||||
|
||||
@Autowired
|
||||
public StreamService(StreamRepository repository) {
|
||||
public StreamService(StreamRepository repository, PatternService patternService) {
|
||||
this.repository = repository;
|
||||
this.patternService = patternService;
|
||||
}
|
||||
|
||||
public Stream save(Stream stream) {
|
||||
if(!stream.getPackets().isEmpty()) {
|
||||
Set<Pattern> matches = new HashSet<>();
|
||||
|
||||
stream.getPackets().forEach(packet -> {
|
||||
matches.addAll(patternService.findMatching(packet.getContent()));
|
||||
});
|
||||
|
||||
stream.setFoundPatterns(matches);
|
||||
}
|
||||
|
||||
final Stream saved = repository.save(stream);
|
||||
log.info("Создан стрим с id {}", saved.getId());
|
||||
return saved;
|
||||
|
||||
@@ -15,14 +15,14 @@ import java.util.List;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class PacketsSubscriptionService {
|
||||
public class StreamSubscriptionService {
|
||||
|
||||
private List<WebSocketSession> subscribers = new ArrayList<>();
|
||||
|
||||
private final ObjectMapper mapper;
|
||||
|
||||
@Autowired
|
||||
public PacketsSubscriptionService(ObjectMapper mapper) {
|
||||
public StreamSubscriptionService(ObjectMapper mapper) {
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user