FilePassivationHandler.java 2.09 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.aemfd.docmanager.internal.passivation;

import com.adobe.aemfd.docmanager.passivation.AbstractPassivationHandler;
import com.adobe.aemfd.docmanager.passivation.PassivationConnection;
import com.adobe.aemfd.docmanager.passivation.PassivationType;
import com.adobe.aemfd.docmanager.source.DocumentSourceHandler;
import com.adobe.aemfd.docmanager.source.FileSourceHandler;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FilePassivationHandler
extends AbstractPassivationHandler {
    private static final Logger logger = LoggerFactory.getLogger(FilePassivationHandler.class);
    private File file;
    private boolean ownFile;

    public FilePassivationHandler(File file, boolean ownFile) {
        super(false, true, false);
        this.file = file;
        this.ownFile = ownFile;
    }

    public PassivationConnection openConnectionForPassivation() throws IOException {
        FileInputStream fis = new FileInputStream(this.file);
        String ct = URLConnection.guessContentTypeFromName(this.file.getName());
        return new PassivationConnection(fis, this.file.length(), ct);
    }

    protected DocumentSourceHandler doPassivate(byte[] preBuffer, PassivationConnection conn, long length) throws IOException {
        return new FileSourceHandler(this.file, conn.getContentType(), this.ownFile);
    }

    public void release(boolean passivated, PassivationType pType) {
        if (this.ownFile && (!passivated || pType.equals((Object)PassivationType.INLINED))) {
            String path = this.file.getAbsolutePath();
            if (!this.file.delete()) {
                logger.warn("Failed to delete managed file at location " + path + ". Please ensure that files passed to Document objects " + "for management do not have existing open handles.");
            }
        }
        this.file = null;
    }
}