7
7
8
8
import static com .google .common .base .Strings .isNullOrEmpty ;
9
9
import static org .eclipse .sw360 .datahandler .common .SW360Constants .CONTENT_TYPE_OPENXML_SPREADSHEET ;
10
+ import static org .eclipse .sw360 .datahandler .common .SW360Constants .XML_FILE_EXTENSION ;
10
11
import static org .springframework .hateoas .server .mvc .WebMvcLinkBuilder .linkTo ;
11
12
12
13
import java .io .IOException ;
35
36
import org .springframework .data .rest .webmvc .RepositoryLinksResource ;
36
37
import org .springframework .hateoas .server .RepresentationModelProcessor ;
37
38
import org .springframework .http .converter .HttpMessageNotReadableException ;
39
+ import org .springframework .security .access .AccessDeniedException ;
38
40
import org .springframework .util .FileCopyUtils ;
39
41
import org .springframework .web .bind .annotation .GetMapping ;
40
42
import org .springframework .web .bind .annotation .RequestParam ;
@@ -84,7 +86,7 @@ public void getProjectReport(
84
86
@ Parameter (description = "Module name." , schema = @ Schema (allowableValues = {
85
87
SW360Constants .PROJECTS , SW360Constants .COMPONENTS , SW360Constants .LICENSES ,
86
88
LICENSE_INFO , LICENSES_RESOURCE_BUNDLE , SW360Constants .PROJECT_RELEASE_SPREADSHEET_WITH_ECCINFO ,
87
- EXPORT_CREATE_PROJ_CLEARING_REPORT
89
+ EXPORT_CREATE_PROJ_CLEARING_REPORT , SW360Constants . SBOM
88
90
}))
89
91
@ RequestParam (value = "module" , required = true ) String module ,
90
92
@ Parameter (description = "Exclude release version from the license info file" )
@@ -101,6 +103,8 @@ public void getProjectReport(
101
103
@ RequestParam (value = "externalIds" , required = false , defaultValue = "" ) String externalIds ,
102
104
@ Parameter (description = "Generate report for only current project or with Sub projects. Can be supplied with modules [" + LICENSE_INFO + ", " + EXPORT_CREATE_PROJ_CLEARING_REPORT + "]" )
103
105
@ RequestParam (value = "withSubProject" , required = false , defaultValue = "false" ) boolean withSubProject ,
106
+ @ Parameter (description = "Type of SBOM file extention" )
107
+ @ RequestParam (value = "bomType" , required = false ) String bomType ,
104
108
HttpServletRequest request ,
105
109
HttpServletResponse response
106
110
) throws TException {
@@ -139,10 +143,18 @@ public void getProjectReport(
139
143
exportProjectCreateClearingRequest (response , sw360User , module , projectId , excludeReleaseVersion ,
140
144
generatorClassName , variant , template , externalIds );
141
145
break ;
146
+ case SW360Constants .SBOM :
147
+ exportSBOM (response , sw360User , module , projectId ,generatorClassName ,
148
+ bomType ,withSubProject );
149
+ break ;
142
150
default :
143
151
break ;
144
152
}
145
- } catch (Exception e ) {
153
+ }
154
+ catch (AccessDeniedException e ) {
155
+ throw e ;
156
+ }
157
+ catch (Exception e ) {
146
158
throw new TException (e .getMessage ());
147
159
}
148
160
}
@@ -371,4 +383,32 @@ private String getBaseUrl(HttpServletRequest request) {
371
383
String ctx = request .getContextPath ();
372
384
return url .substring (0 , url .length () - uri .length () + ctx .length ()) + "/" ;
373
385
}
386
+
387
+
388
+ private void exportSBOM (
389
+ HttpServletResponse response , User sw360User , String module , String projectId ,
390
+ String generatorClassName , String bomType , boolean withSubProject
391
+ ) throws TException {
392
+ try {
393
+ String buff = sw360ReportService .getProjectSBOMBuffer (sw360User , projectId , bomType , withSubProject );
394
+ response .setContentType (SW360Constants .CONTENT_TYPE_JSON );
395
+ String fileName = sw360ReportService .getSBOMFileName (sw360User , projectId , module , bomType );
396
+ if (null == buff ) {
397
+ throw new TException ("No data available for the user " + sw360User .getEmail ());
398
+ }
399
+ if (SW360Constants .XML_FILE_EXTENSION .equalsIgnoreCase (bomType )) {
400
+ response .setContentType (SW360Constants .CONTENT_TYPE_XML );
401
+ }
402
+ response .setHeader ("Content-Disposition" , String .format ("attachment; filename=\" %s\" " , fileName ));
403
+ copyDataStreamToResponse (response , ByteBuffer .wrap (buff .getBytes ()));
404
+ }
405
+ catch (AccessDeniedException e ) {
406
+ log .error (e );
407
+ throw e ;
408
+ }
409
+ catch (Exception e ) {
410
+ log .error (e );
411
+ throw new TException (e .getMessage ());
412
+ }
413
+ }
374
414
}
0 commit comments