Skip to content

Commit

Permalink
[Image] Finalize image v3 component (#1686)
Browse files Browse the repository at this point in the history
  • Loading branch information
comanV committed Aug 10, 2021
1 parent 31acfe0 commit b5860a2
Show file tree
Hide file tree
Showing 114 changed files with 594 additions and 1,320 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import javax.annotation.PostConstruct;

import com.adobe.cq.wcm.core.components.internal.servlets.DMAssetPostProcessor;
import com.fasterxml.jackson.annotation.JsonInclude;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
Expand Down Expand Up @@ -304,6 +305,7 @@ public int getLazyThreshold() {
}

@Override
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public List<ImageArea> getAreas() {
if (areas == null) {
return Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,20 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
package com.adobe.cq.wcm.core.components.internal.models.v3;

import java.awt.*;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

import javax.annotation.PostConstruct;

import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.models.annotations.Exporter;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.InjectionStrategy;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
Expand All @@ -34,12 +38,16 @@
import com.adobe.cq.export.json.ExporterConstants;
import com.adobe.cq.wcm.core.components.commons.link.Link;
import com.adobe.cq.wcm.core.components.internal.models.v2.ImageAreaImpl;
import com.adobe.cq.wcm.core.components.internal.servlets.EnhancedRendition;
import com.adobe.cq.wcm.core.components.models.Image;
import com.adobe.cq.wcm.core.components.models.ImageArea;
import com.day.cq.commons.DownloadResource;
import com.day.cq.dam.api.Asset;
import com.fasterxml.jackson.annotation.JsonIgnore;

import static com.adobe.cq.wcm.core.components.internal.Utils.getWrappedImageResourceWithInheritance;


@Model(adaptables = SlingHttpServletRequest.class, adapters = {Image.class, ComponentExporter.class}, resourceType = ImageImpl.RESOURCE_TYPE)
@Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION)
public class ImageImpl extends com.adobe.cq.wcm.core.components.internal.models.v2.ImageImpl implements Image {
Expand All @@ -48,13 +56,13 @@ public class ImageImpl extends com.adobe.cq.wcm.core.components.internal.models.

private static final Logger LOGGER = LoggerFactory.getLogger(ImageImpl.class);

@ValueMapValue(name = "width", injectionStrategy = InjectionStrategy.OPTIONAL)
@Nullable
protected String width;

@ValueMapValue(name = "height", injectionStrategy = InjectionStrategy.OPTIONAL)
@Nullable
protected String height;
@PostConstruct
protected void initModel() {
super.initModel();
if (hasContent) {
disableLazyLoading = currentStyle.get(PN_DESIGN_LAZY_LOADING_ENABLED, false);
}
}

@Override
@Nullable
Expand All @@ -77,8 +85,8 @@ protected ImageArea newImageArea(String shape, String coordinates, String relati

@Override
public String getSrcset() {
int[] widthsArray = super.getWidths();
String srcUritemplate = super.getSrcUriTemplate();
int[] widthsArray = getWidths();
String srcUritemplate = getSrcUriTemplate();
String[] srcsetArray = new String[widthsArray.length];
if (widthsArray.length > 0 && srcUritemplate != null) {
String srcUriTemplateDecoded = "";
Expand All @@ -103,18 +111,89 @@ public String getSrcset() {

@Nullable
@Override
public String getWidth() {
return width;
@JsonIgnore
public String getHeight () {
int height = getOriginalDimension().height;
if (height > 0) {
return String.valueOf(height);
}
return null;
}

@Nullable
@Override
public String getHeight() {
return height;
@JsonIgnore
public String getWidth () {
int width = getOriginalDimension().width;
if (width > 0) {
return String.valueOf(width);
}
return null;
}

@Override
@JsonIgnore
public String getSrcUriTemplate() {
return super.getSrcUriTemplate();
}

@Override
@JsonIgnore
@Deprecated
public int getLazyThreshold() {
return 0;
}

@Override
@JsonIgnore
public int @NotNull [] getWidths() {
return super.getWidths();
}

@Override
@JsonIgnore
public boolean isDmImage() {
return super.isDmImage();
}

@Override
@JsonIgnore
@Deprecated
public List<ImageArea> getAreas() {
return super.getAreas();
}

@Override
protected void initResource() {
resource = getWrappedImageResourceWithInheritance(resource, linkHandler);
}

private Dimension getOriginalDimension() {
ValueMap inheritedResourceProperties = resource.getValueMap();
String inheritedFileReference = inheritedResourceProperties.get(DownloadResource.PN_REFERENCE, String.class);
Asset asset;
String resizeWidth = currentStyle.get(PN_DESIGN_RESIZE_WIDTH, String.class);
if (StringUtils.isNotEmpty(inheritedFileReference)) {
final Resource assetResource = request.getResourceResolver().getResource(inheritedFileReference);
if (assetResource != null) {
asset = assetResource.adaptTo(Asset.class);
EnhancedRendition original = null;
if (asset != null) {
original = new EnhancedRendition(asset.getOriginal());
}
if (original != null) {
Dimension dimension = original.getDimension();
if (dimension != null) {
if (resizeWidth != null && Integer.parseInt(resizeWidth) > 0 && Integer.parseInt(resizeWidth) < dimension.getWidth()) {
int calculatedHeight = (int) Math.round(Integer.parseInt(resizeWidth) * (dimension.getHeight() / (float)dimension.getWidth()));
return new Dimension(Integer.parseInt(resizeWidth), calculatedHeight);
}
return dimension;
}
}
}
}
return new Dimension(0, 0);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public class AdaptiveImageServlet extends SlingSafeMethodsServlet {
private static final String SELECTOR_WIDTH_KEY = "width";
private int defaultResizeWidth;
private int maxInputWidth;

private AdaptiveImageServletMetrics metrics;

@SuppressFBWarnings(justification = "This field needs to be transient")
Expand All @@ -115,7 +115,7 @@ public class AdaptiveImageServlet extends SlingSafeMethodsServlet {
@SuppressFBWarnings(justification = "This field needs to be transient")
private transient AssetStore assetStore;

public AdaptiveImageServlet(MimeTypeService mimeTypeService, AssetStore assetStore, AdaptiveImageServletMetrics metrics,
public AdaptiveImageServlet(MimeTypeService mimeTypeService, AssetStore assetStore, AdaptiveImageServletMetrics metrics,
int defaultResizeWidth, int maxInputWidth) {
this.mimeTypeService = mimeTypeService;
this.assetStore = assetStore;
Expand Down Expand Up @@ -767,7 +767,7 @@ private List<String> selectorToList(String selector) throws IllegalArgumentExcep
*/
private Map<String, Integer> getTransformationMap(List<String> selectorList, Resource component) throws IllegalArgumentException {
Map<String, Integer> selectorParameterMap = new HashMap<>();
int width = this.defaultResizeWidth;
int width = this.getResizeWidth(component) > 0 ? this.getResizeWidth(component) : this.defaultResizeWidth;
if (selectorList.size() > 1) {
String widthString = (selectorList.size() > 2 ? selectorList.get(2) : selectorList.get(1));
try {
Expand Down Expand Up @@ -832,7 +832,8 @@ private List<Integer> getAllowedRenditionWidths(@NotNull Resource imageResource)
}
}
if (list.isEmpty()) {
list.add(this.defaultResizeWidth);
int width = this.getResizeWidth(imageResource) > 0 ? this.getResizeWidth(imageResource) : this.defaultResizeWidth;
list.add(width);
}
return list;
}
Expand All @@ -853,6 +854,22 @@ private Integer getAllowedJpegQuality(@NotNull Resource imageResource) {
return allowedJpegQuality;
}

/**
* Returns the allowed resize width from this component's content policy.
*
* @param imageResource the resource identifying the accessed image component
* @return the resize width or 0 if the component doesn't have a content policy or doesn't have this policy property set to an Integer.
*/
private int getResizeWidth(@NotNull Resource imageResource){
int allowedResizeWidth = 0;
ContentPolicy contentPolicy = getContentPolicy(imageResource);
if (contentPolicy != null) {
allowedResizeWidth = contentPolicy.getProperties()
.get(Image.PN_DESIGN_RESIZE_WIDTH, 0);
}
return allowedResizeWidth;
}

private long getRequestLastModifiedSuffix(@Nullable String suffix) {
long requestLastModified = 0;
if (StringUtils.isNotEmpty(suffix) && suffix.contains(".")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
/**
* A {@link Rendition} delegate that facilitates finding out the rendition's dimensions.
*/
class EnhancedRendition {
public class EnhancedRendition {

private static final Logger LOG = LoggerFactory.getLogger(EnhancedRendition.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import org.jetbrains.annotations.NotNull;
import org.osgi.annotation.versioning.ConsumerType;

import com.adobe.cq.wcm.core.components.models.datalayer.ImageData;
import com.adobe.cq.wcm.core.components.commons.link.Link;
import com.adobe.cq.wcm.core.components.models.datalayer.ImageData;
import com.fasterxml.jackson.annotation.JsonIgnore;

/**
Expand Down Expand Up @@ -156,24 +156,23 @@ public interface Image extends Component {

/**
* Name of the resource property that will indicate if the current image should has Image Modifiers settings.
*
*/
String PN_IMAGE_MODIFIERS = "imageModifers";

/**
* Name of the resource property that will indicate imageServerUrl.
* Name of the resource property that will indicate imageServerUrl.
*/
String PN_IMAGE_SERVER_URL = "imageServerUrl";

/**
* Name of the resource property that defines areas of an image map.
*
* <p>
* The property stores map areas as follows:
* [area1][area2][...]
*
* <p>
* Area format:
* [SHAPE(COORDINATES)"HREF"|"TARGET"|"ALT"|(RELATIVE_COORDINATES)]
*
* <p>
* Example:
* [rect(0,0,10,10)"http://www.adobe.com"|"_self"|"alt"|(0,0,0.8,0.8)][circle(10,10,10)"http://www.adobe.com"|"_self"|"alt"|(0.8,0.8,0.8)]
*
Expand All @@ -186,6 +185,13 @@ public interface Image extends Component {
*/
String PN_DESIGN_DYNAMIC_MEDIA_ENABLED = "enableDmFeatures";

/**
* Name of the configuration policy property that will be used for resizing the base images, the ones from {@code src} attribute.
*
* @since com.adobe.cq.wcm.core.components.models 12.22.0
*/
String PN_DESIGN_RESIZE_WIDTH = "resizeWidth";

/**
* Returns the value for the {@code src} attribute of the image.
*
Expand Down Expand Up @@ -333,19 +339,21 @@ default String getSrcset() {
}

/**
* Returns the value for the {@code width} html attribute of the image.
* Returns the width of the base DAM asset image, the one from the {@code src} attribute.
* It will be used as value for the {@code width} attribute of the image, only if the image is a DAM asset and is not SVG.
*
* @return the value for the image's {@code width} attribute, if one was set, or {@code null}.
* @return the width of the base DAM asset image, the one from the {@code src} attribute.
* @since com.adobe.cq.wcm.core.components.models 12.21.0;
*/
default String getWidth() {
return null;
}

/**
* Returns the value for the {@code height} html attribute of the image.
* Returns the height of the base DAM asset image, the one from the {@code src} attribute.
* It will be used as value for the {@code height} attribute of the image, only if the image is a DAM asset and is not SVG.
*
* @return the value for the image's {@code height} attribute, if one was set, or {@code null}.
* @return the height of the base DAM asset image, the one from the {@code src} attribute.
* @since com.adobe.cq.wcm.core.components.models 12.21.0;
*/
default String getHeight() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ default String getFormat() {
* @since com.adobe.cq.wcm.core.components.models.datalayer 1.0.0
*/
@JsonProperty("xdm:tags")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
default String[] getTags() {
return null;
}
Expand All @@ -99,6 +100,7 @@ default Date getLastModifiedDate() {
* @since com.adobe.cq.wcm.core.components.models.datalayer 1.1.0
*/
@JsonProperty("xdm:smartTags")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
default Map<String, Object> getSmartTags() {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* This packages defines models for integration with
* <a href="https://github.com/adobe/adobe-client-data-layer">Adobe Client Data Layer</a>
*/
@Version("1.3.0")
@Version("1.3.1")
package com.adobe.cq.wcm.core.components.models.datalayer;

import org.osgi.annotation.versioning.Version;
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ void testGetDataLayerJson() throws Exception {
Image image = getImageUnderTest(IMAGE6_PATH);
assertNotNull(image.getData());

String expected = "{\"image-db7ae5b54e\":{\"image\":{\"repo:id\":\"60a1a56e-f3f4-4021-a7bf-ac7a51f0ffe5\",\"xdm:tags\":[],\"@type\":\"image/gif\",\"repo:modifyDate\":\"2017-03-20T10:20:39Z\",\"repo:path\":\"/content/dam/core/images/Adobe_Systems_logo_and_wordmark.gif\",\"xdm:smartTags\":{\"nature\":0.74,\"lake\":0.79,\"water\":0.78,\"landscape\":0.75}},\"dc:title\":\"Adobe Logo\",\"@type\":\"core/wcm/components/image/v1/image\",\"xdm:linkURL\":\"/core/content/test-image.html\",\"repo:modifyDate\":\"2017-03-20T10:20:39Z\"}}";
String expected = "{\"image-db7ae5b54e\":{\"image\":{\"repo:id\":\"60a1a56e-f3f4-4021-a7bf-ac7a51f0ffe5\",\"@type\":\"image/gif\",\"repo:modifyDate\":\"2017-03-20T10:20:39Z\",\"repo:path\":\"/content/dam/core/images/Adobe_Systems_logo_and_wordmark.gif\",\"xdm:smartTags\":{\"nature\":0.74,\"lake\":0.79,\"water\":0.78,\"landscape\":0.75}},\"dc:title\":\"Adobe Logo\",\"@type\":\"core/wcm/components/image/v1/image\",\"xdm:linkURL\":\"/core/content/test-image.html\",\"repo:modifyDate\":\"2017-03-20T10:20:39Z\"}}";
assertEquals(Json.createReader(new StringReader(expected)).read(),
Json.createReader(new StringReader(image.getData().getJson())).read());
}
Expand Down
Loading

0 comments on commit b5860a2

Please sign in to comment.