View Javadoc

1   /**
2    * This file Copyright (c) 2009-2010 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 org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  import info.magnolia.cms.core.AggregationState;
40  import info.magnolia.cms.core.Content;
41  import info.magnolia.context.MgnlContext;
42  import info.magnolia.objectfactory.Components;
43  
44  /**
45   * Single point of access for all Link Transformers.
46   * @author had
47   *
48   */
49  public class LinkTransformerManager {
50  
51      private static final Logger log = LoggerFactory.getLogger(LinkTransformerManager.class);
52  
53      private boolean makeBrowserLinksRelative = false;
54      private boolean addContextPathToBrowserLinks = false;
55  
56      public boolean isAddContextPathToBrowserLinks() {
57          return this.addContextPathToBrowserLinks;
58      }
59  
60      public void setAddContextPathToBrowserLinks(boolean addContextPathToBrowserLinks) {
61          this.addContextPathToBrowserLinks = addContextPathToBrowserLinks;
62      }
63  
64      public boolean isMakeBrowserLinksRelative() {
65          return this.makeBrowserLinksRelative;
66      }
67  
68      public void setMakeBrowserLinksRelative(boolean makeBrowserLinksRelative) {
69          this.makeBrowserLinksRelative = makeBrowserLinksRelative;
70      }
71  
72      /**
73       * Gets the current singleton instance.
74       */
75      public static LinkTransformerManager getInstance() {
76          return Components.getSingleton(LinkTransformerManager.class);
77      }
78  
79      /**
80       * 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.
81       */
82      public AbsolutePathTransformer getAbsolute() {
83          return getAbsolute(true);
84      }
85  
86      /**
87       * 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.
88       */
89      public AbsolutePathTransformer getAbsolute(boolean addContextPath) {
90          return new AbsolutePathTransformer(addContextPath, true, true);
91      }
92  
93      /**
94       * 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.
95       */
96      public RelativePathTransformer getRelative(Content page) {
97          return new RelativePathTransformer(page, true, true);
98      }
99  
100     /**
101      * 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.
102      */
103     public RelativePathTransformer getRelative(String absolutePath) {
104         return new RelativePathTransformer(absolutePath, true, true);
105     }
106 
107     /**
108      * 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.
109      */
110     public CompleteUrlPathTransformer getCompleteUrl() {
111         return new CompleteUrlPathTransformer(true, true);
112     }
113 
114     /**
115      * @see EditorLinkTransformer
116      */
117     public EditorLinkTransformer getEditorLink() {
118         return new EditorLinkTransformer();
119     }
120 
121     /**
122      * Creates instance of link transformer that will transform any provided links to either absolute or relative path based on the current server configuration.
123      * @param currentPath Path to make links relative to, if relative path translation is configured on the server.
124      */
125     public LinkTransformer getBrowserLink(String currentPath) {
126         if (MgnlContext.isWebContext()) {
127             if (isMakeBrowserLinksRelative() ) {
128                 final AggregationState state = MgnlContext.getAggregationState();
129                 if (currentPath == null && state != null) {
130                     currentPath = state.getOriginalURI();
131                 }
132                 if (currentPath != null) {
133                     return getRelative(currentPath);
134                 }
135             }
136             return getAbsolute(addContextPathToBrowserLinks);
137         } else {
138             return getCompleteUrl();
139         }
140     }
141 }