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