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 info.magnolia.cms.core.NodeData;
37  import info.magnolia.cms.util.ContentUtil;
38  import info.magnolia.context.MgnlContext;
39  
40  import java.io.InputStream;
41  
42  import javax.jcr.Node;
43  import javax.jcr.RepositoryException;
44  
45  import org.apache.commons.lang3.math.NumberUtils;
46  
47  
48  /**
49   * Wraps a NodeData and exposes it into a simple file-like bean.
50   */
51  public class File {
52  
53      private Node node;
54  
55      private String extension;
56  
57      private String fileName;
58  
59      private String contentType;
60  
61      private int size;
62  
63      /**
64       * @deprecated Since 5.0 use File(Node)
65       */
66      public File(NodeData data) {
67          initFile(data);
68      }
69  
70      public File(Node node) {
71          this.node = node;
72  
73          try {
74              if (node.hasProperty("extension")) {
75                  setExtension(node.getProperty("extension").getString());
76              }
77              if (node.hasProperty("fileName")) {
78                  setFileName(node.getProperty("fileName").getString());
79              }
80              if (node.hasProperty("contentType")) {
81                  setContentType(node.getProperty("contentType").getString());
82              }
83  
84              if (node.hasProperty("size")) {
85                  String sizeString = node.getProperty("size").getString();
86                  if (NumberUtils.isNumber(sizeString)) {
87                      setSize(Integer.parseInt(sizeString));
88                  }
89              }
90          } catch (RepositoryException e) {
91              throw new RuntimeException(e);
92          }
93  
94      }
95  
96      /**
97       * Initialisation method for File(NodeData) constructor.
98       *
99       * @deprecated Since 5.0 use File(Node)
100      */
101     private File initFile(NodeData nodedata) {
102         try {
103             return new File(MgnlContext.getJCRSession(nodedata.getHierarchyManager().getWorkspace().getName()).getNode(nodedata.getHandle()));
104         } catch (RepositoryException e) {
105             throw new RuntimeException(e);
106         }
107     }
108 
109     public String getExtension() {
110         return extension;
111     }
112 
113     public void setExtension(String extension) {
114         this.extension = extension;
115     }
116 
117     public String getFileName() {
118         return fileName;
119     }
120 
121     public void setFileName(String fileName) {
122         this.fileName = fileName;
123     }
124 
125     public String getContentType() {
126         return contentType;
127     }
128 
129     public void setContentType(String contentType) {
130         this.contentType = contentType;
131     }
132 
133     /**
134      * @deprecated Since 5.0.
135      */
136     public String getNodeDataTemplate() {
137         throw new UnsupportedOperationException();
138     }
139 
140     /**
141      * @deprecated Since 5.0.
142      */
143     public void setNodeDataTemplate(String nodeDataTemplate) {
144         throw new UnsupportedOperationException();
145     }
146 
147     public int getSize() {
148         return size;
149     }
150 
151     public void setSize(int size) {
152         this.size = size;
153     }
154 
155     public Node getNode() {
156         return node;
157     }
158 
159     /**
160      * @deprecated Since 5.0 use getNode(Node) instead.
161      */
162     public NodeData getNodeData() {
163         try {
164             if (ContentUtil.asContent(node.getParent()).hasNodeData(node.getName())) {
165                 return ContentUtil.asContent(node.getParent()).getNodeData(node.getName());
166             }
167         } catch (RepositoryException e) {
168             //Cannot retrieve NodeData, return null.
169         }
170         return null;
171     }
172 
173     /**
174      * @deprecated Since 5.0, unsupported on Node API.
175      */
176     public InputStream getStream() {
177         return getNodeData().getStream();
178     }
179 }