View Javadoc
1   /**
2    * This file Copyright (c) 2015-2017 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * {@link StreamSource} version which organises streaming via a temporary file.
55   *
56   * Streamed content can be populated via {@link OutputStream} provided by {@link #getTempFileOutputStream()}.
57   * In order to set up the resource the following attributes are required to be set:
58   * <ul>
59   * <li>tempFileName - name of the buffer temporary file</li>
60   * <li>tempFileExtension - extension of the buffer temporary file</li>
61   * <li>fileName - the name of the downloaded file</li>
62   * </ul>
63   *
64   * <strong>Note:</strong> the streamed resource is by default configured to be not cached
65   * <strong>Note:</strong> the temporary file is attempted to be eagerly deleted once the streaming is over
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                 // Create a temporary file that will hold the data created by the export command.
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 }