View Javadoc

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