Skip to content
This repository has been archived by the owner on Jan 11, 2022. It is now read-only.

Commit

Permalink
use MediaComponentPropertyResolver also in Granite UI components
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert committed Aug 15, 2019
1 parent 3260c78 commit a833654
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
import io.wcm.wcm.commons.component.ComponentPropertyResolver;

/**
* Resolves component properties for Link Handler for the component associated
* with the given resources from content policies and properties defined in the component resource.
* Resolves Link Handler component properties for the component associated
* with the given resource from content policies and properties defined in the component resource.
*/
@ProviderType
public final class LinkComponentPropertyResolver {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* limitations under the License.
* #L%
*/
package io.wcm.handler.media.impl;
package io.wcm.handler.media;

import static io.wcm.handler.media.MediaNameConstants.NN_COMPONENT_MEDIA_RESPONSIVE_IMAGE_SIZES;
import static io.wcm.handler.media.MediaNameConstants.NN_COMPONENT_MEDIA_RESPONSIVE_PICTURE_SOURCES;
Expand All @@ -33,6 +33,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
Expand All @@ -41,20 +42,21 @@
import org.apache.sling.api.resource.ValueMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.osgi.annotation.versioning.ProviderType;

import io.wcm.handler.media.MediaArgs.ImageSizes;
import io.wcm.handler.media.MediaArgs.MediaFormatOption;
import io.wcm.handler.media.MediaArgs.PictureSource;
import io.wcm.handler.media.MediaArgs.WidthOption;
import io.wcm.handler.media.MediaBuilder;
import io.wcm.wcm.commons.component.ComponentPropertyResolution;
import io.wcm.wcm.commons.component.ComponentPropertyResolver;

/**
* Implements resolving initial settings for {@link MediaBuilder} from
* content policies and properties defined in component resources.
* Resolves Media Handler component properties for the component associated
* with the given resource from content policies and properties defined in the component resource.
*/
class MediaComponentPropertyResolver {
@ProviderType
public final class MediaComponentPropertyResolver {

static final String RESPONSIVE_TYPE_IMAGE_SIZES = "imageSizes";
static final String RESPONSIVE_TYPE_PICTURE_SOURCES = "pictureSources";
Expand All @@ -71,24 +73,27 @@ class MediaComponentPropertyResolver {

private final ComponentPropertyResolver resolver;

MediaComponentPropertyResolver(Resource resource) {
/**
* @param resource Resource
*/
public MediaComponentPropertyResolver(@NotNull Resource resource) {
// resolve media component properties 1. from policies and 2. from component definition
resolver = new ComponentPropertyResolver(resource)
resolver = new ComponentPropertyResolver(resource, true)
.contentPolicyResolution(ComponentPropertyResolution.RESOLVE)
.componentPropertiesResolution(ComponentPropertyResolution.RESOLVE_INHERIT);
}

/**
* @return AutoCrop state
*/
boolean isAutoCrop() {
public boolean isAutoCrop() {
return resolver.get(PN_COMPONENT_MEDIA_AUTOCROP, false);
}

/**
* @return List of media formats with and without mandatory setting.
*/
MediaFormatOption[] getMediaFormatOptions() {
public @NotNull MediaFormatOption @Nullable [] getMediaFormatOptions() {
Map<String, MediaFormatOption> mediaFormatOptions = new LinkedHashMap<>();

// media formats with optional mandatory boolean flag(s)
Expand Down Expand Up @@ -130,7 +135,45 @@ else if (mediaFormatsMandatory.length > i) {
}
}

ImageSizes getImageSizes() {
/**
* @return List of media formats with and without mandatory setting.
*/
public @NotNull String @Nullable [] getMediaFormatNames() {
MediaFormatOption[] mediaFormatOptions = getMediaFormatOptions();
if (mediaFormatOptions != null) {
String[] result = Arrays.stream(mediaFormatOptions)
.map(option -> option.getMediaFormatName())
.filter(Objects::nonNull)
.toArray(size -> new String[size]);
if (result.length > 0) {
return result;
}
}
return null;
}

/**
* @return List of media formats with and without mandatory setting.
*/
public @NotNull String @Nullable [] getMandatoryMediaFormatNames() {
MediaFormatOption[] mediaFormatOptions = getMediaFormatOptions();
if (mediaFormatOptions != null) {
String[] result = Arrays.stream(mediaFormatOptions)
.filter(MediaFormatOption::isMandatory)
.map(option -> option.getMediaFormatName())
.filter(Objects::nonNull)
.toArray(size -> new String[size]);
if (result.length > 0) {
return result;
}
}
return null;
}

/**
* @return Image sizes
*/
public @Nullable ImageSizes getImageSizes() {
String responsiveType = getResponsiveType();
if (responsiveType != null && !StringUtils.equals(responsiveType, RESPONSIVE_TYPE_IMAGE_SIZES)) {
return null;
Expand All @@ -144,7 +187,10 @@ ImageSizes getImageSizes() {
return null;
}

PictureSource[] getPictureSources() {
/**
* @return List of picture sources
*/
public @NotNull PictureSource @Nullable [] getPictureSources() {
String responsiveType = getResponsiveType();
if (responsiveType != null && !StringUtils.equals(responsiveType, RESPONSIVE_TYPE_PICTURE_SOURCES)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import io.wcm.handler.media.MediaArgs.PictureSource;
import io.wcm.handler.media.MediaArgs.WidthOption;
import io.wcm.handler.media.MediaBuilder;
import io.wcm.handler.media.MediaComponentPropertyResolver;
import io.wcm.handler.media.MediaRequest;
import io.wcm.handler.media.MediaRequest.MediaPropertyNames;
import io.wcm.handler.media.format.MediaFormat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package io.wcm.handler.media.impl;

import java.io.IOException;
import java.util.Arrays;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
Expand All @@ -29,7 +28,7 @@
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
Expand Down Expand Up @@ -84,10 +83,7 @@ protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHtt

// read and validated request parameters
String[] mediaFormats = StringUtils.split(RequestParam.get(request, RP_MEDIA_FORMATS), ",");
String[] mediaFormatsMandatoryStrings = StringUtils.split(RequestParam.get(request, RP_MEDIA_FORMATS_MANDATORY), ",");
Boolean[] mediaFormatsMandatory = mediaFormatsMandatoryStrings != null ? Arrays.stream(mediaFormatsMandatoryStrings)
.map(BooleanUtils::toBoolean)
.toArray(size -> new Boolean[size]) : null;
String[] mediaFormatsMandatory = StringUtils.split(RequestParam.get(request, RP_MEDIA_FORMATS_MANDATORY), ",");
boolean mediaCropAuto = RequestParam.getBoolean(request, RP_MEDIA_CROPAUTO);
String mediaRef = RequestParam.get(request, RP_MEDIA_REF);

Expand All @@ -101,13 +97,7 @@ protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHtt
for (int i = 0; i < mediaFormats.length; i++) {
boolean mandatory = false;
if (mediaFormatsMandatory != null) {
if (mediaFormatsMandatory.length == 1) {
// backward compatibility: support a single flag for all media formats
mandatory = mediaFormatsMandatory[0];
}
else if (mediaFormatsMandatory.length > i) {
mandatory = mediaFormatsMandatory[i];
}
mandatory = ArrayUtils.contains(mediaFormatsMandatory, mediaFormats[i]);
}
mediaFormatOptions[i] = new MediaFormatOption(mediaFormats[i], mandatory);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@
<%@page import="com.adobe.granite.ui.components.Config"%>
<%@page import="com.adobe.granite.ui.components.ExpressionHelper"%>
<%@page import="io.wcm.handler.media.MediaNameConstants"%>
<%@page import="io.wcm.handler.media.MediaComponentPropertyResolver"%>
<%@page import="io.wcm.handler.media.spi.MediaHandlerConfig"%>
<%@page import="io.wcm.wcm.commons.component.ComponentPropertyResolver"%>
<%@page import="io.wcm.wcm.commons.component.ComponentPropertyResolution"%>
<%@page import="io.wcm.wcm.ui.granite.resource.GraniteUiSyntheticResource"%>
<%@page import="io.wcm.wcm.ui.granite.util.GraniteUi"%>
<%@include file="../../global/global.jsp" %>
Expand Down Expand Up @@ -91,7 +90,7 @@ are overwritten or added.
- mediaFormats (String[]/StringEL)
/**
* Resolving of all media formats is mandatory.
* List of mandatory media formats required by this component.
* If not set the property value is looked up from component properties or policy.
*/
- mediaFormatsMandatory (String[]/StringEL)
Expand Down Expand Up @@ -143,15 +142,13 @@ String[] mediaFormats = null;
String[] mediaFormatsMandatory = null;
boolean mediaCropAuto = false;
if (contentResource != null) {
ComponentPropertyResolver componentPropertyResolver = new ComponentPropertyResolver(contentResource)
.contentPolicyResolution(ComponentPropertyResolution.RESOLVE)
.componentPropertiesResolution(ComponentPropertyResolution.RESOLVE_INHERIT);
MediaComponentPropertyResolver componentPropertyResolver = new MediaComponentPropertyResolver(contentResource);
mediaFormats = getStringArrayWithExpressionSupport("mediaFormats",
MediaNameConstants.PN_COMPONENT_MEDIA_FORMATS, cfg, ex, componentPropertyResolver);
MediaNameConstants.PN_COMPONENT_MEDIA_FORMATS, cfg, ex, componentPropertyResolver.getMediaFormatNames());
mediaFormatsMandatory = getStringArrayWithExpressionSupport("mediaFormatsMandatory",
MediaNameConstants.PN_COMPONENT_MEDIA_FORMATS_MANDATORY, cfg, ex, componentPropertyResolver);
MediaNameConstants.PN_COMPONENT_MEDIA_FORMATS_MANDATORY, cfg, ex, componentPropertyResolver.getMandatoryMediaFormatNames());
mediaCropAuto = getBooleanWithExpressionSupport("mediaCropAuto",
MediaNameConstants.PN_COMPONENT_MEDIA_AUTOCROP, cfg, ex, componentPropertyResolver);
MediaNameConstants.PN_COMPONENT_MEDIA_AUTOCROP, cfg, ex, componentPropertyResolver.isAutoCrop());
// add info about media formats in field description
String mediaFormatsFieldDescription = buildMediaFormatsFieldDescription(mediaFormats, contentResource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
<%@page import="org.apache.sling.api.resource.Resource"%>
<%@page import="io.wcm.handler.media.format.MediaFormat"%>
<%@page import="io.wcm.handler.media.format.MediaFormatHandler"%>
<%@page import="io.wcm.wcm.commons.component.ComponentPropertyResolver"%>
<%!
static String buildMediaFormatsFieldDescription(String[] mediaFormats, Resource resource) {
Expand Down Expand Up @@ -57,7 +56,7 @@ static String buildMediaFormatsFieldDescription(String[] mediaFormats, Resource
}
static String[] getStringArrayWithExpressionSupport(String propertyName, String componentPropertyName,
Config cfg, ExpressionHelper ex, ComponentPropertyResolver componentPropertyResolver) {
Config cfg, ExpressionHelper ex, String[] defaultValue) {
String[] result = null;
Object value = cfg.get(propertyName, (Object)null);
Expand All @@ -76,16 +75,16 @@ static String[] getStringArrayWithExpressionSupport(String propertyName, String
result = cfg.get(propertyName, String[].class);
}
// fallback to component properties
// fallback to default value from component properties
if (result == null) {
result = componentPropertyResolver.get(componentPropertyName, String[].class);
result = defaultValue;
}
return result;
}
static boolean getBooleanWithExpressionSupport(String propertyName, String componentPropertyName,
Config cfg, ExpressionHelper ex, ComponentPropertyResolver componentPropertyResolver) {
Config cfg, ExpressionHelper ex, boolean defaultValue) {
String[] mediaFormats = null;
Boolean result = null;
Expand All @@ -95,9 +94,9 @@ static boolean getBooleanWithExpressionSupport(String propertyName, String compo
result = ex.get((String)value, Boolean.class);
}
// try to get directly from config, fallback to component properties
// try to get directly from config, fallback to default value from component properties
if (result == null) {
result = cfg.get(propertyName, componentPropertyResolver.get(componentPropertyName, false));
result = cfg.get(propertyName, defaultValue);
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@
<%@page import="com.adobe.granite.ui.components.Config"%>
<%@page import="com.adobe.granite.ui.components.ExpressionHelper"%>
<%@page import="io.wcm.handler.media.MediaNameConstants"%>
<%@page import="io.wcm.handler.media.MediaComponentPropertyResolver"%>
<%@page import="io.wcm.handler.media.spi.MediaHandlerConfig"%>
<%@page import="io.wcm.wcm.commons.component.ComponentPropertyResolver"%>
<%@page import="io.wcm.wcm.commons.component.ComponentPropertyResolution"%>
<%@page import="io.wcm.wcm.ui.granite.resource.GraniteUiSyntheticResource"%>
<%@page import="io.wcm.wcm.ui.granite.util.GraniteUi"%>
<%@include file="../../global/global.jsp" %>
Expand Down Expand Up @@ -70,7 +69,7 @@ are overwritten or added.
- mediaFormats (String[]/StringEL)
/**
* Resolving of all media formats is mandatory.
* List of mandatory media formats required by this component.
* If not set the property value is looked up from component properties or policy.
*/
- mediaFormatsMandatory (String[]/StringEL)
Expand Down Expand Up @@ -106,15 +105,13 @@ String[] mediaFormats = null;
String[] mediaFormatsMandatory = null;
boolean mediaCropAuto = false;
if (contentResource != null) {
ComponentPropertyResolver componentPropertyResolver = new ComponentPropertyResolver(contentResource)
.contentPolicyResolution(ComponentPropertyResolution.RESOLVE)
.componentPropertiesResolution(ComponentPropertyResolution.RESOLVE_INHERIT);
MediaComponentPropertyResolver componentPropertyResolver = new MediaComponentPropertyResolver(contentResource);
mediaFormats = getStringArrayWithExpressionSupport("mediaFormats",
MediaNameConstants.PN_COMPONENT_MEDIA_FORMATS, cfg, ex, componentPropertyResolver);
MediaNameConstants.PN_COMPONENT_MEDIA_FORMATS, cfg, ex, componentPropertyResolver.getMediaFormatNames());
mediaFormatsMandatory = getStringArrayWithExpressionSupport("mediaFormatsMandatory",
MediaNameConstants.PN_COMPONENT_MEDIA_FORMATS_MANDATORY, cfg, ex, componentPropertyResolver);
MediaNameConstants.PN_COMPONENT_MEDIA_FORMATS_MANDATORY, cfg, ex, componentPropertyResolver.getMandatoryMediaFormatNames());
mediaCropAuto = getBooleanWithExpressionSupport("mediaCropAuto",
MediaNameConstants.PN_COMPONENT_MEDIA_AUTOCROP, cfg, ex, componentPropertyResolver);
MediaNameConstants.PN_COMPONENT_MEDIA_AUTOCROP, cfg, ex, componentPropertyResolver.isAutoCrop());
// add info about media formats in field description
String mediaFormatsFieldDescription = buildMediaFormatsFieldDescription(mediaFormats, contentResource);
Expand Down

0 comments on commit a833654

Please sign in to comment.