Commit ba2f8451 authored by Deniro StopCovid's avatar Deniro StopCovid

feat: Declare the new functions registerPushNotif and unregisterPushNotif in...

feat: Declare the new functions registerPushNotif and unregisterPushNotif in IRestApiService and empty implementations in RestApiServiceImpl
parent 0f5a86d4
package fr.gouv.stopc.robert.crypto.grpc.server.service.impl;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import javax.annotation.PostConstruct;
......@@ -34,9 +35,10 @@ public class CryptoServerConfigurationServiceImpl implements ICryptoServerConfig
@PostConstruct
private void initTimeStartNtp() {
LocalDate ld = LocalDate.parse(this.propertyLoader.getTimeStart(), DateTimeFormatter.BASIC_ISO_DATE);
timeStartNtp = TimeUtils.convertUnixMillistoNtpSeconds(ld.atStartOfDay().toEpochSecond(ZoneOffset.UTC));
final ZonedDateTime zdt = ld.atStartOfDay().atZone(ZoneId.of("UTC"));
timeStartNtp = TimeUtils.convertUnixMillistoNtpSeconds(zdt.toInstant().toEpochMilli());
}
@Override
public long getServiceTimeStart() {
return this.timeStartNtp;
......
......@@ -6,10 +6,6 @@ import org.springframework.web.client.RestTemplate;
@Configuration
public class Config {
public static final String API_V1 = "/v1";
public static final String API_V2 = "/v2";
@Bean
public RestTemplate restTemplate() {
......
package fr.gouv.stopc.robertserver.ws.controller;
import static fr.gouv.stopc.robertserver.ws.config.Config.API_V2;
import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
......@@ -21,7 +19,7 @@ import fr.gouv.stopc.robertserver.ws.utils.UriConstants;
import fr.gouv.stopc.robertserver.ws.vo.CaptchaInternalCreationVo;
@RestController
@RequestMapping(value = {"${controller.path.prefix}" + API_V2})
@RequestMapping(value = {"${controller.path.prefix}" + UriConstants.API_V2})
public interface ICaptchaController {
@PostMapping(value = UriConstants.CAPTCHA)
......
package fr.gouv.stopc.robertserver.ws.controller;
import static fr.gouv.stopc.robertserver.ws.config.Config.API_V1;
import static fr.gouv.stopc.robertserver.ws.config.Config.API_V2;
import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
......@@ -20,7 +17,8 @@ import fr.gouv.stopc.robertserver.ws.utils.UriConstants;
import fr.gouv.stopc.robertserver.ws.vo.DeleteHistoryRequestVo;
@RestController
@RequestMapping(value = {"${controller.path.prefix}" + API_V1, "${controller.path.prefix}" + API_V2})
@RequestMapping(value = {"${controller.path.prefix}" + UriConstants.API_V1, "${controller.path.prefix}" + UriConstants.API_V2,
"${controller.path.prefix}" + UriConstants.API_V3 })
@Consumes(MediaType.APPLICATION_JSON_VALUE)
@Produces(MediaType.APPLICATION_JSON_VALUE)
public interface IDeleteHistoryController {
......
package fr.gouv.stopc.robertserver.ws.controller;
import static fr.gouv.stopc.robertserver.ws.config.Config.API_V1;
import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
......@@ -19,7 +17,7 @@ import fr.gouv.stopc.robertserver.ws.utils.UriConstants;
import fr.gouv.stopc.robertserver.ws.vo.RegisterVo;
@RestController
@RequestMapping(value = "${controller.path.prefix}" + API_V1)
@RequestMapping(value = "${controller.path.prefix}" + UriConstants.API_V1)
@Consumes(MediaType.APPLICATION_JSON_VALUE)
@Produces(MediaType.APPLICATION_JSON_VALUE)
public interface IRegisterController {
......
package fr.gouv.stopc.robertserver.ws.controller;
import static fr.gouv.stopc.robertserver.ws.config.Config.API_V2;
import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
......@@ -19,7 +17,7 @@ import fr.gouv.stopc.robertserver.ws.utils.UriConstants;
import fr.gouv.stopc.robertserver.ws.vo.RegisterInternalVo;
@RestController
@RequestMapping(value = "${controller.path.prefix}" + API_V2)
@RequestMapping(value = {"${controller.path.prefix}" + UriConstants.API_V2, "${controller.path.prefix}" + UriConstants.API_V3})
@Consumes(MediaType.APPLICATION_JSON_VALUE)
@Produces(MediaType.APPLICATION_JSON_VALUE)
public interface IRegisterInternalController {
......
package fr.gouv.stopc.robertserver.ws.controller;
import static fr.gouv.stopc.robertserver.ws.config.Config.API_V1;
import static fr.gouv.stopc.robertserver.ws.config.Config.API_V2;
import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
......@@ -20,7 +17,8 @@ import fr.gouv.stopc.robertserver.ws.utils.UriConstants;
import fr.gouv.stopc.robertserver.ws.vo.ReportBatchRequestVo;
@RestController
@RequestMapping(value = {"${controller.path.prefix}" + API_V1, "${controller.path.prefix}" + API_V2})
@RequestMapping(value = {"${controller.path.prefix}" + UriConstants.API_V1, "${controller.path.prefix}" + UriConstants.API_V2,
"${controller.path.prefix}" + UriConstants.API_V3})
@Consumes(MediaType.APPLICATION_JSON_VALUE)
@Produces(MediaType.APPLICATION_JSON_VALUE)
public interface IReportController {
......
package fr.gouv.stopc.robertserver.ws.controller;
import static fr.gouv.stopc.robertserver.ws.config.Config.API_V1;
import static fr.gouv.stopc.robertserver.ws.config.Config.API_V2;
import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
......@@ -20,7 +17,8 @@ import fr.gouv.stopc.robertserver.ws.utils.UriConstants;
import fr.gouv.stopc.robertserver.ws.vo.StatusVo;
@RestController
@RequestMapping(value = {"${controller.path.prefix}" + API_V1, "${controller.path.prefix}" + API_V2})
@RequestMapping(value = {"${controller.path.prefix}" + UriConstants.API_V1, "${controller.path.prefix}" + UriConstants.API_V2,
"${controller.path.prefix}" + UriConstants.API_V3})
@Consumes(MediaType.APPLICATION_JSON_VALUE)
@Produces(MediaType.APPLICATION_JSON_VALUE)
public interface IStatusController {
......
package fr.gouv.stopc.robertserver.ws.controller;
import static fr.gouv.stopc.robertserver.ws.config.Config.API_V1;
import static fr.gouv.stopc.robertserver.ws.config.Config.API_V2;
import javax.validation.Valid;
import javax.ws.rs.Consumes;
......@@ -19,7 +17,8 @@ import fr.gouv.stopc.robertserver.ws.utils.UriConstants;
import fr.gouv.stopc.robertserver.ws.vo.UnregisterRequestVo;
@RestController
@RequestMapping(value = {"${controller.path.prefix}" + API_V1, "${controller.path.prefix}" + API_V2})
@RequestMapping(value = {"${controller.path.prefix}" + UriConstants.API_V1, "${controller.path.prefix}" + UriConstants.API_V2,
"${controller.path.prefix}" + UriConstants.API_V3})
@Consumes(MediaType.APPLICATION_JSON_VALUE)
@Produces(MediaType.APPLICATION_JSON_VALUE)
public interface IUnregisterController {
......
package fr.gouv.stopc.robertserver.ws.controller;
import static fr.gouv.stopc.robertserver.ws.config.Config.API_V1;
import java.time.LocalDate;
import java.util.List;
......@@ -15,6 +13,7 @@ import org.springframework.web.bind.annotation.RestController;
import fr.gouv.stopc.robertserver.ws.dto.RobertServerKpi;
import fr.gouv.stopc.robertserver.ws.service.IKpiService;
import fr.gouv.stopc.robertserver.ws.utils.UriConstants;
/**
* Endpoint definition for Kpi generation
......@@ -22,7 +21,8 @@ import fr.gouv.stopc.robertserver.ws.service.IKpiService;
*
*/
@RestController
@RequestMapping(value = "${controller.internal.path.prefix}" + API_V1, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@RequestMapping(value = "${controller.internal.path.prefix}" +
UriConstants.API_V1, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public class KpiController {
/**
......
......@@ -24,6 +24,7 @@ import fr.gouv.stopc.robertserver.database.service.IRegistrationService;
import fr.gouv.stopc.robertserver.ws.dto.ClientConfigDto;
import fr.gouv.stopc.robertserver.ws.dto.RegisterResponseDto;
import fr.gouv.stopc.robertserver.ws.exception.RobertServerException;
import fr.gouv.stopc.robertserver.ws.service.IRestApiService;
import fr.gouv.stopc.robertserver.ws.utils.MessageConstants;
import fr.gouv.stopc.robertserver.ws.vo.RegisterVo;
import lombok.extern.slf4j.Slf4j;
......@@ -35,6 +36,7 @@ public abstract class AbstractRegisterControllerImpl {
protected IServerConfigurationService serverConfigurationService;
protected IApplicationConfigService applicationConfigService;
protected ICryptoServerGrpcClient cryptoServerClient;
protected IRestApiService restApiService;
protected ResponseEntity<RegisterResponseDto> postCheckRegister(RegisterVo registerVo) throws RobertServerException {
......@@ -79,6 +81,9 @@ public abstract class AbstractRegisterControllerImpl {
registerResponseDto.setTuples(Base64.encode(identity.getTuples().toByteArray()));
registerResponseDto.setTimeStart(this.serverConfigurationService.getServiceTimeStart());
Optional.ofNullable(registerVo.getPushInfo()).ifPresent(this.restApiService::registerPushNotif);
return ResponseEntity.status(HttpStatus.CREATED).body(registerResponseDto);
}
......
......@@ -14,6 +14,7 @@ import fr.gouv.stopc.robertserver.ws.controller.IRegisterController;
import fr.gouv.stopc.robertserver.ws.dto.RegisterResponseDto;
import fr.gouv.stopc.robertserver.ws.exception.RobertServerException;
import fr.gouv.stopc.robertserver.ws.service.CaptchaService;
import fr.gouv.stopc.robertserver.ws.service.IRestApiService;
import fr.gouv.stopc.robertserver.ws.vo.RegisterVo;
@Service
......@@ -26,13 +27,15 @@ public class RegisterControllerImpl extends AbstractRegisterControllerImpl imple
final IServerConfigurationService serverConfigurationService,
final IApplicationConfigService applicationConfigService,
final CaptchaService captchaService,
final ICryptoServerGrpcClient cryptoServerClient) {
final ICryptoServerGrpcClient cryptoServerClient,
final IRestApiService restApiService) {
this.registrationService = registrationService;
this.serverConfigurationService = serverConfigurationService;
this.applicationConfigService = applicationConfigService;
this.captchaService = captchaService;
this.cryptoServerClient = cryptoServerClient;
this.restApiService = restApiService;
}
......
......@@ -15,6 +15,7 @@ import fr.gouv.stopc.robertserver.ws.controller.IRegisterInternalController;
import fr.gouv.stopc.robertserver.ws.dto.RegisterResponseDto;
import fr.gouv.stopc.robertserver.ws.exception.RobertServerException;
import fr.gouv.stopc.robertserver.ws.service.CaptchaInternalService;
import fr.gouv.stopc.robertserver.ws.service.IRestApiService;
import fr.gouv.stopc.robertserver.ws.vo.RegisterInternalVo;
@Service
......@@ -27,13 +28,15 @@ public class RegisterInternalControllerImpl extends AbstractRegisterControllerIm
final IServerConfigurationService serverConfigurationService,
final IApplicationConfigService applicationConfigService,
final CaptchaInternalService captchaInternalService,
final ICryptoServerGrpcClient cryptoServerClient) {
final ICryptoServerGrpcClient cryptoServerClient,
final IRestApiService restApiService) {
this.registrationService = registrationService;
this.serverConfigurationService = serverConfigurationService;
this.applicationConfigService = applicationConfigService;
this.captchaInternalService = captchaInternalService;
this.cryptoServerClient = cryptoServerClient;
this.restApiService = restApiService;
}
......
......@@ -17,7 +17,6 @@ import fr.gouv.stopc.robert.crypto.grpc.server.messaging.GetIdFromStatusResponse
import fr.gouv.stopc.robert.server.common.service.IServerConfigurationService;
import fr.gouv.stopc.robert.server.common.utils.TimeUtils;
import fr.gouv.stopc.robertserver.database.model.ApplicationConfigurationModel;
import fr.gouv.stopc.robertserver.database.model.EpochExposition;
import fr.gouv.stopc.robertserver.database.model.Registration;
import fr.gouv.stopc.robertserver.database.service.IApplicationConfigService;
import fr.gouv.stopc.robertserver.database.service.IRegistrationService;
......@@ -26,6 +25,7 @@ import fr.gouv.stopc.robertserver.ws.dto.ClientConfigDto;
import fr.gouv.stopc.robertserver.ws.dto.StatusResponseDto;
import fr.gouv.stopc.robertserver.ws.exception.RobertServerException;
import fr.gouv.stopc.robertserver.ws.service.AuthRequestValidationService;
import fr.gouv.stopc.robertserver.ws.service.IRestApiService;
import fr.gouv.stopc.robertserver.ws.utils.PropertyLoader;
import fr.gouv.stopc.robertserver.ws.vo.StatusVo;
import lombok.extern.slf4j.Slf4j;
......@@ -44,18 +44,23 @@ public class StatusControllerImpl implements IStatusController {
private final PropertyLoader propertyLoader;
private final IRestApiService restApiService;
@Inject
public StatusControllerImpl(
final IServerConfigurationService serverConfigurationService,
final IRegistrationService registrationService,
final IApplicationConfigService applicationConfigService,
final AuthRequestValidationService authRequestValidationService,
final PropertyLoader propertyLoader) {
final PropertyLoader propertyLoader,
final IRestApiService restApiService) {
this.serverConfigurationService = serverConfigurationService;
this.registrationService = registrationService;
this.applicationConfigService = applicationConfigService;
this.authRequestValidationService = authRequestValidationService;
this.propertyLoader = propertyLoader;
this.restApiService = restApiService;
}
@Override
......@@ -92,6 +97,9 @@ public class StatusControllerImpl implements IStatusController {
Optional<ResponseEntity> responseEntity = validate(record.get(), response.getEpochId(), response.getTuples().toByteArray());
if (responseEntity.isPresent()) {
Optional.ofNullable(statusVo.getPushInfo()).ifPresent(this.restApiService::registerPushNotif);
return responseEntity.get();
} else {
log.info("Status request failed validation");
......
......@@ -5,32 +5,34 @@ import java.util.Optional;
import javax.inject.Inject;
import fr.gouv.stopc.robert.crypto.grpc.server.messaging.DeleteIdRequest;
import fr.gouv.stopc.robert.crypto.grpc.server.messaging.DeleteIdResponse;
import fr.gouv.stopc.robert.crypto.grpc.server.messaging.GetIdFromAuthResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import fr.gouv.stopc.robert.server.common.DigestSaltEnum;
import fr.gouv.stopc.robert.crypto.grpc.server.messaging.DeleteIdResponse;
import fr.gouv.stopc.robertserver.database.model.Registration;
import fr.gouv.stopc.robertserver.database.service.IRegistrationService;
import fr.gouv.stopc.robertserver.ws.controller.IUnregisterController;
import fr.gouv.stopc.robertserver.ws.dto.UnregisterResponseDto;
import fr.gouv.stopc.robertserver.ws.service.AuthRequestValidationService;
import fr.gouv.stopc.robertserver.ws.service.IRestApiService;
import fr.gouv.stopc.robertserver.ws.vo.UnregisterRequestVo;
import io.micrometer.core.instrument.util.StringUtils;
@Service
public class UnregisterControllerImpl implements IUnregisterController {
private final IRegistrationService registrationService;
private final AuthRequestValidationService authRequestValidationService;
private final IRestApiService restApiService;
@Inject
public UnregisterControllerImpl(final IRegistrationService registrationService,
final AuthRequestValidationService authRequestValidationService) {
final AuthRequestValidationService authRequestValidationService,
final IRestApiService restApiService) {
this.registrationService = registrationService;
this.authRequestValidationService = authRequestValidationService;
this.restApiService = restApiService;;
}
@Override
......@@ -54,6 +56,10 @@ public class UnregisterControllerImpl implements IUnregisterController {
UnregisterResponseDto response = UnregisterResponseDto.builder().success(true).build();
if(StringUtils.isNotBlank(unregisterRequestVo.getPushToken())) {
this.restApiService.unregisterPushNotif(unregisterRequestVo.getPushToken());
}
return ResponseEntity.ok(response);
} else {
return ResponseEntity.notFound().build();
......
......@@ -3,8 +3,13 @@ package fr.gouv.stopc.robertserver.ws.service;
import java.util.Optional;
import fr.gouv.stopc.robertserver.ws.dto.VerifyResponseDto;
import fr.gouv.stopc.robertserver.ws.vo.PushInfoVo;
public interface IRestApiService {
Optional<VerifyResponseDto> verifyReportToken(String token, String type);
void registerPushNotif(PushInfoVo pushInfoVo);
void unregisterPushNotif(String pushToken);
}
package fr.gouv.stopc.robertserver.ws.service.impl;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import javax.inject.Inject;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
......@@ -15,6 +18,8 @@ import org.springframework.web.util.UriComponentsBuilder;
import fr.gouv.stopc.robertserver.ws.dto.VerifyResponseDto;
import fr.gouv.stopc.robertserver.ws.service.IRestApiService;
import fr.gouv.stopc.robertserver.ws.utils.PropertyLoader;
import fr.gouv.stopc.robertserver.ws.vo.PushInfoVo;
import io.micrometer.core.instrument.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
@Slf4j
......@@ -41,7 +46,7 @@ public class RestApiServiceImpl implements IRestApiService {
try {
ResponseEntity<VerifyResponseDto> response = restTemplate.getForEntity(buildReportTokenVerificationURI(token, type),
VerifyResponseDto.class);
return Optional.ofNullable(response.getBody());
} catch (RestClientException e) {
log.error("Unable to verify the token due to {}", e.getMessage());
......@@ -63,4 +68,58 @@ public class RestApiServiceImpl implements IRestApiService {
.toUri();
}
private URI buildRegistertPushNotifURI() {
return UriComponentsBuilder.newInstance().scheme("http")
.host(this.propertyLoader.getPushServerHost())
.port(this.propertyLoader.getPushServerPort())
.path(this.propertyLoader.getInternalPathPrefix())
.path(this.propertyLoader.getPushApiVersion())
.path(this.propertyLoader.getPushApiPath())
.build()
.encode()
.toUri();
}
private URI buildUnregistertPushNotifURI(String pushToken) {
Map<String, String> parameters = new HashMap<>();
parameters.put("token", pushToken);
return UriComponentsBuilder.fromUri(this.buildRegistertPushNotifURI())
.path(this.propertyLoader.getPushApiTokenPath())
.build(parameters);
}
@Override
public void registerPushNotif(PushInfoVo pushInfoVo) {
if (Objects.nonNull(pushInfoVo)) {
try {
this.restTemplate.postForEntity(this.buildRegistertPushNotifURI(),
pushInfoVo, Object.class);
log.info("Register to push notification successful");
} catch (RestClientException e) {
log.error("Register to push notification failed due to {}", e.getMessage());
}
}
}
@Override
public void unregisterPushNotif(String pushToken) {
if(StringUtils.isNotBlank(pushToken)) {
try {
this.restTemplate.exchange(this.buildUnregistertPushNotifURI(pushToken),
HttpMethod.DELETE, null, Object.class);
log.info("Unregister to push notification successful");
} catch (RestClientException e) {
log.error("Unregister to push notification failed due to {}", e.getMessage());
}
}
}
}
......@@ -70,4 +70,22 @@ public class PropertyLoader {
@Value("${robert.server.captcha-challenge-timestamp-tolerance}")
private Integer captchaChallengeTimestampTolerance;
@Value("${controller.internal.path.prefix}")
private String internalPathPrefix;
@Value("${push.server.host}")
private String pushServerHost;
@Value("${push.server.port}")
private String pushServerPort;
@Value("${push.api.version}")
private String pushApiVersion;
@Value("${push.api.path}")
private String pushApiPath;
@Value("${push.api.path.token}")
private String pushApiTokenPath;
}
......@@ -2,21 +2,27 @@ package fr.gouv.stopc.robertserver.ws.utils;
public final class UriConstants {
private UriConstants() {
private UriConstants() {
throw new AssertionError();
}
throw new AssertionError();
}
public static final String REPORT = "/report";
public static final String REPORT = "/report";
public static final String STATUS = "/status";
public static final String STATUS = "/status";
public static final String REGISTER = "/register";
public static final String REGISTER = "/register";
public static final String UNREGISTER = "/unregister";
public static final String UNREGISTER = "/unregister";
public static final String DELETE_HISTORY = "/deleteExposureHistory";
public static final String DELETE_HISTORY = "/deleteExposureHistory";
public static final String CAPTCHA = "/captcha";
public static final String CAPTCHA = "/captcha";
public static final String API_V1 = "/v1";
public static final String API_V2 = "/v2";
public static final String API_V3 = "/v3";
}
package fr.gouv.stopc.robertserver.ws.vo;
import javax.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
public class PushInfoVo {
@NotNull
@ToString.Exclude