View Javadoc

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