Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: implement validation for bundle and validate endpoint #745 #1089

Merged
merged 2 commits into from
Jan 23, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
Expand Down Expand Up @@ -34,6 +33,24 @@ public CsvController(CsvService csvService) {
this.csvService = csvService;
}

private void validateFile(MultipartFile file) {
if (file == null || file.isEmpty() || file.getOriginalFilename() == null
|| file.getOriginalFilename().trim().isEmpty()) {
throw new IllegalArgumentException("Validation Error: Uploaded file is missing or empty.");
}

String originalFilename = file.getOriginalFilename();
if (!originalFilename.toLowerCase().endsWith(".zip")) {
throw new IllegalArgumentException("Validation Error: Uploaded file must have a .zip extension.");
}
}

private void validateTenantId(String tenantId) {
if (tenantId == null || tenantId.trim().isEmpty()) {
throw new IllegalArgumentException("Tenant ID must be provided.");
}
}

@PostMapping(value = { "/flatfile/csv/Bundle/$validate",
"/flatfile/csv/Bundle/$validate/" }, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@ResponseBody
Expand All @@ -46,45 +63,28 @@ public Object handleCsvUpload(
HttpServletResponse response)
throws Exception {

// Validate file presence
if (file == null || file.isEmpty() || file.getOriginalFilename() == null
|| file.getOriginalFilename().trim().isEmpty()) {
log.error("CsvController: handleCsvUpload:: Uploaded file is missing or empty");
return new ResponseEntity<>(
"{\"status\":\"Error\",\"message\":\"Validation Error: Uploaded file is missing or empty.\"}",
HttpStatus.BAD_REQUEST);
}
// Validate file extension
String originalFilename = file.getOriginalFilename();
if (!originalFilename.toLowerCase().endsWith(".zip")) {
log.error("CsvController: handleCsvUpload:: Uploaded file is not a .zip file. Filename: " + originalFilename);
return new ResponseEntity<>(
"{\"status\":\"Error\",\"message\":\"Validation Error: Uploaded file must have a .zip extension.\"}",
HttpStatus.BAD_REQUEST);
}
if (tenantId == null || tenantId.trim().isEmpty()) {
log.error("CsvController: handleCsvUpload:: Tenant ID is missing or empty");
throw new IllegalArgumentException("Tenant ID must be provided");
}
return csvService.validateCsvFile(file, request, response, tenantId,origin,sftpSessionId);
validateFile(file);
validateTenantId(tenantId);
return csvService.validateCsvFile(file, request, response, tenantId, origin, sftpSessionId);
}

@PostMapping(value = { "/flatfile/csv/Bundle", "/flatfile/csv/Bundle/" }, consumes = {
MediaType.MULTIPART_FORM_DATA_VALUE })
@ResponseBody
@Async
public List<Object> handleCsvUploadAndConversion(
public ResponseEntity<Object> handleCsvUploadAndConversion(
@Parameter(description = "ZIP file containing CSV data. Must not be null.", required = true) @RequestPart("file") @Nonnull MultipartFile file,
@Parameter(description = "Parameter to specify the Tenant ID. This is a <b>mandatory</b> parameter.", required = true) @RequestHeader(value = Configuration.Servlet.HeaderName.Request.TENANT_ID, required = true) String tenantId,
@Parameter(description = "Parameter to specify origin of the request.", required = false) @RequestParam(value = "origin", required = false,defaultValue = "HTTP") String origin,
@Parameter(description = "Parameter to specify sftp session id.", required = false) @RequestParam(value = "sftp-session-id", required = false) String sftpSessionId,
@Parameter(description = "Optional parameter to specify if transformed validation issue needs to be appended.", required = false) @RequestParam(value = "append-validation-issue", required = false, defaultValue = "true") boolean appendValidationIssue,
HttpServletRequest request,
HttpServletResponse response) throws Exception {

if (tenantId == null || tenantId.trim().isEmpty()) {
throw new IllegalArgumentException("Tenant ID must be provided");
}
return csvService.processZipFile(file, request, response, tenantId,origin,sftpSessionId,appendValidationIssue);

validateFile(file);
validateTenantId(tenantId);
List<Object> processedFiles = csvService.processZipFile(file, request, response, tenantId, origin, sftpSessionId,appendValidationIssue);
return ResponseEntity.ok(processedFiles);

}
}
Loading