Search in sources :

Example 51 with JsonObject

use of javax.json.JsonObject in project sling by apache.

the class VersionInfoServletTest method testHarrayVersionsList.

public void testHarrayVersionsList() throws Exception {
    // No need to test everything in detail, just verify that the output
    // is reformatted with the harray option
    createConfiguration("V");
    waitForSlingStartup();
    waitUntilSlingIsStable();
    final JsonObject versions = JsonUtil.parseObject(getContent(createVersionableNode() + ".V.harray.json", CONTENT_TYPE_JSON));
    assertTrue("Expecting children array", versions.containsKey("__children__"));
    assertEquals("Expecting versions object", "versions", versions.getJsonArray("__children__").getJsonObject(0).getString("__name__"));
}
Also used : JsonObject(javax.json.JsonObject)

Example 52 with JsonObject

use of javax.json.JsonObject in project sling by apache.

the class AccessPrivilegesInfoTest method testGrantedWriteForGroup.

/*
	 * group testuser granted read / granted write
	 */
@Test
public void testGrantedWriteForGroup() throws IOException, JsonException {
    testGroupId = H.createTestGroup();
    testUserId = H.createTestUser();
    testFolderUrl = H.createTestFolder();
    Credentials adminCreds = new UsernamePasswordCredentials("admin", "admin");
    //add testUserId to testGroup
    String groupPostUrl = HttpTest.HTTP_BASE_URL + "/system/userManager/group/" + testGroupId + ".update.html";
    List<NameValuePair> groupPostParams = new ArrayList<NameValuePair>();
    groupPostParams.add(new NameValuePair(":member", testUserId));
    H.assertAuthenticatedPostStatus(adminCreds, groupPostUrl, HttpServletResponse.SC_OK, groupPostParams, null);
    //assign some privileges
    String postUrl = testFolderUrl + ".modifyAce.html";
    List<NameValuePair> postParams = new ArrayList<NameValuePair>();
    postParams.add(new NameValuePair("principalId", testGroupId));
    postParams.add(new NameValuePair("privilege@jcr:read", "granted"));
    postParams.add(new NameValuePair("privilege@jcr:write", "granted"));
    postParams.add(new NameValuePair("privilege@jcr:readAccessControl", "granted"));
    postParams.add(new NameValuePair("privilege@jcr:modifyAccessControl", "granted"));
    H.assertAuthenticatedPostStatus(adminCreds, postUrl, HttpServletResponse.SC_OK, postParams, null);
    String getUrl = testFolderUrl + ".privileges-info.json";
    //fetch the JSON for the test page to verify the settings.
    Credentials testUserCreds = new UsernamePasswordCredentials(testUserId, "testPwd");
    String json = H.getAuthenticatedContent(testUserCreds, getUrl, HttpTest.CONTENT_TYPE_JSON, null, HttpServletResponse.SC_OK);
    assertNotNull(json);
    JsonObject jsonObj = JsonUtil.parseObject(json);
    assertEquals(true, jsonObj.getBoolean("canAddChildren"));
    assertEquals(true, jsonObj.getBoolean("canDeleteChildren"));
    //the parent node must also have jcr:removeChildren granted for 'canDelete' to be true
    assertEquals(false, jsonObj.getBoolean("canDelete"));
    assertEquals(true, jsonObj.getBoolean("canModifyProperties"));
    assertEquals(true, jsonObj.getBoolean("canReadAccessControl"));
    assertEquals(true, jsonObj.getBoolean("canModifyAccessControl"));
    //add a child node to verify the 'canDelete' use case
    String childFolderUrl = H.getTestClient().createNode(testFolderUrl + "/testFolder" + random.nextInt() + SlingPostConstants.DEFAULT_CREATE_SUFFIX, null);
    String childPostUrl = childFolderUrl + ".modifyAce.html";
    postParams = new ArrayList<NameValuePair>();
    postParams.add(new NameValuePair("principalId", testGroupId));
    postParams.add(new NameValuePair("privilege@jcr:read", "granted"));
    postParams.add(new NameValuePair("privilege@jcr:removeNode", "granted"));
    H.assertAuthenticatedPostStatus(adminCreds, childPostUrl, HttpServletResponse.SC_OK, postParams, null);
    String childGetUrl = childFolderUrl + ".privileges-info.json";
    String childJson = H.getAuthenticatedContent(testUserCreds, childGetUrl, HttpTest.CONTENT_TYPE_JSON, null, HttpServletResponse.SC_OK);
    assertNotNull(childJson);
    JsonObject childJsonObj = JsonUtil.parseObject(childJson);
    assertEquals(true, childJsonObj.getBoolean("canDelete"));
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) ArrayList(java.util.ArrayList) JsonObject(javax.json.JsonObject) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) HttpTest(org.apache.sling.commons.testing.integration.HttpTest) Test(org.junit.Test)

