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

Convert Endpoint #201

Merged
merged 50 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
22478a3
Base skeleton code for Conversion.
txdvse Aug 3, 2023
a801034
Initially moved code from the API's Converter.java
juanfpatino Aug 3, 2023
6d00d23
Conversion.java can convert to SVIP, ConvertTest stubbed out with two…
juanfpatino Aug 3, 2023
43a37a0
Merge branch 'dev' of https://github.com/SoftwareDesignLab/SVIP into …
juanfpatino Aug 3, 2023
30a732c
ConvertTest.convertTest stubbed out
juanfpatino Aug 3, 2023
6eda9f1
Reformatted CDX converter (ConvertCDX14.java)
txdvse Aug 5, 2023
fc696da
ConvertSPDX23.java
txdvse Aug 6, 2023
ee7938e
ConvertSPDX23.java
txdvse Aug 6, 2023
e5e0c64
ConvertSPDX23.java
txdvse Aug 6, 2023
e9ce98a
ConvertSPDX23.java and ConvertCDX14.java
txdvse Aug 7, 2023
b7444ac
ConvertTest.java
txdvse Aug 7, 2023
d75bfe9
ConvertTest.java
txdvse Aug 7, 2023
774f15c
ConvertTest.java
txdvse Aug 7, 2023
6f5f4bf
Merge pull request #199 from SoftwareDesignLab/dev-convertRefactor-CDX14
txdvse Aug 7, 2023
1ea9c55
ConvertTest.java
txdvse Aug 7, 2023
38381b0
changelog.md
juanfpatino Aug 7, 2023
9a1dd6d
Changelog date and version correction
juanfpatino Aug 7, 2023
1670dbf
Added quotations
juanfpatino Aug 7, 2023
7af0b0d
docstring and import fixes
txdvse Aug 7, 2023
a7413b5
Merge remote-tracking branch 'origin/dev-convertRefactor' into dev-co…
txdvse Aug 7, 2023
b153887
Code cleanup
juanfpatino Aug 7, 2023
32bf951
Merge remote-tracking branch 'origin/dev-convertRefactor' into dev-co…
juanfpatino Aug 7, 2023
d549b1e
Merge branch 'dev-convertRefactor' of https://github.com/SoftwareDesi…
juanfpatino Aug 7, 2023
b4b9074
Merge branch 'dev-dbRefactor-sboms' of https://github.com/SoftwareDes…
juanfpatino Aug 7, 2023
d8738bb
APITest.java impl SBOMController, ConvertFromAPITest fixed
juanfpatino Aug 7, 2023
90fe519
SVIP builds
juanfpatino Aug 7, 2023
3b24d88
Deserialization logic from old Converter moved to SBOMFileService, De…
juanfpatino Aug 7, 2023
f76893b
SBOMBuilderException, SerializerException in SBOMFileService.convert
juanfpatino Aug 7, 2023
7cf9245
SBOMFileService.convert finished but untested
juanfpatino Aug 7, 2023
f4ec13f
ConvertFromAPITest.CDXTagValueTest assertThrows SerializerException
juanfpatino Aug 7, 2023
a84055b
SBOMFile -> SBOM "casting" temporary fixes. ConvertFromAPITest passes
juanfpatino Aug 7, 2023
8dddad9
extra assertion outside of unit tests
juanfpatino Aug 7, 2023
c8ce871
Todos
juanfpatino Aug 7, 2023
1acdfa3
Incorrect serialization throws SerializerException to controller
juanfpatino Aug 7, 2023
642dd84
changelog.md before deleting 'Converter.java'
juanfpatino Aug 7, 2023
940b51f
deleted `Converter.java`, fixed GenerateFromOSIAPITest.java after doi…
juanfpatino Aug 7, 2023
12efbbb
Code cleanup
juanfpatino Aug 7, 2023
ad39fcf
Usage of UploadSBOMFileInput in SBOMFileService.java
juanfpatino Aug 7, 2023
6e16505
SBOM.id GeneratedValue reverted to Auto
juanfpatino Aug 8, 2023
6cbe8ff
Conversion in the API is no longer dependent on `Utils.java`
juanfpatino Aug 8, 2023
d5555bb
'UploadSBOMFileInput.java' generates id, better todos
juanfpatino Aug 8, 2023
429e784
Code cleanup
juanfpatino Aug 8, 2023
74e2767
SBOM.id private again, /convert and /upload tested with frontend
juanfpatino Aug 8, 2023
b6bb9c0
better todos
juanfpatino Aug 8, 2023
75c8a26
Removed commented line in UploadSBOMFileInput.java
juanfpatino Aug 8, 2023
59da2b4
Removed SBOMFileIdentifierGenerator.java and uses
juanfpatino Aug 8, 2023
8cc4981
SBOMFileService.getSBOMObject now returns a hashmap of the table name…
juanfpatino Aug 8, 2023
0939210
deserialized SBOM is allowed to be null for error handling
juanfpatino Aug 8, 2023
eb900d6
new name is generated for converted SBOM_File
juanfpatino Aug 8, 2023
ce1fc06
Reverted hashmap signature, new name is generated for converted SBOM
juanfpatino Aug 8, 2023
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
60 changes: 26 additions & 34 deletions api/src/main/java/org/svip/api/controller/SBOMController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,12 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.svip.api.entities.SBOM;
import org.svip.api.entities.SBOMFile;
import org.svip.api.requests.UploadSBOMFileInput;
import org.svip.api.services.SBOMFileService;
import org.svip.api.utils.Converter;
import org.svip.api.utils.Utils;
import org.svip.merge.MergerController;
import org.svip.merge.MergerException;
import org.svip.sbom.builder.objects.SVIPSBOMBuilder;
import org.svip.sbom.builder.SBOMBuilderException;
import org.svip.serializers.SerializerFactory;
import org.svip.serializers.deserializer.Deserializer;
import org.svip.serializers.serializer.Serializer;

