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      @Deprecated
67      public File(NodeData data) {
68          initFile(data);
69      }
70  
71      public File(Node node) {
72          this.node = node;
73  
74          try {
75              if (node.hasProperty("extension")) {
76                  setExtension(node.getProperty("extension").getString());
77              }
78              if (node.hasProperty("fileName")) {
79                  setFileName(node.getProperty("fileName").getString());
80              }
81              if (node.hasProperty("contentType")) {
82                  setContentType(node.getProperty("contentType").getString());
83              }
84  
85              if (node.hasProperty("size")) {
86                  String sizeString = node.getProperty("size").getString();
87                  if (NumberUtils.isNumber(sizeString)) {
88                      setSize(Integer.parseInt(sizeString));
89                  }
90              }
91          } catch (RepositoryException e) {
92              throw new RuntimeException(e);
93          }
94  
95      }
96  
97      /**
98       * Initialisation method for File(NodeData) constructor.
99       *
100      * @deprecated Since 5.0 use File(Node)
101      */
102     @Deprecated
103     private File initFile(NodeData nodedata) {
104         try {
105             return new File(MgnlContext.getJCRSession(nodedata.getHierarchyManager().getWorkspace().getName()).getNode(nodedata.getHandle()));
106         } catch (RepositoryException e) {
107             throw new RuntimeException(e);
108         }
109     }
110 
111     public String getExtension() {
112         return extension;
113     }
114 
115     public void setExtension(String extension) {
116         this.extension = extension;
117     }
118 
119     public String getFileName() {
120         return fileName;
121     }
122 
123     public void setFileName(String fileName) {
124         this.fileName = fileName;
125     }
126 
127     public String getContentType() {
128         return contentType;
129     }
130 
131     public void setContentType(String contentType) {
132         this.contentType = contentType;
133     }
134 
135     /**
136      * @deprecated Since 5.0.
137      */
138     @Deprecated
139     public String getNodeDataTemplate() {
140         throw new UnsupportedOperationException();
141     }
142 
143     /**
144      * @deprecated Since 5.0.
145      */
146     @Deprecated
147     public void setNodeDataTemplate(String nodeDataTemplate) {
148         throw new UnsupportedOperationException();
149     }
150 
151     public int getSize() {
152         return size;
153     }
154 
155     public void setSize(int size) {
156         this.size = size;
157     }
158 
159     public Node getNode() {
160         return node;
161     }
162 
163     /**
164      * @deprecated Since 5.0 use getNode(Node) instead.
165      */
166     @Deprecated
167     public NodeData getNodeData() {
168         try {
169             if (ContentUtil.asContent(node.getParent()).hasNodeData(node.getName())) {
170                 return ContentUtil.asContent(node.getParent()).getNodeData(node.getName());
171             }
172         } catch (RepositoryException e) {
173             //Cannot retrieve NodeData, return null.
174         }
175         return null;
176     }
177 
178     /**
179      * @deprecated Since 5.0, unsupported on Node API.
180      */
181     @Deprecated
182     public InputStream getStream() {
183         return getNodeData().getStream();
184     }
185 }