NextGenDynamicMediaAsset.java

  1. /*
  2.  * #%L
  3.  * wcm.io
  4.  * %%
  5.  * Copyright (C) 2024 wcm.io
  6.  * %%
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *      http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  * #L%
  19.  */
  20. package io.wcm.handler.mediasource.ngdm;

  21. import static com.day.cq.dam.api.DamConstants.DC_DESCRIPTION;
  22. import static com.day.cq.dam.api.DamConstants.DC_TITLE;

  23. import org.apache.commons.lang3.StringUtils;
  24. import org.apache.commons.lang3.builder.ToStringBuilder;
  25. import org.apache.sling.api.resource.Resource;
  26. import org.apache.sling.api.resource.ValueMap;
  27. import org.jetbrains.annotations.NotNull;
  28. import org.jetbrains.annotations.Nullable;

  29. import io.wcm.handler.media.Asset;
  30. import io.wcm.handler.media.MediaArgs;
  31. import io.wcm.handler.media.Rendition;
  32. import io.wcm.handler.media.UriTemplate;
  33. import io.wcm.handler.media.UriTemplateType;
  34. import io.wcm.handler.mediasource.ngdm.impl.NextGenDynamicMediaContext;
  35. import io.wcm.handler.mediasource.ngdm.impl.metadata.NextGenDynamicMediaMetadata;

  36. /**
  37.  * {@link Asset} implementation for Next Gen. Dynamic Media remote assets.
  38.  */
  39. final class NextGenDynamicMediaAsset implements Asset {

  40.   private final NextGenDynamicMediaContext context;
  41.   private final MediaArgs defaultMediaArgs;
  42.   private final ValueMap properties;

  43.   NextGenDynamicMediaAsset(@NotNull NextGenDynamicMediaContext context) {
  44.     this.context = context;
  45.     this.defaultMediaArgs = context.getDefaultMediaArgs();

  46.     NextGenDynamicMediaMetadata metadata = context.getMetadata();
  47.     if (metadata != null) {
  48.       this.properties = metadata.getProperties();
  49.     }
  50.     else {
  51.       this.properties = ValueMap.EMPTY;
  52.     }
  53.   }

  54.   @Override
  55.   public @Nullable String getTitle() {
  56.     return StringUtils.defaultString(properties.get(DC_TITLE, String.class),
  57.         context.getReference().getFileName());
  58.   }

  59.   @Override
  60.   public @Nullable String getAltText() {
  61.     if (defaultMediaArgs.isDecorative()) {
  62.       return "";
  63.     }
  64.     if (!defaultMediaArgs.isForceAltValueFromAsset() && StringUtils.isNotEmpty(defaultMediaArgs.getAltText())) {
  65.       return defaultMediaArgs.getAltText();
  66.     }
  67.     return StringUtils.defaultString(getDescription(), getTitle());
  68.   }

  69.   @Override
  70.   public @Nullable String getDescription() {
  71.     return properties.get(DC_DESCRIPTION, String.class);
  72.   }

  73.   @Override
  74.   public @Nullable String getPath() {
  75.     return context.getReference().toReference();
  76.   }

  77.   @Override
  78.   public @NotNull ValueMap getProperties() {
  79.     return properties;
  80.   }

  81.   @Override
  82.   public @Nullable Rendition getDefaultRendition() {
  83.     return getRendition(defaultMediaArgs);
  84.   }

  85.   @Override
  86.   public @Nullable Rendition getRendition(@NotNull MediaArgs mediaArgs) {
  87.     Rendition rendition = new NextGenDynamicMediaRendition(context, mediaArgs);

  88.     // check if rendition is valid - otherwise return null
  89.     if (StringUtils.isEmpty(rendition.getUrl())) {
  90.       rendition = null;
  91.     }

  92.     return rendition;
  93.   }

  94.   @Override
  95.   public @Nullable Rendition getImageRendition(@NotNull MediaArgs mediaArgs) {
  96.     Rendition rendition = getRendition(mediaArgs);
  97.     if (rendition != null && rendition.isImage()) {
  98.       return rendition;
  99.     }
  100.     return null;
  101.   }

  102.   @Override
  103.   public @Nullable Rendition getDownloadRendition(@NotNull MediaArgs mediaArgs) {
  104.     Rendition rendition = getRendition(mediaArgs);
  105.     if (rendition != null && rendition.isDownload()) {
  106.       return rendition;
  107.     }
  108.     return null;
  109.   }

  110.   @Override
  111.   public @NotNull UriTemplate getUriTemplate(@NotNull UriTemplateType type) {
  112.     return new NextGenDynamicMediaUriTemplate(context, type);
  113.   }

  114.   @Override
  115.   @SuppressWarnings("unchecked")
  116.   public <AdapterType> @Nullable AdapterType adaptTo(@NotNull Class<AdapterType> type) {
  117.     com.day.cq.dam.api.Asset asset = context.getReference().getAsset();
  118.     if (asset != null) {
  119.       if (type == com.day.cq.dam.api.Asset.class) {
  120.         return (AdapterType)asset;
  121.       }
  122.       if (type == Resource.class) {
  123.         return (AdapterType)asset.adaptTo(Resource.class);
  124.       }
  125.     }
  126.     return null;
  127.   }

  128.   @Override
  129.   public String toString() {
  130.     ToStringBuilder sb = new ToStringBuilder(this)
  131.         .append("reference", context.getReference())
  132.         .append("metadata", context.getMetadata());
  133.     com.day.cq.dam.api.Asset asset = context.getReference().getAsset();
  134.     if (asset != null) {
  135.       sb.append("asset", asset.getPath());
  136.     }
  137.     return sb.toString();
  138.   }

  139. }