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..91c4b9e 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,21 +1,14 @@ 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; @@ -52,7 +45,6 @@ public String getDescription() { } public static class Factory { - private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); private final LocalizedPropertyNameProvider localizedPropertyNameProvider; @Inject @@ -86,14 +78,14 @@ 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)) + ); } } }