View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 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.cms.core;
35  
36  import info.magnolia.cms.beans.runtime.File;
37  import info.magnolia.context.MgnlContext;
38  
39  import java.io.UnsupportedEncodingException;
40  import java.net.URLDecoder;
41  import java.util.Locale;
42  
43  import org.apache.commons.lang.StringUtils;
44  
45  /**
46   * Aggregates the necessary information to render content. Filled-in progressively by various filters.
47   *
48   * @author gjoseph
49   * @version $Revision: $ ($Author: $)
50   */
51  public class AggregationState {
52      private String characterEncoding;
53      private String originalURI;
54      private String originalURL;
55      private String originalBrowserURI;
56      private String originalBrowserURL;
57      private String currentURI;
58      private String extension;
59      private File file;
60      private String handle;
61      private Content mainContent;
62      private Content currentContent;
63      private String repository;
64      private String selector;
65      private String templateName;
66      private Locale locale;
67      private boolean isPreviewMode;
68      private String renderableVariation;
69  
70      public void setOriginalURI(String originalURI) {
71          final String strippedURI = stripContextPathIfExists(originalURI);
72          if (this.originalURI != null && !this.originalURI.equals(strippedURI)) {
73              throw new IllegalStateException("Original URI can only be set once ! Existing value is \"" + this.originalURI + "\", tried to replace it with \"" + strippedURI + "\"");
74          }
75          this.originalURI = strippedURI;
76      }
77  
78      public void setOriginalBrowserURI(String originalBrowserURI) {
79          final String strippedURI = stripContextPathIfExists(originalBrowserURI);
80          if (this.originalBrowserURI != null && !this.originalBrowserURI.equals(strippedURI)) {
81              throw new IllegalStateException("Original URI can only be set once ! Existing value is \"" + this.originalURI + "\", tried to replace it with \"" + strippedURI + "\"");
82          }
83          this.originalBrowserURI= strippedURI;
84      }
85  
86      public void setCurrentURI(String currentURI) {
87          this.currentURI = stripContextPathIfExists(currentURI);
88      }
89  
90      /**
91       * Returns the URI of the current request, decoded and without the context path.
92       * This URI might have been modified by various filters.
93       */
94      public String getCurrentURI() {
95          return currentURI;
96      }
97  
98      public String getCharacterEncoding() {
99          if (characterEncoding == null) {
100             throw new IllegalStateException("Character encoding hasn't been setup in AggregationState yet !");
101         }
102         return characterEncoding;
103     }
104 
105 
106     // -- just plain getters and setters below:
107     /**
108      * Returns the original request URI, decoded and without the context path.
109      * Can never be modified.
110      */
111     public String getOriginalURI() {
112         return originalURI;
113     }
114 
115     public String getOriginalURL() {
116         return originalURL;
117     }
118 
119     public void setOriginalURL(String originalURL) {
120         this.originalURL = originalURL;
121     }
122 
123     public String getOriginalBrowserURI() {
124         return originalBrowserURI;
125     }
126 
127     public String getOriginalBrowserURL() {
128         return originalBrowserURL;
129     }
130 
131     public void setOriginalBrowserURL(String originalBrowserURL) {
132         this.originalBrowserURL = originalBrowserURL;
133     }
134 
135     public void setCharacterEncoding(String characterEncoding) {
136         this.characterEncoding = characterEncoding;
137     }
138 
139     public String getExtension() {
140         return extension;
141     }
142 
143     public void setExtension(String extension) {
144         this.extension = extension;
145     }
146 
147     public File getFile() {
148         return file;
149     }
150 
151     public void setFile(File file) {
152         this.file = file;
153     }
154 
155     public String getHandle() {
156         return handle;
157     }
158 
159     public void setHandle(String handle) {
160         this.handle = handle;
161     }
162 
163     public Content getMainContent() {
164         return mainContent;
165     }
166 
167     public void setMainContent(Content mainContent) {
168         this.mainContent = mainContent;
169     }
170 
171     public Content getCurrentContent() {
172         return currentContent;
173     }
174 
175     public void setCurrentContent(Content currentContent) {
176         this.currentContent = currentContent;
177     }
178 
179     public String getRepository() {
180         return repository;
181     }
182 
183     public void setRepository(String repository) {
184         this.repository = repository;
185     }
186 
187     /**
188      * <strong>Warning - this might change in the future - see MAGNOLIA-2343 for details.</strong>
189      */
190     public String getSelector() {
191         return selector;
192     }
193 
194     /**
195      * <strong>Warning - this might change in the future - see MAGNOLIA-2343 for details.</strong>
196      * The provided selector value is decoded upon settings according to rules described in {@link java.net.URLDecoder#decode(java.lang.String, java.lang.String)}
197      */
198     public void setSelector(String selector) {
199         try {
200             this.selector = URLDecoder.decode(selector, getCharacterEncoding());
201         }
202         catch (UnsupportedEncodingException e) {
203             this.selector = selector;
204         }
205     }
206 
207     public String getTemplateName() {
208         return templateName;
209     }
210 
211     public void setTemplateName(String templateName) {
212         this.templateName = templateName;
213     }
214 
215     /**
216      * If the aggregation state local is not set explicitly the contexts locale is returned.
217      * @return The aggregation state level locale, i.e. the locale that should be used for contents
218      */
219     public Locale getLocale() {
220         if (locale == null) {
221             return MgnlContext.getLocale();
222         }
223 
224         return locale;
225     }
226 
227     /**
228      * @param locale The aggregation state level locale, i.e. the locale that should be used for contents
229      */
230     public void setLocale(Locale locale) {
231         this.locale = locale;
232     }
233 
234     public boolean isPreviewMode() {
235         return isPreviewMode;
236     }
237 
238     public void setPreviewMode(boolean previewMode) {
239         isPreviewMode = previewMode;
240     }
241 
242     /**
243      * Gets the name of the renderable variation in use.
244      */
245     public String getRenderableVariation() {
246         return renderableVariation;
247     }
248 
249     /**
250      * Sets the name of the renderable variation in use.
251      */
252     public void setRenderableVariation(String renderableVariation) {
253         this.renderableVariation = renderableVariation;
254     }
255 
256     /**
257      * WARNING: If passing URI without context path but it starts with same text as the context path it will be stripped off as well!!!
258      * @param uri with contextPath (maybe)
259      * @return uri stripped of the prefix matching the contextPath
260      */
261     protected String stripContextPathIfExists(String uri) {
262         // MAGNOLIA-2064 & others ... remove context path only when it is actually present not when page name starts with context path
263         String contextPath = MgnlContext.getContextPath();
264         if (uri != null && uri.startsWith(contextPath + "/")) {
265             return StringUtils.removeStart(uri, contextPath);
266         }
267         return uri;
268     }
269 
270     /**
271      * The original URI/URL can only be set once. A call to this methods resets the original URI/URL and allows to set them freshly.
272      */
273     public void resetURIs() {
274         this.originalURI = null;
275         this.originalURL = null;
276         this.originalBrowserURI = null;
277         this.originalBrowserURL = null;
278         this.currentURI = null;
279     }
280 }