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 javax.jcr.Node;
37  import javax.jcr.RepositoryException;
38  
39  import org.apache.commons.lang3.math.NumberUtils;
40  
41  
42  /**
43   * Wraps a Node and exposes it into a simple file-like bean.
44   */
45  public class File {
46      public static final String PROPERTIES_CONTENTNODE = "properties";
47  
48      public static final String PROPERTY_CONTENTTYPE = "jcr:mimeType";
49  
50      public static final String PROPERTY_ENCODING = "jcr:encoding";
51  
52      public static final String PROPERTY_LASTMODIFIED = "jcr:lastModified";
53  
54      public static final String PROPERTY_SIZE = "size";
55  
56      public static final String PROPERTY_TEMPLATE = "nodeDataTemplate";
57  
58      public static final String PROPERTY_EXTENSION = "extension";
59  
60      public static final String PROPERTY_MIMETYPE = "mimeType";
61  
62      public static final String PROPERTY_FILENAME = "fileName";
63  
64      public static final String PROPERTY_ICON = "icon";
65  
66      public static final String PROPERTY_WIDTH = "width";
67  
68      public static final String PROPERTY_HEIGHT = "height";
69  
70      public static final String EXTENSION = "extension"; // Pdf
71  
72      public static final String EXTENSION_LOWER_CASE = "extensionLowerCase"; // pdf
73  
74      public static final String EXTENSION_UPPER_CASE = "extensionUpperCase"; // PDF
75  
76      public static final String NAME = "name"; // report2004.Pdf
77  
78      public static final String NAME_WITHOUT_EXTENSION = "nameWithoutExtension"; // report2004
79  
80      public static final String CONTENT_TYPE = "jcr:mimeType"; // application/pdf
81  
82      public static final String ICON = "icon"; // the icon for this type
83  
84      public static final String TEMPLATE = "template"; // ((according to dialog))
85  
86      public static final String HANDLE = "handle"; // /en/mainColumnParagraph/04/file
87  
88      /**
89       * /en/mainColumnParagraph/04/file.Pdf.
90       */
91      public static final String PATH_WITHOUT_NAME = "pathWithoutName";
92  
93      /**
94       * path including fileName: <code>/en/mainColumnParagraph/04/file/report2004.Pdf</code>.
95       */
96      public static final String PATH = "path";
97  
98      /**
99       * size in bytes: <code>263492</code>.
100      */
101     public static final String SIZE_BYTES = "sizeBytes";
102 
103     /**
104      * size in KB: <code>257.3</code>.
105      */
106     public static final String SIZE_KB = "sizeKB";
107 
108     /**
109      * size in MB: <code>0.2</code>.
110      */
111     public static final String SIZE_MB = "sizeMB";
112 
113     /**
114      * size and unit depending of size in bytes, KB, or MB: <code>257.3</code>.
115      */
116     public static final String SIZE = "size";
117 
118     private Node node;
119 
120     private String extension;
121 
122     private String fileName;
123 
124     private String contentType;
125 
126     private int size;
127 
128     public File(Node node) {
129         this.node = node;
130 
131         try {
132             if (node.hasProperty("extension")) {
133                 setExtension(node.getProperty("extension").getString());
134             }
135             if (node.hasProperty("fileName")) {
136                 setFileName(node.getProperty("fileName").getString());
137             }
138             if (node.hasProperty("contentType")) {
139                 setContentType(node.getProperty("contentType").getString());
140             }
141 
142             if (node.hasProperty("size")) {
143                 String sizeString = node.getProperty("size").getString();
144                 if (NumberUtils.isNumber(sizeString)) {
145                     setSize(Integer.parseInt(sizeString));
146                 }
147             }
148         } catch (RepositoryException e) {
149             throw new RuntimeException(e);
150         }
151 
152     }
153 
154     public String getExtension() {
155         return extension;
156     }
157 
158     public void setExtension(String extension) {
159         this.extension = extension;
160     }
161 
162     public String getFileName() {
163         return fileName;
164     }
165 
166     public void setFileName(String fileName) {
167         this.fileName = fileName;
168     }
169 
170     public String getContentType() {
171         return contentType;
172     }
173 
174     public void setContentType(String contentType) {
175         this.contentType = contentType;
176     }
177 
178     public int getSize() {
179         return size;
180     }
181 
182     public void setSize(int size) {
183         this.size = size;
184     }
185 
186     public Node getNode() {
187         return node;
188     }
189 
190 }