Scene7AbstractImportHandler.java 18.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.jcr.JcrUtil
 *  com.day.cq.dam.api.Asset
 *  com.day.cq.dam.api.AssetManager
 *  com.day.cq.dam.api.Rendition
 *  com.day.cq.dam.api.handler.AssetHandler
 *  com.day.cq.dam.api.handler.store.AssetStore
 *  com.day.cq.dam.api.thumbnail.ThumbnailConfig
 *  com.day.cq.dam.commons.thumbnail.ThumbnailConfigImpl
 *  javax.jcr.LoginException
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.commons.lang.StringUtils
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.ReferencePolicy
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.commons.mime.MimeTypeService
 *  org.apache.sling.jcr.api.SlingRepository
 *  org.apache.sling.jcr.resource.JcrResourceResolverFactory
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.dam.scene7.impl.importer;

import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.AssetManager;
import com.day.cq.dam.api.Rendition;
import com.day.cq.dam.api.handler.AssetHandler;
import com.day.cq.dam.api.handler.store.AssetStore;
import com.day.cq.dam.api.thumbnail.ThumbnailConfig;
import com.day.cq.dam.commons.thumbnail.ThumbnailConfigImpl;
import com.day.cq.dam.scene7.api.S7Config;
import com.day.cq.dam.scene7.api.Scene7APIClient;
import com.day.cq.dam.scene7.api.Scene7FileMetadataService;
import com.day.cq.dam.scene7.api.importer.LockAssetUtils;
import com.day.cq.dam.scene7.api.importer.Scene7ImportHandler;
import com.day.cq.dam.scene7.api.importer.Scene7ImportHandlerLookupService;
import com.day.cq.dam.scene7.api.model.Scene7Asset;
import com.day.cq.dam.scene7.impl.utils.Scene7AssetUtils;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.jcr.LoginException;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.mime.MimeTypeService;
import org.apache.sling.jcr.api.SlingRepository;
import org.apache.sling.jcr.resource.JcrResourceResolverFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(componentAbstract=1)
public abstract class Scene7AbstractImportHandler
implements Scene7ImportHandler {
    private static final Logger LOG = LoggerFactory.getLogger(Scene7AbstractImportHandler.class);
    private static final String SLING_ORDERED_FOLDER = "sling:OrderedFolder";
    @Reference
    protected MimeTypeService mimeTypeService;
    @Reference
    protected Scene7APIClient scene7APIClient;
    @Reference
    protected Scene7ImportHandlerLookupService handlerLookupService;
    @Reference
    protected Scene7FileMetadataService scene7FileMetadataService;
    @Reference
    protected SlingRepository slingRepository;
    @Reference
    protected JcrResourceResolverFactory resourceResolverFactory;
    @Reference(policy=ReferencePolicy.STATIC)
    private AssetStore store;
    @Reference
    protected LockAssetUtils lockAssetUtils;
    private static final Collection<ThumbnailConfig> thumbnailConfigs;

    @Override
    public Asset importSubAsset(Asset asset, Scene7Asset subasset, S7Config s7Config) throws Exception {
        if (asset == null) {
            throw new NullPointerException("Null asset");
        }
        if (s7Config == null) {
            throw new NullPointerException("Null Scene7 configuration");
        }
        InputStream is = this.getAssetFileStream(subasset, s7Config);
        String mimeType = this.mimeTypeService.getMimeType(subasset.getFileName());
        Asset damSubasset = asset.addSubAsset(subasset.getFileName(), mimeType, is);
        if (damSubasset != null) {
            LOG.debug("Successfully imported sub-asset {}", (Object)damSubasset.getPath());
        } else {
            LOG.error("Unable to import sub-asset {} under {}", (Object)subasset.getFileName(), (Object)asset.getPath());
        }
        return damSubasset;
    }

    protected void createThumbnail(Asset asset) {
        AssetHandler handler = this.store.getAssetHandler("image/jpeg");
        try {
            Rendition rendition = asset.getRendition("s7thumbnail.jpeg");
            if (rendition != null) {
                handler.createThumbnails(asset, rendition, thumbnailConfigs);
            }
        }
        catch (IOException e) {
            LOG.error("Unable to create thumbnails for asset " + asset.getPath(), (Throwable)e);
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    @Override
    public void createAssetImportFolder(Scene7Asset asset, S7Config s7Config) throws Exception {
        if (asset == null) {
            throw new NullPointerException("Null asset");
        }
        if (s7Config == null) {
            throw new NullPointerException("Null Scene7 configuration");
        }
        String damImportPath = Scene7AssetUtils.getAbsoluteTargetPath(s7Config.getRootPath(), s7Config.getTargetPath(), asset.getFolder());
        ResourceResolver rr = null;
        Session session = null;
        if (damImportPath != null) {
            if (damImportPath.endsWith("/") && damImportPath.length() > 1) {
                damImportPath = damImportPath.substring(0, damImportPath.length() - 1);
            }
            try {
                session = this.getSlingScene7AssetServiceSession();
                rr = this.resourceResolverFactory.getResourceResolver(session);
                JcrUtil.createPath((String)damImportPath, (String)"sling:OrderedFolder", (String)"sling:OrderedFolder", (Session)session, (boolean)true);
            }
            finally {
                if (rr != null) {
                    rr.close();
                }
                if (session != null) {
                    session.logout();
                }
            }
        }
    }

    private boolean newRenditionsAreAvailable(Scene7Asset scene7Asset, Asset damAsset) {
        boolean newRenditionsAreAvailable = false;
        int importedRenditions = 0;
        if (scene7Asset != null && damAsset != null) {
            for (Rendition r : damAsset.getRenditions()) {
                if (!r.getName().startsWith("cq5dam.video.s7")) continue;
                ++importedRenditions;
            }
            if (importedRenditions < scene7Asset.getSubAssets().size()) {
                newRenditionsAreAvailable = true;
            }
        }
        return newRenditionsAreAvailable;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    protected Asset createOrUpdateAsset(ResourceResolver rr, S7Config s7Config, Scene7Asset scene7Asset, long s7AssetModifiedTime) throws Exception {
        String targetPath = null;
        Asset damAsset = null;
        targetPath = Scene7AssetUtils.getAbsoluteTargetPath(s7Config.getRootPath(), s7Config.getTargetPath(), this.getImportAsset(scene7Asset).getFolder() + this.getImportAssetFileName(this.getImportAsset(scene7Asset)));
        try {
            this.lockAssetUtils.lockAsset(targetPath);
            AssetManager assetManager = (AssetManager)rr.adaptTo(AssetManager.class);
            if (assetManager != null) {
                Resource damAssetResource = rr.getResource(targetPath);
                if (damAssetResource != null) {
                    damAsset = (Asset)damAssetResource.adaptTo(Asset.class);
                }
                long damAssetScene7LastModifedMetadata = 0;
                if (damAsset != null) {
                    try {
                        Resource metadataResource = damAssetResource.getChild("jcr:content/metadata");
                        if (metadataResource != null) {
                            ValueMap properties = (ValueMap)metadataResource.adaptTo(ValueMap.class);
                            damAssetScene7LastModifedMetadata = (Long)properties.get("dam:scene7LastModified", Long.class);
                        }
                    }
                    catch (Exception e) {
                        LOG.warn("Could not determine the last modified date in Scene7 for Asset {}", (Object)damAsset.getPath());
                    }
                }
                boolean isOlderVersion = damAsset != null && damAssetScene7LastModifedMetadata < s7AssetModifiedTime;
                LOG.debug("Asset {} DAM older imported version exists={}", (Object)targetPath, (Object)isOlderVersion);
                if (damAsset == null || isOlderVersion) {
                    InputStream is = this.getAssetFileStream(scene7Asset, s7Config);
                    if (is != null) {
                        String mimeType = this.getAssetMimeType(this.getImportAsset(scene7Asset));
                        damAsset = assetManager.createAsset(targetPath, is, mimeType, false);
                        if (damAsset != null) {
                            String originalPath;
                            damAsset.setBatchMode(true);
                            this.scene7FileMetadataService.setAssetMetadataProperty(damAsset, "dam:MIMEtype", mimeType);
                            Long wid = this.getImportMetadataAsset(scene7Asset).getWidth();
                            Long hei = this.getImportMetadataAsset(scene7Asset).getHeight();
                            this.scene7FileMetadataService.setAssetMetadataProperty(damAsset, "dam:scene7Width", wid != null ? wid : 250);
                            this.scene7FileMetadataService.setAssetMetadataProperty(damAsset, "dam:scene7Height", hei != null ? hei : 250);
                            this.scene7FileMetadataService.setAssetMetadataOnSync(damAsset, this.getImportMetadataAsset(scene7Asset), s7Config, "PublishComplete");
                            String originalFile = this.getImportMetadataAsset(scene7Asset).getOriginalFile();
                            if (originalFile != null) {
                                this.scene7FileMetadataService.setAssetMetadataProperty(damAsset, "dam:scene7OriginalFile", originalFile);
                            }
                            if ((originalPath = this.getImportMetadataAsset(scene7Asset).getOriginalPath()) != null) {
                                this.scene7FileMetadataService.setAssetMetadataProperty(damAsset, "dam:scene7OriginalPath", originalPath);
                            }
                            for (Map.Entry<String, String> entry : scene7Asset.getAssetProperties().entrySet()) {
                                String key = StringUtils.uncapitalize((String)entry.getKey().replaceAll(" ", ""));
                                String value = entry.getValue();
                                this.scene7FileMetadataService.setAssetMetadataProperty(damAsset, key, value);
                            }
                            is = this.scene7APIClient.getThumbnailForAsset(this.getImportMetadataAsset(scene7Asset), s7Config);
                            if (is != null) {
                                damAsset.addRendition("s7thumbnail.jpeg", is, "image/jpeg");
                                this.createThumbnail(damAsset);
                            } else {
                                LOG.error("Unable to create thumnbail for asset " + targetPath);
                            }
                            this.updateAssetMetadata(damAsset, scene7Asset, s7Config);
                        } else {
                            LOG.error("Unable to create asset at " + targetPath);
                        }
                    } else {
                        LOG.error("Received a null input stream from the Scene7 server for asset {} with asset handle {}", (Object)scene7Asset.getName(), (Object)scene7Asset.getAssetHandle());
                    }
                } else if (this.newRenditionsAreAvailable(scene7Asset, damAsset)) {
                    damAsset.setBatchMode(true);
                    this.updateAssetMetadata(damAsset, scene7Asset, s7Config);
                }
                if (damAsset != null) {
                    this.markAsProcessed(damAsset);
                }
            } else {
                LOG.error("Unable to obtain an AssetManager");
            }
            ((Session)rr.adaptTo(Session.class)).save();
        }
        finally {
            try {
                this.lockAssetUtils.unlockAsset(targetPath);
            }
            catch (Exception e) {
                LOG.error("Unable to unlock S7 asset lock for " + targetPath, (Throwable)e);
            }
        }
        return damAsset;
    }

    protected void markAsProcessed(Asset damAsset) {
        if (damAsset != null) {
            try {
                this.scene7FileMetadataService.setAssetMetadataProperty(damAsset, "dam:scene7ImportProcessed", String.valueOf(true));
            }
            catch (Exception e) {
                LOG.error("Could not mark DAM asset " + damAsset.getPath() + " as processed!", (Throwable)e);
            }
        }
    }

    protected void updateAssetMetadata(Asset damAsset, Scene7Asset scene7Asset, S7Config s7Config) {
        if (damAsset != null && scene7Asset != null) {
            try {
                this.scene7FileMetadataService.setAssetMetadataProperty(damAsset, "dam:scene7PollingImporterMarker", "");
            }
            catch (Exception e) {
                LOG.error("Unable to mark asset " + damAsset.getPath() + " as touched by the Scene7 Polling Importer", (Throwable)e);
            }
        }
    }

    protected Scene7Asset getImportAsset(Scene7Asset scene7Asset) {
        return scene7Asset;
    }

    protected Scene7Asset getImportMetadataAsset(Scene7Asset scene7Asset) {
        return scene7Asset;
    }

    protected String getAssetMimeType(Scene7Asset scene7Asset) {
        return this.mimeTypeService.getMimeType(this.getImportAssetFileName(scene7Asset));
    }

    protected InputStream getAssetFileStream(Scene7Asset scene7Asset, S7Config s7Config) {
        return this.scene7APIClient.getOriginalFile(this.getImportAsset(scene7Asset).getAssetHandle(), s7Config);
    }

    protected String getImportAssetFileName(Scene7Asset scene7Asset) {
        String damFileName = "";
        damFileName = StringUtils.isNotEmpty((String)scene7Asset.getFileName()) ? scene7Asset.getFileName() : scene7Asset.getName();
        return damFileName;
    }

    protected Session getSlingScene7AssetServiceSession() {
        Session session = null;
        try {
            session = this.slingRepository.loginService("scene7assetservice", null);
        }
        catch (LoginException e) {
            LOG.error("Unable to obtain asset session. ", (Throwable)e);
        }
        catch (RepositoryException e) {
            LOG.error("Unable to obtain asset session. ", (Throwable)e);
        }
        return session;
    }

    static {
        ThumbnailConfigImpl config1 = new ThumbnailConfigImpl(140, 100, false);
        ThumbnailConfigImpl config2 = new ThumbnailConfigImpl(48, 48, false);
        ThumbnailConfigImpl config3 = new ThumbnailConfigImpl(319, 319, false);
        thumbnailConfigs = Arrays.asList(new ThumbnailConfig[]{config1, config2, config3});
    }

    protected void bindMimeTypeService(MimeTypeService mimeTypeService) {
        this.mimeTypeService = mimeTypeService;
    }

    protected void unbindMimeTypeService(MimeTypeService mimeTypeService) {
        if (this.mimeTypeService == mimeTypeService) {
            this.mimeTypeService = null;
        }
    }

    protected void bindScene7APIClient(Scene7APIClient scene7APIClient) {
        this.scene7APIClient = scene7APIClient;
    }

    protected void unbindScene7APIClient(Scene7APIClient scene7APIClient) {
        if (this.scene7APIClient == scene7APIClient) {
            this.scene7APIClient = null;
        }
    }

    protected void bindHandlerLookupService(Scene7ImportHandlerLookupService scene7ImportHandlerLookupService) {
        this.handlerLookupService = scene7ImportHandlerLookupService;
    }

    protected void unbindHandlerLookupService(Scene7ImportHandlerLookupService scene7ImportHandlerLookupService) {
        if (this.handlerLookupService == scene7ImportHandlerLookupService) {
            this.handlerLookupService = null;
        }
    }

    protected void bindScene7FileMetadataService(Scene7FileMetadataService scene7FileMetadataService) {
        this.scene7FileMetadataService = scene7FileMetadataService;
    }

    protected void unbindScene7FileMetadataService(Scene7FileMetadataService scene7FileMetadataService) {
        if (this.scene7FileMetadataService == scene7FileMetadataService) {
            this.scene7FileMetadataService = null;
        }
    }

    protected void bindSlingRepository(SlingRepository slingRepository) {
        this.slingRepository = slingRepository;
    }

    protected void unbindSlingRepository(SlingRepository slingRepository) {
        if (this.slingRepository == slingRepository) {
            this.slingRepository = null;
        }
    }

    protected void bindResourceResolverFactory(JcrResourceResolverFactory jcrResourceResolverFactory) {
        this.resourceResolverFactory = jcrResourceResolverFactory;
    }

    protected void unbindResourceResolverFactory(JcrResourceResolverFactory jcrResourceResolverFactory) {
        if (this.resourceResolverFactory == jcrResourceResolverFactory) {
            this.resourceResolverFactory = null;
        }
    }

    protected void bindStore(AssetStore assetStore) {
        this.store = assetStore;
    }

    protected void unbindStore(AssetStore assetStore) {
        if (this.store == assetStore) {
            this.store = null;
        }
    }

    protected void bindLockAssetUtils(LockAssetUtils lockAssetUtils) {
        this.lockAssetUtils = lockAssetUtils;
    }

    protected void unbindLockAssetUtils(LockAssetUtils lockAssetUtils) {
        if (this.lockAssetUtils == lockAssetUtils) {
            this.lockAssetUtils = null;
        }
    }
}