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