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