VirusTotal: : use HttpStatusException

pull/214/head
M66B 2 years ago
parent ea9c2cd2e5
commit 64b05a6641

@ -30,10 +30,10 @@ import androidx.annotation.NonNull;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.jsoup.HttpStatusException;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
@ -62,7 +62,8 @@ public class VirusTotal {
static Bundle lookup(Context context, File file, String apiKey) throws NoSuchAlgorithmException, IOException, JSONException { static Bundle lookup(Context context, File file, String apiKey) throws NoSuchAlgorithmException, IOException, JSONException {
Bundle result = new Bundle(); Bundle result = new Bundle();
Pair<Integer, String> response = call(context, "api/v3/files/" + getHash(file), apiKey); String u = "api/v3/files/";
Pair<Integer, String> response = call(context, u + getHash(file), apiKey);
if (response.first == HttpsURLConnection.HTTP_OK) { if (response.first == HttpsURLConnection.HTTP_OK) {
// https://developers.virustotal.com/reference/files // https://developers.virustotal.com/reference/files
// Example: https://gist.github.com/M66B/4ea95fdb93fb10bf4047761fcc9ec21a // Example: https://gist.github.com/M66B/4ea95fdb93fb10bf4047761fcc9ec21a
@ -88,16 +89,17 @@ public class VirusTotal {
result.putString("label", label); result.putString("label", label);
} }
} else if (response.first != HttpsURLConnection.HTTP_NOT_FOUND) } else if (response.first != HttpsURLConnection.HTTP_NOT_FOUND)
throw new FileNotFoundException(response.second); throw new HttpStatusException(response.second, response.first, u);
return result; return result;
} }
static String upload(Context context, File file, String apiKey) throws IOException, JSONException { static String upload(Context context, File file, String apiKey) throws IOException, JSONException {
// Get upload URL // Get upload URL
Pair<Integer, String> response = call(context, "api/v3/files/upload_url", apiKey); String u = "api/v3/files/upload_url";
Pair<Integer, String> response = call(context, u, apiKey);
if (response.first != HttpsURLConnection.HTTP_OK) if (response.first != HttpsURLConnection.HTTP_OK)
throw new FileNotFoundException(response.second); throw new HttpStatusException(response.second, response.first, u);
JSONObject jurl = new JSONObject(response.second); JSONObject jurl = new JSONObject(response.second);
String upload_url = jurl.getString("data"); String upload_url = jurl.getString("data");
@ -155,7 +157,7 @@ public class VirusTotal {
Log.w(ex); Log.w(ex);
} }
Log.w("VT " + error); Log.w("VT " + error);
throw new FileNotFoundException(error); throw new HttpStatusException(error, status, url.toString());
} }
String r = Helper.readStream(connection.getInputStream()); String r = Helper.readStream(connection.getInputStream());
@ -172,9 +174,10 @@ public class VirusTotal {
static void waitForAnalysis(Context context, String id, String apiKey) throws IOException, JSONException, InterruptedException, TimeoutException { static void waitForAnalysis(Context context, String id, String apiKey) throws IOException, JSONException, InterruptedException, TimeoutException {
// Get analysis result // Get analysis result
for (int i = 0; i < VT_ANALYSIS_CHECKS; i++) { for (int i = 0; i < VT_ANALYSIS_CHECKS; i++) {
Pair<Integer, String> analyses = call(context, "api/v3/analyses/" + id, apiKey); String u = "api/v3/analyses/";
Pair<Integer, String> analyses = call(context, u + id, apiKey);
if (analyses.first != HttpsURLConnection.HTTP_OK) if (analyses.first != HttpsURLConnection.HTTP_OK)
throw new FileNotFoundException(analyses.second); throw new HttpStatusException(analyses.second, analyses.first, u);
JSONObject janalysis = new JSONObject(analyses.second); JSONObject janalysis = new JSONObject(analyses.second);
JSONObject jdata = janalysis.getJSONObject("data"); JSONObject jdata = janalysis.getJSONObject("data");

@ -38,6 +38,7 @@ import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -190,7 +191,7 @@ public class FragmentDialogVirusTotal extends FragmentDialogBase {
@Override @Override
protected void onException(Bundle args, Throwable ex) { protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex); Log.unexpectedError(getParentFragmentManager(), ex, !(ex instanceof IOException));
} }
}; };

Loading…
Cancel
Save