View Javadoc

1   /**
2    * This file Copyright (c) 2003-2014 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.ItemType;
43  import info.magnolia.cms.core.NodeData;
44  
45  import javax.jcr.PropertyType;
46  import javax.jcr.RepositoryException;
47  
48  import org.apache.commons.lang.StringUtils;
49  import org.slf4j.Logger;
50  import org.slf4j.LoggerFactory;
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   * @author had
55   *
56   */
57  public class Link {
58  
59      private static final Logger log = LoggerFactory.getLogger(Link.class);
60  
61      private String repository;
62      private String handle;
63      private String uuid;
64      private String nodeDataName;
65      private String extension;
66      private Content node;
67      private NodeData nodeData;
68      private String fileName;
69      private String fallbackHandle;
70      private String anchor;
71      private String parameters;
72  
73      /**
74       * A constructor for undefined links. (i.e linking to a nonexistent page, for instance)
75       */
76      public Link() {
77      }
78  
79      /**
80       * @param content
81       */
82      public Link(Content content) {
83          setNode(content);
84          try {
85              setRepository(content.getWorkspace().getName());
86          } catch (RepositoryException e) {
87              throw new RuntimeException(e);
88          }
89          // should we have Content.hasUUID()? ... DefaultContent hides the fact that some nodes might not have UUIDs (we can link to other content then just the one in website
90          if (content.isNodeType(ItemType.MIX_REFERENCEABLE)) {
91              setUUID(content.getUUID());
92          }
93      }
94  
95      public Link(String repoName, Content parent, NodeData nodedata) {
96          setNode(parent);
97          setRepository(repoName);
98          setNodeData(nodedata);
99          setNodeDataName(nodedata.getName());
100     }
101 
102     public String getExtension() {
103         if(StringUtils.isEmpty(this.extension) && this.getNodeData() != null && this.getNodeData().getType() == PropertyType.BINARY){
104             File binary = new File(nodeData);
105             extension = binary.getExtension();
106         }
107         return StringUtils.defaultIfEmpty(this.extension, ServerConfiguration.getInstance().getDefaultExtension());
108     }
109 
110 
111     public void setExtension(String extension) {
112         this.extension = extension;
113     }
114 
115 
116     public String getFileName() {
117         try {
118             if(StringUtils.isEmpty(this.fileName) && this.getNodeData() != null && this.getNodeData().getType() == PropertyType.BINARY){
119                 File binary = new File(nodeData);
120                 fileName = new URI(null, null, binary.getFileName(), null).toASCIIString();
121             }
122         } catch (URISyntaxException e) {
123         }
124         return fileName;
125     }
126 
127 
128     public void setFileName(String fileName) {
129         this.fileName = fileName;
130     }
131 
132     public Content getNode() {
133         return this.node;
134     }
135 
136 
137     public void setNode(Content node) {
138         this.node = node;
139     }
140 
141     public NodeData getNodeData() {
142         if(this.nodeData == null && StringUtils.isNotEmpty(this.nodeDataName) && this.getNode() != null){
143             this.nodeData = this.getNode().getNodeData(this.nodeDataName);
144         }
145         return this.nodeData;
146     }
147 
148     public void setNodeData(NodeData nodeData) {
149         this.nodeData = nodeData;
150     }
151 
152     public boolean isEditorBinaryLink(){
153         return getNodeData() != null;
154     }
155 
156     public String getNodeDataName() {
157         return this.nodeDataName;
158     }
159 
160     public void setNodeDataName(String nodeDataName) {
161         this.nodeDataName = nodeDataName;
162     }
163 
164     public String getHandle() {
165         if(StringUtils.isEmpty(this.handle)){
166             if(getNode() != null){
167                 handle = getNode().getHandle();
168             } else {
169                 handle = this.getFallbackHandle();
170             }
171         }
172         return this.handle;
173     }
174 
175     public void setHandle(String path) {
176         this.handle = path;
177     }
178 
179     public String getRepository() {
180         return this.repository;
181     }
182 
183     public void setRepository(String repository) {
184         this.repository = repository;
185     }
186 
187     public String getUUID() {
188         if(StringUtils.isEmpty(this.uuid) && this.getNode() != null){
189             this.uuid = this.getNode().getUUID();
190         }
191         return this.uuid;
192     }
193 
194     public void setUUID(String uuid) {
195         this.uuid = uuid;
196     }
197 
198     public String getFallbackHandle() {
199         return this.fallbackHandle;
200     }
201 
202     public void setFallbackHandle(String fallbackPath) {
203         this.fallbackHandle = fallbackPath;
204     }
205 
206     public String getAnchor() {
207         return this.anchor;
208     }
209 
210     public void setAnchor(String anchor) {
211         this.anchor = anchor;
212     }
213 
214     public String getParameters() {
215         return this.parameters;
216     }
217 
218     public void setParameters(String parameters) {
219         this.parameters = parameters;
220     }
221 }