List of usage examples for org.json.simple JSONObject containsKey
boolean containsKey(Object key);
From source file:org.PrimeSoft.MCPainter.utils.JSONExtensions.java
/** * Try to get the JSONArray/* w w w. j a v a 2 s.co m*/ * * @param o * @param property * @return */ public static JSONArray tryGetArray(JSONObject o, String property) { if (o == null || property == null) { return null; } if (!o.containsKey(property)) { return null; } Object value = o.get(property); if (value instanceof JSONArray) { return (JSONArray) value; } return null; }
From source file:org.sead.sda.LandingPage.java
private void addROProperty_License(String propertyName, JSONObject preference, JSONObject researchObject, Map<String, List<String>> properties) { if (keyMapList.containsKey(propertyName.toLowerCase())) { Object valJSON = preference.get(keyMapList.get(propertyName.toLowerCase())); String license = null;//from w ww. j a v a 2s . c o m if (researchObject.containsKey(keyMapList.get(propertyName.toLowerCase()))) { license = researchObject.get(keyMapList.get(propertyName.toLowerCase())).toString(); } if (valJSON != null) { // some properties my come as arrays List<String> list = new ArrayList<String>(); if (valJSON instanceof JSONArray) { for (int i = 0; i < ((JSONArray) valJSON).size(); i++) { if (((JSONArray) valJSON).get(i) != null) { if (researchObject.containsKey(keyMapList.get(propertyName.toLowerCase()))) { if (!license.equals(((JSONArray) valJSON).get(i).toString())) { list.add(license); list.add(((JSONArray) valJSON).get(i).toString()); } else { list.add(((JSONArray) valJSON).get(i).toString()); } } else { list.add(((JSONArray) valJSON).get(i).toString()); } } } properties.put(propertyName, list); } else { if (researchObject.containsKey(keyMapList.get(propertyName.toLowerCase()))) { if (!license.equals(valJSON.toString())) { list.add(license); list.add(valJSON.toString()); } else { list.add(valJSON.toString()); } } else { list.add(valJSON.toString()); } properties.put(propertyName, list); } } else { List<String> list = new ArrayList<String>(); list.add(license); properties.put(propertyName, list); } } else { List<String> temp = new ArrayList<String>(); temp.add("No " + propertyName + " Found"); properties.put(propertyName, temp); } }
From source file:org.springfield.lou.application.types.workflow.collectiondef.CollectionSearchController.java
public void decadeSelected(Screen s, JSONObject data) { String decade = ""; if (data.containsKey("options")) { JSONArray options = (JSONArray) data.get("options"); if (options.size() > 0) { decade = (String) ((JSONObject) options.get(0)).get("value"); }/*from w w w. java2 s . co m*/ } query.put("decade", decade); updateSearch(); }
From source file:org.springfield.lou.application.types.workflow.collectiondef.CollectionSearchController.java
public void licenseSelected(Screen s, JSONObject data) { String license = ""; if (data.containsKey("options")) { JSONArray options = (JSONArray) data.get("options"); if (options.size() > 0) { license = (String) ((JSONObject) options.get(0)).get("value"); }//from ww w . j av a 2 s . c om } query.put("license", license); updateSearch(); }
From source file:org.springfield.lou.application.types.workflow.home.UserCollectionsController.java
public void collectionHandler(Screen s, JSONObject data) { if (!data.containsKey("eventtype")) { return;/*from w w w .j a va 2s .co m*/ } if (data.get("eventtype").equals("addCollection")) { } if (data.get("eventtype").equals("collectionDetails")) { if (!data.containsKey("collection")) { return; } String uri = (String) data.get("collection"); //Workaround for uri that seems to contain double slash before the id from FsNode uri = uri.replace("//", "/"); FsNode node = Fs.getNode(uri); //TODO:workaround to not disturb QANDR work on IBC, FsNode should have a direct toJSONObject function List<FsNode> nodes = new ArrayList<FsNode>(); nodes.add(node); JSONObject results = FSList.parseNodeList(uri, nodes).toJSONObject(screen.getLanguageCode(), "title,description"); System.out.println(results.toJSONString()); screen.get("#collectionDetails").update(results); } }
From source file:org.tridas.io.formats.tridasjson.TridasJSONFile.java
private static String getUniqueChildKey(JSONObject item, String key, int increment) { if (increment > 100) { log.error("Increment too high - must be in infinite loop"); return key; }/* w ww . j a v a2s . co m*/ String newkey = key; if (increment > 0) { newkey = key + "(" + increment + ")"; } if (item.containsKey(newkey)) { increment++; return getUniqueChildKey(item, key, increment); } return newkey; }
From source file:org.wso2.carbon.apimgt.impl.definitions.APIDefinitionFromOpenAPISpec.java
/** * This method returns URI templates according to the given swagger file * * @param api API//from w ww . ja va2s . c o m * @param resourceConfigsJSON swaggerJSON * @return URI Templates * @throws APIManagementException */ @Override public Set<URITemplate> getURITemplates(API api, String resourceConfigsJSON) throws APIManagementException { JSONParser parser = new JSONParser(); JSONObject swagger; Set<URITemplate> uriTemplates = new LinkedHashSet<URITemplate>(); Set<Scope> scopes = getScopes(resourceConfigsJSON); try { swagger = (JSONObject) parser.parse(resourceConfigsJSON); if (swagger.get("paths") != null) { JSONObject paths = (JSONObject) swagger.get("paths"); for (Object o : paths.keySet()) { String uriTempVal = (String) o; //if url template is a custom attribute "^x-" ignore. if (uriTempVal.startsWith("x-") || uriTempVal.startsWith("X-")) { continue; } JSONObject path = (JSONObject) paths.get(uriTempVal); // Following code check is done to handle $ref objects supported by swagger spec // See field types supported by "Path Item Object" in swagger spec. if (path.containsKey("$ref")) { log.info( "Reference " + uriTempVal + " path object was ignored when generating URL template " + "for api \"" + api.getId().getApiName() + '\"'); continue; } boolean isHttpVerbDefined = false; for (Object o1 : path.keySet()) { String httpVerb = (String) o1; if (APIConstants.SWAGGER_SUMMARY.equals(httpVerb.toLowerCase()) || APIConstants.SWAGGER_DESCRIPTION.equals(httpVerb.toLowerCase()) || APIConstants.SWAGGER_SERVERS.equals(httpVerb.toLowerCase()) || APIConstants.PARAMETERS.equals(httpVerb.toLowerCase()) || httpVerb.startsWith("x-") || httpVerb.startsWith("X-")) { // openapi 3.x allow 'summary', 'description' and extensions in PathItem Object. // which we are not interested at this point continue; } //Only continue for supported operations else if (APIConstants.SUPPORTED_METHODS.contains(httpVerb.toLowerCase())) { isHttpVerbDefined = true; JSONObject operation = (JSONObject) path.get(httpVerb); URITemplate template = new URITemplate(); Scope scope = APIUtil.findScopeByKey(scopes, (String) operation.get(APIConstants.SWAGGER_X_SCOPE)); String authType = (String) operation.get(APIConstants.SWAGGER_X_AUTH_TYPE); if ("Application & Application User".equals(authType)) { authType = APIConstants.AUTH_APPLICATION_OR_USER_LEVEL_TOKEN; } else if ("Application User".equals(authType)) { authType = APIConstants.AUTH_APPLICATION_USER_LEVEL_TOKEN; } else if ("None".equals(authType)) { authType = APIConstants.AUTH_NO_AUTHENTICATION; } else if ("Application".equals(authType)) { authType = APIConstants.AUTH_APPLICATION_LEVEL_TOKEN; } else { authType = APIConstants.AUTH_APPLICATION_OR_USER_LEVEL_TOKEN; } template.setThrottlingTier( (String) operation.get(APIConstants.SWAGGER_X_THROTTLING_TIER)); template.setThrottlingTiers( (String) operation.get(APIConstants.SWAGGER_X_THROTTLING_TIER)); template.setMediationScript( (String) operation.get(APIConstants.SWAGGER_X_MEDIATION_SCRIPT)); template.setMediationScripts(httpVerb.toUpperCase(), (String) operation.get(APIConstants.SWAGGER_X_MEDIATION_SCRIPT)); template.setUriTemplate(uriTempVal); template.setHTTPVerb(httpVerb.toUpperCase()); template.setHttpVerbs(httpVerb.toUpperCase()); template.setAuthType(authType); template.setAuthTypes(authType); template.setScope(scope); template.setScopes(scope); uriTemplates.add(template); } else { handleException("The HTTP method '" + httpVerb + "' provided for resource '" + uriTempVal + "' is invalid"); } } if (!isHttpVerbDefined) { handleException( "Resource '" + uriTempVal + "' has global parameters without " + "HTTP methods"); } } } } catch (ParseException e) { handleException("Invalid resource configuration ", e); } return uriTemplates; }
From source file:org.wso2.carbon.apimgt.impl.definitions.APIDefinitionFromOpenAPISpec.java
/** * Called using the jaggery api. Checks if the swagger contains valid api scopes. * * @param swagger Swagger definition/*from ww w . jav a2s . com*/ * @return true if the scope definition is valid * @throws APIManagementException */ public Boolean validateScopesFromSwagger(String swagger) throws APIManagementException { try { Set<Scope> scopes = getScopes(swagger); JSONParser parser = new JSONParser(); JSONObject swaggerJson; swaggerJson = (JSONObject) parser.parse(swagger); if (swaggerJson.get("paths") != null) { JSONObject paths = (JSONObject) swaggerJson.get("paths"); for (Object uriTempKey : paths.keySet()) { String uriTemp = (String) uriTempKey; //if url template is a custom attribute "^x-" ignore. if (uriTemp.startsWith("x-") || uriTemp.startsWith("X-")) { continue; } JSONObject path = (JSONObject) paths.get(uriTemp); // Following code check is done to handle $ref objects supported by swagger spec // See field types supported by "Path Item Object" in swagger spec. if (path.containsKey("$ref")) { continue; } for (Object httpVerbKey : path.keySet()) { String httpVerb = (String) httpVerbKey; JSONObject operation = (JSONObject) path.get(httpVerb); String operationScope = (String) operation.get(APIConstants.SWAGGER_X_SCOPE); Scope scope = APIUtil.findScopeByKey(scopes, operationScope); if (scope == null && operationScope != null) { return false; } } } } return true; } catch (APIManagementException e) { handleException("Error when validating scopes", e); return false; } catch (ParseException e) { handleException("Error when validating scopes", e); return false; } }
From source file:org.wso2.carbon.apimgt.impl.definitions.APIDefinitionFromSwagger20.java
/** * This method returns URI templates according to the given swagger file * * @param api API/*ww w.j av a 2s.co m*/ * @param resourceConfigsJSON swaggerJSON * @return URI Templates * @throws APIManagementException */ @Override public Set<URITemplate> getURITemplates(API api, String resourceConfigsJSON) throws APIManagementException { JSONParser parser = new JSONParser(); JSONObject swagger; Set<URITemplate> uriTemplates = new LinkedHashSet<URITemplate>(); Set<Scope> scopes = getScopes(resourceConfigsJSON); try { swagger = (JSONObject) parser.parse(resourceConfigsJSON); if (swagger.get("paths") != null) { JSONObject paths = (JSONObject) swagger.get("paths"); for (Object o : paths.keySet()) { String uriTempVal = (String) o; //if url template is a custom attribute "^x-" ignore. if (uriTempVal.startsWith("x-") || uriTempVal.startsWith("X-")) { continue; } JSONObject path = (JSONObject) paths.get(uriTempVal); // Following code check is done to handle $ref objects supported by swagger spec // See field types supported by "Path Item Object" in swagger spec. if (path.containsKey("$ref")) { log.info( "Reference " + uriTempVal + " path object was ignored when generating URL template " + "for api \"" + api.getId().getApiName() + '\"'); continue; } for (Object o1 : path.keySet()) { String httpVerb = (String) o1; //Only continue for supported operations if (APIConstants.SUPPORTED_METHODS.contains(httpVerb.toLowerCase())) { JSONObject operation = (JSONObject) path.get(httpVerb); URITemplate template = new URITemplate(); Scope scope = APIUtil.findScopeByKey(scopes, (String) operation.get(APIConstants.SWAGGER_X_SCOPE)); String authType = (String) operation.get(APIConstants.SWAGGER_X_AUTH_TYPE); if ("Application & Application User".equals(authType)) { authType = APIConstants.AUTH_APPLICATION_OR_USER_LEVEL_TOKEN; } else if ("Application User".equals(authType)) { authType = APIConstants.AUTH_APPLICATION_USER_LEVEL_TOKEN; } else if ("None".equals(authType)) { authType = APIConstants.AUTH_NO_AUTHENTICATION; } else if ("Application".equals(authType)) { authType = APIConstants.AUTH_APPLICATION_LEVEL_TOKEN; } else { authType = APIConstants.AUTH_APPLICATION_OR_USER_LEVEL_TOKEN; } template.setThrottlingTier( (String) operation.get(APIConstants.SWAGGER_X_THROTTLING_TIER)); template.setThrottlingTiers( (String) operation.get(APIConstants.SWAGGER_X_THROTTLING_TIER)); template.setMediationScript( (String) operation.get(APIConstants.SWAGGER_X_MEDIATION_SCRIPT)); template.setMediationScripts(httpVerb.toUpperCase(), (String) operation.get(APIConstants.SWAGGER_X_MEDIATION_SCRIPT)); template.setUriTemplate(uriTempVal); template.setHTTPVerb(httpVerb.toUpperCase()); template.setHttpVerbs(httpVerb.toUpperCase()); template.setAuthType(authType); template.setAuthTypes(authType); template.setScope(scope); template.setScopes(scope); uriTemplates.add(template); } } } } } catch (ParseException e) { handleException("Invalid resource configuration ", e); } return uriTemplates; }
From source file:org.wso2.carbon.apimgt.impl.utils.APIUtil.java
public static boolean isSandboxEndpointsExists(API api) { JSONParser parser = new JSONParser(); JSONObject config = null; try {/* ww w . java 2 s. c om*/ config = (JSONObject) parser.parse(api.getEndpointConfig()); if (config.containsKey("sandbox_endpoints")) { return true; } } catch (ParseException e) { log.error(APIConstants.MSG_JSON_PARSE_ERROR, e); } catch (ClassCastException e) { log.error(APIConstants.MSG_JSON_PARSE_ERROR, e); } return false; }