11package com .evolvedbinary .bblValidator .controller ;
22
3- import com .evolvedbinary .bblValidator .dto .ValidationError ;
3+ import com .evolvedbinary .bblValidator .dto .ErrorResponse ;
4+ import com .evolvedbinary .bblValidator .dto .ResponseObject ;
45import com .evolvedbinary .bblValidator .dto .ValidationForm ;
56import com .evolvedbinary .bblValidator .dto .ValidationResponse ;
67import com .evolvedbinary .bblValidator .service .CsvValidationService ;
78import com .evolvedbinary .bblValidator .service .FileDownloadService ;
9+ import com .evolvedbinary .bblValidator .service .SchemaService ;
10+ import io .micronaut .core .annotation .Nullable ;
11+ import io .micronaut .http .HttpResponse ;
812import io .micronaut .http .MediaType ;
913import io .micronaut .http .annotation .Body ;
1014import io .micronaut .http .annotation .Consumes ;
1620import org .slf4j .LoggerFactory ;
1721
1822import java .io .IOException ;
23+ import java .nio .file .Files ;
1924import java .nio .file .Path ;
20- import java .util .List ;
2125
2226@ Controller ("/validate" )
2327public class ValidateController {
@@ -27,6 +31,8 @@ public class ValidateController {
2731 FileDownloadService fileDownloadService ;
2832 @ Inject
2933 CsvValidationService csvValidationService ;
34+ @ Inject
35+ SchemaService schemaService ;
3036
3137 /**
3238 * Handles form URL encoded validation requests.
@@ -36,14 +42,21 @@ public class ValidateController {
3642 */
3743 @ Post
3844 @ Consumes (MediaType .APPLICATION_FORM_URLENCODED )
39- public ValidationResponse validateForm (@ Body ValidationForm form ) {
45+ public HttpResponse <ResponseObject > validateForm (@ Body final ValidationForm form ) {
46+ if (null == schemaService .getSchema (form .schemaId ())) {
47+ return HttpResponse .badRequest ().body (new ErrorResponse (ErrorResponse .Code .SCHEMA_NOT_FOUND ,"Schema not found with ID: " + form .schemaId ()));
48+ }
4049 try {
41- Path downloadedFile = fileDownloadService .downloadToTemp (form .url ());
42- LOG .info ("File downloaded to: {}" , downloadedFile );
43- return performValidation (downloadedFile , form .schemaId ());
44- } catch (IOException e ) {
45- LOG .error ("Failed to download file from URL: {}" , form .url (), e );
46- return createErrorResponse ("Download failed: " + e .getMessage (), 0 );
50+ final Path downloadedFile = fileDownloadService .downloadToTemp (form .url ());
51+ LOG .trace ("File downloaded to: {}" , downloadedFile );
52+ try {
53+ return HttpResponse .ok (performValidation (downloadedFile , form .schemaId ()));
54+ } finally {
55+ Files .delete (downloadedFile );
56+ }
57+ } catch (final IOException e ) {
58+ LOG .trace ("Failed to download file from URL: {}" , form .url ());
59+ return HttpResponse .badRequest ().body (new ErrorResponse (ErrorResponse .Code .NON_RESOLVABLE_URL ,"Unable to resolve url : " + form .url ()));
4760 }
4861 }
4962
@@ -56,15 +69,25 @@ public ValidationResponse validateForm(@Body ValidationForm form) {
5669 */
5770 @ Post
5871 @ Consumes (MediaType .TEXT_CSV )
59- public ValidationResponse validateCsv (@ QueryValue ("schema-id" ) String schemaId ,
60- @ Body String csvContent ) {
72+ public HttpResponse <ResponseObject > validateCsv (@ QueryValue ("schema-id" ) final String schemaId ,
73+ @ Nullable @ Body final String csvContent ) {
74+ if (schemaService .getSchema (schemaId ) == null ) {
75+ return HttpResponse .badRequest ().body (new ErrorResponse (ErrorResponse .Code .SCHEMA_NOT_FOUND ,"Schema not found with ID: " + schemaId ));
76+ }
77+ if (csvContent == null || csvContent .isEmpty ()) {
78+ return HttpResponse .badRequest ().body (new ErrorResponse (ErrorResponse .Code .NO_CSV ,"Empty CSV content" ));
79+ }
6180 try {
62- Path tempFile = fileDownloadService .saveContentToTemp (csvContent );
63- LOG .info ("CSV content saved to: {}" , tempFile );
64- return performValidation (tempFile , schemaId );
65- } catch (IOException e ) {
81+ final Path tempFile = fileDownloadService .saveContentToTemp (csvContent );
82+ try {
83+ LOG .trace ("CSV content saved to: {}" , tempFile );
84+ return HttpResponse .ok (performValidation (tempFile , schemaId ));
85+ } finally {
86+ Files .delete (tempFile );
87+ }
88+ } catch (final IOException e ) {
6689 LOG .error ("Failed to save CSV content to temp file" , e );
67- return createErrorResponse ( "Failed to save content : " + e .getMessage (), 0 );
90+ return HttpResponse . serverError (). body ( new ErrorResponse ( ErrorResponse . Code . UNEXPECTED_ERROR , "Unable to store CSV : " + e .getMessage ()) );
6891 }
6992 }
7093
@@ -77,31 +100,32 @@ public ValidationResponse validateCsv(@QueryValue("schema-id") String schemaId,
77100 */
78101 @ Post
79102 @ Consumes (MediaType .ALL )
80- public ValidationResponse validateParams (@ QueryValue ("schema-id" ) String schemaId ,
81- @ QueryValue String url ) {
103+ public HttpResponse <ResponseObject > validateParams (@ QueryValue ("schema-id" ) final String schemaId ,
104+ @ QueryValue final String url ) {
105+ if (schemaService .getSchema (schemaId ) == null ) {
106+ return HttpResponse .badRequest ().body (new ErrorResponse (ErrorResponse .Code .SCHEMA_NOT_FOUND ,"Schema not found with ID: " + schemaId ));
107+ }
82108 try {
83- Path downloadedFile = fileDownloadService .downloadToTemp (url );
84- LOG .info ("File downloaded to: {}" , downloadedFile );
85- return performValidation (downloadedFile , schemaId );
86- } catch (IOException e ) {
87- LOG .error ("Failed to download file from URL: {}" , url , e );
88- return createErrorResponse ("Download failed: " + e .getMessage (), 0 );
109+ final Path downloadedFile = fileDownloadService .downloadToTemp (url );
110+ LOG .trace ("File downloaded to: {}" , downloadedFile );
111+ try {
112+ return HttpResponse .ok (performValidation (downloadedFile , schemaId ));
113+ } finally {
114+ Files .delete (downloadedFile );
115+ }
116+ } catch (final IOException e ) {
117+ LOG .trace ("Failed to download file from URL: {}" , url );
118+ return HttpResponse .badRequest ().body (new ErrorResponse (ErrorResponse .Code .NON_RESOLVABLE_URL ,"Unable to resolve url : " + url ));
89119 }
90120 }
91121
92- private ValidationResponse performValidation (Path csvFile , String schemaId ) {
93- CsvValidationService .ValidationResult result = csvValidationService .validateCsvFile (csvFile , schemaId );
122+ private ResponseObject performValidation (final Path csvFile , final String schemaId ) {
123+ final CsvValidationService .ValidationResult result = csvValidationService .validateCsvFile (csvFile , schemaId );
94124
95125 if (result .hasErrorMessage ()) {
96- return createErrorResponse ( result . getErrorMessage (), result .getExecutionTimeMs ());
126+ return new ErrorResponse ( ErrorResponse . Code . VALIDATION_ERROR , "An error occurred: " + result .getErrorMessage ());
97127 }
98128
99129 return new ValidationResponse (result .isValid (), result .getErrors (), result .getExecutionTimeMs ());
100130 }
101-
102- private ValidationResponse createErrorResponse (String errorMessage , long executionTimeMs ) {
103- return new ValidationResponse (false ,
104- List .of (new ValidationError (errorMessage , 0 , 0 )),
105- executionTimeMs );
106- }
107131}
0 commit comments