in api-reference-examples/java/te-tag-query/com/facebook/threatexchange/Net.java [234:355]
public static List<ThreatDescriptor> getInfoForIDs(
List<String> ids,
boolean verbose,
boolean showURLs,
boolean includeIndicatorInOutput
) {
// Check well-formattedness of descriptor IDs (which may have come from
// arbitrary data on stdin).
for (String id : ids) {
try {
Long.valueOf(id);
} catch (NumberFormatException e) {
System.err.printf("Malformed descriptor ID \"%s\"\n", id);
System.exit(1);
}
}
// See also
// https://developers.facebook.com/docs/threat-exchange/reference/apis/threat-descriptor/v6.0
// for available fields
String url = TE_BASE_URL
+ "/?access_token=" + APP_TOKEN
+ "&ids=%5B" + String.join(",", ids) + "%5D"
+ "&fields=raw_indicator,type,added_on,last_updated,first_active,last_active,expired_on,confidence,owner,privacy_type,review_status,status,severity,share_level,tags,description";
if (showURLs) {
System.out.println("URL:");
System.out.println(url);
}
List<ThreatDescriptor> threatDescriptors = new ArrayList<ThreatDescriptor>();
try (InputStream response = new URL(url).openConnection().getInputStream()) {
// {
// "990927953l366387": {
// "raw_indicator": "87f4b261064696075fffceee39471952",
// "type": "HASH_MD5",
// "added_on": "2018-03-21T18:47:23+0000",
// "confidence": 100,
// "owner": {
// "id": "788842735455502",
// "email": "contactemail\u0040companyname.com",
// "name": "Name of App"
// },
// "review_status": "REVIEWED_AUTOMATICALLY",
// "severity": "WARNING",
// "share_level": "AMBER",
// "tags": {
// "data": [
// {
// "id": "8995447960580728",
// "text": "media_type_video"
// },
// {
// "id": "6000177b99449380",
// "text": "media_priority_test"
// }
// ]
// },
// "id": "4019972332766623"
// },
// ...
// }
JSONObject outer = (JSONObject) new JSONParser().parse(new InputStreamReader(response));
for (Iterator iterator = outer.keySet().iterator(); iterator.hasNext();) {
String key = (String) iterator.next();
JSONObject item = (JSONObject) outer.get(key);
if (verbose) {
System.out.println(item.toString());
}
JSONObject owner = (JSONObject)item.get("owner");
JSONObject td_subjective_tags = (JSONObject)item.get("tags");
List<String> tagTexts = new ArrayList<String>();
if (td_subjective_tags != null) {
JSONArray tag_data = (JSONArray)td_subjective_tags.get("data");
int n = tag_data.size();
for (int j = 0; j < n; j++) {
JSONObject tag = (JSONObject) tag_data.get(j);
String tagText = (String)tag.get("text");
tagTexts.add(tagText);
}
Collections.sort(tagTexts); // canonicalize
}
String description = (String)item.get("description");
if (description == null) {
description = "";
}
ThreatDescriptor threatDescriptor = new ThreatDescriptor(
(String)item.get("id"),
(String)item.get("raw_indicator"),
(String)item.get("type"),
(String)item.get("added_on"),
(String)item.get("last_updated"),
(String)item.get("first_active"), // may be null
(String)item.get("last_active"), // may be null
(String)item.get("expired_on"), // may be null
Long.toString((Long)item.get("confidence")),
(String)owner.get("id"),
(String)owner.get("email"),
(String)owner.get("name"),
(String)item.get("privacy_type"),
(String)item.get("review_status"),
(String)item.get("status"),
(String)item.get("severity"),
(String)item.get("share_level"),
tagTexts,
description
);
threatDescriptors.add(threatDescriptor);
}
} catch (Exception e) {
e.printStackTrace(System.err);
System.exit(1);
}
return threatDescriptors;
}