import java.util.ArrayList;
import java.util.Optional;
import java.util.Random;
import org.svip.serializers.exceptions.DeserializerException;
import org.svip.serializers.exceptions.SerializerException;

/**
* REST API Controller for managing SBOM and SBOM operations
Expand All @@ -44,7 +35,7 @@ public class SBOMController {
*
* @param sbomService Service for handling SBOM queries
*/
public SBOMController(SBOMFileService sbomService){
public SBOMController(SBOMFileService sbomService) {
this.sbomService = sbomService;
}

Expand All @@ -55,7 +46,7 @@ public SBOMController(SBOMFileService sbomService){

/**
* USAGE. Send POST request to /sboms with one SBOM Input data.
*
* <p>
* The API will respond with an HTTP 200 and the ID used to identify the SBOM file.
*
* @param uploadSBOMInput Input required to create a new SBOM instance from a request
Expand All @@ -79,7 +70,7 @@ public ResponseEntity<Long> upload(@RequestBody UploadSBOMFileInput uploadSBOMIn
// Problem with parsing
LOGGER.error("POST /svip/sboms - " + e.getMessage());
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
} catch (Exception e){
} catch (Exception e) {
// Problem with uploading
LOGGER.error("POST /svip/sboms - " + e.getMessage());
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
Expand All @@ -99,7 +90,7 @@ public ResponseEntity<Long> merge(@RequestBody Long[] ids) {

Long mergeID = this.sbomService.merge(ids);
// todo convert should probably throw errors instead of returning null if error occurs
if(mergeID == null)
if (mergeID == null)
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);

// Return final merged ID
Expand All @@ -122,14 +113,15 @@ public ResponseEntity<Long> merge(@RequestBody Long[] ids) {
* @return ID of converted SBOM
*/
@PutMapping("/sboms")
public ResponseEntity<Long> convert(@RequestParam("id") Long id, @RequestParam("schema") SerializerFactory.Schema schema,
@RequestParam("format") SerializerFactory.Format format,
@RequestParam("overwrite") Boolean overwrite) {

Long convertID = this.sbomService.convert(id, schema, format, overwrite);
// todo convert should probably throw errors instead of returning null if error occurs
if(convertID == null)
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
public ResponseEntity<?> convert(@RequestParam("id") Long id, @RequestParam("schema") SerializerFactory.Schema schema,
@RequestParam("format") SerializerFactory.Format format,
@RequestParam("overwrite") Boolean overwrite) {
Long convertID;
try {
convertID = this.sbomService.convert(id, schema, format, overwrite);
} catch (DeserializerException | SBOMBuilderException | SerializerException | JsonProcessingException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}

// Return converted ID
return new ResponseEntity<>(convertID, HttpStatus.OK);
Expand All @@ -142,26 +134,26 @@ public ResponseEntity<Long> convert(@RequestParam("id") Long id, @RequestParam("

/**
* USAGE. Send GET request to /sboms with a URL parameter id to get the deserialized SBOM.
*
* <p>
* The API will respond with an HTTP 200 and the SBOM object json
*
* @param id The id of the SBOM contents to retrieve.
* @return A deserialized SBOM Object in JSON form
*/
@GetMapping("/sbom")
public ResponseEntity<String> getSBOMObjectAsJSON(@RequestParam("id") Long id){
public ResponseEntity<String> getSBOMObjectAsJSON(@RequestParam("id") Long id) {

try{
try {
String sbom = this.sbomService.getSBOMObjectAsJSON(id);

// No SBOM was found
if(sbom == null)
if (sbom == null)
return new ResponseEntity<>(HttpStatus.NO_CONTENT);

// Else return the object
return new ResponseEntity<>(sbom, HttpStatus.OK);

} catch (JsonProcessingException e ){
} catch (JsonProcessingException e) {
// error with Deserialization
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
Expand All @@ -170,7 +162,7 @@ public ResponseEntity<String> getSBOMObjectAsJSON(@RequestParam("id") Long id){

/**
* USAGE. Send GET request to /sboms/content with a URL parameter id to get the contents of the SBOM with the specified ID.
*
* <p>
* The API will respond with an HTTP 200 and the contents of the SBOM file.
*
* @param id The id of the SBOM contents to retrieve.
Expand Down Expand Up @@ -202,7 +194,7 @@ public ResponseEntity<SBOM> getContent(@RequestParam("id") Long id) {
* @return A JSON array of IDs of all currently uploaded SBOM files.
*/
@GetMapping("/sboms")
public ResponseEntity<Long[]> getAllIds(){
public ResponseEntity<Long[]> getAllIds() {

// Get all ids
Long[] ids = this.sbomService.getAllIDs();
Expand All @@ -211,7 +203,7 @@ public ResponseEntity<Long[]> getAllIds(){
LOGGER.info("GET /svip/sboms - Found " + ids.length + " sbom" + (ids.length == 0 ? "." : "s."));

// report nothing if no SBOMs in the database
if(ids.length == 0)
if (ids.length == 0)
return new ResponseEntity<>(HttpStatus.NO_CONTENT);

// Else return the array of stored IDs
Expand All @@ -226,7 +218,7 @@ public ResponseEntity<Long[]> getAllIds(){
/**
* USAGE. Send DELETE request to /delete with a URL parameter id to get the contents of the SBOM with the specified
* ID.
*
* <p>
* The API will respond with an HTTP 200 and the ID of the deleted SBOM file (if found).
*
* @param id The id of the SBOM contents to retrieve.
Expand All @@ -236,7 +228,7 @@ public ResponseEntity<Long[]> getAllIds(){
public ResponseEntity<Long> delete(@RequestParam("id") Long id) {

// Attempt to delete id
if(this.sbomService.deleteSBOMFile(id) == null){
if (this.sbomService.deleteSBOMFile(id) == null) {
LOGGER.warn("DELETE /svip/sboms?id=" + id + " - FILE NOT FOUND");
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
Expand Down
12 changes: 7 additions & 5 deletions api/src/main/java/org/svip/api/controller/SVIPApiController.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
import org.springframework.web.multipart.MultipartFile;
import org.svip.api.entities.SBOMFile;
import org.svip.api.repository.SBOMFileRepository;
import org.svip.api.utils.Converter;
import org.svip.api.services.SBOMFileService;
import org.svip.api.utils.Utils;
import org.svip.conversion.Conversion;
import org.svip.sbom.builder.SBOMBuilderException;
import org.svip.sbom.builder.objects.SVIPSBOMBuilder;
import org.svip.sbom.model.interfaces.generics.Component;
import org.svip.sbom.model.interfaces.generics.SBOM;
Expand Down Expand Up @@ -251,8 +253,7 @@ public ResponseEntity<?> generateParsers(@RequestParam("zipFile") MultipartFile

SBOMFile result = new SBOMFile(projectName + ((format == SerializerFactory.Format.JSON)
? ".json" : ".spdx"), contents);
Random rand = new Random();
result.setId(Utils.generateNewId(rand.nextLong(), rand, sbomFileRepository));
result.setId(SBOMFileService.generateSBOMFileId());
sbomFileRepository.save(result);

return Utils.encodeResponse(result.getId());
Expand All @@ -263,7 +264,7 @@ public ResponseEntity<?> generateParsers(@RequestParam("zipFile") MultipartFile
public ResponseEntity<?> generateOSI(@RequestParam("zipFile") MultipartFile zipFile,
@RequestParam("projectName") String projectName,
@RequestParam("schema") SerializerFactory.Schema schema,
@RequestParam("format") SerializerFactory.Format format) {
@RequestParam("format") SerializerFactory.Format format) throws SBOMBuilderException {
if (osiContainer == null)
return new ResponseEntity<>("OSI has been disabled for this instance.", HttpStatus.NOT_FOUND);

Expand Down Expand Up @@ -357,7 +358,8 @@ public ResponseEntity<?> generateOSI(@RequestParam("zipFile") MultipartFile zipF
HttpStatus.NOT_FOUND);
}
}
Converter.buildSBOM(builder, osiMerged, schema, oldSchema);

Conversion.buildSBOM(osiMerged, schema, oldSchema);
builder.setName(projectName); // Set SBOM name to specified project name TODO should this be done in OSI class?
juanfpatino marked this conversation as resolved.
Show resolved Hide resolved

// Serialize SVIPSBOM to given schema and format
Expand Down
1 change: 1 addition & 0 deletions api/src/main/java/org/svip/api/entities/SBOM.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,5 @@ public Schema getSchema(){
public FileType getFileType() {
return this.fileType;
}

}
7 changes: 3 additions & 4 deletions api/src/main/java/org/svip/api/entities/SBOMFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.persistence.*;
import org.hibernate.annotations.GenericGenerator;
import org.svip.api.repository.SBOMFileIdentifierGenerator;

/**
* Dataclass containing JSON-friendly SBOM objects. Contains {@code fileName} & {@code contents} fields that are capable
Expand All @@ -22,9 +21,9 @@ public class SBOMFile {
* Autogenerated primary key.
*/
@Id
@GeneratedValue(generator = SBOMFileIdentifierGenerator.generatorName)
@GenericGenerator(name = SBOMFileIdentifierGenerator.generatorName,
strategy = "org.svip.api.repository.SBOMFileIdentifierGenerator")
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(nullable = false)
@JsonProperty
private long id;

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import org.svip.api.entities.SBOM;
import org.svip.api.services.SBOMFileService;
import org.svip.serializers.SerializerFactory;
import org.svip.serializers.deserializer.Deserializer;

Expand Down
Loading