30
30
import com .scanoss .filters .factories .FolderFilterFactory ;
31
31
import com .scanoss .processor .*;
32
32
import com .scanoss .rest .ScanApi ;
33
+ import com .scanoss .settings .Bom ;
33
34
import com .scanoss .settings .ScanossSettings ;
34
35
import com .scanoss .utils .JsonUtils ;
35
36
import lombok .*;
49
50
import java .util .concurrent .Executors ;
50
51
import java .util .concurrent .Future ;
51
52
import java .util .function .Predicate ;
53
+ import java .util .stream .Collectors ;
52
54
53
55
import static com .scanoss .ScanossConstants .*;
54
56
@@ -353,13 +355,12 @@ public List<String> wfpFolder(@NonNull String folder) throws ScannerException, W
353
355
*/
354
356
public String scanFile (@ NonNull String filename ) throws ScannerException , WinnowingException {
355
357
String wfp = wfpFile (filename );
356
- if (wfp != null && !wfp .isEmpty ()) {
357
- String response = this .scanApi .scan (wfp , "" , 1 );
358
- if (response != null && !response .isEmpty ()) {
359
- return response ;
360
- }
358
+ if (wfp == null || wfp .isEmpty ()) {
359
+ return "" ;
361
360
}
362
- return "" ;
361
+
362
+ String result = scanApi .scan (wfp , "" , 1 );
363
+ return postProcessResult (result );
363
364
}
364
365
365
366
/**
@@ -385,18 +386,52 @@ public List<String> scanFileList(@NonNull String folder, @NonNull List<String> f
385
386
return postProcessResults (results );
386
387
}
387
388
389
+
388
390
/**
389
- * Post-processes scan results based on BOM (Bill of Materials) settings if available.
390
- * @param results List of raw scan results in JSON string format
391
- * @return Processed results, either modified based on BOM or original results if no BOM exists
391
+ * Processes the result string and provides a post-processed output.
392
+ *
393
+ * @param rawResults the raw result string to be processed.
394
+ * @return the post-processed result string.
392
395
*/
393
- private List <String > postProcessResults (List <String > results ) {
394
- if (settings .getBom () != null ) {
395
- List <ScanFileResult > scanFileResults = JsonUtils .toScanFileResults (results );
396
- List <ScanFileResult > newScanFileResults = this .postProcessor .process (scanFileResults , this .settings .getBom ());
397
- return JsonUtils .toRawJsonString (newScanFileResults );
396
+ private String postProcessResult (String rawResults ) {
397
+ if (rawResults == null || rawResults .isEmpty ()) {
398
+ return "" ;
398
399
}
399
- return results ;
400
+ return postProcessResults (List .of (rawResults )).stream ()
401
+ .findFirst ()
402
+ .orElse ("" );
400
403
}
401
404
405
+ /**
406
+ * Processes the given list of raw scan results by applying deobfuscation and post-processing steps based on settings.
407
+ *
408
+ * @param rawResults a list of raw scan results in string format to be processed
409
+ * @return a list of processed scan results in string format
410
+ */
411
+ private List <String > postProcessResults (List <String > rawResults ) {
412
+ List <ScanFileResult > scanFileResults = JsonUtils .toScanFileResults (rawResults );
413
+
414
+ if (obfuscate ) {
415
+ scanFileResults = deobfuscateResults (scanFileResults );
416
+ }
417
+
418
+ Bom bom = settings .getBom ();
419
+ if (bom != null ) {
420
+ scanFileResults = this .postProcessor .process (scanFileResults , bom );
421
+ }
422
+
423
+ return JsonUtils .toRawJsonString (scanFileResults );
424
+ }
425
+
426
+ /**
427
+ * Deobfuscate the file paths in a list of ScanFileResult.
428
+ *
429
+ * @param scanFileResults List of ScanFileResult to be deobfuscated
430
+ * @return List of ScanFileResult with deobfuscated file paths
431
+ */
432
+ private List <ScanFileResult > deobfuscateResults (@ NonNull List <ScanFileResult > scanFileResults ) {
433
+ return scanFileResults .stream ()
434
+ .map (result -> result .withFilePath (winnowing .deobfuscateFilePath (result .getFilePath ())))
435
+ .collect (Collectors .toList ());
436
+ }
402
437
}
0 commit comments