1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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
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
98
99
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
135
136 public String getNodeDataTemplate() {
137 throw new UnsupportedOperationException();
138 }
139
140
141
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
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
169 }
170 return null;
171 }
172
173
174
175
176 public InputStream getStream() {
177 return getNodeData().getStream();
178 }
179 }