View Javadoc
1   /**
2    * This file Copyright (c) 2003-2015 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.lang3.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     /**
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 Node getMainContentNode() {
178         return mainContentNode;
179     }
180 
181     /**
182      * @deprecated since 5.0 - use #getMainContentNode() instead.
183      */
184     public Content getMainContent() {
185         return ContentUtil.asContent(getMainContentNode());
186     }
187 
188     public void setMainContentNode(final Node mainContentNode) {
189         this.mainContentNode = mainContentNode;
190     }
191 
192     /**
193      * @deprecated since 5.0 - use #setMainContentNode(Node) instead.
194      */
195     public void setMainContent(Content mainContent) {
196         setMainContentNode(mainContent == null ? null : mainContent.getJCRNode());
197     }
198 
199     public Node getCurrentContentNode() {
200         return currentContentNode;
201     }
202 
203     /**
204      * @deprecated since 5.0 - use #getCurrentContentNode() instead.
205      */
206     public Content getCurrentContent() {
207         return ContentUtil.asContent(getCurrentContentNode());
208     }
209 
210     public void setCurrentContentNode(final Node currentContentNode) {
211         this.currentContentNode = currentContentNode;
212     }
213 
214     /**
215      * @deprecated since 5.0 - use #setCurrentContentNode(Node) instead.
216      */
217     public void setCurrentContent(Content currentContent) {
218         setCurrentContentNode(currentContent == null ? null : currentContent.getJCRNode());
219     }
220 
221     public String getRepository() {
222         return repository;
223     }
224 
225     public void setRepository(String repository) {
226         this.repository = repository;
227     }
228 
229     /**
230      * A selector is the part between the first {@link info.magnolia.cms.core.Path#SELECTOR_DELIMITER} and the extension of an URI.
231      * 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
232      * 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.
233      * 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")}.
234      * <p>You can get and iterate over a full selector with the {@link #getSelectors()} method.<p>
235      * <strong>Warning - this might change in the future - see MAGNOLIA-2343 for details.</strong>
236      */
237     public String getSelector() {
238         return selector;
239     }
240 
241     /**
242      * <strong>Warning - this might change in the future - see MAGNOLIA-2343 for details.</strong>
243      * The provided selector value is decoded upon settings according to rules described in {@link java.net.URLDecoder#decode(java.lang.String, java.lang.String)}
244      */
245     public void setSelector(String selector) {
246         try {
247             this.selector = URLDecoder.decode(selector, getCharacterEncoding());
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      *
274      * @return The aggregation state level locale, i.e. the locale that should be used for contents
275      */
276     public Locale getLocale() {
277         if (locale == null) {
278             return MgnlContext.getLocale();
279         }
280 
281         return locale;
282     }
283 
284     /**
285      * @param locale The aggregation state level locale, i.e. the locale that should be used for contents
286      */
287     public void setLocale(Locale locale) {
288         this.locale = locale;
289     }
290 
291     public boolean isPreviewMode() {
292         return isPreviewMode;
293     }
294 
295     public void setPreviewMode(boolean previewMode) {
296         isPreviewMode = previewMode;
297     }
298 
299     public Channel getChannel() {
300         return channel;
301     }
302 
303     public void setChannel(Channel channel) {
304         this.channel = channel;
305     }
306 
307     /**
308      * WARNING: If passing URI without context path but it starts with same text as the context path it will be stripped off as well!!!
309      *
310      * @param uri with contextPath (maybe)
311      * @return uri stripped of the prefix matching the contextPath
312      */
313     protected String stripContextPathIfExists(String uri) {
314         // MAGNOLIA-2064 & others ... remove context path only when it is actually present not when page name starts with context path
315         String contextPath = MgnlContext.getContextPath();
316         if (uri != null && uri.startsWith(contextPath + "/")) {
317             return StringUtils.removeStart(uri, contextPath);
318         }
319         return uri;
320     }
321 
322     /**
323      * 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.
324      */
325     public void resetURIs() {
326         this.originalURI = null;
327         this.originalURL = null;
328         this.originalBrowserURI = null;
329         this.originalBrowserURL = null;
330         this.currentURI = null;
331     }
332 
333     /**
334      * @return an array containing the selectors found in the URI. The array is empty if no selector is in the current aggregation state.
335      *         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
336      *         single selectors are <code>value1</code> and <code>value2</code>. Selectors are delimited by {@link Path#SELECTOR_DELIMITER}.
337      *         <p>
338      *         <strong>Warning - this might change in the future - see MAGNOLIA-2343 for details.</strong>
339      */
340     public String[] getSelectors() {
341         return selectors;
342     }
343 }