View Javadoc

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