diff --git a/changes.xml b/changes.xml index 104d95c1..c581a354 100644 --- a/changes.xml +++ b/changes.xml @@ -27,6 +27,9 @@ MediaHandlerConfig: Make list of allowed IPE editor types configurable (defaults to "image"). + + DefaultMediaFormatListProvider, MediaFormatValidateServlet: Eliminate usage of org.apache.sling.commons.json. + diff --git a/pom.xml b/pom.xml index 8e544b45..465464f6 100644 --- a/pom.xml +++ b/pom.xml @@ -190,6 +190,23 @@ 1.5.1 test + + com.fasterxml.jackson.core + jackson-databind + test + + + com.jayway.jsonpath + json-path + 2.8.0 + test + + + com.jayway.jsonpath + json-path-assert + 2.8.0 + test + diff --git a/src/main/java/io/wcm/handler/media/format/impl/DefaultMediaFormatListProvider.java b/src/main/java/io/wcm/handler/media/format/impl/DefaultMediaFormatListProvider.java index fd40c1f0..3591ad75 100644 --- a/src/main/java/io/wcm/handler/media/format/impl/DefaultMediaFormatListProvider.java +++ b/src/main/java/io/wcm/handler/media/format/impl/DefaultMediaFormatListProvider.java @@ -20,7 +20,9 @@ package io.wcm.handler.media.format.impl; import java.io.IOException; +import java.util.ArrayList; import java.util.Collections; +import java.util.List; import java.util.Set; import javax.servlet.Servlet; @@ -30,12 +32,14 @@ import org.apache.sling.api.SlingHttpServletResponse; import org.apache.sling.api.servlets.HttpConstants; import org.apache.sling.api.servlets.SlingSafeMethodsServlet; -import org.apache.sling.commons.json.JSONArray; -import org.apache.sling.commons.json.JSONException; -import org.apache.sling.commons.json.JSONObject; import org.jetbrains.annotations.NotNull; import org.osgi.service.component.annotations.Component; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.ObjectMapper; + import io.wcm.handler.media.format.MediaFormat; import io.wcm.handler.media.format.MediaFormatHandler; import io.wcm.wcm.commons.contenttype.ContentType; @@ -50,47 +54,39 @@ "sling.servlet.resourceTypes=sling/servlet/default", "sling.servlet.methods=" + HttpConstants.METHOD_GET }) -@SuppressWarnings("deprecation") public final class DefaultMediaFormatListProvider extends SlingSafeMethodsServlet { private static final long serialVersionUID = 1L; @Override protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response) throws ServletException, IOException { - try { - - // get list of media formats for current medialib path - Set mediaFormats = getMediaFormats(request); - - response.setContentType(ContentType.JSON); - - JSONArray mediaFormatList = new JSONArray(); - - if (mediaFormats != null) { - for (MediaFormat mediaFormat : mediaFormats) { - if (!mediaFormat.isInternal()) { - JSONObject mediaFormatItem = new JSONObject(); - mediaFormatItem.put("name", mediaFormat.getName()); - mediaFormatItem.put("text", mediaFormat.toString()); - mediaFormatItem.put("width", mediaFormat.getWidth()); - mediaFormatItem.put("height", mediaFormat.getHeight()); - mediaFormatItem.put("widthMin", mediaFormat.getMinWidth()); - mediaFormatItem.put("heightMin", mediaFormat.getMinHeight()); - mediaFormatItem.put("widthHeightMin", mediaFormat.getMinWidthHeight()); - mediaFormatItem.put("isImage", mediaFormat.isImage()); - mediaFormatItem.put("ratio", mediaFormat.getRatio()); - mediaFormatItem.put("ratioWidth", mediaFormat.getRatioWidthAsDouble()); - mediaFormatItem.put("ratioHeight", mediaFormat.getRatioHeightAsDouble()); - mediaFormatItem.put("ratioDisplayString", mediaFormat.getRatioDisplayString()); - mediaFormatList.put(mediaFormatItem); - } + // get list of media formats for current medialib path + Set mediaFormats = getMediaFormats(request); + + List mediaFormatList = new ArrayList<>(); + if (mediaFormats != null) { + for (MediaFormat mediaFormat : mediaFormats) { + if (!mediaFormat.isInternal()) { + MediaFormatItem mediaFormatItem = new MediaFormatItem(); + mediaFormatItem.name = mediaFormat.getName(); + mediaFormatItem.text = mediaFormat.toString(); + mediaFormatItem.width = mediaFormat.getWidth(); + mediaFormatItem.height = mediaFormat.getHeight(); + mediaFormatItem.widthMin = mediaFormat.getMinWidth(); + mediaFormatItem.heightMin = mediaFormat.getMinHeight(); + mediaFormatItem.widthHeightMin = mediaFormat.getMinWidthHeight(); + mediaFormatItem.isImage = mediaFormat.isImage(); + mediaFormatItem.ratio = mediaFormat.getRatio(); + mediaFormatItem.ratioWidth = mediaFormat.getRatioWidthAsDouble(); + mediaFormatItem.ratioHeight = mediaFormat.getRatioHeightAsDouble(); + mediaFormatItem.ratioDisplayString = mediaFormat.getRatioDisplayString(); + mediaFormatList.add(mediaFormatItem); } } - - response.getWriter().write(mediaFormatList.toString()); - } - catch (JSONException ex) { - throw new ServletException(ex); } + + // serialize to JSON using Jackson + response.setContentType(ContentType.JSON); + response.getWriter().write(new ObjectMapper().writeValueAsString(mediaFormatList)); } protected Set getMediaFormats(SlingHttpServletRequest request) { @@ -103,4 +99,70 @@ protected Set getMediaFormats(SlingHttpServletRequest request) { } } + @JsonInclude(Include.NON_NULL) + static class MediaFormatItem { + private String name; + private String text; + private long width; + private long height; + private long widthMin; + private long heightMin; + private long widthHeightMin; + private boolean isImage; + private double ratio; + private double ratioWidth; + private double ratioHeight; + private String ratioDisplayString; + + public String getName() { + return this.name; + } + + public String getText() { + return this.text; + } + + public long getWidth() { + return this.width; + } + + public long getHeight() { + return this.height; + } + + public long getWidthMin() { + return this.widthMin; + } + + public long getHeightMin() { + return this.heightMin; + } + + public long getWidthHeightMin() { + return this.widthHeightMin; + } + + @JsonProperty("isImage") + public boolean isImage() { + return this.isImage; + } + + public double getRatio() { + return this.ratio; + } + + public double getRatioWidth() { + return this.ratioWidth; + } + + public double getRatioHeight() { + return this.ratioHeight; + } + + public String getRatioDisplayString() { + return this.ratioDisplayString; + } + + } + } diff --git a/src/main/java/io/wcm/handler/media/impl/MediaFormatValidateServlet.java b/src/main/java/io/wcm/handler/media/impl/MediaFormatValidateServlet.java index 1dc727eb..b2031263 100644 --- a/src/main/java/io/wcm/handler/media/impl/MediaFormatValidateServlet.java +++ b/src/main/java/io/wcm/handler/media/impl/MediaFormatValidateServlet.java @@ -34,8 +34,6 @@ import org.apache.sling.api.SlingHttpServletResponse; import org.apache.sling.api.servlets.HttpConstants; import org.apache.sling.api.servlets.SlingSafeMethodsServlet; -import org.apache.sling.commons.json.JSONException; -import org.apache.sling.commons.json.JSONObject; import org.apache.sling.servlets.annotations.SlingServletResourceTypes; import org.jetbrains.annotations.NotNull; import org.osgi.service.component.annotations.Component; @@ -43,6 +41,9 @@ import com.day.cq.i18n.I18n; import com.day.cq.wcm.api.Page; import com.day.cq.wcm.api.PageManager; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.databind.ObjectMapper; import io.wcm.handler.media.Media; import io.wcm.handler.media.MediaArgs.MediaFormatOption; @@ -63,7 +64,6 @@ selectors = MediaFormatValidateServlet.SELECTOR, resourceTypes = "sling/servlet/default", methods = HttpConstants.METHOD_GET) -@SuppressWarnings("deprecation") public final class MediaFormatValidateServlet extends SlingSafeMethodsServlet { private static final long serialVersionUID = 1L; @@ -112,20 +112,15 @@ protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHtt .build(); // response - try { - JSONObject result = new JSONObject(); - result.put("valid", media.isValid()); - if (!media.isValid()) { - I18n i18n = getI18n(request); - result.put("reason", getI18nText(i18n, getMediaInvalidReasonI18nKeyOrMessage(media))); - result.put("reasonTitle", getI18nText(i18n, ASSET_INVALID_I18N_KEY)); - } - response.setContentType(ContentType.JSON); - response.getWriter().write(result.toString()); - } - catch (JSONException ex) { - throw new ServletException(ex); + ResultResponse result = new ResultResponse(); + result.valid = media.isValid(); + if (!media.isValid()) { + I18n i18n = getI18n(request); + result.reason = getI18nText(i18n, getMediaInvalidReasonI18nKeyOrMessage(media)); + result.reasonTitle = getI18nText(i18n, ASSET_INVALID_I18N_KEY); } + response.setContentType(ContentType.JSON); + response.getWriter().write(new ObjectMapper().writeValueAsString(result)); } private String getMediaInvalidReasonI18nKeyOrMessage(@NotNull Media media) { @@ -157,4 +152,25 @@ private I18n getI18n(SlingHttpServletRequest request) { return new I18n(request); } + @JsonInclude(Include.NON_NULL) + static class ResultResponse { + + private boolean valid; + private String reason; + private String reasonTitle; + + public boolean isValid() { + return this.valid; + } + + public String getReason() { + return this.reason; + } + + public String getReasonTitle() { + return this.reasonTitle; + } + + } + } diff --git a/src/test/java/io/wcm/handler/media/format/impl/DefaultMediaFormatListProviderTest.java b/src/test/java/io/wcm/handler/media/format/impl/DefaultMediaFormatListProviderTest.java index 03077391..c0233ae3 100644 --- a/src/test/java/io/wcm/handler/media/format/impl/DefaultMediaFormatListProviderTest.java +++ b/src/test/java/io/wcm/handler/media/format/impl/DefaultMediaFormatListProviderTest.java @@ -19,13 +19,17 @@ */ package io.wcm.handler.media.format.impl; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasItem; -import org.apache.commons.lang3.StringUtils; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import com.jayway.jsonpath.DocumentContext; +import com.jayway.jsonpath.JsonPath; + import io.wcm.handler.media.testcontext.AppAemContext; import io.wcm.handler.media.testcontext.DummyMediaFormats; import io.wcm.testing.mock.aem.junit5.AemContext; @@ -48,9 +52,11 @@ void testGet() throws Exception { underTest.service(context.request(), context.response()); String response = context.response().getOutputAsString(); - assertTrue(StringUtils.contains(response, "\"" + DummyMediaFormats.EDITORIAL_1COL.getName() + "\"")); - assertTrue(StringUtils.contains(response, "\"" + DummyMediaFormats.EDITORIAL_2COL.getName() + "\"")); - assertTrue(StringUtils.contains(response, "\"" + DummyMediaFormats.EDITORIAL_3COL.getName() + "\"")); + + DocumentContext json = JsonPath.parse(response); + assertThat(json, hasJsonPath("$[*].name", hasItem(DummyMediaFormats.EDITORIAL_1COL.getName()))); + assertThat(json, hasJsonPath("$[*].name", hasItem(DummyMediaFormats.EDITORIAL_2COL.getName()))); + assertThat(json, hasJsonPath("$[*].name", hasItem(DummyMediaFormats.EDITORIAL_3COL.getName()))); } }