1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.efaps.admin.program.bundle;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.util.List;
30 import java.util.zip.GZIPOutputStream;
31
32 import org.apache.commons.io.FileUtils;
33 import org.efaps.admin.AbstractAdminObject;
34 import org.efaps.admin.AppConfigHandler;
35 import org.efaps.db.Checkout;
36 import org.efaps.util.EFapsException;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40
41
42
43
44
45
46 public class TempFileBundle
47 implements BundleInterface
48 {
49
50
51
52
53 public static final String TMPFOLDERNAME = "eFapsFileBundles";
54
55
56
57
58 private static final Logger LOG = LoggerFactory.getLogger(AbstractAdminObject.class);
59
60
61
62
63 private static File TMPFOLDER = new File("");
64 static {
65 File tmpfld = AppConfigHandler.get().getTempFolder();
66 if (tmpfld == null) {
67 File temp;
68 try {
69 temp = File.createTempFile("eFaps", ".tmp");
70 tmpfld = temp.getParentFile();
71 temp.delete();
72 } catch (final IOException e) {
73 TempFileBundle.LOG.error("Cannot create temp file", e);
74 }
75
76 }
77 TempFileBundle.TMPFOLDER = new File(tmpfld, TempFileBundle.TMPFOLDERNAME);
78 if (!TempFileBundle.TMPFOLDER.exists()) {
79 final boolean mkdir = TempFileBundle.TMPFOLDER.mkdir();
80 if (!mkdir) {
81 TempFileBundle.LOG.error("Temp folder was not created");
82 }
83 }
84 }
85
86
87
88
89 private File file = null;
90
91
92
93
94 private File gzipFile = null;
95
96
97
98
99 private final long created;
100
101
102
103
104 private String contentType = "text/plain";
105
106
107
108
109 private List<String> oids;
110
111
112
113
114 private String key;
115
116
117
118
119 public TempFileBundle()
120 {
121 this.created = System.currentTimeMillis();
122 }
123
124
125
126
127 @Override
128 public synchronized InputStream getInputStream(final boolean _gziped)
129 throws EFapsException
130 {
131 InputStream ret = null;
132 try {
133 if (_gziped) {
134 if (this.gzipFile == null || !this.gzipFile.exists()) {
135 this.gzipFile = setFile(true);
136 }
137 ret = new FileInputStream(this.gzipFile);
138 } else {
139 if (this.file == null || !this.file.exists()) {
140 this.file = setFile(false);
141 }
142 ret = new FileInputStream(this.file);
143 }
144 } catch (final FileNotFoundException e) {
145 throw new EFapsException(this.getClass(), "getInputStream", e);
146 }
147 return ret;
148 }
149
150
151
152
153 @Override
154 public long getCreationTime()
155 {
156 return this.created;
157 }
158
159
160
161
162
163
164 @Override
165 public String getContentType()
166 {
167 return this.contentType;
168 }
169
170
171
172
173
174
175 public void setContentType(final String _contentType)
176 {
177 this.contentType = _contentType;
178 }
179
180
181
182
183
184
185 public List<String> getOids()
186 {
187 return this.oids;
188 }
189
190
191
192
193
194
195 private File setFile(final boolean _gziped)
196 throws EFapsException
197 {
198 final String filename = _gziped ? this.key + "GZIP" : this.key;
199 final File ret = FileUtils.getFile(TempFileBundle.getTempFolder(), filename);
200 try {
201 final FileOutputStream out = new FileOutputStream(ret);
202 final byte[] buffer = new byte[1024];
203 int bytesRead;
204 if (_gziped) {
205 final GZIPOutputStream zout = new GZIPOutputStream(out);
206 for (final String oid : this.oids) {
207 final Checkout checkout = new Checkout(oid);
208 final InputStream bis = checkout.execute();
209 while ((bytesRead = bis.read(buffer)) != -1) {
210 zout.write(buffer, 0, bytesRead);
211 }
212 }
213 zout.close();
214 } else {
215 for (final String oid : this.oids) {
216 final Checkout checkout = new Checkout(oid);
217 final InputStream bis = checkout.execute();
218 while ((bytesRead = bis.read(buffer)) != -1) {
219 out.write(buffer, 0, bytesRead);
220 }
221 }
222 }
223 out.close();
224 } catch (final IOException e) {
225 throw new EFapsException(this.getClass(), "setFile", e, filename);
226 }
227 return ret;
228 }
229
230
231
232
233
234 public static File getTempFolder()
235 throws EFapsException
236 {
237 return TempFileBundle.TMPFOLDER;
238 }
239
240
241
242
243 @Override
244 public void setKey(final String _key,
245 final List<String> _oids)
246 {
247 this.key = _key;
248 this.oids = _oids;
249 }
250 }