use of org.apache.sling.commons.json.JSONArray in project acs-aem-commons by Adobe-Consulting-Services.
the class WCMViewsServlet method doGet.
@Override
protected final void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json");
if (WCMMode.DISABLED.equals(WCMMode.fromRequest(request))) {
response.setStatus(SlingHttpServletResponse.SC_NOT_FOUND);
response.getWriter().write("");
return;
}
/* Valid WCMMode */
final PageManager pageManager = request.getResourceResolver().adaptTo(PageManager.class);
final Page page = pageManager.getContainingPage(request.getResource());
final WCMViewsResourceVisitor visitor = new WCMViewsResourceVisitor();
visitor.accept(page.getContentResource());
final Set<String> viewSet = new HashSet<String>(visitor.getWCMViews());
// Get the Views provided by the Servlet
for (final Map.Entry<String, String[]> entry : this.defaultViews.entrySet()) {
if (StringUtils.startsWith(page.getPath(), entry.getKey())) {
viewSet.addAll(Arrays.asList(entry.getValue()));
}
}
final List<String> views = new ArrayList<String>(viewSet);
Collections.sort(views);
log.debug("Collected WCM Views {} for Page [ {} ]", views, page.getPath());
final JSONArray jsonArray = new JSONArray();
for (final String view : views) {
final JSONObject json = new JSONObject();
try {
json.put("title", StringUtils.capitalize(view) + " View");
json.put("value", view);
jsonArray.put(json);
} catch (JSONException e) {
log.error("Unable to build WCM Views JSON output.", e);
}
}
response.getWriter().write(jsonArray.toString());
}
use of org.apache.sling.commons.json.JSONArray in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class ImageImpl method buildJson.
private void buildJson() {
Map<String, Object> objectMap = new HashMap<>();
objectMap.put(Image.JSON_SMART_SIZES, new JSONArray(Arrays.asList(ArrayUtils.toObject(smartSizes))));
objectMap.put(Image.JSON_SMART_IMAGES, new JSONArray(Arrays.asList(smartImages)));
objectMap.put(Image.JSON_LAZY_ENABLED, !disableLazyLoading);
json = new JSONObject(objectMap).toString();
}
use of org.apache.sling.commons.json.JSONArray in project sling by apache.
the class JSONAssert method assertEquals.
/**
* Asserts that two JSONObjects are equal.
*/
public static void assertEquals(String message, JSONObject expected, JSONObject actual) throws JSONException {
String header = message == null ? "" : message + ": ";
if (expected == null) {
fail(header + "expected object was null");
}
if (actual == null) {
fail(header + "actual object was null");
}
if (expected == actual) /* || expected.equals( actual ) */
{
return;
}
JSONArray expectedNames = expected.names();
JSONArray actualNames = actual.names();
if (expectedNames == null && actualNames == null) {
return;
}
if (expectedNames == null) {
expectedNames = new JSONArray();
}
if (actualNames == null) {
actualNames = new JSONArray();
}
assertEquals(header + "names sizes differed, expected.names().length()=" + expectedNames.length() + " actual.names().length()=" + actualNames.length(), expectedNames.length(), actualNames.length());
for (Iterator<String> keys = expected.keys(); keys.hasNext(); ) {
String key = keys.next();
Object o1 = expected.opt(key);
Object o2 = actual.opt(key);
if (JSONObject.NULL.equals(o1)) {
if (JSONObject.NULL.equals(o2)) {
continue;
}
fail(header + "objects differed at key [" + key + "];");
} else {
if (JSONObject.NULL.equals(o2)) {
fail(header + "objects differed at key [" + key + "];");
}
}
if (o1 instanceof JSONObject && o2 instanceof JSONObject) {
assertEquals(header + "objects differed at key [" + key + "];", (JSONObject) o1, (JSONObject) o2);
} else if (o1 instanceof JSONArray && o2 instanceof JSONArray) {
assertEquals(header + "objects differed at key [" + key + "];", (JSONArray) o1, (JSONArray) o2);
} else if (o1 instanceof String) {
assertEquals(header + "objects differed at key [" + key + "];", (String) o1, String.valueOf(o2));
} else if (o2 instanceof String) {
assertEquals(header + "objects differed at key [" + key + "];", String.valueOf(o1), (String) o2);
} else {
assertEquals(header + "objects differed at key [" + key + "];", o1, o2);
}
}
}
use of org.apache.sling.commons.json.JSONArray in project sling by apache.
the class JSONAssert method assertEquals.
/**
* Asserts that two JSONArrays are equal.
*/
public static void assertEquals(String message, JSONArray expected, JSONArray actual) throws JSONException {
String header = message == null ? "" : message + ": ";
if (expected == null) {
fail(header + "expected array was null");
}
if (actual == null) {
fail(header + "actual array was null");
}
if (expected == actual || expected.equals(actual)) {
return;
}
if (actual.length() != expected.length()) {
fail(header + "arrays sizes differed, expected.length()=" + expected.length() + " actual.length()=" + actual.length());
}
int max = expected.length();
for (int i = 0; i < max; i++) {
Object o1 = expected.get(i);
Object o2 = actual.get(i);
// handle nulls
if (JSONObject.NULL.equals(o1)) {
if (JSONObject.NULL.equals(o2)) {
continue;
}
fail(header + "arrays first differed at element [" + i + "];");
} else {
if (JSONObject.NULL.equals(o2)) {
fail(header + "arrays first differed at element [" + i + "];");
}
}
if (o1 instanceof JSONArray && o2 instanceof JSONArray) {
JSONArray e = (JSONArray) o1;
JSONArray a = (JSONArray) o2;
assertEquals(header + "arrays first differed at element " + i + ";", e, a);
} else if (o1 instanceof JSONObject && o2 instanceof JSONObject) {
assertEquals(header + "arrays first differed at element [" + i + "];", (JSONObject) o1, (JSONObject) o2);
} else if (o1 instanceof String) {
assertEquals(header + "arrays first differed at element [" + i + "];", (String) o1, String.valueOf(o2));
} else if (o2 instanceof String) {
assertEquals(header + "arrays first differed at element [" + i + "];", String.valueOf(o1), (String) o2);
} else {
assertEquals(header + "arrays first differed at element [" + i + "];", o1, o2);
}
}
}
use of org.apache.sling.commons.json.JSONArray in project acs-aem-commons by Adobe-Consulting-Services.
the class OptionsServletTest method testWithNormalType.
@Test
public void testWithNormalType() throws Exception {
Map<String, ClientLibrary> libraries = new HashMap<String, ClientLibrary>();
String jsOnlyCategory1 = RandomStringUtils.randomAlphanumeric(5);
String jsOnlyCategory2 = RandomStringUtils.randomAlphanumeric(5);
String bothCategory1 = RandomStringUtils.randomAlphanumeric(5);
String bothCategory2 = RandomStringUtils.randomAlphanumeric(5);
addLibrary(libraries, RandomStringUtils.random(10), new String[] { "js" }, new String[] { jsOnlyCategory1, jsOnlyCategory2 });
addLibrary(libraries, RandomStringUtils.random(10), new String[] { "js" }, new String[] { jsOnlyCategory2 });
addLibrary(libraries, RandomStringUtils.random(10), new String[] { "js", "css" }, new String[] { bothCategory1, bothCategory2 });
addLibrary(libraries, RandomStringUtils.random(10), new String[] { "js", "css" }, new String[] { bothCategory2 });
when(manager.getLibraries()).thenReturn(libraries);
MockSlingHttpServletRequest request = new MockSlingHttpServletRequest("/apps/acs-commons/components/utilities/designer/clientlibsmanager/options", "js", "json", null, null);
MockSlingHttpServletResponse response = new MockSlingHttpServletResponse();
servlet.doGet(request, response);
assertEquals("application/json", response.getContentType());
JSONArray array = new JSONArray(response.getOutput().toString());
assertEquals(4, array.length());
}
Aggregations