View Javadoc
1   /**
2    * This file Copyright (c) 2003-2017 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.SelectorUtil;
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          // remove context path only once per request unless current URI is being replaced altogether,
90          // as it happens e.g. in VirtualUriFilter. This is a workaround for the bigger issue of manipulating AggregationState
91          // which in one of the next major versions will need refactoring of some parts of the filter chain. See MAGNOLIA-6858
92          if (currentURI != null && !currentURI.equals(getCurrentURI())) {
93              this.currentURI = stripContextPathIfExists(currentURI);
94          }
95      }
96  
97      public void setQueryString(String queryString) {
98          this.queryString = queryString;
99      }
100 
101     /**
102      * Returns the original request query string.
103      */
104     public String getQueryString() {
105         return queryString;
106     }
107 
108     /**
109      * @return the URI of the current request, decoded and without the context path.
110      *         This URI might have been modified by various filters.
111      */
112     public String getCurrentURI() {
113         return currentURI;
114     }
115 
116     public String getCharacterEncoding() {
117         if (characterEncoding == null) {
118             throw new IllegalStateException("Character encoding hasn't been setup in AggregationState yet !");
119         }
120         return characterEncoding;
121     }
122 
123 
124     // -- just plain getters and setters below:
125 
126     /**
127      * Returns the original request URI, decoded and without the context path.
128      * Can never be modified.
129      */
130     public String getOriginalURI() {
131         return originalURI;
132     }
133 
134     public String getOriginalURL() {
135         return originalURL;
136     }
137 
138     public void setOriginalURL(String originalURL) {
139         this.originalURL = originalURL;
140     }
141 
142     public String getOriginalBrowserURI() {
143         return originalBrowserURI;
144     }
145 
146     public String getOriginalBrowserURL() {
147         return originalBrowserURL;
148     }
149 
150     public void setOriginalBrowserURL(String originalBrowserURL) {
151         this.originalBrowserURL = originalBrowserURL;
152     }
153 
154     public void setCharacterEncoding(String characterEncoding) {
155         this.characterEncoding = characterEncoding;
156     }
157 
158     public String getExtension() {
159         return extension;
160     }
161 
162     public void setExtension(String extension) {
163         this.extension = extension;
164     }
165 
166     public File getFile() {
167         return file;
168     }
169 
170     public void setFile(File file) {
171         this.file = file;
172     }
173 
174     public String getHandle() {
175         return handle;
176     }
177 
178     public void setHandle(String handle) {
179         this.handle = handle;
180     }
181 
182     public Node getMainContentNode() {
183         return mainContentNode;
184     }
185 
186     public void setMainContentNode(final Node mainContentNode) {
187         this.mainContentNode = mainContentNode;
188     }
189 
190     public Node getCurrentContentNode() {
191         return currentContentNode;
192     }
193 
194     public void setCurrentContentNode(final Node currentContentNode) {
195         this.currentContentNode = currentContentNode;
196     }
197 
198     public String getRepository() {
199         return repository;
200     }
201 
202     public void setRepository(String repository) {
203         this.repository = repository;
204     }
205 
206     /**
207      * A selector is the part between the first {@link info.magnolia.cms.util.SelectorUtil#SELECTOR_DELIMITER} and the extension of an URI.
208      * 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
209      * selectors separated from each other by the {@link info.magnolia.cms.util.SelectorUtil#SELECTOR_DELIMITER}. In the above example, single selectors are x and foo=bar.
210      * 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")}.
211      * <p>You can get and iterate over a full selector with the {@link #getSelectors()} method.<p>
212      * <strong>Warning - this might change in the future - see MAGNOLIA-2343 for details.</strong>
213      */
214     public String getSelector() {
215         return selector;
216     }
217 
218     /**
219      * <strong>Warning - this might change in the future - see MAGNOLIA-2343 for details.</strong>
220      * The provided selector value is decoded upon settings according to rules described in {@link java.net.URLDecoder#decode(java.lang.String, java.lang.String)}
221      */
222     public void setSelector(String selector) {
223         try {
224             this.selector = URLDecoder.decode(selector, getCharacterEncoding());
225         } catch (UnsupportedEncodingException e) {
226             this.selector = selector;
227         }
228 
229         //remove attributes set by previous selector (might not be empty after request forwards or includes)
230         for (String sel : selectors) {
231             final String[] splitSelector = sel.split("=");
232             if (splitSelector.length == 2) {
233                 MgnlContext.removeAttribute(splitSelector[0]);
234             }
235         }
236 
237         if (StringUtils.isNotEmpty(selector)) {
238             selectors = this.selector.split(SelectorUtil.SELECTOR_DELIMITER);
239         } else {
240             selectors = new String[0];
241         }
242 
243         for (String sel : selectors) {
244             final String[] splitSelector = sel.split("=");
245             if (splitSelector.length == 2) {
246                 MgnlContext.setAttribute(splitSelector[0], splitSelector[1]);
247             }
248         }
249     }
250 
251     public String getTemplateName() {
252         return templateName;
253     }
254 
255     public void setTemplateName(String templateName) {
256         this.templateName = templateName;
257     }
258 
259     /**
260      * If the aggregation state local is not set explicitly the contexts locale is returned.
261      *
262      * @return The aggregation state level locale, i.e. the locale that should be used for contents
263      */
264     public Locale getLocale() {
265         if (locale == null) {
266             return MgnlContext.getLocale();
267         }
268 
269         return locale;
270     }
271 
272     /**
273      * @param locale The aggregation state level locale, i.e. the locale that should be used for contents
274      */
275     public void setLocale(Locale locale) {
276         this.locale = locale;
277     }
278 
279     public boolean isPreviewMode() {
280         return isPreviewMode;
281     }
282 
283     public void setPreviewMode(boolean previewMode) {
284         isPreviewMode = previewMode;
285     }
286 
287     public Channel getChannel() {
288         return channel;
289     }
290 
291     public void setChannel(Channel channel) {
292         this.channel = channel;
293     }
294 
295     /**
296      * WARNING: If passing URI without context path but it starts with same text as the context path it will be stripped off as well!!!
297      *
298      * @param uri with contextPath (maybe)
299      * @return uri stripped of the prefix matching the contextPath
300      */
301     protected String stripContextPathIfExists(String uri) {
302         // MAGNOLIA-2064 & others ... remove context path only when it is actually present not when page name starts with context path
303         String contextPath = MgnlContext.getContextPath();
304         if (uri != null && uri.startsWith(contextPath + "/")) {
305             return StringUtils.removeStart(uri, contextPath);
306         }
307         return uri;
308     }
309 
310     /**
311      * 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.
312      */
313     public void resetURIs() {
314         this.originalURI = null;
315         this.originalURL = null;
316         this.originalBrowserURI = null;
317         this.originalBrowserURL = null;
318         this.currentURI = null;
319     }
320 
321     /**
322      * @return an array containing the selectors found in the URI. The array is empty if no selector is in the current aggregation state.
323      *         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
324      *         single selectors are <code>value1</code> and <code>value2</code>. Selectors are delimited by {@link info.magnolia.cms.util.SelectorUtil#SELECTOR_DELIMITER}.
325      *         <p>
326      *         <strong>Warning - this might change in the future - see MAGNOLIA-2343 for details.</strong>
327      */
328     public String[] getSelectors() {
329         return selectors;
330     }
331 }