TemporaryCacheFile.java 5.01 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.servlet.ServletOutputStream
 *  javax.servlet.http.HttpServletResponse
 *  org.apache.commons.io.IOUtils
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.httpcache.file;

import com.adobe.granite.httpcache.api.CacheFile;
import com.adobe.granite.httpcache.api.Headers;
import com.adobe.granite.httpcache.utils.TeeServletOutputStream;
import com.adobe.granite.httpcache.utils.TeeWriter;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class TemporaryCacheFile
implements CacheFile {
    private static Logger logger = LoggerFactory.getLogger(TemporaryCacheFile.class);
    private final String key;
    private final Headers headers;
    private final File file;
    private File tmpFile;
    private TeeServletOutputStream sos;
    private PrintWriter pw;
    private TeeWriter tw;

    public TemporaryCacheFile(String key, Headers headers, File file) {
        this.key = key;
        this.headers = headers;
        this.file = file;
    }

    public String getKey() {
        return this.key;
    }

    public Headers getHeaders() {
        return this.headers;
    }

    public void spool(HttpServletResponse response) throws IOException {
        throw new IllegalStateException("Write only");
    }

    public ServletOutputStream getOutputStream(ServletOutputStream base) throws IOException {
        if (this.sos == null) {
            this.sos = new TeeServletOutputStream(base, this.key){

                protected OutputStream createOutputStream() throws IOException {
                    return TemporaryCacheFile.this.createCacheFileOutputStream(null);
                }
            };
        }
        return this.sos;
    }

    public PrintWriter getWriter(PrintWriter base, final String encoding) throws IOException {
        if (this.pw == null) {
            this.tw = new TeeWriter(base, this.key){

                protected Writer createWriter() throws IOException {
                    OutputStream out = TemporaryCacheFile.this.createCacheFileOutputStream(encoding);
                    return new BufferedWriter(new OutputStreamWriter(out, encoding));
                }
            };
            this.pw = new PrintWriter(this.tw);
        }
        return this.pw;
    }

    private OutputStream createCacheFileOutputStream(String encoding) throws IOException {
        File parentDir = this.file.getParentFile();
        if (!parentDir.exists()) {
            parentDir.mkdirs();
        }
        this.tmpFile = File.createTempFile("cache", null, parentDir);
        FileOutputStream out = new FileOutputStream(this.tmpFile);
        if (encoding != null) {
            byte[] b = encoding.getBytes("8859_1");
            out.write(b.length);
            out.write(b);
        } else {
            out.write(0);
        }
        return out;
    }

    public boolean save() {
        if (this.tw != null) {
            IOUtils.closeQuietly((Writer)this.tw);
            if (this.tw.hasError()) {
                logger.warn("Unable to cache: Response writer reported error.");
                this.tmpFile.delete();
                return false;
            }
        } else {
            IOUtils.closeQuietly((OutputStream)((Object)this.sos));
            if (this.sos.hasError()) {
                logger.warn("Unable to cache: Response output stream reported error.");
                this.tmpFile.delete();
                return false;
            }
        }
        if (!this.saveHeaders() || !this.tmpFile.renameTo(this.file)) {
            logger.warn("Unable to rename temporary file to cache target, deleting {}.", (Object)this.tmpFile);
            this.tmpFile.delete();
            return false;
        }
        return true;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private boolean saveHeaders() {
        File headersFile = new File(this.file.getPath() + ".headers");
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(headersFile);
            this.headers.save(out);
            boolean bl = true;
            return bl;
        }
        catch (IOException e) {
            logger.warn("Unable to save headers to: " + headersFile, (Throwable)e);
            boolean bl = false;
            return bl;
        }
        finally {
            IOUtils.closeQuietly((OutputStream)out);
        }
    }

    public void discard() {
        if (this.tw != null) {
            IOUtils.closeQuietly((Writer)this.tw);
        } else {
            IOUtils.closeQuietly((OutputStream)((Object)this.sos));
        }
        if (this.tmpFile != null) {
            this.tmpFile.delete();
        }
    }

}