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.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   * Representation of the link to a content in Magnolia CMS. The target for the link might be a content (page, paragraph) or the node data (binary file).
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       * A constructor for undefined links. (i.e linking to a nonexistent page, for instance).
77       */
78      public Link() {
79      }
80  
81      /**
82       * @deprecated Since 5.0 use Link(Node).
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      * @deprecated Since 5.0 use Link(Node).
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      * Initialisation method for Link(String, Content, NodeData) constructor.
120      *
121      * @deprecated Since 5.0 use Link(Node).
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             //Just return extension if already set, default if not.
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             //Just return fileName.
158         }catch (URISyntaxException e) {
159         }
160         return fileName;
161     }
162 
163     public void setFileName(String fileName) {
164         this.fileName = fileName;
165     }
166 
167     /**
168      * @deprecated Since 5.0 use Link.getJCRNode() instead.
169      */
170     public Content getNode() {
171         return ContentUtil.asContent(this.jcrNode);
172     }
173 
174     /**
175      * @deprecated since 5.0 use Link.setJCRNode() instead.
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             //Just return null;
196         }
197         return this.property;
198     }
199 
200     public void setProperty(Property property){
201         this.property = property;
202     }
203 
204     /**
205      * @deprecated since 5.0 use Link.getProperty() instead.
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      * @deprecated since 5.0 use Link.setProperty() instead.
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      * @deprecated Since 5.0 use Link.getPropertyName() instead.
261      */
262     public String getNodeDataName() {
263         return this.propertyName;
264     }
265 
266     /**
267      * @deprecated Since 5.0 use Link.setPropertyName() instead.
268      */
269     public void setNodeDataName(String nodeDataName) {
270         this.propertyName = nodeDataName;
271     }
272 
273     /**
274      * @deprecated Since 5.0 use Link.getPath instead.
275      */
276     public String getHandle() {
277         return getPath();
278     }
279 
280     /**
281      * @deprecated Since 5.0 use Link.setPath instead.
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      * @deprecated Since 5.0 use Link.getWorkspace instead.
308      */
309     public String getRepository() {
310         return getWorkspace();
311     }
312 
313     /**
314      * @deprecated Since 5.0 use Link.setWorkspace instead.
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      * @deprecated Since 5.0 use Link.getFallbackPath instead.
345      */
346     public String getFallbackHandle() {
347         return getFallbackPath();
348     }
349 
350     /**
351      * @deprecated Since 5.0 use Link.setFallbackPath instead.
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 }