Добавлены DTO для всех entity

This commit is contained in:
serega6531
2020-12-29 14:36:17 +03:00
parent 19ccd3f200
commit 625848cf23
17 changed files with 179 additions and 46 deletions

View File

@@ -1,6 +1,7 @@
package ru.serega6531.packmate.service;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ru.serega6531.packmate.model.FoundPattern;
@@ -8,6 +9,7 @@ import ru.serega6531.packmate.model.Pattern;
import ru.serega6531.packmate.model.enums.PatternDirectionType;
import ru.serega6531.packmate.model.enums.PatternSearchType;
import ru.serega6531.packmate.model.enums.SubscriptionMessageType;
import ru.serega6531.packmate.model.pojo.PatternDto;
import ru.serega6531.packmate.model.pojo.SubscriptionMessage;
import ru.serega6531.packmate.repository.PatternRepository;
@@ -23,6 +25,7 @@ public class PatternService {
private final SubscriptionService subscriptionService;
private final Map<Integer, Pattern> patterns = new HashMap<>();
private final ModelMapper modelMapper = new ModelMapper();
@Autowired
public PatternService(PatternRepository repository,
@@ -80,8 +83,16 @@ public class PatternService {
final Pattern saved = repository.save(pattern);
patterns.put(saved.getId(), saved);
log.info("Added new pattern '{}' with value '{}'", pattern.getName(), pattern.getValue());
subscriptionService.broadcast(new SubscriptionMessage(SubscriptionMessageType.SAVE_PATTERN, saved));
subscriptionService.broadcast(new SubscriptionMessage(SubscriptionMessageType.SAVE_PATTERN, toDto(saved)));
return saved;
}
public Pattern fromDto(PatternDto dto) {
return modelMapper.map(dto, Pattern.class);
}
public PatternDto toDto(Pattern pattern) {
return modelMapper.map(pattern, PatternDto.class);
}
}

View File

@@ -1,12 +1,14 @@
package ru.serega6531.packmate.service;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import ru.serega6531.packmate.model.CtfService;
import ru.serega6531.packmate.model.enums.SubscriptionMessageType;
import ru.serega6531.packmate.model.pojo.ServiceDto;
import ru.serega6531.packmate.model.pojo.SubscriptionMessage;
import ru.serega6531.packmate.repository.ServiceRepository;
@@ -29,6 +31,7 @@ public class ServicesService {
private final InetAddress localIp;
private final Map<Integer, CtfService> services = new HashMap<>();
private final ModelMapper modelMapper = new ModelMapper();
@Autowired
public ServicesService(ServiceRepository repository,
@@ -44,6 +47,10 @@ public class ServicesService {
log.info("Loaded {} services", services.size());
}
public CtfService find(int id) {
return services.get(id);
}
public Optional<CtfService> findService(Inet4Address firstIp, int firstPort, Inet4Address secondIp, int secondPort) {
if (firstIp.equals(localIp)) {
return findByPort(firstPort);
@@ -79,11 +86,19 @@ public class ServicesService {
final CtfService saved = repository.save(service);
services.put(saved.getPort(), saved);
subscriptionService.broadcast(new SubscriptionMessage(SubscriptionMessageType.SAVE_SERVICE, saved));
subscriptionService.broadcast(new SubscriptionMessage(SubscriptionMessageType.SAVE_SERVICE, toDto(saved)));
pcapService.updateFilter(findAll());
return saved;
}
public ServiceDto toDto(CtfService service) {
return modelMapper.map(service, ServiceDto.class);
}
public CtfService fromDto(ServiceDto dto) {
return modelMapper.map(dto, CtfService.class);
}
}

View File

@@ -1,6 +1,7 @@
package ru.serega6531.packmate.service;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
@@ -11,9 +12,7 @@ import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import ru.serega6531.packmate.model.*;
import ru.serega6531.packmate.model.enums.SubscriptionMessageType;
import ru.serega6531.packmate.model.pojo.Pagination;
import ru.serega6531.packmate.model.pojo.SubscriptionMessage;
import ru.serega6531.packmate.model.pojo.UnfinishedStream;
import ru.serega6531.packmate.model.pojo.*;
import ru.serega6531.packmate.repository.StreamRepository;
import ru.serega6531.packmate.service.optimization.RsaKeysHolder;
import ru.serega6531.packmate.service.optimization.StreamOptimizer;
@@ -39,6 +38,7 @@ public class StreamService {
private final boolean ignoreEmptyPackets;
private final java.util.regex.Pattern userAgentPattern = java.util.regex.Pattern.compile("User-Agent: (.+)\\r\\n");
private final ModelMapper modelMapper = new ModelMapper();
@Autowired
public StreamService(StreamRepository repository,
@@ -108,7 +108,7 @@ public class StreamService {
savedStream.setPackets(packets);
savedStream = save(savedStream);
subscriptionService.broadcast(new SubscriptionMessage(SubscriptionMessageType.NEW_STREAM, savedStream));
subscriptionService.broadcast(new SubscriptionMessage(SubscriptionMessageType.NEW_STREAM, streamToDto(savedStream)));
return true;
}
@@ -132,7 +132,7 @@ public class StreamService {
char[] alphabet = "abcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
int l = alphabet.length;
int hashCode = ua.hashCode();
if(hashCode == Integer.MIN_VALUE) { // abs(MIN_VALUE) вернет то же значение
if (hashCode == Integer.MIN_VALUE) { // abs(MIN_VALUE) вернет то же значение
hashCode = Integer.MAX_VALUE;
}
final int hash = Math.abs(hashCode) % (l * l * l);
@@ -182,27 +182,35 @@ public class StreamService {
PageRequest page = PageRequest.of(0, pagination.getPageSize(), pagination.getDirection(), "id");
Specification<Stream> spec;
if(pagination.getDirection() == Sort.Direction.ASC) {
if (pagination.getDirection() == Sort.Direction.ASC) {
spec = streamIdGreaterThan(pagination.getStartingFrom());
} else {
spec = streamIdLessThan(pagination.getStartingFrom());
}
if(service.isPresent()) {
if (service.isPresent()) {
spec = spec.and(streamServiceEquals(service.get()));
}
if(onlyFavorites) {
if (onlyFavorites) {
spec = spec.and(streamIsFavorite());
}
if(pagination.getPattern() != null) {
if (pagination.getPattern() != null) {
spec = spec.and(streamPatternsContains(pagination.getPattern()));
}
return repository.findAll(spec, page).getContent();
}
public StreamDto streamToDto(Stream stream) {
return modelMapper.map(stream, StreamDto.class);
}
public PacketDto packetToDto(Packet packet) {
return modelMapper.map(packet, PacketDto.class);
}
private Specification<Stream> streamServiceEquals(long service) {
return (root, query, cb) -> cb.equal(root.get("service"), service);
}