Skip to content
Snippets Groups Projects

Security

Merged Jitka Viceníková requested to merge security into develop
Compare and Show latest version
2 files
+ 5
4
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -8,30 +8,53 @@ import cz.muni.pa165.data.repository.CarRepository;
import cz.muni.pa165.data.repository.DriverRepository;
import cz.muni.pa165.exceptions.BadRequestException;
import cz.muni.pa165.exceptions.ResourceNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.core.io.Resource;
import org.springframework.web.client.RestClientException;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@Service
public class CarService extends DomainService<Car> {
private static final Logger log = LoggerFactory.getLogger(CarService.class);
private static final String VISUALIZATION_MODULE_DOCKER = "http://visualization:8082";
private static final String VISUALIZATION_MODULE_LOCALHOST = "http://localhost:8082";
private String VISUALIZATION_MODULE_URL;
private static final String VISUALIZATION_MODULE_URI = "/visualization";
private final DriverRepository driverRepository;
private final CarRepository carRepository;
private final CarComponentRepository carComponentRepository;
private final WebClient webClient;
@Autowired
public CarService(CarRepository carRepository, DriverRepository driverRepository,
CarComponentRepository carComponentRepository) {
CarComponentRepository carComponentRepository, WebClient.Builder webClientBuilder) {
super(carRepository, Car.class);
this.driverRepository = driverRepository;
this.carRepository = carRepository;
this.carComponentRepository = carComponentRepository;
//if running in docker, modules cannot communicate through localhost
if (System.getenv("DOCKER") != null) {
VISUALIZATION_MODULE_URL = VISUALIZATION_MODULE_DOCKER;
}
else {
VISUALIZATION_MODULE_URL = VISUALIZATION_MODULE_LOCALHOST;
}
this.webClient = webClientBuilder.baseUrl(VISUALIZATION_MODULE_URL).build();
}
public Car create(List<Long> componentIds) {
@@ -40,6 +63,7 @@ public class CarService extends DomainService<Car> {
return car;
}
@Override
public void delete(Long id) {
Optional<Car> carFromDb = carRepository.findById(id);
if (carFromDb.isEmpty()) {
@@ -74,6 +98,19 @@ public class CarService extends DomainService<Car> {
new ResourceNotFoundException(entityClass, id));
}
@Transactional(readOnly = true)
public Car get(Long id) {
Car car = findById(id);
try {
sendPostRequest(car);
} catch (WebClientException e) {
log.debug(String.format("The visualization module is not reachable on the URL: %s, exception %s", VISUALIZATION_MODULE_URL + VISUALIZATION_MODULE_URI, e));
}
return car;
}
public Car setDriver(Long carId, Long driverId) {
Optional<Driver> driverFromDb = driverRepository.findById(driverId);
if (driverFromDb.isEmpty()) {
@@ -95,7 +132,15 @@ public class CarService extends DomainService<Car> {
carFromDb.get().setDriver(driverFromDb.get());
return repository.save(carFromDb.get());
var savedCar = repository.save(carFromDb.get());
try {
sendPostRequest(savedCar);
} catch (WebClientException e) {
log.debug(String.format("The visualization module is not reachable on the URL: %s, exception %s", VISUALIZATION_MODULE_URL + VISUALIZATION_MODULE_URI, e));
}
return savedCar;
}
public Car removeDriver(Long carId) {
@@ -153,4 +198,17 @@ public class CarService extends DomainService<Car> {
repository.save(car);
}
}
public void sendPostRequest(Car car) throws RestClientException {
Map<String, Object> visualization = new HashMap<>();
visualization.put("car", car);
webClient.post()
.uri(VISUALIZATION_MODULE_URI)
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(visualization)
.retrieve()
.bodyToMono(Resource.class)
.block();
}
}
Loading