View Javadoc

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