Example 53 with JsonObject

use of javax.json.JsonObject in project sling by apache.

the class SimpleDistributionQueueProvider method enableQueueProcessing.

public void enableQueueProcessing(@Nonnull DistributionQueueProcessor queueProcessor, String... queueNames) {
    if (checkpoint) {
        // recover from checkpoints
        log.debug("recovering from checkpoints if needed");
        for (final String queueName : queueNames) {
            log.debug("recovering for queue {}", queueName);
            DistributionQueue queue = getQueue(queueName);
            FilenameFilter filenameFilter = new FilenameFilter() {

                @Override
                public boolean accept(File file, String name) {
                    return name.equals(queueName + "-checkpoint");
                }
            };
            for (File qf : checkpointDirectory.listFiles(filenameFilter)) {
                log.info("recovering from checkpoint {}", qf);
                try {
                    LineIterator lineIterator = IOUtils.lineIterator(new FileReader(qf));
                    while (lineIterator.hasNext()) {
                        String s = lineIterator.nextLine();
                        String[] split = s.split(" ");
                        String id = split[0];
                        String infoString = split[1];
                        Map<String, Object> info = new HashMap<String, Object>();
                        JsonReader reader = Json.createReader(new StringReader(infoString));
                        JsonObject jsonObject = reader.readObject();
                        for (Map.Entry<String, JsonValue> entry : jsonObject.entrySet()) {
                            if (entry.getValue().getValueType().equals(JsonValue.ValueType.ARRAY)) {
                                JsonArray value = jsonObject.getJsonArray(entry.getKey());
                                String[] a = new String[value.size()];
                                for (int i = 0; i < a.length; i++) {
                                    a[i] = value.getString(i);
                                }
                                info.put(entry.getKey(), a);
                            } else if (JsonValue.NULL.equals(entry.getValue())) {
                                info.put(entry.getKey(), null);
                            } else {
                                info.put(entry.getKey(), ((JsonString) entry.getValue()).getString());
                            }
                        }
                        queue.add(new DistributionQueueItem(id, info));
                    }
                    log.info("recovered {} items from queue {}", queue.getStatus().getItemsCount(), queueName);
                } catch (FileNotFoundException e) {
                    log.warn("could not read checkpoint file {}", qf.getAbsolutePath());
                } catch (JsonException e) {
                    log.warn("could not parse info from checkpoint file {}", qf.getAbsolutePath());
                }
            }
        }
        // enable checkpointing
        for (String queueName : queueNames) {
            ScheduleOptions options = scheduler.NOW(-1, 15).canRunConcurrently(false).name(getJobName(queueName + "-checkpoint"));
            scheduler.schedule(new SimpleDistributionQueueCheckpoint(getQueue(queueName), checkpointDirectory), options);
        }
    }
    // enable processing
    for (String queueName : queueNames) {
        ScheduleOptions options = scheduler.NOW(-1, 1).canRunConcurrently(false).name(getJobName(queueName));
        scheduler.schedule(new SimpleDistributionQueueProcessor(getQueue(queueName), queueProcessor), options);
    }
}
Also used : JsonException(javax.json.JsonException) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) FileNotFoundException(java.io.FileNotFoundException) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) LineIterator(org.apache.commons.io.LineIterator) FilenameFilter(java.io.FilenameFilter) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) FileReader(java.io.FileReader) DistributionQueue(org.apache.sling.distribution.queue.DistributionQueue) JsonValue(javax.json.JsonValue) DistributionQueueItem(org.apache.sling.distribution.queue.DistributionQueueItem) JsonArray(javax.json.JsonArray) ScheduleOptions(org.apache.sling.commons.scheduler.ScheduleOptions) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 54 with JsonObject

