use of org.json.simple.JSONArray in project metron by apache.
the class SettingsLoader method loadKnownHosts.
public static Map<String, JSONObject> loadKnownHosts(String config_path) throws ConfigurationException, ParseException {
Configuration hosts = new PropertiesConfiguration(config_path);
Iterator<String> keys = hosts.getKeys();
Map<String, JSONObject> known_hosts = new HashMap<String, JSONObject>();
JSONParser parser = new JSONParser();
while (keys.hasNext()) {
String key = keys.next().trim();
JSONArray value = (JSONArray) parser.parse(hosts.getProperty(key).toString());
known_hosts.put(key, (JSONObject) value.get(0));
}
return known_hosts;
}
use of org.json.simple.JSONArray in project metron by apache.
the class HostFromPropertiesFileAdapterTest method testInitializeAdapter.
@Test
public void testInitializeAdapter() throws Exception {
Map<String, JSONObject> mapKnownHosts = new HashMap<>();
HostFromPropertiesFileAdapter hfa = new HostFromPropertiesFileAdapter(mapKnownHosts);
Assert.assertFalse(hfa.initializeAdapter(null));
JSONArray jsonArray = (JSONArray) JSONValue.parse(expectedKnownHostsString);
Iterator jsonArrayIterator = jsonArray.iterator();
while (jsonArrayIterator.hasNext()) {
JSONObject jsonObject = (JSONObject) jsonArrayIterator.next();
String host = (String) jsonObject.remove("ip");
mapKnownHosts.put(host, jsonObject);
}
hfa = new HostFromPropertiesFileAdapter(mapKnownHosts);
Assert.assertTrue(hfa.initializeAdapter(null));
}
use of org.json.simple.JSONArray in project metron by apache.
the class BasicBroParserTest method testFilesBroMessage.
@SuppressWarnings("rawtypes")
@Test
public void testFilesBroMessage() throws ParseException {
Map rawMessageMap = (Map) jsonParser.parse(filesBroMessage);
JSONObject rawJson = (JSONObject) rawMessageMap.get(rawMessageMap.keySet().iterator().next());
JSONObject broJson = broParser.parse(filesBroMessage.getBytes()).get(0);
String expectedBroTimestamp = "1425845251.334";
Assert.assertEquals(broJson.get("bro_timestamp"), expectedBroTimestamp);
String expectedTimestamp = "1425845251334";
Assert.assertEquals(broJson.get("timestamp").toString(), expectedTimestamp);
Assert.assertEquals(broJson.get("ip_src_addr").toString(), ((JSONArray) rawJson.get("tx_hosts")).get(0).toString());
Assert.assertEquals(broJson.get("ip_dst_addr").toString(), ((JSONArray) rawJson.get("rx_hosts")).get(0).toString());
Assert.assertTrue(broJson.get("original_string").toString().startsWith(rawMessageMap.keySet().iterator().next().toString().toUpperCase()));
Assert.assertEquals(broJson.get("fuid").toString(), rawJson.get("fuid").toString());
Assert.assertEquals(broJson.get("md5").toString(), rawJson.get("md5").toString());
Assert.assertEquals(broJson.get("analyzers").toString(), rawJson.get("analyzers").toString());
Assert.assertTrue(broJson.get("original_string").toString().startsWith("FILES"));
}
use of org.json.simple.JSONArray in project carbon-apimgt by wso2.
the class ApiDAOImpl method getPermissionsStringForApi.
/**
* This returns the json string containing the role permissions for a given API
*
* @param connection - DB connection
* @param apiId - apiId of the API
* @return permission string
* @throws SQLException - if error occurred while getting permissionMap of API from DB
*/
private String getPermissionsStringForApi(Connection connection, String apiId) throws SQLException {
JSONArray permissionArray = new JSONArray();
Map<String, Integer> permissionMap = getPermissionMapForApi(connection, apiId);
for (Map.Entry<String, Integer> entry : permissionMap.entrySet()) {
JSONObject jsonObject = new JSONObject();
jsonObject.put(APIMgtConstants.Permission.GROUP_ID, entry.getKey());
jsonObject.put(APIMgtConstants.Permission.PERMISSION, APIUtils.constructApiPermissionsListForValue(entry.getValue()));
permissionArray.add(jsonObject);
}
if (!permissionArray.isEmpty()) {
return permissionArray.toString();
} else {
return "";
}
}
use of org.json.simple.JSONArray in project carbon-apimgt by wso2.
the class SubscriptionThrottlePolicyMappingUtil method fromSubscriptionThrottlePolicyToDTO.
/**
* Converts a single Subscription Policy model into REST API DTO
*
* @param policy Subscription Policy model object
* @return Converted Subscription policy REST API DTO object
* @throws SubscriptionThrottlePolicyException - If error occurs
*/
public static SubscriptionThrottlePolicyDTO fromSubscriptionThrottlePolicyToDTO(SubscriptionPolicy policy) throws SubscriptionThrottlePolicyException {
try {
SubscriptionThrottlePolicyDTO policyDTO = new SubscriptionThrottlePolicyDTO();
policyDTO = CommonThrottleMappingUtil.updateFieldsFromToPolicyToDTO(policy, policyDTO);
SubscriptionPolicy subscriptionPolicy = policy;
policyDTO.setBillingPlan(subscriptionPolicy.getBillingPlan());
policyDTO.setRateLimitCount(subscriptionPolicy.getRateLimitCount());
policyDTO.setRateLimitTimeUnit(subscriptionPolicy.getRateLimitTimeUnit());
policyDTO.setStopOnQuotaReach(subscriptionPolicy.isStopOnQuotaReach());
byte[] customAttributes = subscriptionPolicy.getCustomAttributes();
if (customAttributes != null && customAttributes.length > 0) {
List<CustomAttributeDTO> customAttributeDTOs = new ArrayList<>();
JSONParser parser = new JSONParser();
JSONArray attributeArray = (JSONArray) parser.parse(new String(customAttributes, StandardCharsets.UTF_8));
for (Object attributeObj : attributeArray) {
JSONObject attribute = (JSONObject) attributeObj;
CustomAttributeDTO customAttributeDTO = CommonThrottleMappingUtil.getCustomAttribute(attribute.get(RestApiConstants.THROTTLING_CUSTOM_ATTRIBUTE_NAME).toString(), attribute.get(RestApiConstants.THROTTLING_CUSTOM_ATTRIBUTE_VALUE).toString());
customAttributeDTOs.add(customAttributeDTO);
}
policyDTO.setCustomAttributes(customAttributeDTOs);
}
if (policy.getDefaultQuotaPolicy() != null) {
policyDTO.setDefaultLimit(CommonThrottleMappingUtil.fromQuotaPolicyToDTO(policy.getDefaultQuotaPolicy()));
}
return policyDTO;
} catch (ParseException | UnsupportedThrottleLimitTypeException e) {
throw new SubscriptionThrottlePolicyException(e.getMessage(), e);
}
}
Aggregations