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