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 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   * @author had
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       * A constructor for undefined links. (i.e linking to a nonexistent page, for instance).
79       */
80      public Link() {
81      }
82  
83      /**
84       * @param content
85       * @deprecated Since 5.0 use Link(Node).
86       */
87      public Link(Content content) {
88          this(content.getJCRNode());
89      }
90  
91      /**
92       * @param node
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      * @deprecated Since 5.0 use Link(Node).
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      * Initialisation method for Link(String, Content, NodeData) constructor.
126      * @param workspaceName
127      * @param parent
128      * @param nodedata
129      * @deprecated Since 5.0 use Link(Node).
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             //Just return extension if already set, default if not.
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             //Just return fileName.
166         }catch (URISyntaxException e) {
167         }
168         return fileName;
169     }
170 
171     public void setFileName(String fileName) {
172         this.fileName = fileName;
173     }
174 
175     /**
176      * @deprecated Since 5.0 use Link.getJCRNode() instead.
177      */
178     public Content getNode() {
179         return ContentUtil.asContent(this.jcrNode);
180     }
181 
182     /**
183      * @deprecated since 5.0 use Link.setJCRNode() instead.
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             //Just return null;
204         }
205         return this.property;
206     }
207 
208     public void setProperty(Property property){
209         this.property = property;
210     }
211 
212     /**
213      * @deprecated since 5.0 use Link.getProperty() instead.
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      * @deprecated since 5.0 use Link.setProperty() instead.
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      * @deprecated Since 5.0 use Link.getPropertyName() instead.
269      */
270     public String getNodeDataName() {
271         return this.propertyName;
272     }
273 
274     /**
275      * @deprecated Since 5.0 use Link.setPropertyName() instead.
276      */
277     public void setNodeDataName(String nodeDataName) {
278         this.propertyName = nodeDataName;
279     }
280 
281     /**
282      * @deprecated Since 5.0 use Link.getPath instead.
283      */
284     public String getHandle() {
285         return getPath();
286     }
287 
288     /**
289      * @deprecated Since 5.0 use Link.setPath instead.
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      * @deprecated Since 5.0 use Link.getWorkspace instead.
316      */
317     public String getRepository() {
318         return getWorkspace();
319     }
320 
321     /**
322      * @deprecated Since 5.0 use Link.setWorkspace instead.
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      * @deprecated Since 5.0 use Link.getFallbackPath instead.
353      */
354     public String getFallbackHandle() {
355         return getFallbackPath();
356     }
357 
358     /**
359      * @deprecated Since 5.0 use Link.setFallbackPath instead.
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 }