Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert committed Dec 18, 2023
2 parents 3d7090d + e5ed8d9 commit 602c003
Show file tree
Hide file tree
Showing 17 changed files with 285 additions and 73 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/maven-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
java: [11, 17]
java: [11, 17, 21]
os: [ubuntu-latest]
distribution: [temurin]

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/maven-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Configure GIT
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release-from-tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
permissions:
contents: write
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: ncipollo/release-action@v1
with:
body: 'Changes: https://wcm.io/handler/media/changes-report.html'
Expand Down
9 changes: 9 additions & 0 deletions changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@
xsi:schemaLocation="http://maven.apache.org/changes/1.0.0 http://maven.apache.org/plugins/maven-changes-plugin/xsd/changes-1.0.0.xsd">
<body>

<release version="1.16.0" date="2023-12-18">
<action type="add" dev="sseifert" issue="14">
MediaHandlerConfig: Make list of allowed IPE editor types configurable (defaults to "image").
</action>
<action type="update" dev="sseifert" issue="26">
DefaultMediaFormatListProvider, MediaFormatValidateServlet: Eliminate usage of org.apache.sling.commons.json.
</action>
</release>

<release version="1.15.8" date="2023-09-08">
<action type="update" dev="joerghoh" issue="22">
DAM Renditions: Read width/height of rendition lazy, as this can be expensive when not configured properly or Asset metadata is missing for other reasons.
Expand Down
27 changes: 22 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
<parent>
<groupId>io.wcm</groupId>
<artifactId>io.wcm.parent_toplevel</artifactId>
<version>2.2.2</version>
<version>2.2.8</version>
<relativePath/>
</parent>

<groupId>io.wcm</groupId>
<artifactId>io.wcm.handler.media</artifactId>
<version>1.15.8</version>
<version>1.16.0</version>
<packaging>jar</packaging>

<name>Media Handler</name>
Expand All @@ -49,7 +49,7 @@
<site.url.module.prefix>handler/media</site.url.module.prefix>

<!-- Enable reproducible builds -->
<project.build.outputTimestamp>2023-09-08T14:08:30Z</project.build.outputTimestamp>
<project.build.outputTimestamp>2023-12-18T11:18:37Z</project.build.outputTimestamp>
</properties>

<dependencies>
Expand Down Expand Up @@ -138,7 +138,7 @@
<dependency>
<groupId>io.wcm</groupId>
<artifactId>io.wcm.testing.aem-mock.junit5</artifactId>
<version>5.3.0</version>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -187,7 +187,24 @@
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>1.5.0</version>
<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>

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,41 @@
"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;

private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

@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(OBJECT_MAPPER.writeValueAsString(mediaFormatList));
}

protected Set<MediaFormat> getMediaFormats(SlingHttpServletRequest request) {
Expand All @@ -103,4 +101,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 @@ -356,6 +356,7 @@ private static MediaComponentPropertyResolver getMediaComponentPropertyResolver(
}

@Override
@SuppressWarnings("PMD.OptimizableToArrayCall")
public @NotNull Media build() {
if (!pictureSourceSets.isEmpty()) {
this.mediaArgs.pictureSources(pictureSourceSets.toArray(new PictureSource[pictureSourceSets.size()]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@
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;

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;
Expand All @@ -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;

Expand All @@ -74,6 +74,8 @@ public final class MediaFormatValidateServlet extends SlingSafeMethodsServlet {
static final String RP_MEDIA_CROPAUTO = "mediaCropAuto";
static final String RP_MEDIA_REF = "mediaRef";

private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

/**
* Prefix for i18n keys to generated messages for media invalid reasons.
*/
Expand Down Expand Up @@ -112,20 +114,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(OBJECT_MAPPER.writeValueAsString(result));
}

private String getMediaInvalidReasonI18nKeyOrMessage(@NotNull Media media) {
Expand Down Expand Up @@ -157,4 +154,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;
}

}

}
Loading

0 comments on commit 602c003

Please sign in to comment.