Search in sources :

Example 81 with JSONArray

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;
}
Also used : Configuration(org.apache.commons.configuration.Configuration) XMLConfiguration(org.apache.commons.configuration.XMLConfiguration) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) JSONObject(org.json.simple.JSONObject) HashMap(java.util.HashMap) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration)

Example 82 with JSONArray

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));
}
Also used : JSONObject(org.json.simple.JSONObject) HashMap(java.util.HashMap) JSONArray(org.json.simple.JSONArray) Iterator(java.util.Iterator) Test(org.junit.Test)

Example 83 with JSONArray

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"));
}
Also used : JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) Map(java.util.Map) Test(org.junit.Test)

Example 84 with JSONArray

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 "";
    }
}
Also used : JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) Map(java.util.Map) HashMap(java.util.HashMap)

Example 85 with JSONArray

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);
    }
}
Also used : SubscriptionThrottlePolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.SubscriptionThrottlePolicyDTO) ArrayList(java.util.ArrayList) JSONArray(org.json.simple.JSONArray) SubscriptionThrottlePolicyException(org.wso2.carbon.apimgt.rest.api.admin.exceptions.SubscriptionThrottlePolicyException) UnsupportedThrottleLimitTypeException(org.wso2.carbon.apimgt.rest.api.admin.exceptions.UnsupportedThrottleLimitTypeException) JSONObject(org.json.simple.JSONObject) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) ParseException(org.json.simple.parser.ParseException) CustomAttributeDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.CustomAttributeDTO)

Aggregations

JSONArray (org.json.simple.JSONArray)267 JSONObject (org.json.simple.JSONObject)238 JSONParser (org.json.simple.parser.JSONParser)73 Test (org.junit.Test)40 ParseException (org.json.simple.parser.ParseException)23 ArrayList (java.util.ArrayList)21 HttpClient (org.apache.commons.httpclient.HttpClient)21 IOException (java.io.IOException)17 HashMap (java.util.HashMap)17 GetMethod (org.apache.commons.httpclient.methods.GetMethod)17 Transaction (org.xel.Transaction)12 File (java.io.File)11 List (java.util.List)10 HttpResponse (org.apache.http.HttpResponse)10 BlockchainTest (org.xel.BlockchainTest)10 APICall (org.xel.http.APICall)10 Block (org.xel.Block)9 MapLayer (au.org.emii.portal.menu.MapLayer)8 Map (java.util.Map)8 FileReader (java.io.FileReader)6