Moved monitoring request validation to ValidationUtil
This commit is contained in:
parent
43897c6cbb
commit
b4c4475398
2 changed files with 59 additions and 63 deletions
|
|
@ -10,7 +10,6 @@ import org.springframework.web.multipart.MultipartFile;
|
||||||
import ru.nbch.credit_tracker.config.settings.CreditTrackerSettings;
|
import ru.nbch.credit_tracker.config.settings.CreditTrackerSettings;
|
||||||
import ru.nbch.credit_tracker.entities.request.active_packages.MonitoringActivePackagesRequest;
|
import ru.nbch.credit_tracker.entities.request.active_packages.MonitoringActivePackagesRequest;
|
||||||
import ru.nbch.credit_tracker.entities.request.monitoring_request.MonitoringRequest;
|
import ru.nbch.credit_tracker.entities.request.monitoring_request.MonitoringRequest;
|
||||||
import ru.nbch.credit_tracker.entities.request.monitoring_request.Subject;
|
|
||||||
import ru.nbch.credit_tracker.entities.response.active_packages.MonitoringActivePackagesResponse;
|
import ru.nbch.credit_tracker.entities.response.active_packages.MonitoringActivePackagesResponse;
|
||||||
import ru.nbch.credit_tracker.entities.response.success.MonitoringPackageDeleteResponse;
|
import ru.nbch.credit_tracker.entities.response.success.MonitoringPackageDeleteResponse;
|
||||||
import ru.nbch.credit_tracker.entities.response.success.MonitoringReportListResponse;
|
import ru.nbch.credit_tracker.entities.response.success.MonitoringReportListResponse;
|
||||||
|
|
@ -32,10 +31,8 @@ import ru.nbch.credit_tracker.utils.ZipUtils;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipEntry;
|
||||||
import java.util.zip.ZipInputStream;
|
import java.util.zip.ZipInputStream;
|
||||||
|
|
@ -102,15 +99,13 @@ public class SignalFacade {
|
||||||
throw (SignalClientException) e;
|
throw (SignalClientException) e;
|
||||||
}
|
}
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
//todo throw new SignalServerException(e);
|
throw serverExcp(SignalError.ERROR_100, XmlErrorFormat.MonitoringError);
|
||||||
throw clientExcp(SignalError.ERROR_100, XmlErrorFormat.MonitoringError);
|
|
||||||
} finally {
|
} finally {
|
||||||
if (zipInputStream != null) {
|
if (zipInputStream != null) {
|
||||||
try {
|
try {
|
||||||
zipInputStream.close();
|
zipInputStream.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw serverExcp(SignalError.ERROR_100, XmlErrorFormat.MonitoringError);
|
||||||
//todo throw new SignalServerException(e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -118,7 +113,7 @@ public class SignalFacade {
|
||||||
|
|
||||||
// валидация xml
|
// валидация xml
|
||||||
try {
|
try {
|
||||||
validateMonitoringRequest(monitoringRequest);
|
ValidationUtil.validateMonitoringRequest(monitoringRequest);
|
||||||
} catch (SignalClientException e) {
|
} catch (SignalClientException e) {
|
||||||
signalService.rejectPackage(monitoringRequest);
|
signalService.rejectPackage(monitoringRequest);
|
||||||
throw e;
|
throw e;
|
||||||
|
|
@ -153,58 +148,6 @@ public class SignalFacade {
|
||||||
return ResponseEntity.ok().body(new MonitoringResponse());
|
return ResponseEntity.ok().body(new MonitoringResponse());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void validateMonitoringRequest(MonitoringRequest request) throws SignalClientException {
|
|
||||||
if (request == null) {
|
|
||||||
throw clientExcp(SignalError.ERROR_011, XmlErrorFormat.MonitoringError);
|
|
||||||
}
|
|
||||||
|
|
||||||
validateReruqredTags(request);
|
|
||||||
|
|
||||||
validateTagsTypes(request);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void validateReruqredTags(MonitoringRequest request) throws SignalClientException {
|
|
||||||
if (request.getUserId() == null) {
|
|
||||||
throw clientExcp(SignalError.ERROR_002, XmlErrorFormat.MonitoringError, "UserId");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (request.getPackageName() == null) {
|
|
||||||
throw clientExcp(SignalError.ERROR_002, XmlErrorFormat.MonitoringError, "PackageName");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (request.getPassword() == null) {
|
|
||||||
throw clientExcp(SignalError.ERROR_002, XmlErrorFormat.MonitoringError, "Password");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (request.getSubjects() == null) {
|
|
||||||
throw clientExcp(SignalError.ERROR_002, XmlErrorFormat.MonitoringError, "Subjects");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (request.getSubjects().getSubject().isEmpty()) {
|
|
||||||
throw clientExcp(SignalError.ERROR_002, XmlErrorFormat.MonitoringError, "Subject");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void validateTagsTypes(MonitoringRequest request) throws SignalClientException {
|
|
||||||
if (!ValidationUtil.validatePackageName(request.getPackageName())) {
|
|
||||||
throw clientExcp(SignalError.ERROR_008, XmlErrorFormat.MonitoringError, "Subject");
|
|
||||||
}
|
|
||||||
|
|
||||||
Set<String> uniqueIds = new HashSet<>();
|
|
||||||
for (Subject subject : request.getSubjects().getSubject()) {
|
|
||||||
if (!ValidationUtil.validatePhone(subject.getClientId())) {
|
|
||||||
throw clientExcp(SignalError.ERROR_003, XmlErrorFormat.MonitoringError);
|
|
||||||
}
|
|
||||||
if (!ValidationUtil.validateNPId(subject.getId())) {
|
|
||||||
throw clientExcp(SignalError.ERROR_010, XmlErrorFormat.MonitoringError);
|
|
||||||
}
|
|
||||||
// проверка на уникальность Id внутри одного пакета
|
|
||||||
if (!uniqueIds.add(subject.getId())) {
|
|
||||||
throw clientExcp(SignalError.ERROR_004, XmlErrorFormat.MonitoringError, subject.getId());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void registerFidsAndDebts(Integer packageId, String packageName) {
|
private void registerFidsAndDebts(Integer packageId, String packageName) {
|
||||||
executor.execute(() -> {
|
executor.execute(() -> {
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -1,29 +1,82 @@
|
||||||
package ru.nbch.credit_tracker.utils;
|
package ru.nbch.credit_tracker.utils;
|
||||||
|
|
||||||
|
import ru.nbch.credit_tracker.entities.request.monitoring_request.MonitoringRequest;
|
||||||
|
import ru.nbch.credit_tracker.entities.request.monitoring_request.Subject;
|
||||||
|
import ru.nbch.credit_tracker.enums.SignalError;
|
||||||
|
import ru.nbch.credit_tracker.enums.XmlErrorFormat;
|
||||||
|
import ru.nbch.credit_tracker.exceptions.SignalClientException;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import static ru.nbch.credit_tracker.utils.error.ExceptionUtils.clientExcp;
|
||||||
|
|
||||||
public class ValidationUtil {
|
public class ValidationUtil {
|
||||||
|
|
||||||
private static String phoneRegex = "^\\d{9}$";
|
private static String phoneRegex = "^\\d{9}$";
|
||||||
private static String packageNameRegex = "^[A-Z0-9_]{1,22}$";
|
private static String packageNameRegex = "^[A-Z0-9_]{1,22}$";
|
||||||
private static String npIdRegex = "^\\S{1,36}$";
|
private static String npIdRegex = "^\\S{1,36}$";
|
||||||
|
|
||||||
|
public static void validateMonitoringRequest(MonitoringRequest request) throws SignalClientException {
|
||||||
|
if (request == null) {
|
||||||
|
throw clientExcp(SignalError.ERROR_011, XmlErrorFormat.MonitoringError);
|
||||||
|
}
|
||||||
|
|
||||||
public static boolean validatePhone(String input) {
|
if (request.getUserId() == null) {
|
||||||
|
throw clientExcp(SignalError.ERROR_002, XmlErrorFormat.MonitoringError, "UserId");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.getPackageName() == null) {
|
||||||
|
throw clientExcp(SignalError.ERROR_002, XmlErrorFormat.MonitoringError, "PackageName");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.getPassword() == null) {
|
||||||
|
throw clientExcp(SignalError.ERROR_002, XmlErrorFormat.MonitoringError, "Password");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.getSubjects() == null) {
|
||||||
|
throw clientExcp(SignalError.ERROR_002, XmlErrorFormat.MonitoringError, "Subjects");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.getSubjects().getSubject().isEmpty()) {
|
||||||
|
throw clientExcp(SignalError.ERROR_002, XmlErrorFormat.MonitoringError, "Subject");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!validatePackageName(request.getPackageName())) {
|
||||||
|
throw clientExcp(SignalError.ERROR_008, XmlErrorFormat.MonitoringError, "Subject");
|
||||||
|
}
|
||||||
|
|
||||||
|
Set<String> uniqueIds = new HashSet<>();
|
||||||
|
for (Subject subject : request.getSubjects().getSubject()) {
|
||||||
|
if (!validatePhone(subject.getClientId())) {
|
||||||
|
throw clientExcp(SignalError.ERROR_003, XmlErrorFormat.MonitoringError);
|
||||||
|
}
|
||||||
|
if (!validateNPId(subject.getId())) {
|
||||||
|
throw clientExcp(SignalError.ERROR_010, XmlErrorFormat.MonitoringError);
|
||||||
|
}
|
||||||
|
// проверка на уникальность Id внутри одного пакета
|
||||||
|
if (!uniqueIds.add(subject.getId())) {
|
||||||
|
throw clientExcp(SignalError.ERROR_004, XmlErrorFormat.MonitoringError, subject.getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean validatePhone(String input) {
|
||||||
if(input == null) {
|
if(input == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return Pattern.matches(phoneRegex, input);
|
return Pattern.matches(phoneRegex, input);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean validatePackageName(String input) {
|
private static boolean validatePackageName(String input) {
|
||||||
if(input == null) {
|
if(input == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return Pattern.matches(packageNameRegex, input);
|
return Pattern.matches(packageNameRegex, input);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean validateNPId(String input) {
|
private static boolean validateNPId(String input) {
|
||||||
if(input == null) {
|
if(input == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue