Search in sources :

Example 1 with Acl

use of org.alfresco.repo.solr.Acl in project alfresco-remote-api by Alfresco.

the class AclsGet method buildModel.

private Map<String, Object> buildModel(WebScriptRequest req) throws JSONException, IOException {
    List<Long> aclChangeSetIds = null;
    Content content = req.getContent();
    if (content == null) {
        throw new WebScriptException("Request content is empty");
    }
    JSONObject o = new JSONObject(content.getContent());
    JSONArray aclChangeSetIdsJSON = o.has("aclChangeSetIds") ? o.getJSONArray("aclChangeSetIds") : null;
    if (aclChangeSetIdsJSON == null) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Parameter 'aclChangeSetIds' not provided in request content.");
    } else if (aclChangeSetIdsJSON.length() == 0) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Parameter 'aclChangeSetIds' must hold from 1 or more IDs.");
    }
    aclChangeSetIds = new ArrayList<Long>(aclChangeSetIdsJSON.length());
    for (int i = 0; i < aclChangeSetIdsJSON.length(); i++) {
        aclChangeSetIds.add(aclChangeSetIdsJSON.getLong(i));
    }
    String fromIdParam = req.getParameter("fromId");
    String maxResultsParam = req.getParameter("maxResults");
    Long fromId = (fromIdParam == null ? null : Long.valueOf(fromIdParam));
    int maxResults = (maxResultsParam == null ? 1024 : Integer.valueOf(maxResultsParam));
    // Request according to the paging query style required
    List<Acl> acls = solrTrackingComponent.getAcls(aclChangeSetIds, fromId, maxResults);
    Map<String, Object> model = new HashMap<String, Object>(1, 1.0f);
    model.put("acls", acls);
    if (logger.isDebugEnabled()) {
        logger.debug("Result: \n\tRequest: " + req + "\n\tModel: " + model);
    }
    return model;
}
Also used : HashMap(java.util.HashMap) JSONArray(org.json.JSONArray) Acl(org.alfresco.repo.solr.Acl) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) JSONObject(org.json.JSONObject) Content(org.springframework.extensions.surf.util.Content) JSONObject(org.json.JSONObject)

Example 2 with Acl

use of org.alfresco.repo.solr.Acl in project alfresco-remote-api by Alfresco.

the class SOLRWebScriptTest method aclReadersGetImpl.

private void aclReadersGetImpl() throws Exception {
    List<AclChangeSet> aclChangeSets = solrTrackingComponent.getAclChangeSets(null, null, null, null, 1024);
    List<Long> aclChangeSetIds = new ArrayList<Long>(50);
    for (AclChangeSet aclChangeSet : aclChangeSets) {
        if (aclChangeSet.getAclCount() > 0) {
            aclChangeSetIds.add(aclChangeSet.getId());
            break;
        }
    }
    if (aclChangeSetIds.size() == 0) {
    // No ACLs; not likely
    }
    List<Acl> acls = solrTrackingComponent.getAcls(aclChangeSetIds, null, 1024);
    List<Long> aclIds = new ArrayList<Long>(acls.size());
    JSONObject json = new JSONObject();
    JSONArray aclIdsJSON = new JSONArray();
    for (Acl acl : acls) {
        Long aclId = acl.getId();
        aclIds.add(aclId);
        aclIdsJSON.put(aclId);
    }
    json.put("aclIds", aclIdsJSON);
    // Now get the readers
    List<AclReaders> aclsReaders = solrTrackingComponent.getAclsReaders(aclIds);
    assertEquals("Should have same number of ACLs as supplied", aclIds.size(), aclsReaders.size());
    assertTrue("Must have *some* ACLs here", aclIds.size() > 0);
    Map<Long, Set<String>> readersByAclId = new HashMap<Long, Set<String>>();
    for (AclReaders aclReaders : aclsReaders) {
        readersByAclId.put(aclReaders.getAclId(), aclReaders.getReaders());
    }
    Map<Long, Set<String>> deniedByAclId = new HashMap<Long, Set<String>>();
    for (AclReaders aclReaders : aclsReaders) {
        assertNotNull("AclReaders should not contain null denial set", aclReaders.getDenied());
        deniedByAclId.put(aclReaders.getAclId(), aclReaders.getDenied());
    }
    // Now query using the webscript
    String url = "/api/solr/aclsReaders";
    TestWebScriptServer.PostRequest req = new TestWebScriptServer.PostRequest(url, json.toString(), "application/json");
    Response response = sendRequest(req, Status.STATUS_OK, admin);
    if (logger.isDebugEnabled()) {
        logger.debug(response.getContentAsString());
    }
    json = new JSONObject(response.getContentAsString());
    JSONArray aclsReadersJSON = json.getJSONArray("aclsReaders");
    // Check
    assertEquals("Script and API returned different number of results", readersByAclId.size(), aclsReadersJSON.length());
    // Iterate of the JSON and ensure that the list of ACL readers is correct
    for (int i = 0; i < aclsReadersJSON.length(); i++) {
        // Choose an ACL and check the readers
        JSONObject aclReadersJSON = aclsReadersJSON.getJSONObject(i);
        Long aclIdJSON = aclReadersJSON.getLong("aclId");
        Set<String> readersCheck = readersByAclId.get(aclIdJSON);
        JSONArray readersJSON = aclReadersJSON.getJSONArray("readers");
        assertEquals("Readers list for ACL " + aclIdJSON + " is wrong. ", readersCheck.size(), readersJSON.length());
        for (int j = 0; j < readersJSON.length(); j++) {
            String readerJSON = readersJSON.getString(j);
            assertTrue("Found reader not in check set: " + readerJSON, readersCheck.contains(readerJSON));
        }
        Set<String> deniedCheck = deniedByAclId.get(aclIdJSON);
        JSONArray deniedJSON = aclReadersJSON.getJSONArray("denied");
        assertEquals("Denied list for ACL " + aclIdJSON + " is wrong. ", deniedCheck.size(), deniedJSON.length());
        for (int j = 0; j < deniedJSON.length(); j++) {
            String denyJSON = deniedJSON.getString(j);
            assertTrue("Found denied authority not in check set: " + denyJSON, deniedCheck.contains(denyJSON));
        }
    }
}
Also used : AclChangeSet(org.alfresco.repo.solr.AclChangeSet) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) Acl(org.alfresco.repo.solr.Acl) Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) JSONObject(org.json.JSONObject) TestWebScriptServer(org.springframework.extensions.webscripts.TestWebScriptServer) AclChangeSet(org.alfresco.repo.solr.AclChangeSet) AclReaders(org.alfresco.repo.solr.AclReaders)

