下面列出了org.json.simple.JSONArray#isEmpty ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public List<DockerContainer> getContainers() {
try {
JSONArray value = (JSONArray) doGetRequest("/containers/json?all=1",
Collections.singleton(HttpURLConnection.HTTP_OK));
List<DockerContainer> ret = new ArrayList<>(value.size());
for (Object o : value) {
JSONObject json = (JSONObject) o;
String id = (String) json.get("Id");
String image = (String) json.get("Image");
String name = null;
JSONArray names = (JSONArray) json.get("Names");
if (names != null && !names.isEmpty()) {
name = (String) names.get(0);
}
DockerContainer.Status status = DockerUtils.getContainerStatus((String) json.get("Status"));
ret.add(new DockerContainer(instance, id, image, name, status));
}
return ret;
} catch (DockerException ex) {
LOGGER.log(Level.INFO, null, ex);
}
return Collections.emptyList();
}
/**
* Convert dynamoDB JSONObject list to java ArrayList<Object>.
* @param item - map in JSONObject format to convert
* @return
*/
public static ArrayList<Object> jsonToList(JSONArray item){
ArrayList<Object> list = new ArrayList<Object>();
//populate list
if (!item.isEmpty()){
for (Object o : item){
list.add(typeConversion((JSONObject) o));
}
//item.forEach(p -> list.add(typeConversion((JSONObject) p)));
}
return list;
}
@Override
public String userExists(String identifier, String idType) throws RuntimeException{
String key = "";
if (idType.equals(ID.Type.uid)){
key = ACCOUNT.GUUID;
}else if (idType.equals(ID.Type.email)){
key = ACCOUNT.EMAIL;
}else if (idType.equals(ID.Type.phone)){
key = ACCOUNT.PHONE;
}else{
errorCode = 4;
throw new RuntimeException("userExists(...) reports 'unsupported ID type' " + idType);
}
//all primary user IDs need to be lowerCase in DB!!!
identifier = ID.clean(identifier);
//search parameters:
JSONObject response = searchUserIndex(key, identifier);
//System.out.println("RESPONSE: " + response.toJSONString()); //debug
if (!Connectors.httpSuccess(response)){
errorCode = 4;
throw new RuntimeException("Authentication.userExists(...) reports 'DB query failed! Result unclear!'");
}
JSONArray hits = JSON.getJArray(response, new String[]{"hits", "hits"});
if (hits != null && !hits.isEmpty()){
return JSON.getJObject((JSONObject) hits.get(0), "_source").get(ACCOUNT.GUUID).toString();
}else{
errorCode = 4;
return "";
}
}
public static String userExists(String identifier, String idType) throws RuntimeException{
String key = "";
if (idType.equals(ID.Type.uid)){
key = ACCOUNT.GUUID;
}else if (idType.equals(ID.Type.email)){
key = ACCOUNT.EMAIL;
}else if (idType.equals(ID.Type.phone)){
key = ACCOUNT.PHONE;
}else{
throw new RuntimeException("userExists(...) reports 'unsupported ID type' " + idType);
}
//all primary user IDs need to be lowerCase in DB!!!
identifier = ID.clean(identifier);
//search parameters:
JSONObject response = db.searchSimple(DB.USERS + "/all", key + ":" + identifier);
//System.out.println("RESPONSE: " + response.toJSONString()); //debug
if (!Connectors.httpSuccess(response)){
throw new RuntimeException("Authentication.userExists(...) reports 'DB query failed! Result unclear!'");
}
JSONArray hits = JSON.getJArray(response, new String[]{"hits", "hits"});
if (hits != null && !hits.isEmpty()){
return JSON.getJObject((JSONObject) hits.get(0), "_source").get(ACCOUNT.GUUID).toString();
}else{
return "";
}
}
private void addMethods(final ModelElementFactory factory, final JsObject toObject, final DeclarationScope scope, final JSONObject fromObject) {
JSONArray methods = getJSONArrayProperty(fromObject, METHODS);
if (methods != null) {
for (Object methodO : methods) {
if (methodO instanceof JSONObject) {
JSONObject method = (JSONObject) methodO;
String methodName = getJSONStringProperty(method, NAME);
JSONArray signatures = getJSONArrayProperty(method, "signatures");
String doc = getJSONStringProperty(method, DESCRIPTION);
if (methodName != null && signatures != null) {
for (Object signature : signatures) {
JSONArray params = getJSONArrayProperty((JSONObject) signature, PARAMS);
List<String> paramNames = new ArrayList<String>();
if (params != null && !params.isEmpty()) {
for (Object param : params) {
String paramName = getJSONStringProperty((JSONObject) param, NAME);
if (paramName != null) {
paramNames.add(paramName);
}
}
}
JsObject object = factory.newFunction(scope, toObject, methodName, paramNames, NodeJsUtils.NODEJS_NAME);
object.setDocumentation(Documentation.create(doc, getDocumentationURL(methodName, paramNames)));
toObject.addProperty(object.getName(), object);
addProperties(factory, object, (DeclarationScope) object, method);
addMethods(factory, object, (DeclarationScope) object, method);
}
}
}
}
}
}
/**
* Populates the resourceName-resource map of a given orgName with the package information from the CKAN response.
* @param resources JSON vector from the CKAN response containing resource information
* @param orgName Organization name
* @param pkgName Package name
* @param checkExistence If true, checks if the queried resource already exists in the cache
*/
private void populateResourcesMap(JSONArray resources, String orgName, String pkgName, boolean checkExistence) {
// this check is for debuging purposes
if (resources == null || resources.isEmpty()) {
LOGGER.debug("The resources list is empty, nothing to cache");
return;
} // if
LOGGER.debug("Resources to be populated: " + resources.toJSONString() + "(orgName=" + orgName
+ ", pkgName=" + pkgName + ")");
// iterate on the resources
Iterator<JSONObject> iterator = resources.iterator();
while (iterator.hasNext()) {
// get the resource name and id (resources cannot be in deleted state)
JSONObject factObj = (JSONObject) iterator.next();
String resourceName = (String) factObj.get("name");
String resourceId = (String) factObj.get("id");
// put the resource in the tree and in the resource map
if (checkExistence) {
if (tree.get(orgName).get(pkgName).contains(resourceName)) {
continue;
} // if
} // if
tree.get(orgName).get(pkgName).add(resourceName);
this.setResId(orgName, pkgName, resourceName, resourceId);
LOGGER.debug("Resource found in CKAN, now cached (orgName=" + orgName + " -> pkgName=" + pkgName
+ " -> " + "resourceName/resourceId=" + resourceName + "/" + resourceId + ")");
} // while
}
@Override
public void expirateRecordsCache(long expirationTime) throws CygnusRuntimeError, CygnusPersistenceError {
// Iterate on the cached resource IDs
cache.startResIterator();
while (cache.hasNextRes()) {
// Get the next resource ID
String resId = cache.getNextResId();
// Create the filters for a datastore deletion
String filters = "";
// Get the record pages, some variables
int offset = 0;
boolean morePages = true;
do {
// Get the records within the current page
JSONObject result = (JSONObject) getRecords(resId, null, offset, RECORDSPERPAGE).get("result");
JSONArray records = (JSONArray) result.get("records");
try {
for (Object recordObj : records) {
JSONObject record = (JSONObject) recordObj;
long id = (Long) record.get("_id");
String recvTime = (String) record.get("recvTime");
long recordTime = CommonUtils.getMilliseconds(recvTime);
long currentTime = new Date().getTime();
if (recordTime < (currentTime - (expirationTime * 1000))) {
if (filters.isEmpty()) {
filters += "{\"_id\":[" + id;
} else {
filters += "," + id;
} // if else
} else {
// Since records are sorted by _id, once the first not expirated record is found the loop
// can finish
morePages = false;
break;
} // if else
} // for
} catch (ParseException e) {
throw new CygnusRuntimeError("Data expiration error", "ParseException", e.getMessage());
} // try catch
if (records.isEmpty()) {
morePages = false;
} else {
offset += RECORDSPERPAGE;
} // if else
} while (morePages);
if (filters.isEmpty()) {
LOGGER.debug("No records to be deleted");
} else {
filters += "]}";
LOGGER.debug("Records to be deleted, resId=" + resId + ", filters=" + filters);
deleteRecords(resId, filters);
} // if else
} // while
}
@Override
public Map<String, SmartHomeDevice> getDevices(){
JSONObject response = httpGET(this.host + "/rest/items");
//System.out.println("openHAB REST response: " + response); //DEBUG
if (Connectors.httpSuccess(response)){
//use the chance to update the "names by type" buffer
this.bufferedDevicesByType = new ConcurrentHashMap<>();
JSONArray devicesArray = null;
if (response.containsKey("JSONARRAY")){
devicesArray = JSON.getJArray(response, "JSONARRAY"); //this should usually be the one triggered
}else if (response.containsKey("STRING")){
String arrayAsString = JSON.getString(response, "STRING");
if (arrayAsString.trim().startsWith("[")){
devicesArray = JSON.parseStringToArrayOrFail(arrayAsString);
}
}
if (devicesArray == null){
//ERROR
return null;
}
if (devicesArray.isEmpty()){
//Fail with empty array
Debugger.println("Service:OpenHAB - devices array was empty!", 1);
return new HashMap<String, SmartHomeDevice>();
}
//Build devices map
Map<String, SmartHomeDevice> devices = new HashMap<>();
try{
for (Object o : devicesArray){
JSONObject hubDevice = (JSONObject) o;
//System.out.println("openHAB device JSON: " + hubDevice); //DEBUG
//Build unified object for SEPIA
SmartHomeDevice shd = buildDeviceFromResponse(hubDevice);
//devices
if (shd != null){
devices.put(shd.getMetaValueAsString("id"), shd);
//fill buffer
String deviceType = shd.getType();
String deviceName = shd.getName();
if (Is.notNullOrEmpty(deviceType) && Is.notNullOrEmpty(deviceName)){
deviceName = SmartHomeDevice.getCleanedUpName(deviceName); //NOTE: use "clean" name!
if (!deviceName.isEmpty() && (boolean) shd.getMeta().get("namedBySepia")){
Set<String> deviceNamesOfType = this.bufferedDevicesByType.get(deviceType);
if (deviceNamesOfType == null){
deviceNamesOfType = new HashSet<>();
this.bufferedDevicesByType.put(deviceType, deviceNamesOfType);
}
deviceNamesOfType.add(deviceName);
}
}
}
}
//store new buffer
bufferedDevicesOfHostByType.put(this.host, this.bufferedDevicesByType);
return devices;
}catch (Exception e){
//Fail with faulty array
Debugger.println("Service:OpenHAB - devices array seems to be broken! Msg.: " + e.getMessage(), 1);
Debugger.printStackTrace(e, 3);
return new HashMap<String, SmartHomeDevice>();
}
}else{
//Fail with server contact error
Debugger.println("Service:OpenHAB - failed to get devices from server!", 1);
return null;
}
}
/**
* Populates the package map of a given orgName with the package information from the CKAN response.
* @param packages JSON vector from the CKAN response containing package information
* @param orgName Organization name
* @throws CygnusBadConfiguration
* @throws CygnusRuntimeError
* @throws CygnusPersistenceError
*/
private void populatePackagesMap(JSONArray packages, String orgName)
throws CygnusBadConfiguration, CygnusRuntimeError, CygnusPersistenceError {
// this check is for debuging purposes
if (packages == null || packages.isEmpty()) {
LOGGER.debug("The pacakges list is empty, nothing to cache");
return;
} // if
LOGGER.debug("Packages to be populated: " + packages.toJSONString() + "(orgName=" + orgName + ")");
// iterate on the packages
Iterator<JSONObject> iterator = packages.iterator();
while (iterator.hasNext()) {
// get the package name
JSONObject pkg = (JSONObject) iterator.next();
String pkgName = (String) pkg.get("name");
// check if the package is in "deleted" state
String pkgState = pkg.get("state").toString();
if (pkgState.equals("deleted")) {
throw new CygnusBadConfiguration("The package '" + pkgName + "' exists but it is in a deleted state");
} // if
// put the package in the tree and in the packages map
String pkgId = pkg.get("id").toString();
tree.get(orgName).put(pkgName, new ArrayList<String>());
this.setPkgId(orgName, pkgName, pkgId);
LOGGER.debug("Package found in CKAN, now cached (orgName=" + orgName + " -> pkgName/pkgId=" + pkgName
+ "/" + pkgId + ")");
// from CKAN 2.4, the organization_show method does not return the per-package list of resources
JSONArray resources = getResources(pkgName);
// populate the resources map
LOGGER.debug("Going to populate the resources cache (orgName=" + orgName + ", pkgName=" + pkgName
+ ")");
populateResourcesMap(resources, orgName, pkgName, false);
} // while
}
/**
* Make a connection to the BukkitDev API and request the newest file's details.
*
* @return true if successful.
*/
private boolean read() {
try {
final URLConnection conn = this.url.openConnection();
conn.setConnectTimeout(5000);
if (this.apiKey != null) {
conn.addRequestProperty("X-API-Key", this.apiKey);
}
conn.addRequestProperty("User-Agent", Updater.USER_AGENT);
conn.setDoOutput(true);
final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
final String response = reader.readLine();
final JSONArray array = (JSONArray) JSONValue.parse(response);
if (array.isEmpty()) {
this.plugin.getLogger().warning("The updater could not find any files for the project id " + this.id);
this.result = UpdateResult.FAIL_BADID;
return false;
}
JSONObject latestUpdate = (JSONObject) array.get(array.size() - 1);
this.versionName = (String) latestUpdate.get(Updater.TITLE_VALUE);
this.versionLink = (String) latestUpdate.get(Updater.LINK_VALUE);
this.versionType = (String) latestUpdate.get(Updater.TYPE_VALUE);
this.versionGameVersion = (String) latestUpdate.get(Updater.VERSION_VALUE);
return true;
} catch (final IOException e) {
if (e.getMessage().contains("HTTP response code: 403")) {
this.plugin.getLogger().severe("dev.bukkit.org rejected the API key provided in plugins/Updater/config.yml");
this.plugin.getLogger().severe("Please double-check your configuration to ensure it is correct.");
this.result = UpdateResult.FAIL_APIKEY;
} else {
this.plugin.getLogger().severe("The updater could not contact dev.bukkit.org for updating.");
this.plugin.getLogger().severe("If you have not recently modified your configuration and this is the first time you are seeing this message, the site may be experiencing temporary downtime.");
this.result = UpdateResult.FAIL_DBO;
}
this.plugin.getLogger().log(Level.SEVERE, null, e);
return false;
}
}
@Override
public ServiceResult getResult(NluResult nluResult) {
//initialize result
ServiceBuilder api = new ServiceBuilder(nluResult,
getInfoFreshOrCache(nluResult.input, this.getClass().getCanonicalName()));
//get interview parameters
JSONObject sentenceJson = nluResult.getRequiredParameter(PARAMETERS.SENTENCES).getData();
JSONArray sentencesArray = (JSONArray) sentenceJson.get(InterviewData.ARRAY);
//get background parameters
String reply = nluResult.getParameter(PARAMETERS.REPLY); //a reply
List<String> flexParameters = new ArrayList<>(); //flex parameters
for (int i=0; i<VAR_N; i++){
String var = nluResult.getParameter(VAR_BASE + (i+1));
if (Is.notNullOrEmpty(var)){
flexParameters.add(var);
}
}
Debugger.println("cmd: sentence connect: " + sentencesArray, 2); //debug
if (sentencesArray == null || sentencesArray.isEmpty()){
api.status = "fail";
}else{
//Normalizer_Interface normalizer = Config.input_normalizers.get(api.language);
int goodResults = 0;
for (Object o : sentencesArray){
String s = (String) o;
//Replace flex parameters (variables)
for (int i=0; i<flexParameters.size(); i++){
String tag = "<" + VAR_BASE + (i+1) + ">";
if (s.contains(tag)){
s = s.replace(tag, flexParameters.get(i));
}
}
//TODO: making a clean input would be better
nluResult.input.clearParameterResultStorage();
nluResult.input.inputType = "question";
nluResult.input.text = s;
nluResult.input.textRaw = s;
//norm - we don't need this, its in the interpretation chain
//nluResult.input.text = normalizer.normalize_text(s);
//interpret
NluResult thisRes = new InterpretationChain()
.setSteps(Config.nluInterpretationSteps).getResult(nluResult.input);
//System.out.println("cmd_sum: " + thisRes.cmdSummary + " - in: " + thisRes.language); //DEBUG
//push - filter no_results and chained sentence_connect commands (to prevent endless loop)
if (!thisRes.getCommand().equals(CMD.SENTENCE_CONNECT)
&& !thisRes.getCommand().equals(CMD.NO_RESULT) && !thisRes.getCommand().equals(CMD.RESULT_REDIRECT)
){
//build action
api.addAction(ACTIONS.QUEUE_CMD);
api.putActionInfo("info", "direct_cmd");
api.putActionInfo("cmd", thisRes.cmdSummary);
api.putActionInfo("lang", thisRes.language);
api.putActionInfo("newReceiver", Config.assistantId); //make sure this comes back to the assistant
//api.actionInfo_put_info("options", JSON.make(ACTIONS.SKIP_TTS, true));
goodResults++;
}
//use new result for next command? TODO: I think we need some context handling here
//nluResult = thisRes;
}
//reply - silent or custom
if (!reply.isEmpty()){
reply = AnswerTools.handleUserAnswerSets(reply);
api.setCustomAnswer(reply);
}
if (goodResults == 0){
api.status = "fail";
}else{
api.status = "success";
}
}
//finally build the API_Result
ServiceResult result = api.buildResult();
//return result.result_JSON.toJSONString();
return result;
}
private static HashMap<String, Object> graphhopper_get_coordinates(String address, String language){
//requirements
if (Is.nullOrEmpty(Config.graphhopper_key)){
Debugger.println("GeoCoding - Graphhopper API-key is missing! Please add one via the config file to use the service.", 1);
return null;
}
HashMap<String, Object> result = new HashMap<String, Object>();
try {
String add_params = "&debug=false&limit=1&key=" + Config.graphhopper_key;
String url = "https://graphhopper.com/api/1/geocode?q=" + URLEncoder.encode(address, "UTF-8") + "&locale=" + language + add_params;
//System.out.println("gh-url: " + url); //debug
long tic = System.currentTimeMillis();
JSONObject response = Connectors.httpGET(url.trim());
Statistics.addExternalApiHit("Graphhopper Geocoder");
Statistics.addExternalApiTime("Graphhopper Geocoder", tic);
JSONArray hits = (JSONArray) response.get("hits");
if (!hits.isEmpty()){
JSONObject points = (JSONObject) ((JSONObject) hits.get(0)).get("point");
double latitude = (double) points.get("lat");
double longitude = (double) points.get("lng");
String osm_value = (String) ((JSONObject) hits.get(0)).get("osm_value");
String country, state, city, street, postcode;
if (osm_value.matches("country")) country = (String) ((JSONObject) hits.get(0)).get("name");
else country = (String) ((JSONObject) hits.get(0)).get("country");
if (osm_value.matches("state")) state = (String) ((JSONObject) hits.get(0)).get("name");
else state = (String) ((JSONObject) hits.get(0)).get("state");
if (osm_value.matches("(city|town|village)")) city = (String) ((JSONObject) hits.get(0)).get("name");
else city = (String) ((JSONObject) hits.get(0)).get("city");
if (osm_value.matches("(residential|footway)")) street = (String) ((JSONObject) hits.get(0)).get("name");
else street = (String) ((JSONObject) hits.get(0)).get("street");
postcode = (String) ((JSONObject) hits.get(0)).get("postcode");
//fill result
result.put(LOCATION.LAT, latitude); result.put(LOCATION.LNG, longitude);
result.put(LOCATION.COUNTRY, country); result.put(LOCATION.STATE, state); result.put(LOCATION.POSTAL_CODE, postcode);
result.put(LOCATION.CITY, city); result.put(LOCATION.STREET, street); result.put(LOCATION.STREET_NBR, null);
}
} catch (Exception e) {
e.printStackTrace();
return null;
/*
result.put(LOCATION.LAT, null); result.put(LOCATION.LNG, null);
result.put(LOCATION.COUNTRY, null); result.put(LOCATION.STATE, null); result.put(LOCATION.POSTAL_CODE, null);
result.put(LOCATION.CITY, null); result.put(LOCATION.STREET, null); result.put(LOCATION.STREET_NBR, null);
*/
}
return result;
}
private static HashMap<String, Object> google_get_address(String latitude, String longitude, String language) {
//requirements
if (Is.nullOrEmpty(Config.google_maps_key)){
Debugger.println("GeoCoding - Google API-key is missing! Please add one via the config file to use Maps.", 1);
return null;
}
HashMap<String, Object> result = new HashMap<String, Object>();
try {
String add_key = "";
if (!Config.google_maps_key.equals("test")){
add_key = "&key=" + Config.google_maps_key;
}
String add_region = "®ion=" + language + "&language=" + language; //TODO: split region and language?
String url = "https://maps.googleapis.com/maps/api/geocode/json?latlng="
+ latitude + "," + longitude
+ add_region + add_key;
//make the HTTP GET call to Google Geocode API
long tic = System.currentTimeMillis();
JSONObject response = Connectors.httpGET(url.trim());
JSONArray results = (JSONArray) response.get("results");
Statistics.addExternalApiHit("Google Geocoder (reverse)");
Statistics.addExternalApiTime("Google Geocoder (reverse)", tic);
//TODO: handle over_query_limit with a) 1s break b) 2s break c) possible quota end
if (!results.isEmpty()){
// get the city and country from formatted_address
//String formatted_address = (String) ((JSONObject) results.get(0)).get("formatted_address"); // e.g. Faro District, Portugal
//String[] parts = formatted_address.split(",");
//city = parts[parts.length - 2];
//country = parts[parts.length - 1]; // in the last part of formatted_address is the country
JSONArray address_components = (JSONArray) ((JSONObject) results.get(0)).get("address_components");
String city = null, country = null, postcode = null, street = null, state = null, street_nbr = null;
int i = 0;
for (Object obj : address_components)
{
String currentComponent = ((JSONObject) obj).get("types").toString();
// "types" : [ "street_number" ]
if (currentComponent.contains("\"street_number\""))
{
street_nbr = (String) ((JSONObject) address_components.get(i)).get("long_name");
}
// "types" : [ "route" ]
else if (currentComponent.contains("\"route\""))
{
street = (String) ((JSONObject) address_components.get(i)).get("long_name");
}
// search for GoogleOutput: "types" : [ "locality", "political" ]
else if (currentComponent.contains("\"locality\""))
{
city = (String) ((JSONObject) address_components.get(i)).get("long_name");
}
// "types" : [ "sublocality", "political" ] -- best replacement for city ???
else if (currentComponent.contains("\"sublocality\""))
{
if (city == null)
city = (String) ((JSONObject) address_components.get(i)).get("long_name");
}
// "types" : [ "administrative_area_level_1", "political" ] -- it should be the state
else if (currentComponent.contains("\"administrative_area_level_1\""))
{
state = (String) ((JSONObject) address_components.get(i)).get("long_name");
}
// search for GoogleOutput: "types" : [ "country", "political" ]
else if (currentComponent.contains("\"country\""))
{
country = (String) ((JSONObject) address_components.get(i)).get("long_name");
}
// search for GoogleOutput: "types" : [ "postal_code" ]
else if (currentComponent.contains("\"postal_code\""))
{
postcode = (String) ((JSONObject) address_components.get(i)).get("long_name");
}
i++;
}
String formatted_address = (String) ((JSONObject) results.get(0)).get("formatted_address");
//fill result
result.put(LOCATION.NAME, formatted_address.replaceFirst(",.*?$", "").trim());
result.put(LOCATION.LAT, latitude); result.put(LOCATION.LNG, longitude);
result.put(LOCATION.COUNTRY, country); result.put(LOCATION.STATE, state); result.put(LOCATION.POSTAL_CODE, postcode);
result.put(LOCATION.CITY, city); result.put(LOCATION.STREET, street); result.put(LOCATION.STREET_NBR, street_nbr);
}
}catch (Exception e) {
e.printStackTrace();
return null;
/*
result.put(LOCATION.LAT, null); result.put(LOCATION.LNG, null);
result.put(LOCATION.COUNTRY, null); result.put(LOCATION.STATE, null); result.put(LOCATION.POSTAL_CODE, null);
result.put(LOCATION.CITY, null); result.put(LOCATION.STREET, null); result.put(LOCATION.STREET_NBR, null);
*/
}
return result;
}
private static JSONArray google_get_POI(String search, String latitude, String longitude, String language, String types) {
//requirements
if (Is.nullOrEmpty(Config.google_maps_key)){
Debugger.println("GeoCoding - Google API-key is missing! Please add one via the config file to use Maps.", 1);
return new JSONArray();
}
JSONArray places = new JSONArray();
int N = 8; //max results
try {
//use GPS and radius?
String addGPS = "";
if (!latitude.isEmpty() && !longitude.isEmpty()){
addGPS = "&location=" + latitude + "," + longitude;
addGPS += "&radius=" + String.valueOf(15000);
}
//use types?
String addTypes = "";
if (types != null){
addTypes = types;
}
String addKey = "";
if (!Config.google_maps_key.equals("test")){
addKey = "&key=" + Config.google_maps_key;
}
String url = URLBuilder.getString("https://maps.googleapis.com/maps/api/place/textsearch/json",
"?query=", search,
"&language=", language,
"&types=", addTypes
//"&opennow=", true,
);
url += addKey;
url += addGPS;
//System.out.println("google_get_POI - search: " + search); //debug
//System.out.println("google_get_POI - URL: " + url); //debug
//make the HTTP GET call to Google Geocode API
long tic = System.currentTimeMillis();
JSONObject response = Connectors.httpGET(url.trim());
Statistics.addExternalApiHit("Google Places");
Statistics.addExternalApiTime("Google Places", tic);
//System.out.println("google_get_POI - Result: " + response.toJSONString()); //debug
//TODO: handle over_query_limit with a) 1s break b) 2s break c) possible quota end
JSONArray results = (JSONArray) response.get("results");
if (results != null && !results.isEmpty()){
int i = 0;
for (Object obj : results)
{
JSONObject placeJSON = new JSONObject();
JSONObject resJSON = (JSONObject) obj;
/*
"formatted_address" : "Wattenscheider Str. 39, 45307 Essen, Deutschland"
"geometry" : { "location" : { "lat" : 51.4621956, "lng" : 7.0873221 } }
"icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png"
"name" : "Krayer Hof"
"types" : [ "restaurant", "food", "point_of_interest", "establishment" ]
*/
JSON.add(placeJSON, LOCATION.ADDRESS_TEXT, resJSON.get("formatted_address"));
JSON.add(placeJSON, LOCATION.IMAGE, resJSON.get("icon"));
JSON.add(placeJSON, "types", resJSON.get("types"));
JSON.add(placeJSON, LOCATION.NAME, resJSON.get("name"));
JSON.add(placeJSON, LOCATION.LAT, ((JSONObject)((JSONObject) resJSON.get("geometry")).get("location")).get("lat"));
JSON.add(placeJSON, LOCATION.LNG, ((JSONObject)((JSONObject) resJSON.get("geometry")).get("location")).get("lng"));
JSON.add(places, placeJSON);
i++;
if (i > N){
break;
}
}
}
}catch (Exception e) {
Debugger.println("google_get_POI - failed due to: " + e.getMessage(), 1);
Debugger.printStackTrace(e, 5);
return new JSONArray();
}
return places;
}
public boolean getResult(String ip) throws APIException {
if (ip == null) {
throw new IllegalArgumentException("ip cannot be null.");
}
if (!ValidationUtil.isValidIp(ip)) {
throw new IllegalArgumentException("ip is invalid.");
}
ConfigurationNode sourceConfigNode = getSourceConfigNode();
String key = sourceConfigNode.getNode("key").getString();
if (key == null || key.isEmpty()) {
throw new APIException(true, "Key is not defined for " + getName());
}
JSONObject json;
try {
json = JSONWebUtil.getJSONObject(new URL("https://api.shodan.io/shodan/host/" + ip + "?key=" + key), "GET", (int) getCachedConfig().getTimeout(), "egg82/AntiVPN");
} catch (IOException | ParseException | ClassCastException ex) {
throw new APIException(false, "Could not get result from " + getName());
}
if (json == null) {
throw new APIException(false, "Could not get result from " + getName());
}
JSONArray tags = (JSONArray) json.get("tags");
if (tags == null) {
throw new APIException(false, "Could not get result from " + getName());
}
if (tags.isEmpty()) {
return false;
}
for (Object tag : tags) {
String t = (String) tag;
if (t.equalsIgnoreCase("proxy") || t.equalsIgnoreCase("vpn")) {
return true;
}
}
return false;
}
/**
* Checks if the resource is cached. If not cached, CKAN is queried in order to update the cache.
* This method assumes the given organization and package exist and they are cached.
* @param orgName Organization name
* @param pkgName Package name
* @param resName Resource name
* @return True if the organization was cached, false otherwise
* @throws CygnusBadConfiguration
* @throws CygnusRuntimeError
* @throws CygnusPersistenceError
*/
public boolean isCachedRes(String orgName, String pkgName, String resName)
throws CygnusBadConfiguration, CygnusRuntimeError, CygnusPersistenceError {
// check if the resource has already been cached
if (tree.get(orgName).get(pkgName).contains(resName)) {
LOGGER.debug("Resource found in the cache (orgName=" + orgName + ", pkgName=" + pkgName + ", resName="
+ resName + ")");
return true;
} // if
LOGGER.debug("Resource not found in the cache, querying CKAN for the whole package containing it "
+ "(orgName=" + orgName + ", pkgName=" + pkgName + ", resName=" + resName + ")");
// reached this point, we need to query CKAN about the resource, in order to know if it exists in CKAN
// nevertheless, the CKAN API allows us to query for a certain resource by id, not by name...
// the only solution seems to query for the whole package and check again
// query CKAN for the organization information
String ckanURL = "/api/3/action/package_show?id=" + pkgName;
ArrayList<Header> headers = new ArrayList<>();
headers.add(new BasicHeader("Authorization", apiKey));
JsonResponse res = doRequest("GET", ckanURL, true, headers, null);
switch (res.getStatusCode()) {
case 200:
// the package exists in CKAN
LOGGER.debug("Package found in CKAN, going to update the cached resources (orgName=" + orgName
+ ", pkgName=" + pkgName + ")");
// there is no need to check if the package is in "deleted" state...
// there is no need to put the package in the tree nor put it in the package map...
// get the resource and populate the resource map
JSONObject result = (JSONObject) res.getJsonObject().get("result");
JSONArray resources = (JSONArray) result.get("resources");
if (resources.isEmpty()) {
return false;
} else {
LOGGER.debug("Going to populate the resources cache (orgName=" + orgName + ", pkgName=" + pkgName
+ ")");
populateResourcesMap(resources, orgName, pkgName, true);
// check if the resource is within the resources cache, once populated
if (tree.get(orgName).get(pkgName).contains(resName)) {
LOGGER.debug("Resource found in the cache, once queried CKAN " + "(orgName=" + orgName
+ ", pkgName=" + pkgName + ", resName=" + resName + ")");
return true;
} else {
LOGGER.debug("Resource not found in the cache, once queried CKAN " + "(orgName=" + orgName
+ ", pkgName=" + pkgName + ", resName=" + resName + ")");
return false;
} // if else
} // if else
case 404:
return false;
default:
throw new CygnusPersistenceError("Could not check if the resource exists ("
+ "orgName=" + orgName + ", pkgName=" + pkgName + ", resName=" + resName
+ ", statusCode=" + res.getStatusCode() + ")");
} // switch
}
public DockerContainerDetail getDetail(DockerContainer container) throws DockerException {
JSONObject value = getRawDetails(DockerEntityType.Container, container.getId());
String name = (String) value.get("Name");
DockerContainer.Status status = DockerContainer.Status.STOPPED;
JSONObject state = (JSONObject) value.get("State");
if (state != null) {
boolean paused = (Boolean) getOrDefault(state, "Paused", false);
if (paused) {
status = DockerContainer.Status.PAUSED;
} else {
boolean running = (Boolean) getOrDefault(state, "Running", false);
if (running) {
status = DockerContainer.Status.RUNNING;
}
}
}
boolean tty = false;
boolean stdin = false;
JSONObject config = (JSONObject) value.get("Config");
if (config != null) {
tty = (boolean) getOrDefault(config, "Tty", false);
stdin = (boolean) getOrDefault(config, "OpenStdin", false);
}
JSONObject ports = (JSONObject) ((JSONObject) value.get("NetworkSettings")).get("Ports");
if (ports == null || ports.isEmpty()) {
return new DockerContainerDetail(name, status, stdin, tty);
} else {
List<PortMapping> portMapping = new ArrayList<>();
for (String containerPortData : (Set<String>) ports.keySet()) {
JSONArray hostPortsArray = (JSONArray) ports.get(containerPortData);
if (hostPortsArray != null && !hostPortsArray.isEmpty()) {
Matcher m = PORT_PATTERN.matcher(containerPortData);
if (m.matches()) {
int containerPort = Integer.parseInt(m.group(1));
String type = m.group(2).toUpperCase(Locale.ENGLISH);
int hostPort = Integer.parseInt((String) ((JSONObject) hostPortsArray.get(0)).get("HostPort"));
String hostIp = (String) ((JSONObject) hostPortsArray.get(0)).get("HostIp");
portMapping.add(new PortMapping(ExposedPort.Type.valueOf(type), containerPort, hostPort, hostIp));
} else {
LOGGER.log(Level.FINE, "Unparsable port: {0}", containerPortData);
}
}
}
return new DockerContainerDetail(name, status, stdin, tty, portMapping);
}
}
/**
* Read the list of objects from the file containing the ground truth for
* the current file.
*
* @param filePath the name of the file containing the ground truth.
* @param frameInfo the frame info
* @param objectList the object list
*/
public void readFile(String filePath, FrameInfo frameInfo, List<Objects> objectList) {
// add the file extension
String jsonFilePath = filePath + "_GT.json";
File file = new File(jsonFilePath);
if ((!file.exists()) || (file.length() == 0)) {
return;
}
try (FileReader fileRead = new FileReader(jsonFilePath);) {
// convert Object to JSONObject
JSONObject jsonDataList = (JSONObject) jsonParser.parse(fileRead);
// reading the frame specific information
readFrameInformation(jsonDataList, frameInfo);
// reading the array of objects, json format
JSONArray jsonObjList = (JSONArray) jsonDataList.get("Objects List");
// go over the list of objects from the json file and convert them to Objects format
for (Object jsonObj : jsonObjList) {
JSONObject jsonObject = (JSONObject) jsonObj;
Objects obj = null;
long objectId = (long) jsonObject.get("object_id");
String segmentationType = (String) jsonObject.get("segmentation_type");
// create the object based on the segmentation type
if (segmentationType.equalsIgnoreCase(ConstantsLabeling.LABEL_SCRIBBLE)) {
obj = new ObjectScribble();
// reading the array of objects
JSONArray jsonCropList = (JSONArray) jsonObject.get("Crops List");
if (!jsonCropList.isEmpty()) {
// add the crops to the scribble object
for (Object jsonCropObj : jsonCropList) {
// parse the JSON crop objects
JSONObject jsonCrop = (JSONObject) jsonCropObj;
// create a new crop object and fill in the needed data: position, scribble map
CropObject cropObj = new CropObject();
cropObj.setPositionOrig(new Rectangle((int) ((long) jsonCrop.get(POSITION_X)), (int) ((long) jsonCrop.get(POSITION_Y)),
(int) ((long) jsonCrop.get(WIDTH)), (int) ((long) jsonCrop.get(HEIGHT))));
cropObj.setScribbleList(getScribbleList((String) jsonCrop.get("scribble_map_path")));
cropObj.setObjectMap(new byte[(int) ((long) jsonCrop.get(WIDTH))][(int) ((long) jsonCrop.get(HEIGHT))]);
// add the crop to the object list of crops
((ObjectScribble) obj).addToCropList(cropObj);
}
}
} else if (segmentationType.equalsIgnoreCase(ConstantsLabeling.LABEL_2D_BOUNDING_BOX)) {
obj = new ObjectBBox();
} else if (segmentationType.equalsIgnoreCase(ConstantsLabeling.LABEL_POLYGON)) {
obj = new ObjectPolygon();
// load the saved polygon object
String objectMapPath = (String) jsonObject.get(OUTPUT_MAP_PATH);
((ObjectPolygon) obj).setPolygon(getPolygon(objectMapPath));
}
// fill in the common data of the object
if (obj != null) {
obj.setObjectId(objectId);
obj.setSegmentationType(segmentationType);
obj.setSegmentationSource(ConstantsLabeling.LABEL_SOURCE_MANUAL);
obj.setObjectType(Utils.capitalize((String) jsonObject.get("type")));
obj.setObjectClass(Utils.capitalize((String) jsonObject.get("class")));
obj.setObjectValue(Utils.capitalize((String) jsonObject.get("value")));
obj.setOccluded((String) jsonObject.get("occluded"));
obj.setOuterBBox(new Rectangle((int) ((long) jsonObject.get(POSITION_X)), (int) ((long) jsonObject.get(POSITION_Y)),
(int) ((long) jsonObject.get(WIDTH)), (int) ((long) jsonObject.get(HEIGHT))));
obj.setColor(Utils.getColorOfObjByID(objectId));
// add the object in the object list if it does not exist yet
if (!isObjInObjList(obj, objectList)) {
// add the object in the list
objectList.add(obj);
}
}
}
// printing all the values
log.trace("Frame: {}", frameInfo);
log.trace("Objects:");
jsonObjList.stream().forEach(obj -> log.trace("\t {}", obj));
} catch (IOException | ParseException ex) {
log.error("Read objects from the json file error");
log.debug("Read objects from the json file error {}", ex);
}
}
/**
* Make a connection to the BukkitDev API and request the newest file's details.
*
* @return true if successful.
*/
private boolean read() {
try {
final URLConnection conn = this.url.openConnection();
conn.setConnectTimeout(5000);
if (this.apiKey != null) {
conn.addRequestProperty("X-API-Key", this.apiKey);
}
conn.addRequestProperty("User-Agent", PluginUpdater.USER_AGENT);
conn.setDoOutput(true);
final BufferedReader reader =
new BufferedReader(new InputStreamReader(conn.getInputStream()));
final String response = reader.readLine();
final JSONArray array = (JSONArray) JSONValue.parse(response);
if (array.isEmpty()) {
this.plugin.getLogger()
.warning("The updater could not find any files for the project id " + this.id);
this.result = UpdateResult.FAIL_BADID;
return false;
}
JSONObject latestUpdate = (JSONObject) array.get(array.size() - 1);
this.versionName = (String) latestUpdate.get(PluginUpdater.TITLE_VALUE);
this.versionLink = (String) latestUpdate.get(PluginUpdater.LINK_VALUE);
this.versionType = (String) latestUpdate.get(PluginUpdater.TYPE_VALUE);
this.versionGameVersion = (String) latestUpdate.get(PluginUpdater.VERSION_VALUE);
this.versionCustom = this.getCustomVersion();
return true;
} catch (final IOException e) {
if (e.getMessage().contains("HTTP response code: 403")) {
this.plugin.getLogger()
.severe("dev.bukkit.org rejected the API key provided in plugins/updater/config.yml");
this.plugin.getLogger()
.severe("Please double-check your configuration to ensure it is correct.");
this.result = UpdateResult.FAIL_APIKEY;
} else {
this.plugin.getLogger()
.severe("The updater could not contact dev.bukkit.org for updating.");
this.plugin.getLogger().severe(
"If you have not recently modified your configuration and this is the first time you are seeing this message, the site may be experiencing temporary downtime.");
this.result = UpdateResult.FAIL_DBO;
}
this.plugin.getLogger().log(Level.SEVERE, null, e);
return false;
}
}
/**
* This method is used to check for PII Categories for a particular consent management purpose
*
* @param purpose The consent purpose
* @return Boolean if there are PII Categories for the purpose
*/
private static boolean hasPIICategories(JSONObject purpose) {
JSONArray piiCategories = (JSONArray) purpose.get(PII_CATEGORIES);
return !piiCategories.isEmpty();
}