1 /** 2 * This file Copyright (c) 2003-2013 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 org.apache.commons.io.IOUtils; 37 import org.apache.commons.lang.StringUtils; 38 import org.slf4j.Logger; 39 import org.slf4j.LoggerFactory; 40 41 import java.io.FileInputStream; 42 import java.io.FileNotFoundException; 43 import java.io.InputStream; 44 import java.io.Serializable; 45 46 47 /** 48 * A wrapper for a temporary uploaded file. 49 * 50 * User: sameercharles Date: Apr 28, 2003 Time: 11:20:59 AM 51 * @author Sameer Charles 52 * @version 1.1 53 */ 54 public class Document implements Serializable { 55 private static final Logger log = LoggerFactory.getLogger(Document.class); 56 57 /** 58 * Generated id. 59 */ 60 private static final long serialVersionUID = -1183373762999086707L; 61 62 63 /** 64 * request parameter name. 65 */ 66 private String atomName; 67 68 /** 69 * File name, without extension. 70 */ 71 private String fileName; 72 73 /** 74 * File extension. 75 */ 76 private String extension; 77 78 /** 79 * Mime type. 80 */ 81 private String type; 82 83 /** 84 * Underlying file. 85 */ 86 private java.io.File file; 87 88 /** 89 * A reference to the file input stream. 90 */ 91 transient private FileInputStream inputStream; 92 93 /** 94 * package private constructor. 95 */ 96 Document() { 97 } 98 99 /** 100 * Used to create a document based on a existing file. 101 */ 102 public Document(java.io.File file, String type) { 103 String fileName = file.getName(); 104 this.setFile(file); 105 this.setType(type); 106 this.setExtension(StringUtils.substringAfterLast(fileName, ".")); 107 this.setFileName(StringUtils.substringBeforeLast(fileName, ".")); 108 } 109 110 /** 111 * Sets the parameter name. 112 * @param name parameter name 113 */ 114 public void setAtomName(String name) { 115 this.atomName = name; 116 } 117 118 /** 119 * Returns the parameter name. 120 * @return parameter name 121 */ 122 public String getAtomName() { 123 return this.atomName; 124 } 125 126 /** 127 * Sets the file name without extension. 128 * @param name file name without extension 129 */ 130 public void setFileName(String name) { 131 this.fileName = name; 132 } 133 134 /** 135 * Returns the file name without extension. 136 * @return file name 137 */ 138 public String getFileName() { 139 return this.fileName; 140 } 141 142 /** 143 * Returns the full file name with extension (if existing). 144 * @return file name with extension 145 */ 146 public String getFileNameWithExtension() { 147 if (StringUtils.isEmpty(this.extension)) { 148 return this.fileName; 149 } 150 151 return this.fileName + "." + this.extension; //$NON-NLS-1$ 152 } 153 154 /** 155 * Sets the mime type for this file. 156 * @param type mime type 157 */ 158 public void setType(String type) { 159 this.type = type; 160 } 161 162 /** 163 * Returns the mime type for this file. 164 * @return mime type 165 */ 166 public String getType() { 167 return this.type; 168 } 169 170 /** 171 * Sets a reference to the uploaded file. 172 * @param in file 173 */ 174 public void setFile(java.io.File in) { 175 this.file = in; 176 } 177 178 public void setExtension(String ext) { 179 this.extension = ext; 180 } 181 182 /** 183 * Returns the file extension. 184 * @return file extension 185 */ 186 public String getExtension() { 187 return this.extension; 188 } 189 190 /** 191 * Returns the file length in bytes. 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 * @return stream from the uploaded file 202 */ 203 public InputStream getStream() { 204 try { 205 return (this.inputStream = (new FileInputStream(this.file))); 206 } 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 * @return a reference to the uploaded file. 217 */ 218 public java.io.File getFile() { 219 return this.file; 220 } 221 222 /** 223 * Delete the file, taking care of closing an open input stream. 224 */ 225 public void delete() { 226 IOUtils.closeQuietly(inputStream); 227 this.file.delete(); 228 } 229 }