Example 3 with Acl

use of org.alfresco.repo.solr.Acl in project alfresco-remote-api by Alfresco.

the class SOLRWebScriptTest method testAclsGet.

public void testAclsGet() throws Exception {
    List<AclChangeSet> aclChangeSets = solrTrackingComponent.getAclChangeSets(null, null, null, null, 100);
    if (aclChangeSets.size() == 0) {
        // Can't test, but very unlikely
        return;
    }
    // Build JSON using these
    JSONObject json = new JSONObject();
    JSONArray aclChangeSetIdsJSON = new JSONArray();
    int count = 0;
    List<Long> aclChangeSetIds = new ArrayList<Long>();
    for (AclChangeSet aclChangeSet : aclChangeSets) {
        if (count >= 512) {
            break;
        }
        if (aclChangeSet.getAclCount() == 0) {
            // No ACLs
            continue;
        }
        Long aclChangeSetId = aclChangeSet.getId();
        aclChangeSetIdsJSON.put(aclChangeSetId);
        aclChangeSetIds.add(aclChangeSetId);
        count++;
    }
    json.put("aclChangeSetIds", aclChangeSetIdsJSON);
    String url = "/api/solr/acls";
    TestWebScriptServer.PostRequest req = new TestWebScriptServer.PostRequest(url, json.toString(), "application/json");
    Response response = sendRequest(req, Status.STATUS_OK, admin);
    if (logger.isDebugEnabled()) {
        logger.debug(response.getContentAsString());
    }
    json = new JSONObject(response.getContentAsString());
    JSONArray acls = json.getJSONArray("acls");
    // Check
    List<Acl> aclsCheck = solrTrackingComponent.getAcls(aclChangeSetIds, null, 512);
    assertEquals("Script and API returned different number of results", aclsCheck.size(), acls.length());
}
Also used : JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) Acl(org.alfresco.repo.solr.Acl) Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) JSONObject(org.json.JSONObject) TestWebScriptServer(org.springframework.extensions.webscripts.TestWebScriptServer) AclChangeSet(org.alfresco.repo.solr.AclChangeSet)

Aggregations

Acl (org.alfresco.repo.solr.Acl)3 JSONArray (org.json.JSONArray)3 JSONObject (org.json.JSONObject)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 AclChangeSet (org.alfresco.repo.solr.AclChangeSet)2 TestWebScriptServer (org.springframework.extensions.webscripts.TestWebScriptServer)2 Response (org.springframework.extensions.webscripts.TestWebScriptServer.Response)2 Set (java.util.Set)1 AclReaders (org.alfresco.repo.solr.AclReaders)1 Content (org.springframework.extensions.surf.util.Content)1 WebScriptException (org.springframework.extensions.webscripts.WebScriptException)1