Java Code Examples for net.sf.json.JSONObject#optJSONArray()

The following examples show how to use net.sf.json.JSONObject#optJSONArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RecordPermissionsRest.java    From mobi with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Updates the policy policy file with the corresponding 'update' permissions from the record policy.
 *
 * @param json The json from record policy update
 * @param policyType The PolicyType from the policy policy
 * @return The update XACML policy
 */
private XACMLPolicy updatePolicyPolicy(String json, PolicyType policyType) {
    AttributeDesignatorType subjectIdAttrDesig = createSubjectIdAttrDesig();
    AttributeDesignatorType groupAttrDesig = createGroupAttrDesig();

    JSONObject jsonObject = JSONObject.fromObject(json);
    JSONObject updateObj = jsonObject.optJSONObject("urn:update");
    if (updateObj == null) {
        throw new IllegalArgumentException("Invalid JSON representation of a Policy. Missing update rule.");
    }

    AnyOfType readUserAnyOf = policyType.getRule().get(0).getTarget().getAnyOf().get(1);
    AnyOfType updateUserAnyOf = policyType.getRule().get(1).getTarget().getAnyOf().get(1);
    readUserAnyOf.getAllOf().clear();
    updateUserAnyOf.getAllOf().clear();
    if (updateObj.opt("everyone") == null) {
        throw new IllegalArgumentException("Invalid JSON representation of a Policy. Missing everyone field.");
    }
    if (updateObj.getBoolean("everyone")) {
        MatchType userRoleMatch = createUserRoleMatch();
        AllOfType everyoneAllOf = new AllOfType();
        everyoneAllOf.getMatch().add(userRoleMatch);
        readUserAnyOf.getAllOf().add(everyoneAllOf);
        updateUserAnyOf.getAllOf().add(everyoneAllOf);
    } else {
        JSONArray usersArray = updateObj.optJSONArray("users");
        JSONArray groupsArray = updateObj.optJSONArray("groups");
        if (usersArray == null || groupsArray == null) {
            throw new IllegalArgumentException("Invalid JSON representation of a Policy."
                    + " Users or groups not set properly for update rule");
        }
        addUsersOrGroupsToAnyOf(usersArray, subjectIdAttrDesig, readUserAnyOf, updateUserAnyOf);
        addUsersOrGroupsToAnyOf(groupsArray, groupAttrDesig, readUserAnyOf, updateUserAnyOf);
    }

    return policyManager.createPolicy(policyType);
}
 
Example 2
Source File: ProvRestTest.java    From mobi with GNU Affero General Public License v3.0 5 votes vote down vote up
private void assertActivities(JSONObject result, List<String> expected) {
    assertTrue(result.containsKey("activities"));
    JSONArray activities = result.optJSONArray("activities");
    assertNotNull(activities);
    assertEquals(activities.size(), expected.size());
    assertResourceOrder(activities, expected);
}
 
Example 3
Source File: ProvRestTest.java    From mobi with GNU Affero General Public License v3.0 5 votes vote down vote up
private void assertEntities(JSONObject result, List<String> expected) {
    assertTrue(result.containsKey("entities"));
    JSONArray entities = result.optJSONArray("entities");
    assertNotNull(entities);
    assertEquals(entities.size(), expected.size());
    List<String> resources = getResources(entities);
    assertTrue(resources.containsAll(expected));
}
 
Example 4
Source File: RecordPermissionsRest.java    From mobi with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Converts a record policy abridged JSON into a XACML policy object.
 *
 * @param json The record policy JSON
 * @param policyType the PolicyType from the record policy
 * @return The updated XACMLPolicy object
 */
