diff --git a/src/main/java/com/merkle/oss/magnolia/imaging/flexible/model/LocalizedAsset.java b/src/main/java/com/merkle/oss/magnolia/imaging/flexible/model/LocalizedAsset.java index df3735d..df9fea4 100644 --- a/src/main/java/com/merkle/oss/magnolia/imaging/flexible/model/LocalizedAsset.java +++ b/src/main/java/com/merkle/oss/magnolia/imaging/flexible/model/LocalizedAsset.java @@ -1,20 +1,16 @@ package com.merkle.oss.magnolia.imaging.flexible.model; import com.merkle.oss.magnolia.imaging.flexible.util.LocalizedPropertyNameProvider; -import info.magnolia.context.MgnlContext; import info.magnolia.dam.api.Asset; import info.magnolia.dam.api.AssetDecorator; +import info.magnolia.dam.jcr.AbstractJcrItem; import info.magnolia.dam.jcr.AssetNodeTypes; -import info.magnolia.dam.jcr.DamConstants; import info.magnolia.jcr.util.PropertyUtil; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.inject.Inject; -import javax.jcr.Node; -import javax.jcr.RepositoryException; -import javax.jcr.Session; import java.lang.invoke.MethodHandles; import java.util.Locale; import java.util.Optional; @@ -86,14 +82,18 @@ private String getDescription(final Locale locale, final Asset asset) { } private Optional getLocalizedStringFromNode(final Asset asset, final Locale locale, final String key) { - try { - final Session damSession = MgnlContext.getJCRSession(DamConstants.WORKSPACE); - final Node assetNode = damSession.getNode(asset.getPath()); - return Optional.ofNullable(PropertyUtil.getString(assetNode, localizedPropertyNameProvider.get(assetNode, key, locale))); - } catch (RepositoryException e) { - LOG.error("failed to get localized string from node locale: " + locale + " key " + key + " asset: " + asset.getPath(), e); - return Optional.empty(); - } + return Optional + .of(asset) + .filter(AbstractJcrItem.class::isInstance) + .map(AbstractJcrItem.class::cast) + .map(AbstractJcrItem::getNode) + .map(assetNode -> + PropertyUtil.getString(assetNode, localizedPropertyNameProvider.get(assetNode, key, locale)) + ) + .or(() -> { + LOG.warn("failed to get localized string from node locale: {} key {} asset: {}", locale, key, asset.getPath()); + return Optional.empty(); + }); } } }