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.link;
35
36 import java.net.URI;
37 import java.net.URISyntaxException;
38
39 import info.magnolia.cms.beans.config.ServerConfiguration;
40 import info.magnolia.cms.beans.runtime.File;
41 import info.magnolia.cms.core.Content;
42 import info.magnolia.cms.core.NodeData;
43 import info.magnolia.cms.util.ContentUtil;
44 import info.magnolia.context.MgnlContext;
45 import info.magnolia.jcr.RuntimeRepositoryException;
46 import info.magnolia.jcr.util.NodeTypes;
47 import info.magnolia.objectfactory.Components;
48
49 import javax.jcr.Node;
50 import javax.jcr.Property;
51 import javax.jcr.PropertyType;
52 import javax.jcr.RepositoryException;
53
54 import org.apache.commons.lang.StringUtils;
55 import org.apache.jackrabbit.JcrConstants;
56
57
58
59
60
61
62 public class Link {
63
64 private String workspace;
65 private String path;
66 private String uuid;
67 private String extension;
68 private String fileName;
69 private String fallbackPath;
70 private String anchor;
71 private String parameters;
72
73 private Node jcrNode;
74 private Property property;
75 private String propertyName;
76
77
78
79
80 public Link() {
81 }
82
83
84
85
86
87 public Link(Content content) {
88 this(content.getJCRNode());
89 }
90
91
92
93
94 public Link(Node node) {
95 try {
96 setJCRNode(node);
97 setWorkspace(node.getSession().getWorkspace().getName());
98 if (node.isNodeType(JcrConstants.MIX_REFERENCEABLE)) {
99 setUUID(node.getIdentifier());
100 }
101 } catch (RepositoryException e) {
102 throw new RuntimeRepositoryException(e);
103 }
104 }
105
106
107
108
109 public Link(String workspaceName, Content parent, NodeData nodedata) {
110 initLink(workspaceName, parent, nodedata);
111 }
112
113 public Link(Property property) {
114 try{
115 setJCRNode(property.getParent());
116 setWorkspace(property.getSession().getWorkspace().toString());
117 setProperty(property);
118 setPropertyName(property.getName());
119 }catch(RepositoryException e){
120 throw new RuntimeRepositoryException(e);
121 }
122 }
123
124
125
126
127
128
129
130
131 public Link initLink(String workspaceName, Content parent, NodeData nodedata){
132 try {
133 if(nodedata.getType() != PropertyType.BINARY){
134 return new Link(nodedata.getJCRProperty());
135 }
136 return new Link(MgnlContext.getJCRSession(nodedata.getHierarchyManager().getWorkspace().getName()).getNode(nodedata.getHandle()));
137 } catch (RepositoryException e) {
138 throw new RuntimeRepositoryException(e);
139 }
140 }
141
142 public String getExtension() {
143 try{
144 if(StringUtils.isEmpty(this.extension) && this.getJCRNode() != null && this.getJCRNode().isNodeType(NodeTypes.Resource.NAME)){
145 File binary = new File(jcrNode);
146 extension = binary.getExtension();
147 }
148 }catch(RepositoryException e){
149
150 }
151 return StringUtils.defaultIfEmpty(this.extension, Components.getComponent(ServerConfiguration.class).getDefaultExtension());
152 }
153
154 public void setExtension(String extension) {
155 this.extension = extension;
156 }
157
158 public String getFileName() {
159 try{
160 if(StringUtils.isEmpty(this.fileName) && this.getJCRNode() != null && this.getJCRNode().isNodeType(NodeTypes.Resource.NAME)){
161 File binary = new File(jcrNode);
162 fileName = new URI(null, null, binary.getFileName(), null).toASCIIString();
163 }
164 }catch(RepositoryException e){
165
166 }catch (URISyntaxException e) {
167 }
168 return fileName;
169 }
170
171 public void setFileName(String fileName) {
172 this.fileName = fileName;
173 }
174
175
176
177
178 public Content getNode() {
179 return ContentUtil.asContent(this.jcrNode);
180 }
181
182
183
184
185 public void setNode(Content node) {
186 this.jcrNode = node.getJCRNode();
187 }
188
189 public Node getJCRNode() {
190 return this.jcrNode;
191 }
192
193 public void setJCRNode(Node jcrNode) {
194 this.jcrNode = jcrNode;
195 }
196
197 public Property getProperty() throws LinkException{
198 try{
199 if(this.property == null && StringUtils.isNotEmpty(this.propertyName) && this.getJCRNode() != null && this.getJCRNode().hasProperty(propertyName)){
200 this.property = this.getJCRNode().getProperty(this.propertyName);
201 }
202 }catch(RepositoryException e){
203
204 }
205 return this.property;
206 }
207
208 public void setProperty(Property property){
209 this.property = property;
210 }
211
212
213
214
215 public NodeData getNodeData() {
216 try {
217 if(this.jcrNode != null && this.jcrNode.isNodeType(NodeTypes.Resource.NAME)){
218 return ContentUtil.asContent(jcrNode.getParent()).getNodeData(jcrNode.getName());
219 }
220 else if(this.property == null && StringUtils.isNotEmpty(this.propertyName) && this.getJCRNode() != null){
221 this.property = this.getJCRNode().getProperty(this.propertyName);
222 }
223 if(property == null){
224 return null;
225 }
226 return ContentUtil.asContent(this.property.getParent()).getNodeData(this.propertyName);
227 } catch (RepositoryException e) {
228 throw new RuntimeRepositoryException(e);
229 }
230 }
231
232
233
234
235 public void setNodeData(NodeData nodeData) {
236 if(nodeData != null){
237 try {
238 if(nodeData.getType() != PropertyType.BINARY){
239 this.property = nodeData.getJCRProperty();
240 }else{
241 this.jcrNode = nodeData.getParent().getJCRNode().getNode(nodeData.getName());
242 }
243 } catch (RepositoryException e) {
244 throw new RuntimeRepositoryException(e);
245 }
246 }else{
247 this.property = null;
248 }
249 }
250
251 public boolean isEditorBinaryLink(){
252 try {
253 return getJCRNode().isNodeType(NodeTypes.Resource.NAME);
254 } catch (RepositoryException e) {
255 throw new RuntimeRepositoryException(e);
256 }
257 }
258
259 public String getPropertyName() {
260 return this.propertyName;
261 }
262
263 public void setPropertyName(String propertyName) {
264 this.propertyName = propertyName;
265 }
266
267
268
269
270 public String getNodeDataName() {
271 return this.propertyName;
272 }
273
274
275
276
277 public void setNodeDataName(String nodeDataName) {
278 this.propertyName = nodeDataName;
279 }
280
281
282
283
284 public String getHandle() {
285 return getPath();
286 }
287
288
289
290
291 public void setHandle(String path) {
292 setPath(path);
293 }
294
295 public String getPath() {
296 if(StringUtils.isEmpty(this.path)){
297 if(getJCRNode() != null){
298 try {
299 path = getJCRNode().getPath();
300 } catch (RepositoryException e) {
301 throw new RuntimeRepositoryException(e);
302 }
303 } else {
304 path = this.getFallbackHandle();
305 }
306 }
307 return this.path;
308 }
309
310 public void setPath(String path) {
311 this.path = path;
312 }
313
314
315
316
317 public String getRepository() {
318 return getWorkspace();
319 }
320
321
322
323
324 public void setRepository(String repository) {
325 setWorkspace(repository);
326 }
327
328 public String getWorkspace() {
329 return this.workspace;
330 }
331
332 public void setWorkspace(String workspace) {
333 this.workspace = workspace;
334 }
335
336 public String getUUID() {
337 if(StringUtils.isEmpty(this.uuid) && this.getJCRNode() != null){
338 try {
339 this.uuid = this.getJCRNode().getIdentifier();
340 } catch (RepositoryException e) {
341 throw new RuntimeRepositoryException(e);
342 }
343 }
344 return this.uuid;
345 }
346
347 public void setUUID(String uuid) {
348 this.uuid = uuid;
349 }
350
351
352
353
354 public String getFallbackHandle() {
355 return getFallbackPath();
356 }
357
358
359
360
361 public void setFallbackHandle(String fallbackPath) {
362 setFallbackPath(fallbackPath);
363 }
364
365 public String getFallbackPath() {
366 return this.fallbackPath;
367 }
368
369 public void setFallbackPath(String fallbackPath) {
370 this.fallbackPath = fallbackPath;
371 }
372
373 public String getAnchor() {
374 return this.anchor;
375 }
376
377 public void setAnchor(String anchor) {
378 this.anchor = anchor;
379 }
380
381 public String getParameters() {
382 return this.parameters;
383 }
384
385 public void setParameters(String parameters) {
386 this.parameters = parameters;
387 }
388 }