Skip to content

Commit 1c6afff

Browse files
committed
Added backend functionalities for weaknesses
1 parent 7693ccb commit 1c6afff

File tree

26 files changed

+514
-259
lines changed

26 files changed

+514
-259
lines changed

src/main/java/com/sage/Main.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package com.sage;
22

3-
import com.sage.controller.MainController;
3+
import com.sage.controller.FileController;
44
import com.sage.controller.VulnerabilityController;
55
import com.sage.controller.WeaknessController;
66
import com.sage.utility.FileReaderUtility;
7+
import com.sage.utility.JSConsoleBridge;
78
import javafx.application.Application;
89
import javafx.scene.Scene;
910
import javafx.scene.image.Image;
@@ -24,21 +25,34 @@ public static void main(String[] args) {
2425
}
2526

2627
@Override
27-
public void start(Stage stage) throws Exception {
28+
public void start(Stage stage) {
2829
WebView webView = new WebView();
2930
WebEngine webEngine = webView.getEngine();
3031

32+
webEngine.setOnError(event -> LOGGER.warning("[Frontend] An error occured on the frontend: " + event.getMessage()));
33+
3134
File htmlFile = new File(getClass().getResource("/html/index.html").getFile());
3235
webEngine.load(htmlFile.toURI().toString());
36+
webEngine.setJavaScriptEnabled(true);
3337

3438
// Wait until WebView is fully loaded before injecting WeaknessController
3539
webEngine.getLoadWorker().stateProperty().addListener((obs, oldState, newState) -> {
3640
if (newState == javafx.concurrent.Worker.State.SUCCEEDED) {
3741
JSObject window = (JSObject) webEngine.executeScript("window");
38-
window.setMember("mainController", MainController.getInstance());
42+
window.setMember("fileController", FileController.getInstance());
3943
window.setMember("weaknessController", WeaknessController.getInstance());
4044
window.setMember("vulnerabilityController", VulnerabilityController.getInstance());
4145
window.setMember("fileReader", FileReaderUtility.getInstance());
46+
window.setMember("javaConsole", new JSConsoleBridge());
47+
48+
webEngine.executeScript("""
49+
console.log = function(msg) {
50+
javaConsole.log(msg);
51+
};
52+
console.error = function(msg) {
53+
javaConsole.error(msg);
54+
};
55+
""");
4256
}
4357
});
4458

src/main/java/com/sage/controller/MainController.java renamed to src/main/java/com/sage/controller/FileController.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,33 @@
22

33
import java.util.HashMap;
44
import java.util.logging.Logger;
5-
import java.util.List;
65

76
import com.sage.service.FileService;
87
import com.sage.service.VulnerabilityService;
8+
import com.sage.utility.JsonParser;
99

10-
public class MainController {
11-
private static final Logger LOGGER = Logger.getLogger(MainController.class.getName());
10+
public class FileController {
11+
private static final Logger LOGGER = Logger.getLogger(FileController.class.getName());
1212

13-
private static MainController instance;
13+
private static FileController instance;
1414
private static FileService fileService;
1515
private static VulnerabilityService vulnerabilityService;
1616

17-
private MainController() {
18-
17+
private FileController() {
1918
fileService = new FileService();
2019
vulnerabilityService = new VulnerabilityService();
2120
}
2221

23-
public static MainController getInstance() {
22+
public static FileController getInstance() {
2423
if (instance == null)
25-
instance = new MainController();
24+
instance = new FileController();
2625
return instance;
2726
}
2827

29-
public HashMap<String, Integer> process(String filePath) {
30-
System.out.println("Processing file: " + filePath);
28+
public String process(String filePath) {
3129
if (fileService.processFile(filePath))
32-
return vulnerabilityService.getStatistics();
33-
LOGGER.severe(String.format("[MainController] Failed to process SAST-Report-File: %s", filePath));
30+
return JsonParser.asJsonString(vulnerabilityService.getStatistics());
31+
LOGGER.severe(String.format("[FileController] Failed to process SAST-Report-File: %s", filePath));
3432
throw new InternalError("File: " + filePath + " could not be processed\n");
3533
}
3634

src/main/java/com/sage/controller/VulnerabilityController.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.sage.controller;
22

3+
import com.sage.dto.VulnerabilityDto;
34
import com.sage.service.VulnerabilityService;
45

56
public class VulnerabilityController {
@@ -15,4 +16,8 @@ public static VulnerabilityController getInstance() {
1516
instance = new VulnerabilityController();
1617
return instance;
1718
}
19+
20+
public VulnerabilityDto getById(Integer id) {
21+
return vulnerabilityService.getById(id);
22+
}
1823
}

src/main/java/com/sage/dao/VulnerabilityDao.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,41 +30,56 @@ public boolean create(VulnerabilityModel entity) {
3030
tx.commit();
3131
return true;
3232
} catch (Exception e) {
33+
if (tx.isActive())
34+
tx.rollback();
35+
3336
LOGGER.severe("[VulnerabilityDao] Error while inserting new vulnerability entity into database: " + e.getMessage());
3437
return false;
3538
}
3639
}
3740

3841
@Override
3942
public VulnerabilityModel read(Integer key) {
40-
tx.begin();
41-
VulnerabilityModel vulnerabilityModel = em.find(VulnerabilityModel.class, key);
42-
tx.commit();
43-
return vulnerabilityModel;
43+
try {
44+
tx.begin();
45+
VulnerabilityModel vulnerabilityModel = em.find(VulnerabilityModel.class, key);
46+
tx.commit();
47+
return vulnerabilityModel;
48+
} catch (Exception e) {
49+
LOGGER.warning("[VulnerabilityDao] Error while trying to fetch entity with (id)=" + key);
50+
return null;
51+
}
52+
4453
}
4554

4655
@Override
4756
VulnerabilityModel update(Integer key, VulnerabilityModel newEntity) {
48-
// TODO Auto-generated method stub
57+
tx.begin();
58+
VulnerabilityModel entity = em.find(VulnerabilityModel.class, key);
59+
if (entity == null) {
60+
LOGGER.warning(String.format("[VulnerabilityDao] Error while updating entity. Entity with id=(%d) could not be found.", key));
61+
return null;
62+
} else {
63+
// update (actually not even necessary for this project :P
64+
}
65+
4966
throw new UnsupportedOperationException("Unimplemented method 'update'");
5067
}
5168

5269
@Override
5370
boolean delete(Integer key) {
5471
// TODO Auto-generated method stub
72+
// Not necessary for this project :P
5573
throw new UnsupportedOperationException("Unimplemented method 'delete'");
5674
}
5775

5876
public List<VulnerabilityModel> readAll() {
5977
String query = "SELECT * FROM vulnerabilities;";
6078

6179
List<Object[]> results = em.createNativeQuery(query).getResultList();
62-
List<VulnerabilityModel> vulnerabilityModels = new ArrayList<VulnerabilityModel>();
80+
List<VulnerabilityModel> vulnerabilityModels = new ArrayList<>();
6381

6482
for (Object[] row : results) {
65-
for (int i = 0; i < row.length; i++) {
66-
System.out.println(row[i]);
67-
}
6883
VulnerabilityModel vulnerabilityModel = new VulnerabilityModel(
6984
((Number) row[0]).intValue(),
7085
(String) row[1],

src/main/java/com/sage/dao/WeaknessDao.java

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,47 @@
22

33
import com.sage.model.weakness.WeaknessModel;
44

5+
import java.util.ArrayList;
6+
import java.util.List;
7+
58
public class WeaknessDao extends Dao<WeaknessModel, Integer> {
69

7-
public WeaknessDao() {
10+
private static WeaknessDao instance;
11+
12+
private WeaknessDao() {
813
super();
914
}
1015

16+
public static WeaknessDao getInstance() {
17+
if (instance == null)
18+
instance = new WeaknessDao();
19+
return instance;
20+
}
21+
1122
@Override
1223
public boolean create(WeaknessModel entity) {
1324
try {
1425
tx.begin();
1526
em.persist(entity);
1627
tx.commit();
17-
1828
return true;
1929
} catch (Exception e) {
2030
if (tx.isActive())
2131
tx.rollback();
22-
32+
2333
LOGGER.severe(
2434
"[WeaknessDao] Error while trying to persist entity: " + entity.toString() + "\nRolling back: " + e.getMessage());
25-
e.printStackTrace();
2635
return false;
2736
}
2837
}
2938

3039
@Override
3140
public WeaknessModel read(Integer key) {
3241
try {
33-
return em.find(WeaknessModel.class, key);
42+
tx.begin();
43+
WeaknessModel weaknessModel = em.find(WeaknessModel.class, key);
44+
tx.commit();
45+
return weaknessModel;
3446
} catch (Exception e) {
3547
LOGGER.severe("[WeaknessDao] Error while trying to fetch entity with (id)=" + key);
3648
return null;
@@ -49,4 +61,44 @@ boolean delete(Integer key) {
4961
throw new UnsupportedOperationException("Unimplemented method 'delete'");
5062
}
5163

64+
public List<WeaknessModel> readAll() {
65+
String query = "SELECT * FROM weaknesses;";
66+
67+
List<Object[]> results = em.createQuery(query).getResultList();
68+
List<WeaknessModel> weaknessModels = new ArrayList<>();
69+
70+
for (Object[] row : results) {
71+
WeaknessModel weaknessModel = new WeaknessModel(
72+
((Number) row[0]).intValue(),
73+
((Number) row[1]).intValue(),
74+
(String) row[2],
75+
(String) row[3],
76+
(String) row[4],
77+
(String) row[5]
78+
);
79+
weaknessModels.add(weaknessModel);
80+
}
81+
82+
return weaknessModels;
83+
}
84+
85+
public List<WeaknessModel> fetchAllByVulnerabilityId(Integer vulnerabilityId) {
86+
String query = "SELECT * FROM weaknesses WHERE vulnerability_id = " + vulnerabilityId + ";";
87+
88+
List<Object[]> results = em.createQuery(query).getResultList();
89+
List<WeaknessModel> weaknessModels = new ArrayList<>();
90+
91+
for (Object[] row : results) {
92+
WeaknessModel weaknessModel = new WeaknessModel(
93+
((Number) row[0]).intValue(),
94+
((Number) row[1]).intValue(),
95+
(String) row[2],
96+
(String) row[3],
97+
(String) row[4],
98+
(String) row[5]
99+
);
100+
weaknessModels.add(weaknessModel);
101+
}
102+
return weaknessModels;
103+
}
52104
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.sage.dto;
2+
3+
import com.sage.model.vulnerability.VulnerabilityModel;
4+
import com.sage.model.weakness.WeaknessModel;
5+
6+
import java.util.List;
7+
8+
public class VulnerabilityDto extends VulnerabilityModel {
9+
private List<WeaknessModel> weaknesses;
10+
11+
public VulnerabilityDto(Integer id, String category, String name, String description, String cve, String severity, String locationFile, String locationLineStart, List<WeaknessModel> weaknesses) {
12+
super(id, category, name, description, cve, severity, locationFile, locationLineStart);
13+
this.weaknesses = weaknesses;
14+
}
15+
16+
public VulnerabilityDto(VulnerabilityModel vulnerability, List<WeaknessModel> weaknesses) {
17+
super(
18+
vulnerability.getId(),
19+
vulnerability.getCategory(),
20+
vulnerability.getName(),
21+
vulnerability.getDescription(),
22+
vulnerability.getCve(),
23+
vulnerability.getSeverity(),
24+
vulnerability.getLocationFile(),
25+
vulnerability.getLocationLineStart()
26+
);
27+
this.weaknesses = weaknesses;
28+
}
29+
30+
public List<WeaknessModel> getWeaknesses() {
31+
return this.weaknesses;
32+
}
33+
34+
public VulnerabilityModel toModel() {
35+
return new VulnerabilityModel(
36+
this.getId(),
37+
this.getCategory(),
38+
this.getName(),
39+
this.getDescription(),
40+
this.getCve(),
41+
this.getSeverity(),
42+
this.getLocationFile(),
43+
this.getLocationLineStart()
44+
);
45+
}
46+
}

src/main/java/com/sage/model/vulnerability/VulnerabilityModel.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package com.sage.model.vulnerability;
22

33
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.sage.dto.VulnerabilityDto;
5+
import com.sage.model.weakness.WeaknessModel;
46
import jakarta.persistence.*;
57

8+
import java.util.List;
9+
610
/**
711
* This class represents the database table of a vulnerability and should be used for any CRUD-relating operations.
812
*/
@@ -113,6 +117,10 @@ public String asSQLValues() {
113117
);
114118
}
115119

120+
public VulnerabilityDto toDto(List<WeaknessModel> weaknesses) {
121+
return new VulnerabilityDto(this, weaknesses);
122+
}
123+
116124
private String escape(String s) {
117125
return s == null ? "" : s.replace("'", "''"); // escape single quotes
118126
}
@@ -131,4 +139,18 @@ public boolean equals(Object o) {
131139
VulnerabilityModel vulnerabilityModel = (VulnerabilityModel) o;
132140
return vulnerabilityModel.getId() == this.id;
133141
}
142+
143+
@Override
144+
public int hashCode() {
145+
int result = id.hashCode();
146+
result = 31 * result + category.hashCode();
147+
result = 31 * result + name.hashCode();
148+
result = 31 * result + description.hashCode();
149+
result = 31 * result + cve.hashCode();
150+
result = 31 * result + severity.hashCode();
151+
result = 31 * result + locationFile.hashCode();
152+
result = 31 * result + locationLineStart.hashCode();
153+
result = 31 * result + locationLineEnd.hashCode();
154+
return result;
155+
}
134156
}

src/main/java/com/sage/model/weakness/WeaknessModel.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.sage.model.weakness;
22

3+
import com.fasterxml.jackson.databind.JsonNode;
34
import com.sage.utility.JsonParser;
45

56
import jakarta.persistence.Column;
@@ -26,6 +27,11 @@ public class WeaknessModel {
2627

2728
public WeaknessModel() {}
2829

30+
public WeaknessModel(Integer id, Integer vulnerabilityId) {
31+
this.id = id;
32+
this.vulnerabilityId = vulnerabilityId;
33+
}
34+
2935
public WeaknessModel(Integer id, Integer vulnerabilityId, String type, String name, String value, String url) {
3036
this. id = id;
3137
this.vulnerabilityId = vulnerabilityId;
@@ -46,6 +52,15 @@ public WeaknessDto toWeaknessDto() {
4652
return new WeaknessDto(type, name, value, url);
4753
}
4854

55+
public static WeaknessModel fromJsonNode(Integer id, Integer vulnerabilityId, JsonNode jsonNode) {
56+
WeaknessModel model = new WeaknessModel(id, vulnerabilityId);
57+
model.type = jsonNode.get("type").asText();
58+
model.name = jsonNode.get("name").asText();
59+
model.value = jsonNode.get("value").asText();
60+
model.url = jsonNode.get("url").asText();
61+
return model;
62+
}
63+
4964
@Override
5065
public String toString() {
5166
return JsonParser.asJsonString(this);

0 commit comments

Comments
 (0)