private XACMLPolicy recordJsonToPolicy(String json, PolicyType policyType) {
    String masterBranch = policyType.getRule().get(4).getTarget().getAnyOf().get(0).getAllOf().get(0).getMatch()
            .get(1).getAttributeValue().getContent().get(0).toString();
    policyType.getRule().clear();

    AttributeDesignatorType actionIdAttrDesig = createActionIdAttrDesig();
    AttributeDesignatorType subjectIdAttrDesig = createSubjectIdAttrDesig();
    AttributeDesignatorType groupAttrDesig = createGroupAttrDesig();
    AttributeDesignatorType branchAttrDesig = createBranchAttrDesig();
    AttributeValueType branchAttrVal = createAttributeValue(XSD.STRING, masterBranch);

    JSONObject jsonObject = JSONObject.fromObject(json);
    Iterator<?> keys = jsonObject.keys();

    while (keys.hasNext()) {
        String key = (String)keys.next();
        TargetType target = new TargetType();
        RuleType rule = createRule(EffectType.PERMIT, key, target);
        AttributeValueType ruleTypeAttrVal = new AttributeValueType();
        ruleTypeAttrVal.setDataType(XSD.STRING);

        // Setup Rule Type
        MatchType ruleTypeMatch = createMatch(XACML.STRING_EQUALS, actionIdAttrDesig, ruleTypeAttrVal);
        ruleTypeAttrVal.getContent().clear();
        switch (key) {
            case "urn:read":
                ruleTypeAttrVal.getContent().add(Read.TYPE);
                break;
            case "urn:delete":
                ruleTypeAttrVal.getContent().add(Delete.TYPE);
                break;
            case "urn:update":
                ruleTypeAttrVal.getContent().add(Update.TYPE);
                break;
            case "urn:modify":
                ruleTypeAttrVal.getContent().add("http://mobi.com/ontologies/catalog#Modify");
                break;
            case "urn:modifyMaster":
                ruleTypeAttrVal.getContent().add("http://mobi.com/ontologies/catalog#Modify");
                break;
            default:
                throw new MobiException("Invalid rule key: " + key);
        }

        AnyOfType anyOfType = new AnyOfType();
        AllOfType allOfType = new AllOfType();
        anyOfType.getAllOf().add(allOfType);
        allOfType.getMatch().add(ruleTypeMatch);

        if (key.equalsIgnoreCase("urn:modifyMaster")) {
            MatchType branchMatch = createMatch(XACML.STRING_EQUALS, branchAttrDesig, branchAttrVal);
            allOfType.getMatch().add(branchMatch);
        }
        rule.getTarget().getAnyOf().add(anyOfType);

        // Setup Users
        AnyOfType usersGroups = new AnyOfType();
        JSONObject jsonRule = jsonObject.optJSONObject(key);
        if (jsonRule == null) {
            throw new IllegalArgumentException("Invalid JSON representation of a Policy. Missing rule " + key);
        }
        if (jsonRule.opt("everyone") == null) {
            throw new IllegalArgumentException("Invalid JSON representation of a Policy. Missing everyone field"
                    + "for " + key);
        }
        if (jsonRule.getBoolean("everyone")) {
            MatchType userRoleMatch = createUserRoleMatch();
            AllOfType everyoneAllOf = new AllOfType();
            everyoneAllOf.getMatch().add(userRoleMatch);
            usersGroups.getAllOf().add(everyoneAllOf);
        } else {
            JSONArray usersArray = jsonRule.optJSONArray("users");
            JSONArray groupsArray = jsonRule.optJSONArray("groups");
            if (usersArray == null || groupsArray == null) {
                throw new IllegalArgumentException("Invalid JSON representation of a Policy."
                        + " Users or groups not set properly for " + key);
            }
            addUsersOrGroupsToAnyOf(usersArray, subjectIdAttrDesig, usersGroups);
            addUsersOrGroupsToAnyOf(groupsArray, groupAttrDesig, usersGroups);
        }
        rule.getTarget().getAnyOf().add(usersGroups);

        if (key.equalsIgnoreCase("urn:modify")) {
            ConditionType condition = new ConditionType();
            condition.setExpression(createMasterBranchExpression(masterBranch));
            rule.setCondition(condition);
        }
        policyType.getRule().add(rule);
    }

    return policyManager.createPolicy(policyType);
}