Skip to content

Commit

Permalink
eliminate usage of org.apache.sling.commons.json
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert committed Dec 15, 2023
1 parent 80d7f44 commit 2b10220
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 41 deletions.
17 changes: 17 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,23 @@
<version>1.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path-assert</artifactId>
<version>2.8.0</version>
<scope>test</scope>
</dependency>

<!-- Java ImageIO Support for TIFF -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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<MediaFormat> 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<MediaFormat> mediaFormats = getMediaFormats(request);

List<MediaFormatItem> 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<MediaFormat> getMediaFormats(SlingHttpServletRequest request) {
Expand All @@ -103,4 +99,70 @@ protected Set<MediaFormat> 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;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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())));
}

}

0 comments on commit 2b10220

Please sign in to comment.