View Javadoc
1   /**
2    * This file Copyright (c) 2009-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.core.AggregationState;
37  import info.magnolia.context.MgnlContext;
38  import info.magnolia.objectfactory.Components;
39  
40  import java.util.HashMap;
41  import java.util.Map;
42  
43  import javax.jcr.Node;
44  
45  import org.slf4j.Logger;
46  import org.slf4j.LoggerFactory;
47  
48  /**
49   * Single point of access for all Link Transformers.
50   */
51  public class LinkTransformerManager {
52  
53      private static final Logger log = LoggerFactory.getLogger(LinkTransformerManager.class);
54  
55      private boolean makeBrowserLinksRelative = false;
56      private boolean addContextPathToBrowserLinks = false;
57  
58      private Map<String, LinkTransformer> transformers;
59  
60      public LinkTransformerManager() {
61          this.transformers = new HashMap<String, LinkTransformer>();
62      }
63  
64      public Map<String, LinkTransformer> getTransformers() {
65          return this.transformers;
66      }
67  
68      public void setTransformers(Map<String, LinkTransformer> transformers) {
69          this.transformers = transformers;
70      }
71  
72      public void addTransformer(String key, LinkTransformer transformer) {
73          this.transformers.put(key, transformer);
74      }
75  
76      public boolean isAddContextPathToBrowserLinks() {
77          return this.addContextPathToBrowserLinks;
78      }
79  
80      public void setAddContextPathToBrowserLinks(boolean addContextPathToBrowserLinks) {
81          this.addContextPathToBrowserLinks = addContextPathToBrowserLinks;
82      }
83  
84      public boolean isMakeBrowserLinksRelative() {
85          return this.makeBrowserLinksRelative;
86      }
87  
88      public void setMakeBrowserLinksRelative(boolean makeBrowserLinksRelative) {
89          this.makeBrowserLinksRelative = makeBrowserLinksRelative;
90      }
91  
92      /**
93       * Gets the current singleton instance.
94       */
95      public static LinkTransformerManager getInstance() {
96          return Components.getComponent(LinkTransformerManager.class);
97      }
98  
99      /**
100      * Gets registered absolute path transformer.
101      */
102     public LinkTransformer getAbsoluteTransformer() {
103         return transformers.get("absolute");
104     }
105 
106     /**
107      * Gets registered editor link transformer.
108      */
109     public LinkTransformer getEditorTransformer() {
110         return transformers.get("editor");
111     }
112 
113     /**
114      * Gets registered relative path transformer.
115      */
116     public LinkTransformer getRelativeTransformer() {
117         return transformers.get("relative");
118     }
119 
120     /**
121      * Gets registered complete URL transformer.
122      */
123     public LinkTransformer getCompleteURLTransformer() {
124         return transformers.get("completeURL");
125     }
126 
127     /**
128      * Gets registered complete URL transformer.
129      */
130     public LinkTransformer getI18nTransformer() {
131         return transformers.get("i18n");
132     }
133 
134     /**
135      * Creates instance of absolute link transformer that will prepend the context path, will use URI2Repository mapping while constructing links and will localize the link if localization is set up.
136      */
137     public AbsolutePathTransformer getAbsolute() {
138         return getAbsolute(true);
139     }
140 
141     /**
142      * Creates instance of absolute link transformer that will optionally prepend the context path, but will always use URI2Repository mapping while constructing links and will localize the link if localization is set up.
143      */
144     public AbsolutePathTransformer getAbsolute(boolean addContextPath) {
145         if (getAbsoluteTransformer() == null) {
146             return new AbsolutePathTransformer(addContextPath, true, true);
147         }
148         AbsolutePathTransformer/link/AbsolutePathTransformer.html#AbsolutePathTransformer">AbsolutePathTransformer transformer = (AbsolutePathTransformer) transformers.get("absolute");
149         transformer.setAddContextPath(addContextPath);
150         transformer.setUseI18N(true);
151         transformer.setUseURI2RepositoryMapping(true);
152         return transformer;
153     }
154 
155     /**
156      * Creates instance of Relative link transformer that will translate path to the provided Link relative to the content provided here. During the translation all valid URI2repository mappings and i18n will be applied.
157      */
158     public RelativePathTransformer getRelative(Node page) {
159         if (getRelativeTransformer() == null) {
160             return new RelativePathTransformer(page, true, true);
161         }
162         RelativePathTransformer/link/RelativePathTransformer.html#RelativePathTransformer">RelativePathTransformer transformer = (RelativePathTransformer) transformers.get("relative");
163         transformer.setUseI18N(true);
164         transformer.setUseURI2RepositoryMapping(true);
165         transformer.setAbsolutSourcePath(page);
166         return transformer;
167     }
168 
169     /**
170      * Creates instance of Relative link transformer that will translate path to the provided Link relative to path provided here. During the translation all valid URI2repository mappings and i18n will be applied.
171      */
172     public RelativePathTransformer getRelative(String absolutePath) {
173         if (getRelativeTransformer() == null) {
174             return new RelativePathTransformer(absolutePath, true, true);
175         }
176         RelativePathTransformer/link/RelativePathTransformer.html#RelativePathTransformer">RelativePathTransformer transformer = (RelativePathTransformer) transformers.get("relative");
177         transformer.setUseI18N(true);
178         transformer.setUseURI2RepositoryMapping(true);
179         transformer.setAbsolutSourcePath(absolutePath);
180         return transformer;
181     }
182 
183     /**
184      * Creates instance of Complete URL link transformer that will create fully qualified and localized link to content denoted by Link provided to its transform method.
185      */
186     public CompleteUrlPathTransformer getCompleteUrl() {
187         if (getCompleteURLTransformer() == null) {
188             return new CompleteUrlPathTransformer(true, true);
189         }
190         CompleteUrlPathTransformernk/CompleteUrlPathTransformer.html#CompleteUrlPathTransformer">CompleteUrlPathTransformer transformer = (CompleteUrlPathTransformer) transformers.get("completeURL");
191         transformer.setUseURI2RepositoryMapping(true);
192         transformer.setUseI18N(true);
193         return transformer;
194     }
195 
196     /**
197      * @see EditorLinkTransformer
198      */
199     public EditorLinkTransformer getEditorLink() {
200         if (getEditorTransformer() == null) {
201             return new EditorLinkTransformer();
202         }
203         return (EditorLinkTransformer) transformers.get("editor");
204     }
205 
206     /**
207      * Creates instance of link transformer that will transform any provided links to either absolute or relative path based on the current server configuration.
208      *
209      * @param currentPath Path to make links relative to, if relative path translation is configured on the server.
210      */
211     public LinkTransformer getBrowserLink(String currentPath) {
212         if (MgnlContext.isWebContext()) {
213             if (isMakeBrowserLinksRelative()) {
214                 final AggregationState state = MgnlContext.getAggregationState();
215                 if (currentPath == null && state != null) {
216                     currentPath = state.getOriginalURI();
217                 }
218                 if (currentPath != null) {
219                     return getRelative(currentPath);
220                 }
221             }
222             return getAbsolute(addContextPathToBrowserLinks);
223         }
224         return getCompleteUrl();
225     }
226 }