StoredCacheFile.java
3.9 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
/*
* 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 java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
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 StoredCacheFile
implements CacheFile {
private static Logger logger = LoggerFactory.getLogger(StoredCacheFile.class);
private final String key;
private final File file;
private Headers headers;
private String encoding;
public StoredCacheFile(String key, File file) {
this.key = key;
this.file = file;
}
public String getKey() {
return this.key;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public Headers getHeaders() {
if (this.headers == null) {
File headersFile = new File(this.file.getPath() + ".headers");
FileInputStream in = null;
try {
in = new FileInputStream(headersFile);
this.headers = new Headers();
this.headers.load(in);
}
catch (IOException e) {
logger.error("Unable to load headers.", (Throwable)e);
}
finally {
IOUtils.closeQuietly((InputStream)in);
}
}
return this.headers;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void spool(HttpServletResponse response) throws IOException {
InputStream in = this.openInputStream();
if (this.encoding != null) {
InputStreamReader reader = new InputStreamReader(in, this.encoding);
try {
IOUtils.copy((Reader)new InputStreamReader(in, this.encoding), (Writer)response.getWriter());
}
finally {
IOUtils.closeQuietly((Reader)reader);
}
}
try {
IOUtils.copy((InputStream)in, (OutputStream)response.getOutputStream());
}
finally {
IOUtils.closeQuietly((InputStream)in);
}
}
private InputStream openInputStream() throws IOException {
FileInputStream in = new FileInputStream(this.file);
try {
int len = in.read();
if (len != 0 && len < 255) {
int n;
byte[] b = new byte[len];
for (int off = 0; off < len; off += n) {
n = in.read(b, off, len - off);
if (n >= 0) continue;
throw new EOFException("Not enough bytes for encoding (" + len + " expected)");
}
this.encoding = new String(b, 0, b.length, "8859_1");
}
}
catch (IOException e) {
IOUtils.closeQuietly((InputStream)in);
throw e;
}
return in;
}
public ServletOutputStream getOutputStream(ServletOutputStream base) throws IOException {
throw new IllegalStateException("Read only");
}
public PrintWriter getWriter(PrintWriter base, String encoding) throws IOException {
throw new IllegalStateException("Read only");
}
public boolean save() {
throw new IllegalStateException("Read only");
}
public void discard() {
throw new IllegalStateException("Read only");
}
}