View Javadoc
1   /**
2    * This file Copyright (c) 2003-2018 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.cms.beans.runtime;
35  
36  import java.io.FileInputStream;
37  import java.io.FileNotFoundException;
38  import java.io.InputStream;
39  import java.io.Serializable;
40  
41  import org.apache.commons.io.IOUtils;
42  import org.apache.commons.lang3.StringUtils;
43  import org.slf4j.Logger;
44  import org.slf4j.LoggerFactory;
45  
46  
47  /**
48   * A wrapper for a temporary uploaded file.
49   */
50  public class Document implements Serializable {
51      private static final Logger log = LoggerFactory.getLogger(Document.class);
52  
53      /**
54       * request parameter name.
55       */
56      private String atomName;
57  
58      /**
59       * File name, without extension.
60       */
61      private String fileName;
62  
63      /**
64       * File extension.
65       */
66      private String extension;
67  
68      /**
69       * Mime type.
70       */
71      private String type;
72  
73      /**
74       * Underlying file.
75       */
76      private java.io.File file;
77  
78      /**
79       * A reference to the file input stream.
80       */
81      transient private FileInputStream inputStream;
82  
83      /**
84       * package private constructor.
85       */
86      Document() {
87      }
88  
89      /**
90       * Used to create a document based on a existing file.
91       */
92      public Document(java.io.File file, String type) {
93          String fileName = file.getName();
94          this.setFile(file);
95          this.setType(type);
96          this.setExtension(StringUtils.substringAfterLast(fileName, "."));
97          this.setFileName(StringUtils.substringBeforeLast(fileName, "."));
98      }
99  
100     /**
101      * Sets the parameter name.
102      *
103      * @param name parameter name
104      */
105     public void setAtomName(String name) {
106         this.atomName = name;
107     }
108 
109     /**
110      * Returns the parameter name.
111      *
112      * @return parameter name
113      */
114     public String getAtomName() {
115         return this.atomName;
116     }
117 
118     /**
119      * Sets the file name without extension.
120      *
121      * @param name file name without extension
122      */
123     public void setFileName(String name) {
124         this.fileName = name;
125     }
126 
127     /**
128      * Returns the file name without extension.
129      *
130      * @return file name
131      */
132     public String getFileName() {
133         return this.fileName;
134     }
135 
136     /**
137      * Returns the full file name with extension (if existing).
138      *
139      * @return file name with extension
140      */
141     public String getFileNameWithExtension() {
142         if (StringUtils.isEmpty(this.extension)) {
143             return this.fileName;
144         }
145 
146         return this.fileName + "." + this.extension;
147     }
148 
149     /**
150      * Sets the mime type for this file.
151      *
152      * @param type mime type
153      */
154     public void setType(String type) {
155         this.type = type;
156     }
157 
158     /**
159      * Returns the mime type for this file.
160      *
161      * @return mime type
162      */
163     public String getType() {
164         return this.type;
165     }
166 
167     /**
168      * Sets a reference to the uploaded file.
169      *
170      * @param in file
171      */
172     public void setFile(java.io.File in) {
173         this.file = in;
174     }
175 
176     public void setExtension(String ext) {
177         this.extension = ext;
178     }
179 
180     /**
181      * Returns the file extension.
182      *
183      * @return file extension
184      */
185     public String getExtension() {
186         return this.extension;
187     }
188 
189     /**
190      * Returns the file length in bytes.
191      *
192      * @return file length
193      */
194     public long getLength() {
195         return this.file.length();
196     }
197 
198     /**
199      * Returns a stream from the uploaded file. Note that subsequent invocation will always return a reference to the
200      * same input stream.
201      *
202      * @return stream from the uploaded file
203      */
204     public InputStream getStream() {
205         try {
206             return (this.inputStream = (new FileInputStream(this.file)));
207         } catch (FileNotFoundException e) {
208             log.error("Failed to locate file {}", this.file.getAbsolutePath());
209             return null;
210         }
211     }
212 
213     /**
214      * Returns the uploaded file. Users should normally use getStream, but getFile() can be used when you need to
215      * repeatedly access the file. <strong>The obtained file should never be deleted by the caller</strong>
216      *
217      * @return a reference to the uploaded file.
218      */
219     public java.io.File getFile() {
220         return this.file;
221     }
222 
223     /**
224      * Delete the file, taking care of closing an open input stream.
225      */
226     public void delete() {
227         IOUtils.closeQuietly(inputStream);
228         this.file.delete();
229     }
230 }