|
33 | 33 | */ |
34 | 34 | public class ImportScanService2 { |
35 | 35 |
|
36 | | - private final Config config; |
37 | | - |
38 | | - public ImportScanService2(Config config) { |
39 | | - super(); |
40 | | - this.config = config; |
41 | | - } |
42 | | - |
43 | | - /** |
44 | | - * @return The DefectDojo Authentication Header |
45 | | - */ |
46 | | - private HttpHeaders getDefectDojoAuthorizationHeaders() { |
47 | | - return new Foo(config, new ProxyConfigFactory().create()).generateAuthorizationHeaders(); |
| 36 | + private final Config config; |
| 37 | + |
| 38 | + public ImportScanService2(Config config) { |
| 39 | + super(); |
| 40 | + this.config = config; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @return The DefectDojo Authentication Header |
| 45 | + */ |
| 46 | + private HttpHeaders getDefectDojoAuthorizationHeaders() { |
| 47 | + return new Foo(config, new ProxyConfigFactory().create()).generateAuthorizationHeaders(); |
| 48 | + } |
| 49 | + |
| 50 | + protected RestTemplate setupRestTemplate() { |
| 51 | + return new Foo(config, new ProxyConfigFactory().create()).createRestTemplate(); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Before version 1.5.4. testName (in DefectDojo _test_type_) must be defectDojoScanName, afterwards, you can have somethings else |
| 56 | + */ |
| 57 | + protected ImportScanResponse createFindings(ScanFile scanFile, String endpoint, long lead, String currentDate, ScanType scanType, long testType, MultiValueMap<String, Object> options) { |
| 58 | + var restTemplate = this.setupRestTemplate(); |
| 59 | + HttpHeaders headers = getDefectDojoAuthorizationHeaders(); |
| 60 | + headers.setContentType(MediaType.MULTIPART_FORM_DATA); |
| 61 | + restTemplate.setMessageConverters(List.of( |
| 62 | + new FormHttpMessageConverter(), |
| 63 | + new ResourceHttpMessageConverter(), |
| 64 | + new MappingJackson2HttpMessageConverter()) |
| 65 | + ); |
| 66 | + |
| 67 | + MultiValueMap<String, Object> mvn = new LinkedMultiValueMap<>(); |
| 68 | + |
| 69 | + mvn.add("lead", Long.toString(lead)); |
| 70 | + mvn.add("scan_date", currentDate); |
| 71 | + mvn.add("scan_type", scanType.getTestType()); |
| 72 | + mvn.add("close_old_findings", "true"); |
| 73 | + mvn.add("skip_duplicates", "false"); |
| 74 | + mvn.add("test_type", String.valueOf(testType)); |
| 75 | + |
| 76 | + for (String theKey : options.keySet()) { |
| 77 | + mvn.remove(theKey); |
48 | 78 | } |
| 79 | + mvn.addAll(options); |
49 | 80 |
|
50 | | - protected RestTemplate setupRestTemplate() { |
51 | | - return new Foo(config, new ProxyConfigFactory().create()).createRestTemplate(); |
52 | | - } |
53 | | - |
54 | | - /** |
55 | | - * Before version 1.5.4. testName (in DefectDojo _test_type_) must be defectDojoScanName, afterwards, you can have somethings else |
56 | | - */ |
57 | | - protected ImportScanResponse createFindings(ScanFile scanFile, String endpoint, long lead, String currentDate, ScanType scanType, long testType, MultiValueMap<String, Object> options) { |
58 | | - var restTemplate = this.setupRestTemplate(); |
59 | | - HttpHeaders headers = getDefectDojoAuthorizationHeaders(); |
60 | | - headers.setContentType(MediaType.MULTIPART_FORM_DATA); |
61 | | - restTemplate.setMessageConverters(List.of( |
62 | | - new FormHttpMessageConverter(), |
63 | | - new ResourceHttpMessageConverter(), |
64 | | - new MappingJackson2HttpMessageConverter()) |
65 | | - ); |
66 | | - |
67 | | - MultiValueMap<String, Object> mvn = new LinkedMultiValueMap<>(); |
68 | | - |
69 | | - mvn.add("lead", Long.toString(lead)); |
70 | | - mvn.add("scan_date", currentDate); |
71 | | - mvn.add("scan_type", scanType.getTestType()); |
72 | | - mvn.add("close_old_findings", "true"); |
73 | | - mvn.add("skip_duplicates", "false"); |
74 | | - mvn.add("test_type", String.valueOf(testType)); |
75 | | - |
76 | | - for (String theKey : options.keySet()) { |
77 | | - mvn.remove(theKey); |
| 81 | + try { |
| 82 | + ByteArrayResource contentsAsResource = new ByteArrayResource(scanFile.getContent().getBytes(StandardCharsets.UTF_8)) { |
| 83 | + @Override |
| 84 | + public String getFilename() { |
| 85 | + return scanFile.getName(); |
78 | 86 | } |
79 | | - mvn.addAll(options); |
80 | | - |
81 | | - try { |
82 | | - ByteArrayResource contentsAsResource = new ByteArrayResource(scanFile.getContent().getBytes(StandardCharsets.UTF_8)) { |
83 | | - @Override |
84 | | - public String getFilename() { |
85 | | - return scanFile.getName(); |
86 | | - } |
87 | | - }; |
| 87 | + }; |
88 | 88 |
|
89 | | - mvn.add("file", contentsAsResource); |
| 89 | + mvn.add("file", contentsAsResource); |
90 | 90 |
|
91 | | - var payload = new HttpEntity<>(mvn, headers); |
| 91 | + var payload = new HttpEntity<>(mvn, headers); |
92 | 92 |
|
93 | | - return restTemplate.exchange(config.getUrl() + "/api/v2/" + endpoint + "/", HttpMethod.POST, payload, ImportScanResponse.class).getBody(); |
94 | | - } catch (HttpClientErrorException e) { |
95 | | - throw new PersistenceException("Failed to attach findings to engagement."); |
96 | | - } |
| 93 | + return restTemplate.exchange(config.getUrl() + "/api/v2/" + endpoint + "/", HttpMethod.POST, payload, ImportScanResponse.class).getBody(); |
| 94 | + } catch (HttpClientErrorException e) { |
| 95 | + throw new PersistenceException("Failed to attach findings to engagement."); |
97 | 96 | } |
| 97 | + } |
98 | 98 |
|
99 | 99 |
|
100 | | - public ImportScanResponse importScan(ScanFile scanFile, long engagementId, long lead, String currentDate, ScanType scanType, long testType) { |
101 | | - var additionalValues = new LinkedMultiValueMap<String, Object>(); |
102 | | - additionalValues.add("engagement", Long.toString(engagementId)); |
| 100 | + public ImportScanResponse importScan(ScanFile scanFile, long engagementId, long lead, String currentDate, ScanType scanType, long testType) { |
| 101 | + var additionalValues = new LinkedMultiValueMap<String, Object>(); |
| 102 | + additionalValues.add("engagement", Long.toString(engagementId)); |
103 | 103 |
|
104 | | - return this.importScan(scanFile, engagementId, lead, currentDate, scanType, testType, additionalValues); |
105 | | - } |
| 104 | + return this.importScan(scanFile, engagementId, lead, currentDate, scanType, testType, additionalValues); |
| 105 | + } |
106 | 106 |
|
107 | | - public ImportScanResponse reimportScan(ScanFile scanFile, long testId, long lead, String currentDate, ScanType scanType, long testType) { |
108 | | - var additionalValues = new LinkedMultiValueMap<String, Object>(); |
109 | | - additionalValues.add("test", Long.toString(testId)); |
| 107 | + public ImportScanResponse reimportScan(ScanFile scanFile, long testId, long lead, String currentDate, ScanType scanType, long testType) { |
| 108 | + var additionalValues = new LinkedMultiValueMap<String, Object>(); |
| 109 | + additionalValues.add("test", Long.toString(testId)); |
110 | 110 |
|
111 | | - return this.reimportScan(scanFile, testId, lead, currentDate, scanType, testType, additionalValues); |
112 | | - } |
| 111 | + return this.reimportScan(scanFile, testId, lead, currentDate, scanType, testType, additionalValues); |
| 112 | + } |
113 | 113 |
|
114 | | - //overloading with optional parameter |
115 | | - public ImportScanResponse importScan(ScanFile scanFile, long engagementId, long lead, String currentDate, ScanType scanType, long testType, LinkedMultiValueMap<String, Object> additionalValues) { |
116 | | - additionalValues.add("engagement", Long.toString(engagementId)); |
| 114 | + //overloading with optional parameter |
| 115 | + public ImportScanResponse importScan(ScanFile scanFile, long engagementId, long lead, String currentDate, ScanType scanType, long testType, LinkedMultiValueMap<String, Object> additionalValues) { |
| 116 | + additionalValues.add("engagement", Long.toString(engagementId)); |
117 | 117 |
|
118 | | - return this.createFindings(scanFile, "import-scan", lead, currentDate, scanType, testType, additionalValues); |
119 | | - } |
| 118 | + return this.createFindings(scanFile, "import-scan", lead, currentDate, scanType, testType, additionalValues); |
| 119 | + } |
120 | 120 |
|
121 | | - public ImportScanResponse reimportScan(ScanFile scanFile, long testId, long lead, String currentDate, ScanType scanType, long testType, LinkedMultiValueMap<String, Object> additionalValues) { |
122 | | - additionalValues.add("test", Long.toString(testId)); |
| 121 | + public ImportScanResponse reimportScan(ScanFile scanFile, long testId, long lead, String currentDate, ScanType scanType, long testType, LinkedMultiValueMap<String, Object> additionalValues) { |
| 122 | + additionalValues.add("test", Long.toString(testId)); |
123 | 123 |
|
124 | | - return this.createFindings(scanFile, "reimport-scan", lead, currentDate, scanType, testType, additionalValues); |
125 | | - } |
| 124 | + return this.createFindings(scanFile, "reimport-scan", lead, currentDate, scanType, testType, additionalValues); |
| 125 | + } |
126 | 126 |
|
127 | | - @Data |
128 | | - public static class ImportScanResponse { |
129 | | - @JsonProperty |
130 | | - protected Boolean verified; |
| 127 | + @Data |
| 128 | + public static class ImportScanResponse { |
| 129 | + @JsonProperty |
| 130 | + protected Boolean verified; |
131 | 131 |
|
132 | | - @JsonProperty |
133 | | - protected Boolean active; |
| 132 | + @JsonProperty |
| 133 | + protected Boolean active; |
134 | 134 |
|
135 | | - @JsonProperty("test") |
136 | | - protected long testId; |
137 | | - } |
| 135 | + @JsonProperty("test") |
| 136 | + protected long testId; |
| 137 | + } |
138 | 138 | } |
0 commit comments