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.collections.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
60
61
62
63
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
73 public DelegatingBlobCachedEntry(long contentLength, String contentType, String characterEncoding, int statusCode, MultiMap headers, long modificationDate, String originalUrl, int timeToLiveInSeconds) throws IOException {
74 super(contentType, characterEncoding, statusCode, headers, modificationDate, originalUrl, timeToLiveInSeconds);
75 this.contentLength = contentLength;
76 }
77
78 @Override
79 protected boolean canServeGzipContent() {
80 return false;
81 }
82
83 @Override
84 public void replay(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
85 File contentFile = getContentFileBoundToTheRequest(request);
86 if(contentFile != null){
87 log.debug("About to serve response from {}", contentFile.getAbsolutePath());
88 super.replay(request, response, chain);
89 }
90 else{
91 chain.doFilter(request, response);
92 }
93 }
94
95 @Override
96 protected void writeContent(HttpServletRequest request, HttpServletResponse response, FilterChain chain, boolean acceptsGzipEncoding) throws IOException, ServletException {
97 response.setContentLength((int) contentLength);
98
99 File contentFile = getContentFileBoundToTheRequest(request);
100 if(contentFile != null){
101 log.debug("Streaming out output from {}", contentFile.getAbsolutePath());
102 FileInputStream contentStream = FileUtils.openInputStream(contentFile);
103 try{
104 IOUtils.copy(contentStream,response.getOutputStream());
105 }finally{
106 IOUtils.closeQuietly(contentStream);
107 log.debug("About to delete temp file {}", contentFile.getAbsolutePath());
108 if(!contentFile.delete()){
109 log.error("Can't delete file: " + contentFile);
110 }
111 }
112 }
113 else{
114
115 throw new IllegalStateException("No content file attached to the request!");
116 }
117 }
118
119 private File getContentFileBoundToTheRequest(HttpServletRequest request) {
120 return (File) request.getAttribute(CONTENT_FILE_ATTRIBUTE);
121 }
122
123 public void bindContentFileToCurrentRequest(HttpServletRequest request, File contentFile){
124 request.setAttribute(DelegatingBlobCachedEntry.CONTENT_FILE_ATTRIBUTE, contentFile);
125 }
126
127 }