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.ui.framework.util;
35
36 import info.magnolia.cms.core.Path;
37
38 import java.io.File;
39 import java.io.FileInputStream;
40 import java.io.FileNotFoundException;
41 import java.io.FileOutputStream;
42 import java.io.IOException;
43 import java.io.InputStream;
44 import java.io.OutputStream;
45
46 import org.apache.commons.lang3.StringUtils;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 import com.vaadin.server.DownloadStream;
51 import com.vaadin.server.StreamResource;
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 public class TempFileStreamResource extends StreamResource {
68
69 private static final Logger log = LoggerFactory.getLogger(TempFileStreamResource.class);
70
71 public TempFileStreamResource(String filename) {
72 super(new TempFileStreamSource(), filename);
73 setCacheTime(-1);
74 }
75
76 public TempFileStreamResource() {
77 this(null);
78 }
79
80 @Override
81 public TempFileStreamSource getStreamSource() {
82 return (TempFileStreamSource) super.getStreamSource();
83 }
84
85 @Override
86 public DownloadStream getStream() {
87 final DownloadStream stream = super.getStream();
88 if (!StringUtils.isBlank(getFilename())) {
89 stream.setParameter("Content-Disposition", "attachment;filename=\"" + getFilename() + "\"");
90 }
91 return stream;
92 }
93
94 public void setTempFileName(String name) {
95 getStreamSource().tempFileName = name;
96 }
97
98 public void setTempFileExtension(String extension) {
99 getStreamSource().tempFileExtension = extension;
100 }
101
102 public OutputStream getTempFileOutputStream() throws IOException {
103 return getStreamSource().getOutputStream();
104 }
105
106 private static class TempFileStreamSource implements StreamSource {
107
108 private String tempFileName;
109
110 private String tempFileExtension;
111
112 private File tempFile = null;
113
114 private FileOutputStream fileOutputStream = null;
115
116 @Override
117 public InputStream getStream() {
118 try {
119 return new DeleteOnCloseFileInputStream(tempFile);
120 } catch (FileNotFoundException e) {
121 return null;
122 }
123 }
124
125 OutputStream getOutputStream() throws IOException {
126 if (fileOutputStream == null) {
127 fileOutputStream = new FileOutputStream(getTempFile());
128 }
129 return fileOutputStream;
130 }
131
132 File getTempFile() throws IOException {
133 if (tempFile == null) {
134
135
136 tempFile = File.createTempFile(tempFileName, tempFileExtension, Path.getTempDirectory());
137
138 }
139 return tempFile;
140 }
141 }
142
143 private static class DeleteOnCloseFileInputStream extends FileInputStream {
144
145 private File file;
146
147 public DeleteOnCloseFileInputStream(File file) throws FileNotFoundException {
148 super(file);
149 this.file = file;
150 }
151
152 @Override
153 public void close() throws IOException {
154 super.close();
155 if (file.exists() && !file.delete()) {
156 log.warn("Could not delete temporary export file {}", file.getAbsolutePath());
157 }
158 }
159
160 }
161 }