Update ServiceController endpoints
This commit is contained in:
@@ -8,8 +8,9 @@ import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import ru.serega6531.packmate.model.CtfService;
|
||||
import ru.serega6531.packmate.model.pojo.ServiceCreateDto;
|
||||
import ru.serega6531.packmate.model.pojo.ServiceDto;
|
||||
import ru.serega6531.packmate.model.pojo.ServiceUpdateDto;
|
||||
import ru.serega6531.packmate.service.ServicesService;
|
||||
|
||||
import java.util.List;
|
||||
@@ -27,9 +28,7 @@ public class ServiceController {
|
||||
|
||||
@GetMapping
|
||||
public List<ServiceDto> getServices() {
|
||||
return service.findAll().stream()
|
||||
.map(service::toDto)
|
||||
.toList();
|
||||
return service.findAll();
|
||||
}
|
||||
|
||||
@DeleteMapping("/{port}")
|
||||
@@ -38,9 +37,13 @@ public class ServiceController {
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public CtfService addService(@RequestBody ServiceDto dto) {
|
||||
CtfService newService = this.service.fromDto(dto);
|
||||
return this.service.save(newService);
|
||||
public ServiceDto addService(@RequestBody ServiceCreateDto dto) {
|
||||
return this.service.create(dto);
|
||||
}
|
||||
|
||||
@PostMapping("/{port}")
|
||||
public ServiceDto updateService(@PathVariable int port, @RequestBody ServiceUpdateDto dto) {
|
||||
return this.service.update(port, dto);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package ru.serega6531.packmate.model.pojo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class ServiceCreateDto {
|
||||
|
||||
private int port;
|
||||
private String name;
|
||||
private boolean decryptTls;
|
||||
private boolean processChunkedEncoding;
|
||||
private boolean ungzipHttp;
|
||||
private boolean urldecodeHttpRequests;
|
||||
private boolean mergeAdjacentPackets;
|
||||
private boolean parseWebSockets;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package ru.serega6531.packmate.model.pojo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class ServiceUpdateDto {
|
||||
|
||||
private int port;
|
||||
private String name;
|
||||
private boolean decryptTls;
|
||||
private boolean processChunkedEncoding;
|
||||
private boolean ungzipHttp;
|
||||
private boolean urldecodeHttpRequests;
|
||||
private boolean mergeAdjacentPackets;
|
||||
private boolean parseWebSockets;
|
||||
|
||||
}
|
||||
@@ -4,8 +4,8 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.pcap4j.core.PcapNativeException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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.pcap.NoOpPcapWorker;
|
||||
import ru.serega6531.packmate.pcap.PcapWorker;
|
||||
@@ -40,14 +40,14 @@ public class PcapService {
|
||||
}
|
||||
}
|
||||
|
||||
public void updateFilter(Collection<CtfService> services) {
|
||||
public void updateFilter(Collection<ServiceDto> services) {
|
||||
String filter;
|
||||
|
||||
if (services.isEmpty()) {
|
||||
filter = "tcp or udp";
|
||||
} else {
|
||||
final String ports = services.stream()
|
||||
.map(CtfService::getPort)
|
||||
.map(ServiceDto::getPort)
|
||||
.map(p -> "port " + p)
|
||||
.collect(Collectors.joining(" or "));
|
||||
|
||||
|
||||
@@ -6,9 +6,12 @@ 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 org.springframework.transaction.annotation.Transactional;
|
||||
import ru.serega6531.packmate.model.CtfService;
|
||||
import ru.serega6531.packmate.model.enums.SubscriptionMessageType;
|
||||
import ru.serega6531.packmate.model.pojo.ServiceCreateDto;
|
||||
import ru.serega6531.packmate.model.pojo.ServiceDto;
|
||||
import ru.serega6531.packmate.model.pojo.ServiceUpdateDto;
|
||||
import ru.serega6531.packmate.model.pojo.SubscriptionMessage;
|
||||
import ru.serega6531.packmate.repository.ServiceRepository;
|
||||
|
||||
@@ -67,8 +70,11 @@ public class ServicesService {
|
||||
return Optional.ofNullable(services.get(port));
|
||||
}
|
||||
|
||||
public Collection<CtfService> findAll() {
|
||||
return services.values();
|
||||
public List<ServiceDto> findAll() {
|
||||
return services.values()
|
||||
.stream()
|
||||
.map(this::toDto)
|
||||
.toList();
|
||||
}
|
||||
|
||||
public void deleteByPort(int port) {
|
||||
@@ -82,9 +88,27 @@ public class ServicesService {
|
||||
updateFilter();
|
||||
}
|
||||
|
||||
public CtfService save(CtfService service) {
|
||||
log.info("Added or edited service '{}' at port {}", service.getName(), service.getPort());
|
||||
@Transactional
|
||||
public ServiceDto create(ServiceCreateDto dto) {
|
||||
CtfService service = fromDto(dto);
|
||||
|
||||
log.info("Added service '{}' at port {}", service.getName(), service.getPort());
|
||||
|
||||
return save(service);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public ServiceDto update(int port, ServiceUpdateDto dto) {
|
||||
CtfService service = repository.findById(port).orElseThrow();
|
||||
|
||||
log.info("Edited service '{}' at port {}", service.getName(), service.getPort());
|
||||
|
||||
modelMapper.map(dto, service);
|
||||
service.setPort(port);
|
||||
return save(service);
|
||||
}
|
||||
|
||||
private ServiceDto save(CtfService service) {
|
||||
final CtfService saved = repository.save(service);
|
||||
services.put(saved.getPort(), saved);
|
||||
|
||||
@@ -92,18 +116,18 @@ public class ServicesService {
|
||||
|
||||
updateFilter();
|
||||
|
||||
return saved;
|
||||
return toDto(saved);
|
||||
}
|
||||
|
||||
public void updateFilter() {
|
||||
pcapService.updateFilter(findAll());
|
||||
}
|
||||
|
||||
public ServiceDto toDto(CtfService service) {
|
||||
private ServiceDto toDto(CtfService service) {
|
||||
return modelMapper.map(service, ServiceDto.class);
|
||||
}
|
||||
|
||||
public CtfService fromDto(ServiceDto dto) {
|
||||
private CtfService fromDto(ServiceCreateDto dto) {
|
||||
return modelMapper.map(dto, CtfService.class);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user