Search in sources :

Example 6 with JSONObject

use of org.apache.sling.commons.json.JSONObject in project sling by apache.

the class NodeTypesJSONServlet method doGet.

@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    response.setContentType("application/json; charset=UTF-8");
    PrintWriter writer = response.getWriter();
    Resource resource = request.getResource();
    Node currentNode = resource.adaptTo(Node.class);
    try {
        NodeTypeIterator nodeTypeIterator = currentNode.getSession().getWorkspace().getNodeTypeManager().getAllNodeTypes();
        JSONObject nodeTypes = new JSONObject();
        while (nodeTypeIterator.hasNext()) {
            NodeType nodeType = nodeTypeIterator.nextNodeType();
            if (nodeType.getName() != null) {
                JSONNodeType jsonNodeType = new JSONNodeType(nodeType);
                nodeTypes.put(nodeType.getName(), jsonNodeType.getJson());
            }
        }
        writer.println(nodeTypes.toString(2));
        writer.flush();
        writer.close();
    } catch (RepositoryException e) {
        log.error("Could not generate the node types.", e);
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    } catch (JSONException e) {
        log.error("Could not generate the node types.", e);
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject) Node(javax.jcr.Node) NodeType(javax.jcr.nodetype.NodeType) Resource(org.apache.sling.api.resource.Resource) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) JSONException(org.apache.sling.commons.json.JSONException) RepositoryException(javax.jcr.RepositoryException) PrintWriter(java.io.PrintWriter)

Example 7 with JSONObject

use of org.apache.sling.commons.json.JSONObject in project sling by apache.

the class MockNodeTypeGenerator method assertEqualsWithServletResult.

public void assertEqualsWithServletResult(String filename) throws JSONException, ServletException, IOException {
    NodeTypesJSONServlet generationServlet = new NodeTypesJSONServlet();
    generationServlet.service(request, response);
    verify(response, never()).sendError(anyInt());
    String resultingJSON = new String(outStream.toByteArray(), "utf-8");
    String expectedNTJSON = getExpectedNTJSON(filename);
    JSONObject actualJsonObjectFromServlet = new JSONObject(new JSONTokener(resultingJSON));
    JSONAssert.assertEquals("Actual JSON: " + resultingJSON + "\nExpected JSON: " + expectedNTJSON, new JSONObject(new JSONTokener(expectedNTJSON)), actualJsonObjectFromServlet);
}
Also used : JSONTokener(org.apache.sling.commons.json.JSONTokener) JSONObject(org.apache.sling.commons.json.JSONObject) NodeTypesJSONServlet(org.apache.sling.jcr.js.nodetypes.NodeTypesJSONServlet)

Example 8 with JSONObject

use of org.apache.sling.commons.json.JSONObject in project acs-aem-commons by Adobe-Consulting-Services.

the class PackageHelperImplTest method testGetPathFilterSetPreviewJSON.

@Test
public void testGetPathFilterSetPreviewJSON() throws Exception {
    final List<PathFilterSet> pathFilterSets = new ArrayList<PathFilterSet>();
    pathFilterSets.add(new PathFilterSet("/a/b/c"));
    pathFilterSets.add(new PathFilterSet("/d/e/f"));
    pathFilterSets.add(new PathFilterSet("/g/h/i"));
    final String actual = packageHelper.getPathFilterSetPreviewJSON(pathFilterSets);
    final JSONObject json = new JSONObject(actual);
    assertEquals("preview", json.getString("status"));
    assertEquals("Not applicable (Preview)", json.getString("path"));
    final String[] expectedFilterSets = new String[] { "/a/b/c", "/d/e/f", "/g/h/i" };
    JSONArray actualArray = json.getJSONArray("filterSets");
    for (int i = 0; i < actualArray.length(); i++) {
        JSONObject tmp = actualArray.getJSONObject(i);
        assertTrue(ArrayUtils.contains(expectedFilterSets, tmp.get("rootPath")));
    }
    assertEquals(expectedFilterSets.length, actualArray.length());
}
Also used : PathFilterSet(org.apache.jackrabbit.vault.fs.api.PathFilterSet) JSONObject(org.apache.sling.commons.json.JSONObject) ArrayList(java.util.ArrayList) JSONArray(org.apache.sling.commons.json.JSONArray) Test(org.junit.Test)

Example 9 with JSONObject

use of org.apache.sling.commons.json.JSONObject in project acs-aem-commons by Adobe-Consulting-Services.

the class PackageHelperImplTest method testGetErrorJSON.

@Test
public void testGetErrorJSON() throws Exception {
    final String expectedMessage = "This is a test error message!";
    final String actual = packageHelper.getErrorJSON(expectedMessage);
    final JSONObject json = new JSONObject(actual);
    assertEquals("error", json.getString("status"));
    assertEquals(expectedMessage, json.getString("msg"));
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject) Test(org.junit.Test)

Example 10 with JSONObject

use of org.apache.sling.commons.json.JSONObject in project acs-aem-commons by Adobe-Consulting-Services.

the class PackageHelperImplTest method testGetSuccessJSON.

@Test
public void testGetSuccessJSON() throws Exception {
    final String actual = packageHelper.getSuccessJSON(packageOne);
    final JSONObject json = new JSONObject(actual);
    assertEquals("success", json.getString("status"));
    assertEquals("/etc/packages/testGroup/testPackageName-1.0.0.zip", json.getString("path"));
    final String[] expectedFilterSets = new String[] { "/a/b/c", "/d/e/f", "/g/h/i" };
    JSONArray actualArray = json.getJSONArray("filterSets");
    for (int i = 0; i < actualArray.length(); i++) {
        JSONObject tmp = actualArray.getJSONObject(i);
        assertTrue(ArrayUtils.contains(expectedFilterSets, tmp.get("rootPath")));
    }
    assertEquals(expectedFilterSets.length, actualArray.length());
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject) JSONArray(org.apache.sling.commons.json.JSONArray) Test(org.junit.Test)

Aggregations

JSONObject (org.apache.sling.commons.json.JSONObject)74 JSONArray (org.apache.sling.commons.json.JSONArray)22 Test (org.junit.Test)20 JSONException (org.apache.sling.commons.json.JSONException)16 HashMap (java.util.HashMap)15 ValueMap (org.apache.sling.api.resource.ValueMap)9 Map (java.util.Map)8 ArrayList (java.util.ArrayList)6 ServletException (javax.servlet.ServletException)6 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)6 Resource (org.apache.sling.api.resource.Resource)6 RepositoryException (javax.jcr.RepositoryException)5 Calendar (java.util.Calendar)4 ValueMapDecorator (org.apache.sling.api.wrappers.ValueMapDecorator)4 Config (com.adobe.acs.commons.workflow.bulk.execution.model.Config)3 IOException (java.io.IOException)3 LinkedHashMap (java.util.LinkedHashMap)3 Session (javax.jcr.Session)3 ModifiableValueMapDecorator (org.apache.sling.api.wrappers.ModifiableValueMapDecorator)3 Form (com.adobe.acs.commons.forms.Form)2