1
2
3
4 package org.rcfaces.core.internal.content;
5
6 import java.io.File;
7 import java.io.FileInputStream;
8 import java.io.FileOutputStream;
9 import java.io.IOException;
10 import java.io.InputStream;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.rcfaces.core.internal.Constants;
15 import org.rcfaces.core.internal.webapp.ExtendedHttpServlet;
16
17
18
19
20
21
22 public class FileBuffer implements IFileBuffer {
23 private static final String REVISION = "$Revision: 1.2 $";
24
25 private static final Log LOG = LogFactory.getLog(FileBuffer.class);
26
27 private static final String TEMP_FILE_PREFIX = "fileBuffer_";
28
29 private static final String TEMP_FILE_SUFFIX;
30 static {
31 TEMP_FILE_SUFFIX = "." + Constants.getVersion() + ".dat";
32 }
33
34 private static boolean securityError;
35
36 private final String bufferName;
37
38 private File file;
39
40 private String hash;
41
42 private String etag;
43
44 private int size;
45
46 private long lastModified;
47
48 private String contentType;
49
50 private boolean errored;
51
52 private String redirectedURL;
53
54 public FileBuffer(String bufferName) {
55 this.bufferName = bufferName;
56 }
57
58 public boolean isInitialized() {
59 return contentType != null;
60 }
61
62 public String getRedirection() {
63 return redirectedURL;
64 }
65
66 public void initializeRedirection(String url) {
67 this.redirectedURL = url;
68 }
69
70 public void initialize(String contentType, byte buffer[], long lastModified)
71 throws IOException {
72
73 if (LOG.isTraceEnabled()) {
74 LOG.trace("Initialize fileBuffer '" + bufferName + "'.");
75 }
76
77 this.contentType = contentType;
78
79 try {
80 file = File.createTempFile(TEMP_FILE_PREFIX, TEMP_FILE_SUFFIX);
81
82 } catch (IOException ex) {
83 LOG.error("Can not create temp file for file buffer ! (bufferName="
84 + bufferName + ")", ex);
85
86 throw ex;
87 }
88
89 if (LOG.isTraceEnabled()) {
90 LOG.trace("Temp file associated to '" + bufferName + "' is "
91 + file.getAbsolutePath());
92 }
93
94 try {
95 file.deleteOnExit();
96
97 } catch (SecurityException ex) {
98 if (securityError == false) {
99 securityError = true;
100
101 LOG.error(ex);
102 }
103 }
104
105 hash = ExtendedHttpServlet.computeHash(buffer);
106 etag = ExtendedHttpServlet.computeETag(buffer);
107 this.size = buffer.length;
108
109 FileOutputStream fileOutputStream = new FileOutputStream(file);
110 try {
111 fileOutputStream.write(buffer);
112
113 } catch (IOException ex) {
114 LOG.error("Can not write temp file for file buffer ! (bufferName="
115 + bufferName + ")", ex);
116
117 throw ex;
118
119 } finally {
120 fileOutputStream.close();
121 }
122
123 this.lastModified = lastModified;
124
125 if (LOG.isDebugEnabled()) {
126 LOG.debug("Store file buffer '" + bufferName + "' into " + size
127 + " bytes. (disk location='" + file.getAbsolutePath()
128 + "')");
129 }
130 }
131
132 public int getSize() {
133 return size;
134 }
135
136 public InputStream getContent() throws IOException {
137 return new FileInputStream(file);
138 }
139
140 public String getName() {
141 return bufferName;
142 }
143
144 public String getContentType() {
145 return contentType;
146 }
147
148 public long getModificationDate() {
149 return lastModified;
150 }
151
152 public String getHash() {
153 return hash;
154 }
155
156 public String getETag() {
157 return etag;
158 }
159
160 public boolean isErrored() {
161 return errored;
162 }
163
164 public void setErrored() {
165 errored = true;
166 }
167
168 public String toString() {
169 return "[FileBuffer name='" + bufferName + "' errored=" + errored
170 + " file='" + file + "' contentType='" + contentType
171 + "' lastModified=" + lastModified + " size=" + size + "]";
172 }
173
174 protected void finalize() throws Throwable {
175 if (file != null) {
176 if (LOG.isDebugEnabled()) {
177 LOG.error("Finalize file buffer, delete file='"
178 + file.getAbsolutePath() + "'.");
179 }
180
181 try {
182 file.delete();
183
184 } catch (Throwable th) {
185 LOG.error("Can not delete file '" + file.getAbsolutePath()
186 + "'.", th);
187 }
188 file = null;
189 }
190
191 super.finalize();
192 }
193
194 }