Skip to content

Commit

Permalink
Adaption from Resource to MediaComponentPropertyResolver (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert authored Dec 22, 2023
1 parent 7a04bfa commit 3d5a9c8
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@

import org.apache.sling.api.adapter.Adaptable;
import org.apache.sling.api.adapter.AdapterFactory;
import org.apache.sling.api.resource.Resource;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import io.wcm.handler.media.MediaComponentPropertyResolver;
import io.wcm.handler.media.spi.MediaHandlerConfig;
import io.wcm.sling.commons.caservice.ContextAwareServiceResolver;
import io.wcm.wcm.commons.component.ComponentPropertyResolverFactory;

/**
* Adapts resources or requests to {@link MediaHandlerConfig} via {@link ContextAwareServiceResolver}.
Expand All @@ -34,19 +37,25 @@
property = {
AdapterFactory.ADAPTABLE_CLASSES + "=org.apache.sling.api.resource.Resource",
AdapterFactory.ADAPTABLE_CLASSES + "=org.apache.sling.api.SlingHttpServletRequest",
AdapterFactory.ADAPTER_CLASSES + "=io.wcm.handler.media.spi.MediaHandlerConfig"
AdapterFactory.ADAPTER_CLASSES + "=io.wcm.handler.media.spi.MediaHandlerConfig",
AdapterFactory.ADAPTER_CLASSES + "=io.wcm.handler.media.MediaComponentPropertyResolver"
})
public class MediaHandlerConfigAdapterFactory implements AdapterFactory {
public class MediaHandlerAdapterFactory implements AdapterFactory {

@Reference
private ContextAwareServiceResolver serviceResolver;
@Reference
private ComponentPropertyResolverFactory componentPropertyResolverFactory;

@SuppressWarnings({ "unchecked", "null" })
@Override
public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
if (type == MediaHandlerConfig.class) {
return (AdapterType)serviceResolver.resolve(MediaHandlerConfig.class, (Adaptable)adaptable);
}
if (type == MediaComponentPropertyResolver.class && adaptable instanceof Resource) {
return (AdapterType)new MediaComponentPropertyResolver((Resource)adaptable, componentPropertyResolverFactory);
}
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,14 @@ String[] mediaFormats = null;
String[] mediaFormatsMandatory = null;
boolean mediaCropAuto = false;
if (contentResource != null) {
MediaComponentPropertyResolver componentPropertyResolver = new MediaComponentPropertyResolver(contentResource);
mediaFormats = getStringArrayWithExpressionSupport("mediaFormats",
MediaNameConstants.PN_COMPONENT_MEDIA_FORMATS, cfg, ex, componentPropertyResolver.getMediaFormatNames());
mediaFormatsMandatory = getStringArrayWithExpressionSupport("mediaFormatsMandatory",
MediaNameConstants.PN_COMPONENT_MEDIA_FORMATS_MANDATORY, cfg, ex, componentPropertyResolver.getMandatoryMediaFormatNames());
mediaCropAuto = getBooleanWithExpressionSupport("mediaCropAuto",
MediaNameConstants.PN_COMPONENT_MEDIA_AUTOCROP, cfg, ex, componentPropertyResolver.isAutoCrop());
try (MediaComponentPropertyResolver componentPropertyResolver = contentResource.adaptTo(MediaComponentPropertyResolver.class)) {
mediaFormats = getStringArrayWithExpressionSupport("mediaFormats",
MediaNameConstants.PN_COMPONENT_MEDIA_FORMATS, cfg, ex, componentPropertyResolver.getMediaFormatNames());
mediaFormatsMandatory = getStringArrayWithExpressionSupport("mediaFormatsMandatory",
MediaNameConstants.PN_COMPONENT_MEDIA_FORMATS_MANDATORY, cfg, ex, componentPropertyResolver.getMandatoryMediaFormatNames());
mediaCropAuto = getBooleanWithExpressionSupport("mediaCropAuto",
MediaNameConstants.PN_COMPONENT_MEDIA_AUTOCROP, cfg, ex, componentPropertyResolver.isAutoCrop());
}
// add info about media formats in field description
String mediaFormatsFieldDescription = buildMediaFormatsFieldDescription(mediaFormats, contentResource, i18n);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,14 @@ String[] mediaFormats = null;
String[] mediaFormatsMandatory = null;
boolean mediaCropAuto = false;
if (contentResource != null) {
MediaComponentPropertyResolver componentPropertyResolver = new MediaComponentPropertyResolver(contentResource);
mediaFormats = getStringArrayWithExpressionSupport("mediaFormats",
MediaNameConstants.PN_COMPONENT_MEDIA_FORMATS, cfg, ex, componentPropertyResolver.getMediaFormatNames());
mediaFormatsMandatory = getStringArrayWithExpressionSupport("mediaFormatsMandatory",
MediaNameConstants.PN_COMPONENT_MEDIA_FORMATS_MANDATORY, cfg, ex, componentPropertyResolver.getMandatoryMediaFormatNames());
mediaCropAuto = getBooleanWithExpressionSupport("mediaCropAuto",
MediaNameConstants.PN_COMPONENT_MEDIA_AUTOCROP, cfg, ex, componentPropertyResolver.isAutoCrop());
try (MediaComponentPropertyResolver componentPropertyResolver = contentResource.adaptTo(MediaComponentPropertyResolver.class)) {
mediaFormats = getStringArrayWithExpressionSupport("mediaFormats",
MediaNameConstants.PN_COMPONENT_MEDIA_FORMATS, cfg, ex, componentPropertyResolver.getMediaFormatNames());
mediaFormatsMandatory = getStringArrayWithExpressionSupport("mediaFormatsMandatory",
MediaNameConstants.PN_COMPONENT_MEDIA_FORMATS_MANDATORY, cfg, ex, componentPropertyResolver.getMandatoryMediaFormatNames());
mediaCropAuto = getBooleanWithExpressionSupport("mediaCropAuto",
MediaNameConstants.PN_COMPONENT_MEDIA_AUTOCROP, cfg, ex, componentPropertyResolver.isAutoCrop());
}
// add info about media formats in field description
String mediaFormatsFieldDescription = buildMediaFormatsFieldDescription(mediaFormats, contentResource, i18n);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.wrappers.ValueMapDecorator;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

Expand All @@ -58,9 +57,9 @@
import io.wcm.handler.media.MediaArgs.PictureSource;
import io.wcm.handler.media.MediaArgs.WidthOption;
import io.wcm.handler.media.testcontext.AppAemContext;
import io.wcm.sling.commons.adapter.AdaptTo;
import io.wcm.testing.mock.aem.junit5.AemContext;
import io.wcm.testing.mock.aem.junit5.AemContextExtension;
import io.wcm.wcm.commons.component.ComponentPropertyResolverFactory;

@ExtendWith(AemContextExtension.class)
class MediaComponentPropertyResolverTest {
Expand All @@ -69,19 +68,12 @@ class MediaComponentPropertyResolverTest {

private final AemContext context = AppAemContext.newAemContext();

private ComponentPropertyResolverFactory componentPropertyResolverFactory;

@BeforeEach
void setUp() {
componentPropertyResolverFactory = context.getService(ComponentPropertyResolverFactory.class);
}

@Test
void testIsAutoCrop_Default() throws Exception {
Resource resource = context.create().resource("/content/r1");
context.resourceResolver().commit();

try (MediaComponentPropertyResolver underTest = new MediaComponentPropertyResolver(resource, componentPropertyResolverFactory)) {
try (MediaComponentPropertyResolver underTest = AdaptTo.notNull(resource, MediaComponentPropertyResolver.class)) {
assertFalse(underTest.isAutoCrop());
}
}
Expand All @@ -94,7 +86,7 @@ void testIsAutoCrop_Component() throws Exception {
PROPERTY_RESOURCE_TYPE, RESOURCE_TYPE);
context.resourceResolver().commit();

try (MediaComponentPropertyResolver underTest = new MediaComponentPropertyResolver(resource, componentPropertyResolverFactory)) {
try (MediaComponentPropertyResolver underTest = AdaptTo.notNull(resource, MediaComponentPropertyResolver.class)) {
assertTrue(underTest.isAutoCrop());
}
}
Expand All @@ -110,7 +102,7 @@ void testIsAutoCrop_Component_Subresource() throws Exception {
Resource subresource2 = context.create().resource(subresource1, "subresource2");
context.resourceResolver().commit();

try (MediaComponentPropertyResolver underTest = new MediaComponentPropertyResolver(subresource2, componentPropertyResolverFactory)) {
try (MediaComponentPropertyResolver underTest = AdaptTo.notNull(subresource2, MediaComponentPropertyResolver.class)) {
assertTrue(underTest.isAutoCrop());
}
}
Expand All @@ -126,7 +118,7 @@ void testIsAutoCrop_Component_Policy() throws Exception {
PROPERTY_RESOURCE_TYPE, RESOURCE_TYPE);
context.resourceResolver().commit();

try (MediaComponentPropertyResolver underTest = new MediaComponentPropertyResolver(resource, componentPropertyResolverFactory)) {
try (MediaComponentPropertyResolver underTest = AdaptTo.notNull(resource, MediaComponentPropertyResolver.class)) {
assertFalse(underTest.isAutoCrop());
}
}
Expand All @@ -148,7 +140,7 @@ void testGetMediaFormatOptions_Single() throws Exception {
PROPERTY_RESOURCE_TYPE, RESOURCE_TYPE);
context.resourceResolver().commit();

try (MediaComponentPropertyResolver underTest = new MediaComponentPropertyResolver(resource, componentPropertyResolverFactory)) {
try (MediaComponentPropertyResolver underTest = AdaptTo.notNull(resource, MediaComponentPropertyResolver.class)) {
assertArrayEquals(new MediaFormatOption[] {
new MediaFormatOption("home_stage", false)
}, underTest.getMediaFormatOptions());
Expand All @@ -163,7 +155,7 @@ void testGetMediaFormatOptions_Multi() throws Exception {
PROPERTY_RESOURCE_TYPE, RESOURCE_TYPE);
context.resourceResolver().commit();

try (MediaComponentPropertyResolver underTest = new MediaComponentPropertyResolver(resource, componentPropertyResolverFactory)) {
try (MediaComponentPropertyResolver underTest = AdaptTo.notNull(resource, MediaComponentPropertyResolver.class)) {
assertArrayEquals(new MediaFormatOption[] {
new MediaFormatOption("home_stage", false),
new MediaFormatOption("home_teaser", false)
Expand All @@ -186,7 +178,7 @@ void testGetMediaFormatOptions_Multi_MandatoryLegacy() throws Exception {
PROPERTY_RESOURCE_TYPE, RESOURCE_TYPE);
context.resourceResolver().commit();

try (MediaComponentPropertyResolver underTest = new MediaComponentPropertyResolver(resource, componentPropertyResolverFactory)) {
try (MediaComponentPropertyResolver underTest = AdaptTo.notNull(resource, MediaComponentPropertyResolver.class)) {
assertArrayEquals(new MediaFormatOption[] {
new MediaFormatOption("home_stage", true),
new MediaFormatOption("home_teaser", true)
Expand All @@ -212,7 +204,7 @@ void testGetMediaFormatOptions_Multi_Mandatory() throws Exception {
PROPERTY_RESOURCE_TYPE, RESOURCE_TYPE);
context.resourceResolver().commit();

try (MediaComponentPropertyResolver underTest = new MediaComponentPropertyResolver(resource, componentPropertyResolverFactory)) {
try (MediaComponentPropertyResolver underTest = AdaptTo.notNull(resource, MediaComponentPropertyResolver.class)) {
assertArrayEquals(new MediaFormatOption[] {
new MediaFormatOption("home_stage", true),
new MediaFormatOption("home_teaser", false)
Expand All @@ -237,7 +229,7 @@ void testGetMediaFormatOptions_Multi_Mandatory_Names() throws Exception {
PROPERTY_RESOURCE_TYPE, RESOURCE_TYPE);
context.resourceResolver().commit();

try (MediaComponentPropertyResolver underTest = new MediaComponentPropertyResolver(resource, componentPropertyResolverFactory)) {
try (MediaComponentPropertyResolver underTest = AdaptTo.notNull(resource, MediaComponentPropertyResolver.class)) {
assertArrayEquals(new MediaFormatOption[] {
new MediaFormatOption("home_stage", true),
new MediaFormatOption("home_teaser", false),
Expand Down Expand Up @@ -283,7 +275,7 @@ void testGetImageSizes_NotExisting() throws Exception {
PROPERTY_RESOURCE_TYPE, RESOURCE_TYPE);
context.resourceResolver().commit();

try (MediaComponentPropertyResolver underTest = new MediaComponentPropertyResolver(resource, componentPropertyResolverFactory)) {
try (MediaComponentPropertyResolver underTest = AdaptTo.notNull(resource, MediaComponentPropertyResolver.class)) {
assertNull(underTest.getImageSizes());
}
}
Expand All @@ -296,7 +288,7 @@ void testGetImageSizes_Empty() throws Exception {
PROPERTY_RESOURCE_TYPE, RESOURCE_TYPE);
context.resourceResolver().commit();

try (MediaComponentPropertyResolver underTest = new MediaComponentPropertyResolver(resource, componentPropertyResolverFactory)) {
try (MediaComponentPropertyResolver underTest = AdaptTo.notNull(resource, MediaComponentPropertyResolver.class)) {
assertNull(underTest.getImageSizes());
}
}
Expand All @@ -311,7 +303,7 @@ void testGetImageSizes_Valid() throws Exception {
PROPERTY_RESOURCE_TYPE, RESOURCE_TYPE);
context.resourceResolver().commit();

try (MediaComponentPropertyResolver underTest = new MediaComponentPropertyResolver(resource, componentPropertyResolverFactory)) {
try (MediaComponentPropertyResolver underTest = AdaptTo.notNull(resource, MediaComponentPropertyResolver.class)) {
assertEquals(new ImageSizes("sizes1", new WidthOption[] {
new WidthOption(200, true),
new WidthOption(400, true),
Expand All @@ -330,7 +322,7 @@ void testGetImageSizes_Invalid() throws Exception {
PROPERTY_RESOURCE_TYPE, RESOURCE_TYPE);
context.resourceResolver().commit();

try (MediaComponentPropertyResolver underTest = new MediaComponentPropertyResolver(resource, componentPropertyResolverFactory)) {
try (MediaComponentPropertyResolver underTest = AdaptTo.notNull(resource, MediaComponentPropertyResolver.class)) {
assertNull(underTest.getImageSizes());
}
}
Expand All @@ -346,7 +338,7 @@ void testGetImageSizes_Valid_Active() throws Exception {
PROPERTY_RESOURCE_TYPE, RESOURCE_TYPE);
context.resourceResolver().commit();

try (MediaComponentPropertyResolver underTest = new MediaComponentPropertyResolver(resource, componentPropertyResolverFactory)) {
try (MediaComponentPropertyResolver underTest = AdaptTo.notNull(resource, MediaComponentPropertyResolver.class)) {
assertEquals(new ImageSizes("sizes1", 200, 400), underTest.getImageSizes());
}
}
Expand All @@ -362,7 +354,7 @@ void testGetImageSizes_Valid_NotActive() throws Exception {
PROPERTY_RESOURCE_TYPE, RESOURCE_TYPE);
context.resourceResolver().commit();

try (MediaComponentPropertyResolver underTest = new MediaComponentPropertyResolver(resource, componentPropertyResolverFactory)) {
try (MediaComponentPropertyResolver underTest = AdaptTo.notNull(resource, MediaComponentPropertyResolver.class)) {
assertNull(underTest.getImageSizes());
}
}
Expand All @@ -374,7 +366,7 @@ void testGetPictureSources_NotExisting() throws Exception {
PROPERTY_RESOURCE_TYPE, RESOURCE_TYPE);
context.resourceResolver().commit();

try (MediaComponentPropertyResolver underTest = new MediaComponentPropertyResolver(resource, componentPropertyResolverFactory)) {
try (MediaComponentPropertyResolver underTest = AdaptTo.notNull(resource, MediaComponentPropertyResolver.class)) {
assertNull(underTest.getPictureSources());
}
}
Expand All @@ -387,7 +379,7 @@ void testGetPictureSources_Empty() throws Exception {
PROPERTY_RESOURCE_TYPE, RESOURCE_TYPE);
context.resourceResolver().commit();

try (MediaComponentPropertyResolver underTest = new MediaComponentPropertyResolver(resource, componentPropertyResolverFactory)) {
try (MediaComponentPropertyResolver underTest = AdaptTo.notNull(resource, MediaComponentPropertyResolver.class)) {
assertNull(underTest.getImageSizes());
}
}
Expand All @@ -408,7 +400,7 @@ void testGetPictureSources_Valid() throws Exception {
PROPERTY_RESOURCE_TYPE, RESOURCE_TYPE);
context.resourceResolver().commit();

try (MediaComponentPropertyResolver underTest = new MediaComponentPropertyResolver(resource, componentPropertyResolverFactory)) {
try (MediaComponentPropertyResolver underTest = AdaptTo.notNull(resource, MediaComponentPropertyResolver.class)) {
assertArrayEquals(new PictureSource[] {
new PictureSource("home_stage")
.media("media1")
Expand Down Expand Up @@ -437,7 +429,7 @@ void testGetPictureSources_Invalid() throws Exception {
PROPERTY_RESOURCE_TYPE, RESOURCE_TYPE);
context.resourceResolver().commit();

try (MediaComponentPropertyResolver underTest = new MediaComponentPropertyResolver(resource, componentPropertyResolverFactory)) {
try (MediaComponentPropertyResolver underTest = AdaptTo.notNull(resource, MediaComponentPropertyResolver.class)) {
assertNull(underTest.getPictureSources());
}
}
Expand All @@ -455,7 +447,7 @@ void testGetPictureSources_Valid_Active() throws Exception {
context.resourceResolver().commit();
context.resourceResolver().commit();

try (MediaComponentPropertyResolver underTest = new MediaComponentPropertyResolver(resource, componentPropertyResolverFactory)) {
try (MediaComponentPropertyResolver underTest = AdaptTo.notNull(resource, MediaComponentPropertyResolver.class)) {
assertArrayEquals(new PictureSource[] {
new PictureSource("home_stage").widths(200, 400)
}, underTest.getPictureSources());
Expand All @@ -474,7 +466,7 @@ void testGetPictureSources_Valid_Inactive() throws Exception {
PROPERTY_RESOURCE_TYPE, RESOURCE_TYPE);
context.resourceResolver().commit();

try (MediaComponentPropertyResolver underTest = new MediaComponentPropertyResolver(resource, componentPropertyResolverFactory)) {
try (MediaComponentPropertyResolver underTest = AdaptTo.notNull(resource, MediaComponentPropertyResolver.class)) {
assertNull(underTest.getPictureSources());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

import io.wcm.handler.media.format.impl.MediaFormatProviderManagerImpl;
import io.wcm.handler.media.impl.DefaultMediaHandlerConfig;
import io.wcm.handler.media.impl.MediaHandlerConfigAdapterFactory;
import io.wcm.handler.media.impl.MediaHandlerAdapterFactory;
import io.wcm.handler.media.spi.MediaFormatProvider;
import io.wcm.handler.media.spi.MediaHandlerConfig;
import io.wcm.handler.mediasource.dam.impl.dynamicmedia.DynamicMediaSupportServiceImpl;
Expand Down Expand Up @@ -100,24 +100,24 @@ public static AemContext newAemContext(@Nullable ResourceResolverType resourceRe
public void execute(@NotNull AemContext context) throws Exception {

// handler SPI
context.registerInjectActivateService(new SiteRootDetectorImpl());
context.registerInjectActivateService(new UrlHandlerAdapterFactory());
context.registerInjectActivateService(new ClientlibProxyRewriterImpl());
context.registerInjectActivateService(new DefaultUrlHandlerConfig());
context.registerInjectActivateService(SiteRootDetectorImpl.class);
context.registerInjectActivateService(UrlHandlerAdapterFactory.class);
context.registerInjectActivateService(ClientlibProxyRewriterImpl.class);
context.registerInjectActivateService(DefaultUrlHandlerConfig.class);
context.registerService(UrlHandlerConfig.class, new DummyUrlHandlerConfig());
context.registerInjectActivateService(new MediaHandlerConfigAdapterFactory());
context.registerInjectActivateService(new DefaultMediaHandlerConfig());
context.registerInjectActivateService(MediaHandlerAdapterFactory.class);
context.registerInjectActivateService(DefaultMediaHandlerConfig.class);
context.registerService(MediaHandlerConfig.class, new DummyMediaHandlerConfig());

// context path strategy
MockCAConfig.contextPathStrategyAbsoluteParent(context, DummyUrlHandlerConfig.SITE_ROOT_LEVEL);

// media formats
context.registerService(MediaFormatProvider.class, new DummyMediaFormatProvider());
context.registerInjectActivateService(new MediaFormatProviderManagerImpl());
context.registerInjectActivateService(MediaFormatProviderManagerImpl.class);

// dynamic media
context.registerInjectActivateService(new DynamicMediaSupportServiceImpl());
context.registerInjectActivateService(DynamicMediaSupportServiceImpl.class);

// sling models registration
context.addModelsForPackage(
Expand Down
Loading

0 comments on commit 3d5a9c8

Please sign in to comment.