From 91a387fb4cd0db20319a09bc608368ec43302863 Mon Sep 17 00:00:00 2001 From: Stefan Seifert Date: Tue, 18 Jun 2024 15:09:32 +0200 Subject: [PATCH] use AemObjectReflectionToStringBuilder from wcm commons --- pom.xml | 2 +- src/main/java/io/wcm/handler/media/Media.java | 1 + .../java/io/wcm/handler/media/MediaArgs.java | 4 +- .../io/wcm/handler/media/MediaRequest.java | 4 +- .../AemObjectsReflectionToStringBuilder.java | 89 ------------------ .../mediasource/dam/impl/DamAsset.java | 4 +- .../mediasource/inline/InlineAsset.java | 4 +- ...mObjectsReflectionToStringBuilderTest.java | 94 ------------------- 8 files changed, 10 insertions(+), 192 deletions(-) delete mode 100644 src/main/java/io/wcm/handler/media/impl/AemObjectsReflectionToStringBuilder.java delete mode 100644 src/test/java/io/wcm/handler/media/impl/AemObjectsReflectionToStringBuilderTest.java diff --git a/pom.xml b/pom.xml index 8d6d08d3..c883a945 100644 --- a/pom.xml +++ b/pom.xml @@ -82,7 +82,7 @@ io.wcm io.wcm.wcm.commons - 1.10.0 + 1.10.1-SNAPSHOT compile diff --git a/src/main/java/io/wcm/handler/media/Media.java b/src/main/java/io/wcm/handler/media/Media.java index 12a72bbe..a44ff33a 100644 --- a/src/main/java/io/wcm/handler/media/Media.java +++ b/src/main/java/io/wcm/handler/media/Media.java @@ -284,6 +284,7 @@ public void setMediaInvalidReasonCustomMessage(@Nullable String mediaInvalidReas @Override public String toString() { ToStringBuilder sb = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE); + sb.append("valid", isValid()); if (isValid()) { sb.append("url", getUrl()); } diff --git a/src/main/java/io/wcm/handler/media/MediaArgs.java b/src/main/java/io/wcm/handler/media/MediaArgs.java index 5957ea0d..1c478684 100644 --- a/src/main/java/io/wcm/handler/media/MediaArgs.java +++ b/src/main/java/io/wcm/handler/media/MediaArgs.java @@ -37,12 +37,12 @@ import org.osgi.annotation.versioning.ProviderType; import io.wcm.handler.media.format.MediaFormat; -import io.wcm.handler.media.impl.AemObjectsReflectionToStringBuilder; import io.wcm.handler.media.markup.DragDropSupport; import io.wcm.handler.media.markup.IPERatioCustomize; import io.wcm.handler.mediasource.dam.AemRenditionType; import io.wcm.handler.url.UrlMode; import io.wcm.wcm.commons.contenttype.FileExtension; +import io.wcm.wcm.commons.util.AemObjectReflectionToStringBuilder; /** * Holds parameters to influence the media resolving process. @@ -844,7 +844,7 @@ public String toString() { sb.append("webOptimizedImageDeliveryDisabled", webOptimizedImageDeliveryDisabled); } if (properties != null && !properties.isEmpty()) { - sb.append("properties", AemObjectsReflectionToStringBuilder.filteredValueMap(properties)); + sb.append("properties", AemObjectReflectionToStringBuilder.filteredValueMap(properties)); } return sb.build(); } diff --git a/src/main/java/io/wcm/handler/media/MediaRequest.java b/src/main/java/io/wcm/handler/media/MediaRequest.java index 1f0e1da1..3137cd7e 100644 --- a/src/main/java/io/wcm/handler/media/MediaRequest.java +++ b/src/main/java/io/wcm/handler/media/MediaRequest.java @@ -29,7 +29,7 @@ import org.jetbrains.annotations.Nullable; import org.osgi.annotation.versioning.ProviderType; -import io.wcm.handler.media.impl.AemObjectsReflectionToStringBuilder; +import io.wcm.wcm.commons.util.AemObjectReflectionToStringBuilder; /** * Holds all properties that are part of a media handling request. @@ -123,7 +123,7 @@ public String toString() { io.wcm.wcm.commons.util.ToStringStyle.SHORT_PREFIX_OMIT_NULL_STYLE); if (resource != null) { sb.append("resource", resource.getPath()); - sb.append("resourceProperties", AemObjectsReflectionToStringBuilder.filteredValueMap(resource.getValueMap())); + sb.append("resourceProperties", AemObjectReflectionToStringBuilder.filteredValueMap(resource.getValueMap())); } if (mediaRef != null) { sb.append("mediaRef", mediaRef); diff --git a/src/main/java/io/wcm/handler/media/impl/AemObjectsReflectionToStringBuilder.java b/src/main/java/io/wcm/handler/media/impl/AemObjectsReflectionToStringBuilder.java deleted file mode 100644 index 16dda12e..00000000 --- a/src/main/java/io/wcm/handler/media/impl/AemObjectsReflectionToStringBuilder.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * #%L - * wcm.io - * %% - * Copyright (C) 2024 wcm.io - * %% - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * #L% - */ -package io.wcm.handler.media.impl; - -import java.lang.reflect.Field; -import java.util.Map; -import java.util.TreeMap; -import java.util.stream.Collectors; - -import org.apache.commons.lang3.builder.ReflectionToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; -import org.apache.sling.api.resource.Resource; -import org.apache.sling.api.resource.ValueMap; - -import com.day.cq.wcm.api.Page; - -/** - * Extends ReflectionToStringBuilder to provide custom handling for AEM-related objects - * (Resource, ValueMap, Page) for a more compact log output. - */ -public class AemObjectsReflectionToStringBuilder extends ReflectionToStringBuilder { - - /** - * @param object Object to output - */ - public AemObjectsReflectionToStringBuilder(Object object) { - super(object); - } - - /** - * @param object Object to output - * @param style Style - */ - public AemObjectsReflectionToStringBuilder(Object object, ToStringStyle style) { - super(object, style); - } - - @Override - protected Object getValue(Field field) throws IllegalAccessException { - if (Resource.class.isAssignableFrom(field.getType())) { - Resource resource = (Resource)field.get(this.getObject()); - if (resource != null) { - return resource.getPath(); - } - } - else if (ValueMap.class.isAssignableFrom(field.getType())) { - ValueMap valueMap = (ValueMap)field.get(this.getObject()); - if (valueMap != null) { - return filteredValueMap(valueMap); - } - } - else if (Page.class.isAssignableFrom(field.getType())) { - Page page = (Page)field.get(this.getObject()); - if (page != null) { - return page.getPath(); - } - } - return super.getValue(field); - } - - /** - * Filter value map to exclude jcr:* properties and null values. - * @param props Value map - * @return Filtered value map, sorted by key - */ - public static Map filteredValueMap(ValueMap props) { - return props.entrySet().stream() - .filter(entry -> !entry.getKey().startsWith("jcr:") && entry.getValue() != null) - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (o1, o2) -> o1, TreeMap::new)); - } - -} diff --git a/src/main/java/io/wcm/handler/mediasource/dam/impl/DamAsset.java b/src/main/java/io/wcm/handler/mediasource/dam/impl/DamAsset.java index ac5e134e..da11a0b5 100644 --- a/src/main/java/io/wcm/handler/mediasource/dam/impl/DamAsset.java +++ b/src/main/java/io/wcm/handler/mediasource/dam/impl/DamAsset.java @@ -40,11 +40,11 @@ import io.wcm.handler.media.Rendition; import io.wcm.handler.media.UriTemplate; import io.wcm.handler.media.UriTemplateType; -import io.wcm.handler.media.impl.AemObjectsReflectionToStringBuilder; import io.wcm.handler.media.spi.MediaHandlerConfig; import io.wcm.handler.mediasource.dam.AssetRendition; import io.wcm.handler.mediasource.dam.impl.dynamicmedia.DynamicMediaSupportService; import io.wcm.handler.mediasource.dam.impl.weboptimized.WebOptimizedImageDeliveryService; +import io.wcm.wcm.commons.util.AemObjectReflectionToStringBuilder; /** * {@link Asset} implementation for DAM assets. @@ -224,7 +224,7 @@ public AdapterType adaptTo(Class type) { @Override public String toString() { - return new AemObjectsReflectionToStringBuilder(this, + return new AemObjectReflectionToStringBuilder(this, io.wcm.wcm.commons.util.ToStringStyle.SHORT_PREFIX_OMIT_NULL_STYLE).build(); } diff --git a/src/main/java/io/wcm/handler/mediasource/inline/InlineAsset.java b/src/main/java/io/wcm/handler/mediasource/inline/InlineAsset.java index 96febe41..284d1d0f 100644 --- a/src/main/java/io/wcm/handler/mediasource/inline/InlineAsset.java +++ b/src/main/java/io/wcm/handler/mediasource/inline/InlineAsset.java @@ -35,8 +35,8 @@ import io.wcm.handler.media.Rendition; import io.wcm.handler.media.UriTemplate; import io.wcm.handler.media.UriTemplateType; -import io.wcm.handler.media.impl.AemObjectsReflectionToStringBuilder; import io.wcm.handler.media.spi.MediaHandlerConfig; +import io.wcm.wcm.commons.util.AemObjectReflectionToStringBuilder; /** * {@link Asset} implementation for inline media objects stored in a node in a content page. @@ -168,7 +168,7 @@ public AdapterType adaptTo(Class type) { @Override public String toString() { - return new AemObjectsReflectionToStringBuilder(this, + return new AemObjectReflectionToStringBuilder(this, io.wcm.wcm.commons.util.ToStringStyle.SHORT_PREFIX_OMIT_NULL_STYLE).build(); } diff --git a/src/test/java/io/wcm/handler/media/impl/AemObjectsReflectionToStringBuilderTest.java b/src/test/java/io/wcm/handler/media/impl/AemObjectsReflectionToStringBuilderTest.java deleted file mode 100644 index e902f4a1..00000000 --- a/src/test/java/io/wcm/handler/media/impl/AemObjectsReflectionToStringBuilderTest.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * #%L - * wcm.io - * %% - * Copyright (C) 2024 wcm.io - * %% - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * #L% - */ -package io.wcm.handler.media.impl; - -import static com.day.cq.commons.jcr.JcrConstants.JCR_CREATED; -import static com.day.cq.commons.jcr.JcrConstants.JCR_PRIMARYTYPE; -import static com.day.cq.commons.jcr.JcrConstants.NT_UNSTRUCTURED; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; - -import java.util.Date; -import java.util.Map; - -import org.apache.commons.lang3.builder.ToStringStyle; -import org.apache.sling.api.resource.Resource; -import org.apache.sling.api.resource.ValueMap; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; - -import com.day.cq.wcm.api.Page; - -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; -import io.wcm.handler.media.testcontext.AppAemContext; -import io.wcm.sling.commons.resource.ImmutableValueMap; -import io.wcm.testing.mock.aem.junit5.AemContext; -import io.wcm.testing.mock.aem.junit5.AemContextExtension; - -@ExtendWith(AemContextExtension.class) -class AemObjectsReflectionToStringBuilderTest { - - private final AemContext context = AppAemContext.newAemContext(); - - @SuppressWarnings("null") - private static final ValueMap VALUEMAP_SAMPLE = ImmutableValueMap.of( - "prop1", "value1", - JCR_CREATED, new Date(), - JCR_PRIMARYTYPE, NT_UNSTRUCTURED, - "prop2", 5, - "prop3", null); - - @Test - void testBuild() { - ClassWithFields obj = new ClassWithFields(); - obj.prop1 = "value1"; - obj.resource = context.create().resource("/content/resource1", - "prop2", "value2"); - obj.page = context.create().page("/content/page1"); - obj.props = VALUEMAP_SAMPLE; - - assertEquals("[page=/content/page1,prop1=value1,props={prop1=value1, prop2=5},resource=/content/resource1]", - new AemObjectsReflectionToStringBuilder(obj, ToStringStyle.NO_CLASS_NAME_STYLE).build()); - } - - @Test - void testBuild_NullObjects() { - ClassWithFields obj = new ClassWithFields(); - - assertNotNull(new AemObjectsReflectionToStringBuilder(obj).build()); - } - - @Test - void testFilteredValueMap() { - Map filtered = AemObjectsReflectionToStringBuilder.filteredValueMap(VALUEMAP_SAMPLE); - - assertEquals(Map.of("prop1", "value1", "prop2", 5), filtered); - } - - @SuppressWarnings("unused") - @SuppressFBWarnings("URF_UNREAD_FIELD") - private static final class ClassWithFields { - String prop1; - Resource resource; - Page page; - ValueMap props; - } - -}