use of javax.json.JsonObject in project sling by apache.

the class DistributionUtils method getQueueItems.

public static List<Map<String, Object>> getQueueItems(SlingInstance instance, String queueUrl) throws IOException, JsonException {
    List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
    JsonObject json = getResource(instance, queueUrl + ".infinity");
    Iterator<String> keys = json.keySet().iterator();
    while (keys.hasNext()) {
        String key = keys.next();
        JsonObject queueItem;
        Object item = json.get(key);
        if (item instanceof JsonObject) {
            queueItem = (JsonObject) item;
        } else if (item instanceof JsonString) {
            try {
                queueItem = Json.createReader(new StringReader(((JsonString) item).getString())).readObject();
            } catch (JsonException ex) {
                queueItem = null;
            }
        } else {
            queueItem = null;
        }
        if (queueItem != null && queueItem.containsKey("id")) {
            Map<String, Object> itemProperties = new HashMap<String, Object>();
            itemProperties.put("id", queueItem.getString("id"));
            itemProperties.put("paths", JsonUtil.unbox(queueItem.get("paths")));
            itemProperties.put("action", JsonUtil.unbox(queueItem.get("action")));
            itemProperties.put("userid", JsonUtil.unbox(queueItem.get("userid")));
            itemProperties.put("attempts", JsonUtil.unbox(queueItem.get("attempts")));
            itemProperties.put("time", JsonUtil.unbox(queueItem.get("time")));
            itemProperties.put("state", JsonUtil.unbox(queueItem.get("state")));
            result.add(itemProperties);
        }
    }
    return result;
}
Also used : JsonException(javax.json.JsonException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) JsonString(javax.json.JsonString) HashMap(java.util.HashMap) Map(java.util.Map)

Example 55 with JsonObject

use of javax.json.JsonObject in project sling by apache.

the class DistributionUtils method getQueues.

public static Map<String, Map<String, Object>> getQueues(SlingInstance instance, String agentName) throws IOException, JsonException {
    Map<String, Map<String, Object>> result = new HashMap<String, Map<String, Object>>();
    JsonObject json = getResource(instance, queueUrl(agentName) + ".infinity");
    JsonArray items = json.getJsonArray("items");
    for (int i = 0; i < items.size(); i++) {
        String queueName = items.getString(i);
        Map<String, Object> queueProperties = new HashMap<String, Object>();
        JsonObject queue = json.getJsonObject(queueName);
        queueProperties.put("empty", queue.getBoolean("empty"));
        queueProperties.put("itemsCount", queue.getInt("itemsCount"));
        result.put(queueName, queueProperties);
    }
    return result;
}
Also used : JsonArray(javax.json.JsonArray) HashMap(java.util.HashMap) JsonObject(javax.json.JsonObject) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

JsonObject (javax.json.JsonObject)302 Test (org.junit.Test)110 JsonArray (javax.json.JsonArray)97 HashMap (java.util.HashMap)68 StringReader (java.io.StringReader)66 ArrayList (java.util.ArrayList)58 Credentials (org.apache.commons.httpclient.Credentials)52 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)52 JsonString (javax.json.JsonString)50 JsonReader (javax.json.JsonReader)49 NameValuePair (org.apache.commons.httpclient.NameValuePair)43 HttpTest (org.apache.sling.commons.testing.integration.HttpTest)41 HashSet (java.util.HashSet)20 Map (java.util.Map)20 JsonException (javax.json.JsonException)20 JsonObjectBuilder (javax.json.JsonObjectBuilder)20 Response (javax.ws.rs.core.Response)19 LinkedHashMap (java.util.LinkedHashMap)17 JsonValue (javax.json.JsonValue)14 StringWriter (java.io.StringWriter)13