Skip to content

Commit

Permalink
Fix lab 12 snippet (#190)
Browse files Browse the repository at this point in the history
* fix repetition of enum description

* fix lab 12 snippet

---------

Co-authored-by: i590615 <[email protected]>
  • Loading branch information
panayot-marinov and panayotmarinov authored Jan 11, 2025
1 parent 24dc4f6 commit a77b794
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions 12-http-rest/snippets/VirusScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,53 @@
public class VirusScanner {

// signup here to get your API key: https://developers.virustotal.com/v3.0/reference#getting-started
private static final String VIRUS_TOTAL_API_KEY_HEADER = "x-apikey";
private static final String VIRUS_TOTAL_API_KEY = "YOUR_API_KEY_HERE";

public static void main(String[] args) throws Exception {

var client = HttpClient.newBuilder().build();

Path localFile = Paths.get("7z.exe");
if (!Files.exists(localFile)) {
System.err.println("File does not exist: " + localFile.toAbsolutePath());
return;
}

Map<Object, Object> data = new LinkedHashMap<>();
data.put("apikey", VIRUS_TOTAL_API_KEY);
data.put("file", localFile);
String boundary = new BigInteger(256, new Random()).toString();

var request = HttpRequest.newBuilder()
.header("Content-Type", "multipart/form-data;boundary=" + boundary)
.POST(ofMimeMultipartData(data, boundary))
.uri(URI.create("https://www.virustotal.com/vtapi/v2/file/scan"))
.uri(URI.create("https://www.virustotal.com/api/v3/files"))
.header(VIRUS_TOTAL_API_KEY_HEADER, VIRUS_TOTAL_API_KEY)
.build();

HttpResponse<String> vtResponse = client.send(request, HttpResponse.BodyHandlers.ofString());
HttpResponse<String> uploadResponse = client.send(request, HttpResponse.BodyHandlers.ofString());

System.out.println(vtResponse.body());
System.out.println(uploadResponse.body());

Gson gson = new Gson();
Map<String, String> map = gson.fromJson(vtResponse.body(), Map.class);
String resource = map.get("resource");
String link = map.get("permalink");

System.out.println(link);

URI uri = new URI("https", "www.virustotal.com", "/vtapi/v2/file/report",
"apikey=" + VIRUS_TOTAL_API_KEY + "&resource=" + resource, null);

HttpResponse<String> status = client.send(HttpRequest.newBuilder(uri).build(),
Map<?, ?> uploadResponseMap = gson.fromJson(uploadResponse.body(), Map.class);
// The JSON for a successful file scan v3 response looks like:
// {
// "data": {
// "type": "analysis",
// "id": "<analysis_id>"
// }
// }

Map<?, ?> dataMap = (Map<?, ?>) uploadResponseMap.get("data");
String analysisId = (String) dataMap.get("id");

URI uri = new URI("https", "www.virustotal.com", "/api/v3/analyses/" + analysisId,
null, null);

HttpResponse<String> status = client.send(HttpRequest.newBuilder(uri)
.header(VIRUS_TOTAL_API_KEY_HEADER, VIRUS_TOTAL_API_KEY)
.build(),
HttpResponse.BodyHandlers.ofString());

System.out.println(status.body());
Expand All @@ -68,7 +81,7 @@ public static HttpRequest.BodyPublisher ofMimeMultipartData(Map<Object, Object>
if (entry.getValue() instanceof Path) {
var path = (Path) entry.getValue();
String mimeType = Files.probeContentType(path);
byteArrays.add(("\"" + entry.getKey() + "\"; file=\"" + path.getFileName()
byteArrays.add(("\"" + entry.getKey() + "\"; filename=\"" + path.getFileName()
+ "\"" + System.lineSeparator() + "Content-Type: " + mimeType + System.lineSeparator() +
System.lineSeparator()).getBytes(StandardCharsets.UTF_8));
byteArrays.add(Files.readAllBytes(path));
Expand Down

0 comments on commit a77b794

Please sign in to comment.