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