TemporaryCacheFile.java
5.01 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
/*
* 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();
}
}
}