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   * @version $Revision: $ ($Author: $)
49   */
50  public class AggregationState {
51      private String characterEncoding;
52      private String originalURI;
53      private String originalURL;
54      private String originalBrowserURI;
55      private String originalBrowserURL;
56      private String currentURI;
57      private String queryString;
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[] selectors = new String[0];
66      private String templateName;
67      private Locale locale;
68      private boolean isPreviewMode;
69      private Channel channel = new Channel();
70  
71      public void setOriginalURI(String originalURI) {
72          final String strippedURI = stripContextPathIfExists(originalURI);
73          if (this.originalURI != null && !this.originalURI.equals(strippedURI)) {
74              throw new IllegalStateException("Original URI can only be set once ! Existing value is \"" + this.originalURI + "\", tried to replace it with \"" + strippedURI + "\"");
75          }
76          this.originalURI = strippedURI;
77      }
78  
79      public void setOriginalBrowserURI(String originalBrowserURI) {
80          final String strippedURI = stripContextPathIfExists(originalBrowserURI);
81          if (this.originalBrowserURI != null && !this.originalBrowserURI.equals(strippedURI)) {
82              throw new IllegalStateException("Original URI can only be set once ! Existing value is \"" + this.originalURI + "\", tried to replace it with \"" + strippedURI + "\"");
83          }
84          this.originalBrowserURI= strippedURI;
85      }
86  
87      public void setCurrentURI(String currentURI) {
88          this.currentURI = stripContextPathIfExists(currentURI);
89      }
90  
91      public void setQueryString(String queryString) {
92          this.queryString = queryString;
93      }
94  
95      /**
96       * Returns the original request query string.
97       */
98      public String getQueryString() {
99          return queryString;
100     }
101 
102     /**
103      * @return the URI of the current request, decoded and without the context path.
104      * This URI might have been modified by various filters.
105      */
106     public String getCurrentURI() {
107         return currentURI;
108     }
109 
110     public String getCharacterEncoding() {
111         if (characterEncoding == null) {
112             throw new IllegalStateException("Character encoding hasn't been setup in AggregationState yet !");
113         }
114         return characterEncoding;
115     }
116 
117 
118     // -- just plain getters and setters below:
119     /**
120      * Returns the original request URI, decoded and without the context path.
121      * Can never be modified.
122      */
123     public String getOriginalURI() {
124         return originalURI;
125     }
126 
127     public String getOriginalURL() {
128         return originalURL;
129     }
130 
131     public void setOriginalURL(String originalURL) {
132         this.originalURL = originalURL;
133     }
134 
135     public String getOriginalBrowserURI() {
136         return originalBrowserURI;
137     }
138 
139     public String getOriginalBrowserURL() {
140         return originalBrowserURL;
141     }
142 
143     public void setOriginalBrowserURL(String originalBrowserURL) {
144         this.originalBrowserURL = originalBrowserURL;
145     }
146 
147     public void setCharacterEncoding(String characterEncoding) {
148         this.characterEncoding = characterEncoding;
149     }
150 
151     public String getExtension() {
152         return extension;
153     }
154 
155     public void setExtension(String extension) {
156         this.extension = extension;
157     }
158 
159     public File getFile() {
160         return file;
161     }
162 
163     public void setFile(File file) {
164         this.file = file;
165     }
166 
167     public String getHandle() {
168         return handle;
169     }
170 
171     public void setHandle(String handle) {
172         this.handle = handle;
173     }
174 
175     public Content getMainContent() {
176         return mainContent;
177     }
178 
179     public void setMainContent(Content mainContent) {
180         this.mainContent = mainContent;
181     }
182 
183     public Content getCurrentContent() {
184         return currentContent;
185     }
186 
187     public void setCurrentContent(Content currentContent) {
188         this.currentContent = currentContent;
189     }
190 
191     public String getRepository() {
192         return repository;
193     }
194 
195     public void setRepository(String repository) {
196         this.repository = repository;
197     }
198 
199     /**
200      * A selector is the part between the first {@link info.magnolia.cms.core.Path#SELECTOR_DELIMITER} and the extension of an URI.
201      * I.e. given a URI like {@code http://myserver/mypage~x~foo=bar~.html} the entire selector is {@code ~x~foo=bar~}. A selector can be split in turn into several
202      * selectors separated from each other by the {@link info.magnolia.cms.core.Path#SELECTOR_DELIMITER}. In the above example, single selectors are x and foo=bar.
203      * The latter is a {@code name=value} selector which is set in the MgnlContext as an attribute with scope {@code Context.LOCAL_SCOPE}. You can retrieve its value via {@code MgnlContext.getAttribute("foo")}.
204      * <p>You can get and iterate over a full selector with the {@link #getSelectors()} method.<p>
205      * <strong>Warning - this might change in the future - see MAGNOLIA-2343 for details.</strong>
206      */
207     public String getSelector() {
208         return selector;
209     }
210 
211     /**
212      * <strong>Warning - this might change in the future - see MAGNOLIA-2343 for details.</strong>
213      * The provided selector value is decoded upon settings according to rules described in {@link java.net.URLDecoder#decode(java.lang.String, java.lang.String)}
214      */
215     public void setSelector(String selector) {
216         try {
217             this.selector = URLDecoder.decode(selector, getCharacterEncoding());
218         }
219         catch (UnsupportedEncodingException e) {
220             this.selector = selector;
221         }
222 
223         if(StringUtils.isNotEmpty(selector)) {
224             selectors = this.selector.split(Path.SELECTOR_DELIMITER);
225         }
226         for(String sel : selectors) {
227             final String[] splitSelector = sel.split("=");
228             if(splitSelector.length == 2) {
229                 MgnlContext.setAttribute(splitSelector[0], splitSelector[1]);
230             }
231         }
232     }
233 
234     public String getTemplateName() {
235         return templateName;
236     }
237 
238     public void setTemplateName(String templateName) {
239         this.templateName = templateName;
240     }
241 
242     /**
243      * If the aggregation state local is not set explicitly the contexts locale is returned.
244      * @return The aggregation state level locale, i.e. the locale that should be used for contents
245      */
246     public Locale getLocale() {
247         if (locale == null) {
248             return MgnlContext.getLocale();
249         }
250 
251         return locale;
252     }
253 
254     /**
255      * @param locale The aggregation state level locale, i.e. the locale that should be used for contents
256      */
257     public void setLocale(Locale locale) {
258         this.locale = locale;
259     }
260 
261     public boolean isPreviewMode() {
262         return isPreviewMode;
263     }
264 
265     public void setPreviewMode(boolean previewMode) {
266         isPreviewMode = previewMode;
267     }
268 
269     public Channel getChannel() {
270         return channel;
271     }
272 
273     public void setChannel(Channel channel) {
274         this.channel = channel;
275     }
276 
277     /**
278      * WARNING: If passing URI without context path but it starts with same text as the context path it will be stripped off as well!!!
279      * @param uri with contextPath (maybe)
280      * @return uri stripped of the prefix matching the contextPath
281      */
282     protected String stripContextPathIfExists(String uri) {
283         // MAGNOLIA-2064 & others ... remove context path only when it is actually present not when page name starts with context path
284         String contextPath = MgnlContext.getContextPath();
285         if (uri != null && uri.startsWith(contextPath + "/")) {
286             return StringUtils.removeStart(uri, contextPath);
287         }
288         return uri;
289     }
290 
291     /**
292      * 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.
293      */
294     public void resetURIs() {
295         this.originalURI = null;
296         this.originalURL = null;
297         this.originalBrowserURI = null;
298         this.originalBrowserURL = null;
299         this.currentURI = null;
300     }
301     /**
302      * @return an array containing the selectors found in the URI. The array is empty if no selector is in the current aggregation state.
303      * Given a URI like this {@code http://www.magnolia-cms.com/node~value1~value2~.html?someparam=booo}, the entire selector is {@code ~value1~value2~}, whereas the
304      * single selectors are <code>value1</code> and <code>value2</code>. Selectors are delimited by {@link Path#SELECTOR_DELIMITER}.
305      * <p>
306      * <strong>Warning - this might change in the future - see MAGNOLIA-2343 for details.</strong>
307      */
308     public String[] getSelectors() {
309         return selectors;
310     }
311 }