Skip to content

Commit 2b5e94f

Browse files
Preparing to release 3.0.0
1 parent d8bd866 commit 2b5e94f

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* This file is part of Dependency-Track.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* Copyright (c) Steve Springett. All Rights Reserved.
17+
*/
18+
package org.owasp.dependencytrack.integration;
19+
20+
import com.mashape.unirest.http.HttpResponse;
21+
import com.mashape.unirest.http.JsonNode;
22+
import com.mashape.unirest.http.Unirest;
23+
import com.mashape.unirest.http.exceptions.UnirestException;
24+
import org.apache.commons.io.FileUtils;
25+
import org.datanucleus.util.Base64;
26+
import org.json.JSONObject;
27+
import org.owasp.dependencytrack.util.HttpClientFactory;
28+
import java.io.File;
29+
import java.io.IOException;
30+
import java.util.UUID;
31+
32+
public class ApiClient {
33+
34+
static {
35+
Unirest.setHttpClient(HttpClientFactory.createClient());
36+
}
37+
38+
private String baseUrl;
39+
private String apiKey;
40+
41+
public ApiClient(String baseUrl, String apiKey) {
42+
this.baseUrl = baseUrl;
43+
this.apiKey = apiKey;
44+
}
45+
46+
public UUID createProject(String name, String version) throws UnirestException {
47+
final HttpResponse<JsonNode> response = Unirest.put(baseUrl + "/api/v1/project")
48+
.header("Content-Type", "application/json")
49+
.header("X-API-Key", apiKey)
50+
.body(new JSONObject()
51+
.put("name", name)
52+
.put("version", version)
53+
)
54+
.asJson();
55+
if (response.getStatus() == 201) {
56+
return UUID.fromString(response.getBody().getObject().getString("uuid"));
57+
}
58+
System.out.println("Error creating project " + name + " status: " + response.getStatus());
59+
return null;
60+
}
61+
62+
public boolean uploadBom(UUID uuid, File bom) throws IOException, UnirestException {
63+
final HttpResponse<JsonNode> response = Unirest.put(baseUrl + "/api/v1/bom")
64+
.header("Content-Type", "application/json")
65+
.header("X-API-Key", apiKey)
66+
.body(new JSONObject()
67+
.put("project", uuid.toString())
68+
.put("bom", Base64.encode(FileUtils.readFileToByteArray(bom)))
69+
)
70+
.asJson();
71+
return (response.getStatus() == 200);
72+
}
73+
74+
public boolean uploadScan(UUID uuid, File scan) throws IOException, UnirestException {
75+
final HttpResponse<JsonNode> response = Unirest.put(baseUrl + "/api/v1/scan")
76+
.header("Content-Type", "application/json")
77+
.header("X-API-Key", apiKey)
78+
.body(new JSONObject()
79+
.put("project", uuid.toString())
80+
.put("scan", Base64.encode(FileUtils.readFileToByteArray(scan)))
81+
)
82+
.asJson();
83+
return (response.getStatus() == 200);
84+
}
85+
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* This file is part of Dependency-Track.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* Copyright (c) Steve Springett. All Rights Reserved.
17+
*/
18+
package org.owasp.dependencytrack.integration;
19+
20+
import org.junit.Test;
21+
import java.io.File;
22+
import java.util.UUID;
23+
24+
public class PopulateData {
25+
26+
private static final String BASE_URL = "http://localhost:8080";
27+
private static final String API_KEY = "hETzpWanQkXV6KsJsfPuFoNBRZdiiDyY";
28+
29+
@Test
30+
public void doit() throws Exception {
31+
ApiClient api = new ApiClient(BASE_URL, API_KEY);
32+
UUID uuid = api.createProject("SonarQube", "5.6");
33+
34+
File file = new File(this.getClass().getResource("/integration/sonarqube-6.5.spdx").getFile());
35+
36+
if (file.exists()) {
37+
System.out.println("Found It");
38+
api.uploadBom(uuid, file);
39+
}
40+
41+
}
42+
43+
44+
public static void main(String[] args) throws Exception {
45+
final PopulateData populateData = new PopulateData();
46+
populateData.doit();
47+
}
48+
}

0 commit comments

Comments
 (0)