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