|
| 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 | +} |
0 commit comments