Merge remote-tracking branch 'origin/feature-ml-integration' into dev

This commit is contained in:
heyethereum
2024-08-14 21:16:45 +08:00
11 changed files with 47 additions and 50 deletions

View File

@@ -16,4 +16,8 @@ public class GlobalExceptionHandler {
public ResponseEntity<ErrorResponse> handleResourceAlreadyExistsException(ResourceAlreadyExists e) {
return new ResponseEntity<>(new ErrorResponse(e.getMessage(), HttpStatus.BAD_REQUEST.value()), HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(InvalidFormatExceptions.class)
public ResponseEntity<ErrorResponse> handleInvalidFormatException(InvalidFormatExceptions e) {
return new ResponseEntity<>(new ErrorResponse(e.getMessage(), HttpStatus.BAD_REQUEST.value()), HttpStatus.BAD_REQUEST);
}
}

View File

@@ -0,0 +1,7 @@
package com.safeqr.app.exceptions;
public class InvalidFormatExceptions extends RuntimeException {
public InvalidFormatExceptions(String message){
super(message);
}
}

View File

@@ -3,11 +3,9 @@ package com.safeqr.app.qrcode.controller;
import static com.safeqr.app.constants.APIConstants.*;
import static com.safeqr.app.constants.CommonConstants.*;
import com.safeqr.app.qrcode.dto.request.QRCodePayload;
import com.safeqr.app.qrcode.dto.RedirectCountResponse;
import com.safeqr.app.qrcode.dto.response.BaseScanResponse;
import com.safeqr.app.qrcode.entity.QRCodeTypeEntity;
import com.safeqr.app.qrcode.service.QRCodeTypeService;
import com.safeqr.app.qrcode.service.RedirectCountService;
import com.safeqr.app.qrcode.service.URLVerificationService;
import com.safeqr.app.qrcode.service.VirusTotalService;
import org.slf4j.Logger;
@@ -35,9 +33,6 @@ public class QRCodeTypeController {
@Autowired
private VirusTotalService virusTotalService;
@Autowired
private RedirectCountService redirectCountService;
@GetMapping(value = API_URL_QRCODE_GET_ALL)
public ResponseEntity<List<QRCodeTypeEntity>> getAllTypes() {
return ResponseEntity.ok(qrCodeTypeService.getAllTypes());
@@ -79,9 +74,5 @@ public class QRCodeTypeController {
}
}
@PostMapping(API_URL_QRCODE_REDIRECT_COUNT)
public ResponseEntity<RedirectCountResponse> checkRedirects(@RequestBody QRCodePayload payload) {
return ResponseEntity.ok(redirectCountService.countRedirects(payload).block());
}
}

View File

@@ -1,9 +0,0 @@
package com.safeqr.app.qrcode.dto;
import lombok.Data;
@Data
public class RedirectCountResponse {
private int redirectCount;
private String message;
}

View File

@@ -2,6 +2,7 @@
package com.safeqr.app.qrcode.entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
@@ -39,6 +40,7 @@ public class QRCodeEntity {
@Column(name = "created_at", insertable = false, updatable = false)
private LocalDateTime createdAt;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@Column(name = "result_category")
private String result = CLASSIFY_UNKNOWN;
}

View File

@@ -23,6 +23,7 @@ import java.util.UUID;
@NoArgsConstructor
@AllArgsConstructor
public class URLEntity {
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@Column(name="classifications")
private String classifications;

View File

@@ -27,6 +27,8 @@ public final class SMSModel extends QRCodeModel<SMSEntity> {
@Override
public void setDetails() {
details = SMSEntity.builder().qrCodeId(data.getId()).build();
smsVerificationService.parseSMSString(details, data.getContents());
// Insert into sms table
smsVerificationService.insertDB(details);
}

View File

@@ -138,6 +138,9 @@ public class QRCodeTypeService {
QRCodeModel<?> qrCodeModel = qrCodeFactoryProvider.createQRCodeInstance(scannedQR);
qrCodeModel.setDetails();
// Get classifications based on verifications
scannedQR.setResult(qrCodeModel.retrieveClassification());
return qrCodeModel;
}
// Returns Default type as text if it does not fit into any of the category

View File

@@ -1,30 +0,0 @@
package com.safeqr.app.qrcode.service;
import com.safeqr.app.qrcode.dto.request.QRCodePayload;
import com.safeqr.app.qrcode.dto.RedirectCountResponse;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Service
public class RedirectCountService {
private static final Logger logger = LoggerFactory.getLogger(RedirectCountService.class);
public Mono<RedirectCountResponse> countRedirects(QRCodePayload payload) {
String url = payload.getData();
logger.info("RedirectCountService: countRedirects: url={}", url);
return WebClient.create()
.get()
.uri("https://google.com")// replace with url when logic is complete
.exchangeToMono(response -> {
RedirectCountResponse redirectCountResponse = new RedirectCountResponse();
redirectCountResponse.setRedirectCount(response.cookies().size());
redirectCountResponse.setMessage("Redirect count calculated.");
return Mono.just(redirectCountResponse);
});
}
}

View File

@@ -1,5 +1,6 @@
package com.safeqr.app.qrcode.service;
import com.safeqr.app.exceptions.InvalidFormatExceptions;
import com.safeqr.app.exceptions.ResourceNotFoundExceptions;
import com.safeqr.app.qrcode.entity.SMSEntity;
import com.safeqr.app.qrcode.repository.SMSRepository;
@@ -29,4 +30,29 @@ public class SMSVerificationService {
smsRepository.save(smsEntity);
}
public void parseSMSString(SMSEntity smsEntity, String smsto) throws IllegalArgumentException{
// Validate the string format
if (smsto == null || smsto.isEmpty()) {
throw new InvalidFormatExceptions("sms cannot be null or empty.");
}
// Remove the "SMSTO:" prefix
String data = smsto.substring(6);
// Split the data into phone number and message
String[] parts = data.split(":", 2);
// If both phone number and message are available
if (parts.length == 2) {
String phone = parts[0];
String message = parts[1];
// Populate the SMSEntity object
smsEntity.setPhone(phone);
smsEntity.setMessage(message);
} else {
// Handle the case where the format is invalid
throw new InvalidFormatExceptions("Invalid SMSTO format. Expected format: SMSTO:<phone>:<message>");
}
}
}

View File

@@ -2,7 +2,7 @@ package com.safeqr.app.user.service;
import com.safeqr.app.exceptions.ResourceAlreadyExists;
import com.safeqr.app.exceptions.ResourceNotFoundExceptions;
import com.safeqr.app.qrcode.entity.ScanBookmarkEntity;
import com.safeqr.app.qrcode.entity.ScanHistoryEntity;
import com.safeqr.app.qrcode.repository.ScanBookmarkRepository;
import com.safeqr.app.qrcode.repository.ScanHistoryRepository;
@@ -14,7 +14,7 @@ import com.safeqr.app.user.repository.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;