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.jcr.RuntimeRepositoryException;
39  import info.magnolia.jcr.util.NodeTypes;
40  import info.magnolia.objectfactory.Components;
41  
42  import java.net.URI;
43  import java.net.URISyntaxException;
44  
45  import javax.jcr.Node;
46  import javax.jcr.Property;
47  import javax.jcr.RepositoryException;
48  
49  import org.apache.commons.lang3.StringUtils;
50  import org.apache.jackrabbit.JcrConstants;
51  
52  /**
53   * 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).
54   */
55  public class Link {
56  
57      private String workspace;
58      private String path;
59      private String uuid;
60      private String extension;
61      private String fileName;
62      private String fallbackPath;
63      private String anchor;
64      private String parameters;
65  
66      private Node jcrNode;
67      private Property property;
68      private String propertyName;
69  
70      /**
71       * A constructor for undefined links. (i.e linking to a nonexistent page, for instance).
72       */
73      public Link() {
74      }
75  
76      public Link(Node node) {
77          try {
78              setJCRNode(node);
79              setWorkspace(node.getSession().getWorkspace().getName());
80              if (node.isNodeType(JcrConstants.MIX_REFERENCEABLE)) {
81                  setUUID(node.getIdentifier());
82              }
83          } catch (RepositoryException e) {
84              throw new RuntimeRepositoryException(e);
85          }
86      }
87  
88      public Link(Property property) {
89          try {
90              setJCRNode(property.getParent());
91              setWorkspace(property.getSession().getWorkspace().toString());
92              setProperty(property);
93              setPropertyName(property.getName());
94          } catch (RepositoryException e) {
95              throw new RuntimeRepositoryException(e);
96          }
97      }
98  
99      public String getExtension() {
100         try {
101             if (StringUtils.isEmpty(this.extension) && this.getJCRNode() != null && this.getJCRNode().isNodeType(NodeTypes.Resource.NAME)) {
102                 Fileme/File.html#File">File binary = new File(jcrNode);
103                 extension = binary.getExtension();
104             }
105         } catch (RepositoryException e) {
106             //Just return extension if already set, default if not.
107         }
108         return StringUtils.defaultIfEmpty(this.extension, Components.getComponent(ServerConfiguration.class).getDefaultExtension());
109     }
110 
111     public void setExtension(String extension) {
112         this.extension = extension;
113     }
114 
115     public String getFileName() {
116         try {
117             if (StringUtils.isEmpty(this.fileName) && this.getJCRNode() != null && this.getJCRNode().isNodeType(NodeTypes.Resource.NAME)) {
118                 Fileme/File.html#File">File binary = new File(jcrNode);
119                 fileName = new URI(null, null, binary.getFileName(), null).toASCIIString();
120             }
121         } catch (RepositoryException e) {
122             //Just return fileName.
123         } catch (URISyntaxException e) {
124         }
125         return fileName;
126     }
127 
128     public void setFileName(String fileName) {
129         this.fileName = fileName;
130     }
131 
132     public Node getJCRNode() {
133         return this.jcrNode;
134     }
135 
136     public void setJCRNode(Node jcrNode) {
137         this.jcrNode = jcrNode;
138     }
139 
140     public Property getProperty() throws LinkException {
141         try {
142             if (this.property == null && StringUtils.isNotEmpty(this.propertyName) && this.getJCRNode() != null && this.getJCRNode().hasProperty(propertyName)) {
143                 this.property = this.getJCRNode().getProperty(this.propertyName);
144             }
145         } catch (RepositoryException e) {
146             //Just return null;
147         }
148         return this.property;
149     }
150 
151     public void setProperty(Property property) {
152         this.property = property;
153     }
154 
155     public boolean isEditorBinaryLink() {
156         try {
157             return getJCRNode().isNodeType(NodeTypes.Resource.NAME);
158         } catch (RepositoryException e) {
159             throw new RuntimeRepositoryException(e);
160         }
161     }
162 
163     public String getPropertyName() {
164         return this.propertyName;
165     }
166 
167     public void setPropertyName(String propertyName) {
168         this.propertyName = propertyName;
169     }
170 
171     public String getPath() {
172         if (StringUtils.isEmpty(this.path)) {
173             if (getJCRNode() != null) {
174                 try {
175                     path = getJCRNode().getPath();
176                 } catch (RepositoryException e) {
177                     throw new RuntimeRepositoryException(e);
178                 }
179             } else {
180                 path = this.getFallbackPath();
181             }
182         }
183         return this.path;
184     }
185 
186     public void setPath(String path) {
187         this.path = path;
188     }
189 
190     public String getWorkspace() {
191         return this.workspace;
192     }
193 
194     public void setWorkspace(String workspace) {
195         this.workspace = workspace;
196     }
197 
198     public String getUUID() {
199         if (StringUtils.isEmpty(this.uuid) && this.getJCRNode() != null) {
200             try {
201                 this.uuid = this.getJCRNode().getIdentifier();
202             } catch (RepositoryException e) {
203                 throw new RuntimeRepositoryException(e);
204             }
205         }
206         return this.uuid;
207     }
208 
209     public void setUUID(String uuid) {
210         this.uuid = uuid;
211     }
212 
213     public String getFallbackPath() {
214         return this.fallbackPath;
215     }
216 
217     public void setFallbackPath(String fallbackPath) {
218         this.fallbackPath = fallbackPath;
219     }
220 
221     public String getAnchor() {
222         return this.anchor;
223     }
224 
225     public void setAnchor(String anchor) {
226         this.anchor = anchor;
227     }
228 
229     public String getParameters() {
230         return this.parameters;
231     }
232 
233     public void setParameters(String parameters) {
234         this.parameters = parameters;
235     }
236 }