Skip to content

Commit

Permalink
support additional metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert committed Nov 26, 2024
1 parent 8ea027b commit 6908da9
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 36 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
<dependency>
<groupId>io.wcm</groupId>
<artifactId>io.wcm.testing.aem-mock.junit5</artifactId>
<version>5.6.2</version>
<version>5.6.3-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,17 @@ public long expireAfterRead(String key, AccessTokenResponse value, long currentT

@SuppressWarnings("null")
private @Nullable AccessTokenResponse processResponse(@NotNull CloseableHttpResponse response) throws IOException {
switch (response.getStatusLine().getStatusCode()) {
case HttpStatus.SC_OK:
String jsonResponse = EntityUtils.toString(response.getEntity());
AccessTokenResponse accessTokenResponse = OBJECT_MAPPER.readValue(jsonResponse, AccessTokenResponse.class);
log.trace("HTTP response for access token reqeust from {} returned a response, expires in {} sec",
imsTokenApiUrl, accessTokenResponse.expiresInSec);
return accessTokenResponse;
default:
log.warn("Unexpected HTTP response for access token request from {}: {}", imsTokenApiUrl, response.getStatusLine());
break;
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String jsonResponse = EntityUtils.toString(response.getEntity());
AccessTokenResponse accessTokenResponse = OBJECT_MAPPER.readValue(jsonResponse, AccessTokenResponse.class);
log.trace("HTTP response for access token reqeust from {} returned a response, expires in {} sec",
imsTokenApiUrl, accessTokenResponse.expiresInSec);
return accessTokenResponse;
}
else {
log.warn("Unexpected HTTP response for access token request from {}: {}", imsTokenApiUrl, response.getStatusLine());
return null;
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
final class MetadataResponse {

public RepositoryMetadata repositoryMetadata;
public AssetMetadata assetMetadata;
public Map<String, Object> assetMetadata;

@JsonIgnoreProperties(ignoreUnknown = true)
static final class RepositoryMetadata {
Expand All @@ -53,14 +53,4 @@ static final class SmartCrop {
public double normalizedHeight;
}

@JsonIgnoreProperties(ignoreUnknown = true)
static final class AssetMetadata {
@JsonProperty("dam:assetStatus")
public String assetStatus;
@JsonProperty("tiff:ImageWidth")
public long tiffImageWidth;
@JsonProperty("tiff:ImageLength")
public long tiffImageLength;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,22 @@

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.wrappers.ValueMapDecorator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.json.JsonMapper;

import io.wcm.handler.media.Dimension;
import io.wcm.handler.mediasource.ngdm.impl.metadata.MetadataResponse.AssetMetadata;
import io.wcm.handler.mediasource.ngdm.impl.metadata.MetadataResponse.RepositoryMetadata;
import io.wcm.wcm.commons.contenttype.ContentType;

Expand All @@ -46,16 +48,23 @@ public final class NextGenDynamicMediaMetadata {
private final String mimeType;
private final Dimension dimension;
private final String assetStatus;
private final ValueMap properties;
private final List<SmartCrop> smartCrops;

private static final JsonMapper OBJECT_MAPPER = new JsonMapper();
static final String RT_RENDITION_SMARTCROP = "dam/rendition/smartcrop";

NextGenDynamicMediaMetadata(@Nullable String mimeType, @Nullable Dimension dimension,
@Nullable String assetStatus, @Nullable List<SmartCrop> smartCrops) {
@Nullable String assetStatus, @Nullable ValueMap properties, @Nullable List<SmartCrop> smartCrops) {
this.mimeType = mimeType;
this.dimension = dimension;
this.assetStatus = assetStatus;
if (properties != null) {
this.properties = properties;
}
else {
this.properties = ValueMap.EMPTY;
}
if (smartCrops != null) {
this.smartCrops = smartCrops;
}
Expand Down Expand Up @@ -85,6 +94,13 @@ public String getAssetStatus() {
return this.assetStatus;
}

/**
* @return Asset properties
*/
public ValueMap getProperties() {
return properties;
}

/**
* @return Named smart crop definitions.
*/
Expand Down Expand Up @@ -114,15 +130,17 @@ public String toString() {
public static @NotNull NextGenDynamicMediaMetadata fromJson(@NotNull String jsonResponse) throws JsonProcessingException {
MetadataResponse response = OBJECT_MAPPER.readValue(jsonResponse, MetadataResponse.class);
RepositoryMetadata respositoryMetadata = response.repositoryMetadata;
AssetMetadata assetMetadata = response.assetMetadata;
Map<String, Object> assetMetadata = response.assetMetadata;
ValueMap properties = null;

long width = 0;
long height = 0;
String assetStatus = null;
if (assetMetadata != null) {
width = assetMetadata.tiffImageWidth;
height = assetMetadata.tiffImageLength;
assetStatus = assetMetadata.assetStatus;
properties = new ValueMapDecorator(assetMetadata);
width = properties.get("tiff:ImageWidth", 0L);
height = properties.get("tiff:ImageLength", 0L);
assetStatus = properties.get("dam:assetStatus", String.class);
}
Dimension dimension = toDimension(width, height);

Expand All @@ -138,7 +156,7 @@ public String toString() {
}
}

return new NextGenDynamicMediaMetadata(mimeType, dimension, assetStatus, smartCrops);
return new NextGenDynamicMediaMetadata(mimeType, dimension, assetStatus, properties, smartCrops);
}

private static @Nullable Dimension toDimension(long width, long height) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import static io.wcm.handler.mediasource.ngdm.impl.metadata.MetadataSample.METADATA_JSON_IMAGE_FULL;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

import org.apache.http.HttpStatus;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -117,6 +118,8 @@ void testValidToken() {
assertEquals(1500, dimension.getWidth());
assertEquals(900, dimension.getHeight());
assertEquals("image/jpeg", metadata.getMimeType());
assertEquals("Test Image", metadata.getProperties().get("dc:title", String.class));
assertEquals("Test Description", metadata.getProperties().get("dc:description", String.class));
}

@Test
Expand All @@ -136,6 +139,8 @@ void testInvalidToken_FallbackNoAuth() {
assertEquals(1200, dimension.getWidth());
assertEquals(800, dimension.getHeight());
assertEquals("image/jpeg", metadata.getMimeType());
assertNull(metadata.getProperties().get("dc:title", String.class));
assertNull(metadata.getProperties().get("dc:description", String.class));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import static com.day.cq.dam.api.DamConstants.ASSET_STATUS_APPROVED;
import static io.wcm.handler.mediasource.ngdm.impl.metadata.MetadataSample.METADATA_JSON_IMAGE;
import static io.wcm.handler.mediasource.ngdm.impl.metadata.MetadataSample.METADATA_JSON_IMAGE_FULL;
import static io.wcm.handler.mediasource.ngdm.impl.metadata.MetadataSample.METADATA_JSON_PDF;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand All @@ -46,7 +47,6 @@ void testEmptyJson() throws JsonProcessingException {
assertNull(metadata.getAssetStatus());
assertTrue(metadata.getSmartCrops().isEmpty());
assertFalse(metadata.isValid());
assertEquals("[assetStatus=<null>,dimension=<null>,mimeType=<null>,smartCrops=[]]", metadata.toString());
}

@Test
Expand All @@ -60,10 +60,21 @@ void testSampleJson_Image() throws JsonProcessingException {
assertEquals(ASSET_STATUS_APPROVED, metadata.getAssetStatus());
assertEquals(2, metadata.getSmartCrops().size());
assertTrue(metadata.isValid());
assertEquals("[assetStatus=approved,dimension=[width=1200,height=800],mimeType=image/jpeg,smartCrops=["
+ "[cropDimension=[left=0,top=462,width=1200,height=675],name=Landscape,ratio=1.7777777777777777], "
+ "[cropDimension=[left=202,top=0,width=399,height=798],name=Portrait,ratio=0.5]"
+ "]]", metadata.toString());
}

@Test
void testSampleJson_Image_Full() throws JsonProcessingException {
NextGenDynamicMediaMetadata metadata = NextGenDynamicMediaMetadata.fromJson(METADATA_JSON_IMAGE_FULL);
assertEquals(ContentType.JPEG, metadata.getMimeType());
Dimension dimension = metadata.getDimension();
assertNotNull(dimension);
assertEquals(1500, dimension.getWidth());
assertEquals(900, dimension.getHeight());
assertEquals(ASSET_STATUS_APPROVED, metadata.getAssetStatus());
assertEquals("Test Image", metadata.getProperties().get("dc:title", String.class));
assertEquals("Test Description", metadata.getProperties().get("dc:description", String.class));
assertEquals(0, metadata.getSmartCrops().size());
assertTrue(metadata.isValid());
}

@Test
Expand All @@ -74,7 +85,6 @@ void testSampleJson_PDF() throws JsonProcessingException {
assertEquals(ASSET_STATUS_APPROVED, metadata.getAssetStatus());
assertTrue(metadata.getSmartCrops().isEmpty());
assertTrue(metadata.isValid());
assertEquals("[assetStatus=approved,dimension=<null>,mimeType=application/pdf,smartCrops=[]]", metadata.toString());
}

@Test
Expand Down

0 comments on commit 6908da9

Please sign in to comment.