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.util.Locale;
40  import java.net.URLDecoder;
41  import java.io.UnsupportedEncodingException;
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 queryString;
59      private String extension;
60      private File file;
61      private String handle;
62      private Content mainContent;
63      private Content currentContent;
64      private String repository;
65      private String selector;
66      private String templateName;
67      private Locale locale;
68      private boolean isPreviewMode;
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      public void setQueryString(String queryString) {
91          this.queryString = queryString;
92      }
93      
94      /**
95       * Returns the original request query string.
96       */
97      public String getQueryString() {
98          return queryString;
99      }
100     
101     /**
102      * Returns the URI of the current request, decoded and without the context path.
103      * This URI might have been modified by various filters.
104      */
105     public String getCurrentURI() {
106         if (currentURI == null) {
107             return originalURI;
108         }
109         return currentURI;
110     }
111 
112     public String getCharacterEncoding() {
113         if (characterEncoding == null) {
114             throw new IllegalStateException("Character encoding hasn't been setup in AggregationState yet !");
115         }
116         return characterEncoding;
117     }
118 
119 
120     // -- just plain getters and setters below:
121     /**
122      * Returns the original request URI, decoded and without the context path.
123      * Can never be modified.
124      */
125     public String getOriginalURI() {
126         return originalURI;
127     }
128 
129     public String getOriginalURL() {
130         return originalURL;
131     }
132 
133     public void setOriginalURL(String originalURL) {
134         this.originalURL = originalURL;
135     }
136 
137     public String getOriginalBrowserURI() {
138         return originalBrowserURI;
139     }
140 
141     public String getOriginalBrowserURL() {
142         return originalBrowserURL;
143     }
144 
145     public void setOriginalBrowserURL(String originalBrowserURL) {
146         this.originalBrowserURL = originalBrowserURL;
147     }
148 
149     public void setCharacterEncoding(String characterEncoding) {
150         this.characterEncoding = characterEncoding;
151     }
152 
153     public String getExtension() {
154         return extension;
155     }
156 
157     public void setExtension(String extension) {
158         this.extension = extension;
159     }
160 
161     public File getFile() {
162         return file;
163     }
164 
165     public void setFile(File file) {
166         this.file = file;
167     }
168 
169     public String getHandle() {
170         return handle;
171     }
172 
173     public void setHandle(String handle) {
174         this.handle = handle;
175     }
176 
177     public Content getMainContent() {
178         return mainContent;
179     }
180 
181     public void setMainContent(Content mainContent) {
182         this.mainContent = mainContent;
183     }
184 
185     public Content getCurrentContent() {
186         return currentContent;
187     }
188 
189     public void setCurrentContent(Content currentContent) {
190         this.currentContent = currentContent;
191     }
192 
193     public String getRepository() {
194         return repository;
195     }
196 
197     public void setRepository(String repository) {
198         this.repository = repository;
199     }
200 
201     /**
202      * <strong>Warning - this might change in the future - see MAGNOLIA-2343 for details.</strong>
203      */
204     public String getSelector() {
205         return selector;
206     }
207 
208     /**
209      * <strong>Warning - this might change in the future - see MAGNOLIA-2343 for details.</strong>
210      * The provided selector value is decoded upon settings according to rules described in {@link java.net.URLDecoder#decode(java.lang.String, java.lang.String)}
211      */
212     public void setSelector(String selector) {
213         try {
214             this.selector = URLDecoder.decode(selector, getCharacterEncoding());
215         }
216         catch (UnsupportedEncodingException e) {
217             this.selector = selector;
218         }
219     }
220 
221     public String getTemplateName() {
222         return templateName;
223     }
224 
225     public void setTemplateName(String templateName) {
226         this.templateName = templateName;
227     }
228 
229     /**
230      * If the aggregation state local is not set explicitly the contexts locale is returned.
231      * @return The aggregation state level locale, i.e. the locale that should be used for contents
232      */
233     public Locale getLocale() {
234         if (locale == null) {
235             return MgnlContext.getLocale();
236         }
237 
238         return locale;
239     }
240 
241     /**
242      * @param locale The aggregation state level locale, i.e. the locale that should be used for contents
243      */
244     public void setLocale(Locale locale) {
245         this.locale = locale;
246     }
247 
248     public boolean isPreviewMode() {
249         return isPreviewMode;
250     }
251 
252     public void setPreviewMode(boolean previewMode) {
253         isPreviewMode = previewMode;
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         } else {
267             return uri;
268         }
269     }
270 
271     /**
272      * 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.
273      */
274     public void resetURIs() {
275         this.originalURI=null;
276         this.originalURL=null;
277         this.originalBrowserURI=null;
278         this.originalBrowserURL=null;
279         // current uri have been resolved from the original, but if original changes, current has to follow, otherwise forward: virtual uri mappings will result in infinite loop since currentURI will be the original one forcing forward to act again and again
280         this.currentURI = null;
281     }
282 }