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 |
|
package info.magnolia.module.cache.filter; |
35 |
|
|
36 |
|
import java.io.File; |
37 |
|
import java.io.FileInputStream; |
38 |
|
import java.io.IOException; |
39 |
|
|
40 |
|
import javax.servlet.FilterChain; |
41 |
|
import javax.servlet.ServletException; |
42 |
|
import javax.servlet.http.HttpServletRequest; |
43 |
|
import javax.servlet.http.HttpServletResponse; |
44 |
|
|
45 |
|
import org.apache.commons.collections4.MultiMap; |
46 |
|
import org.apache.commons.io.FileUtils; |
47 |
|
import org.apache.commons.io.IOUtils; |
48 |
|
import org.slf4j.Logger; |
49 |
|
import org.slf4j.LoggerFactory; |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
@link |
60 |
|
|
61 |
|
@version |
62 |
|
@see |
63 |
|
|
|
|
| 52.9% |
Uncovered Elements: 16 (34) |
Complexity: 9 |
Complexity Density: 0.41 |
|
64 |
|
public class DelegatingBlobCachedEntry extends ContentCachedEntry { |
65 |
|
|
66 |
|
private static Logger log = LoggerFactory.getLogger(DelegatingBlobCachedEntry.class); |
67 |
|
|
68 |
|
|
69 |
|
public static final String CONTENT_FILE_ATTRIBUTE = DelegatingBlobCachedEntry.class.getName() + ".contentFile"; |
70 |
|
|
71 |
|
private final long contentLength; |
72 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
73 |
1 |
public DelegatingBlobCachedEntry(long contentLength, String contentType, String characterEncoding, int statusCode, MultiMap headers, long modificationDate, String originalUrl, int timeToLiveInSeconds) throws IOException {... |
74 |
1 |
super(contentType, characterEncoding, statusCode, headers, modificationDate, originalUrl, timeToLiveInSeconds); |
75 |
1 |
this.contentLength = contentLength; |
76 |
|
} |
77 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
78 |
0 |
@Override... |
79 |
|
protected boolean canServeGzipContent() { |
80 |
0 |
return false; |
81 |
|
} |
82 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
83 |
0 |
@Override... |
84 |
|
public void replay(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException { |
85 |
0 |
File contentFile = getContentFileBoundToTheRequest(request); |
86 |
0 |
if(contentFile != null){ |
87 |
0 |
log.debug("About to serve response from {}", contentFile.getAbsolutePath()); |
88 |
0 |
super.replay(request, response, chain); |
89 |
|
} |
90 |
|
else{ |
91 |
0 |
chain.doFilter(request, response); |
92 |
|
} |
93 |
|
} |
94 |
|
|
|
|
| 75% |
Uncovered Elements: 4 (16) |
Complexity: 3 |
Complexity Density: 0.25 |
|
95 |
1 |
@Override... |
96 |
|
protected void writeContent(HttpServletRequest request, HttpServletResponse response, FilterChain chain, boolean acceptsGzipEncoding) throws IOException, ServletException { |
97 |
1 |
response.setContentLength((int) contentLength); |
98 |
|
|
99 |
1 |
File contentFile = getContentFileBoundToTheRequest(request); |
100 |
1 |
if(contentFile != null){ |
101 |
1 |
log.debug("Streaming out output from {}", contentFile.getAbsolutePath()); |
102 |
1 |
FileInputStream contentStream = FileUtils.openInputStream(contentFile); |
103 |
1 |
try{ |
104 |
1 |
IOUtils.copy(contentStream,response.getOutputStream()); |
105 |
|
}finally{ |
106 |
1 |
IOUtils.closeQuietly(contentStream); |
107 |
1 |
log.debug("About to delete temp file {}", contentFile.getAbsolutePath()); |
108 |
1 |
if(!contentFile.delete()){ |
109 |
0 |
log.error("Can't delete file: " + contentFile); |
110 |
|
} |
111 |
|
} |
112 |
|
} |
113 |
|
else{ |
114 |
|
|
115 |
0 |
throw new IllegalStateException("No content file attached to the request!"); |
116 |
|
} |
117 |
|
} |
118 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
119 |
1 |
private File getContentFileBoundToTheRequest(HttpServletRequest request) {... |
120 |
1 |
return (File) request.getAttribute(CONTENT_FILE_ATTRIBUTE); |
121 |
|
} |
122 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
123 |
0 |
public void bindContentFileToCurrentRequest(HttpServletRequest request, File contentFile){... |
124 |
0 |
request.setAttribute(DelegatingBlobCachedEntry.CONTENT_FILE_ATTRIBUTE, contentFile); |
125 |
|
} |
126 |
|
|
127 |
|
} |